Coverage for jbank/management/commands/parse_svm.py: 77%

1 import logging

2 import os

3 from pprint import pprint

4 from django.core.management.base import CommandParser

5 from django.db import transaction

6 from jbank.helpers import get_or_create_bank_account, save_or_store_media

7 from jbank.files import list_dir_files

8 from jbank.models import ReferencePaymentBatch, ReferencePaymentBatchFile

9 from jbank.parsers import parse_filename_suffix

10 from jbank.svm import parse_svm_batches_from_file, SVM_STATEMENT_SUFFIXES, create_reference_payment_batch

11 from jutil.command import SafeCommand

12

13 logger = logging.getLogger(__name__)

14

15

16 class Command(SafeCommand):

17 help = "Parses bank settlement .SVM (saapuvat viitemaksut) files"

18

19 def add_arguments(self, parser: CommandParser):

20 parser.add_argument("path", type=str)

21 parser.add_argument("--verbose", action="store_true")

22 parser.add_argument("--test", action="store_true")

23 parser.add_argument("--delete-old", action="store_true")

24 parser.add_argument("--auto-create-accounts", action="store_true")

25 parser.add_argument("--resolve-original-filenames", action="store_true")

26 parser.add_argument("--tag", type=str, default="")

27

28 def do(self, *args, **options): # pylint: disable=too-many-branches

29 files = list_dir_files(options["path"])

30 # pprint(files)

31 for filename in files:

32 plain_filename = os.path.basename(filename)

33

34 if parse_filename_suffix(plain_filename).upper() not in SVM_STATEMENT_SUFFIXES:

35 print("Ignoring non-SVM file {}".format(filename))

36 continue

37

38 if options["resolve_original_filenames"]:

39 found = ReferencePaymentBatchFile.objects.filter(referencepaymentbatch__name=plain_filename).first()

40 if found and not found.original_filename:

41 assert isinstance(found, ReferencePaymentBatchFile)

42 found.original_filename = filename

43 found.save(update_fields=["original_filename"])

44 logger.info("Original SVM reference payment batch filename of %s resolved to %s", found, filename)

45

46 if options["delete_old"]:

47 ReferencePaymentBatch.objects.filter(name=plain_filename).delete()

48

49 if options["test"]:

50 batches = parse_svm_batches_from_file(filename)

51 pprint(batches)

52 continue

53

54 if not ReferencePaymentBatch.objects.filter(name=plain_filename).first():

55 print("Importing statement file {}".format(filename))

56

57 batches = parse_svm_batches_from_file(filename)

58 if options["verbose"]:

59 pprint(batches)

60

61 with transaction.atomic():

62 file = ReferencePaymentBatchFile(original_filename=filename, tag=options["tag"])

63 file.save()

64 save_or_store_media(file.file, filename)

65 file.save()

66

67 for data in batches:

68 if options["auto_create_accounts"]:

69 for rec_data in data["records"]:

70 account_number = rec_data.get("account_number")

71 if account_number:

72 get_or_create_bank_account(account_number)

73

74 create_reference_payment_batch(data, name=plain_filename, file=file) # pytype: disable=not-callable

75

76 file.get_total_amount(force=True)

77 else:

78 print("Skipping reference payment file {}".format(filename))