Coverage for jbank/management/commands/reparse_svm.py: 0%
27 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-27 13:36 +0700
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-27 13:36 +0700
1# pylint: disable=logging-format-interpolation
2import logging
3from django.core.management.base import CommandParser
4from jbank.models import ReferencePaymentBatchFile, ReferencePaymentRecord
5from jbank.svm import parse_svm_batches_from_file
6from jutil.command import SafeCommand
9logger = logging.getLogger(__name__)
12class Command(SafeCommand):
13 help = "Re-parses old bank settlement .SVM (saapuvat viitemaksut) files. Used for adding missing fields."
15 def add_arguments(self, parser: CommandParser):
16 parser.add_argument("--file", type=str)
18 def do(self, *args, **options):
19 logger.info("Re-parsing SVM files to update fields")
20 qs = ReferencePaymentBatchFile.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, ReferencePaymentBatchFile)
25 logger.info("Processing {} BEGIN".format(file))
26 batches = parse_svm_batches_from_file(file.full_path)
27 for batch in batches:
28 for e in batch["records"]: # pylint: disable=too-many-branches
29 # check missing line_number
30 e2 = ReferencePaymentRecord.objects.filter(
31 batch__file=file,
32 line_number=0,
33 record_type=e["record_type"],
34 account_number=e["account_number"],
35 paid_date=e["paid_date"],
36 archive_identifier=e["archive_identifier"],
37 remittance_info=e["remittance_info"],
38 payer_name=e["payer_name"],
39 currency_identifier=e["currency_identifier"],
40 name_source=e["name_source"],
41 correction_identifier=e["correction_identifier"],
42 delivery_method=e["delivery_method"],
43 receipt_code=e["receipt_code"],
44 ).first()
45 if e2:
46 e2.line_number = e["line_number"]
47 e2.save()
48 logger.info("Updated {} line number to {}".format(e2, e2.line_number))
49 logger.info("Processing {} END".format(file))