Coverage for jbank/management/commands/test_pain001.py: 47%

1 import logging

2 import subprocess

3 from decimal import Decimal

4 from os.path import join

5 from django.conf import settings

6 from django.core.management.base import CommandParser

7 from jbank.sepa import Pain001

8 from jutil.command import SafeCommand

9 from jutil.format import format_xml_bytes

10 from jutil.validators import iban_bic

11

12 logger = logging.getLogger(__name__)

13

14

15 class Command(SafeCommand):

16 help = "Generates pain.001.001.03 compatible SEPA payment file."

17

18 def add_arguments(self, parser: CommandParser):

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

20 parser.add_argument("--validate", action="store_true")

21

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

23 debtor_acc = "FI4947300010416310"

24 p = Pain001(

25 "201802071211XJANITEST",

26 "Kajala Group Oy",

27 debtor_acc,

28 iban_bic(debtor_acc),

29 "020840699",

30 ["Koukkukankareentie 29", "20320 Turku"],

31 "FI",

32 )

33 creditor_acc = "FI8847304720017517"

34 p.add_payment("201802071339A0001", "Jani Kajala", creditor_acc, iban_bic(creditor_acc), Decimal("49.00"), "vuokratilitys")

35 xml_str = format_xml_bytes(p.render_to_bytes()).decode()

36 print(xml_str)

37

38 filename = "/tmp/pain001.xml"

39 with open(filename, "wt", encoding="utf-8") as fp:

40 fp.write(xml_str)

41 print(filename, "written")

42

43 if options["validate"]:

44 # /usr/bin/xmllint --format --pretty 1 --load-trace --debug --schema $1 $2

45 res = subprocess.run(

46 [

47 "/usr/bin/xmllint",

48 "--format",

49 "--pretty",

50 "1",

51 "--load-trace",

52 "--debug",

53 "--schema",

54 join(settings.BASE_DIR, "data/pain001/pain.001.001.03.xsd"),

55 filename,

56 ],

57 check=False,

58 )

59 if res.returncode == 0:

60 print("OK")

61 else:

62 print("FAIL")