Coverage for jbank/management/commands/parse_xm.py: 90%

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.camt import CAMT054_STATEMENT_SUFFIXES, camt054_parse_file, camt054_create_reference_payment_batch, camt054_parse_ntfctn_acct

7 from jbank.files import list_dir_files

8 from jbank.helpers import save_or_store_media, get_or_create_bank_account

9 from jbank.models import ReferencePaymentBatch, ReferencePaymentBatchFile

10 from jbank.parsers import parse_filename_suffix

11 from jutil.command import SafeCommand

12

13 logger = logging.getLogger(__name__)

14

15

16 class Command(SafeCommand):

17 help = "Parses camt.054.001.02 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("--tag", type=str, default="")

26

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

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

29 for filename in files:

30 plain_filename = os.path.basename(filename)

31

32 if parse_filename_suffix(plain_filename).upper() not in CAMT054_STATEMENT_SUFFIXES:

33 print("Ignoring non-camt.054 file {}".format(filename))

34 continue

35

36 if options["delete_old"]:

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

38

39 if options["test"]:

40 camt054_data = camt054_parse_file(filename)

41 pprint(camt054_data)

42 continue

43

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

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

46

47 camt054_data = camt054_parse_file(filename)

48 if options["verbose"]:

49 pprint(camt054_data)

50

51 with transaction.atomic():

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

53 file.save()

54 save_or_store_media(file.file, filename)

55 file.save()

56

57 for ntfctn in camt054_data["BkToCstmrDbtCdtNtfctn"]["Ntfctn"]:

58 if options["auto_create_accounts"]:

59 account_number, currency = camt054_parse_ntfctn_acct(ntfctn)

60 if account_number:

61 get_or_create_bank_account(account_number, currency)

62

63 camt054_create_reference_payment_batch(ntfctn, name=plain_filename, file=file)

64

65 file.get_total_amount(force=True)

66 else:

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