Coverage for jbank/management/commands/reparse_to.py: 0%

1# pylint: disable=logging-format-interpolation

2 import logging

3 from django.core.management.base import CommandParser

4 from jbank.models import StatementFile, StatementRecord

5 from jbank.tito import parse_tiliote_statements_from_file

6 from jutil.command import SafeCommand

7

8

9 logger = logging.getLogger(__name__)

10

11

12 class Command(SafeCommand):

13 help = "Re-parses old bank statement .TO (tiliote) files. Used for adding missing fields."

14

15 def add_arguments(self, parser: CommandParser):

16 parser.add_argument("--file", type=str)

17

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

19 logger.info("Re-parsing TO files to update fields")

20 qs = StatementFile.objects.all()

21 if options["file"]:

22 qs = qs.filter(file=options["file"])

23 for file in qs.order_by("id"):

24 assert isinstance(file, StatementFile)

25 logger.info("Processing {} BEGIN".format(file))

26 statements = parse_tiliote_statements_from_file(file.full_path)

27 for data in statements:

28 for e in data["records"]:

29 # check missing line_number

30 e2 = StatementRecord.objects.filter(

31 statement__file=file,

32 line_number=0,

33 record_number=e["record_number"],

34 archive_identifier=e["archive_identifier"],

35 record_date=e["record_date"],

36 value_date=e["value_date"],

37 paid_date=e["paid_date"],

38 entry_type=e["entry_type"],

39 record_code=e["record_code"],

40 record_description=e["record_description"],

41 receipt_code=e["receipt_code"],

42 delivery_method=e["delivery_method"],

43 name=e["name"],

44 name_source=e["name_source"],

45 recipient_account_number=e["recipient_account_number"],

46 recipient_account_number_changed=e["recipient_account_number_changed"],

47 remittance_info=e["remittance_info"],

48 ).first()

49 if e2:

50 e2.line_number = e["line_number"]

51 e2.save()

52 logger.info("Updated {} line number to {}".format(e2, e2.line_number))

53 logger.info("Processing {} END".format(file))