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

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

## 61 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1# pylint: disable=too-many-branches,logging-format-interpolation

2 import logging

3 import os

4 from pprint import pprint

5 from django.core.management.base import CommandParser

6 from django.db import transaction

7 from jbank.helpers import create_reference_payment_batch, get_or_create_bank_account, save_or_store_media

8 from jbank.files import list_dir_files

9 from jbank.models import ReferencePaymentBatch, ReferencePaymentBatchFile

10 from jbank.parsers import parse_filename_suffix

11 from jbank.svm import parse_svm_batches_from_file, SVM_STATEMENT_SUFFIXES

12 from jutil.command import SafeCommand

13

14 logger = logging.getLogger(__name__)

15

16

17 class Command(SafeCommand):

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

19

20 def add_arguments(self, parser: CommandParser):

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

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

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

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

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

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

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

28

29 def do(self, *args, **options):

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

31 # pprint(files)

32 for filename in files:

33 plain_filename = os.path.basename(filename)

34

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

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

37 continue

38

39 if options["resolve_original_filenames"]:

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

41 if found and not found.original_filename:

42 assert isinstance(found, ReferencePaymentBatchFile)

43 found.original_filename = filename

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

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

46

47 if options["delete_old"]:

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

49

50 if options["test"]:

51 batches = parse_svm_batches_from_file(filename)

52 pprint(batches)

53 continue

54

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

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

57

58 batches = parse_svm_batches_from_file(filename)

59 if options["verbose"]:

60 pprint(batches)

61

62 with transaction.atomic():

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

64 file.save()

65 save_or_store_media(file.file, filename)

66 file.save()

67

68 for data in batches:

69 if options["auto_create_accounts"]:

70 for rec_data in data["records"]:

71 account_number = rec_data.get("account_number")

72 if account_number:

73 get_or_create_bank_account(account_number)

74

75 create_reference_payment_batch(

76 data, name=plain_filename, file=file

77 ) # pytype: disable=not-callable

78

79 file.get_total_amount(force=True)

80 else:

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