test_en16931.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. '''Created 19 Aug 2024 Levi'''
  2. import unittest
  3. import os
  4. from jinja2 import (Environment, PackageLoader)
  5. import pymupdf # noqa
  6. from anafapi.en16931 import Invoice
  7. def extract_numeric(value):
  8. return ''.join([c for c in value if c.isdigit()])
  9. def format_date(value):
  10. return value.strftime('%d.%m.%Y')
  11. class Test(unittest.TestCase):
  12. def test_en16931(self) -> None:
  13. templates = Environment(loader=PackageLoader('py_mentor_imports', 'templates'))
  14. templates.filters['extract_numeric'] = extract_numeric
  15. templates.filters['format_date'] = format_date
  16. template = templates.get_template('en16931.txt')
  17. template2 = templates.get_template('partners_en16931.txt')
  18. # inv = Invoice.from_xml(r'c:\Users\vnc-console\git\anafapi\tests\31434328\FACTURA TRIMISA\2024_08\16\31434328\33291356\4393473874.xml')
  19. invoices = []
  20. for root, _, files in os.walk(r'C:\Users\vnc-console\git\anafapi\tests\22520381\FACTURA TRIMISA\2024_07'):
  21. for file in files:
  22. if file.endswith('.xml') or file.endswith('.xml'.upper()):
  23. # print(os.path.join(root, file))
  24. inv = Invoice.from_xml(os.path.join(root, file))
  25. invoices.append(inv)
  26. # print(inv.all_lines[0])
  27. data = {'AnLucru': inv.issue_date.year,
  28. 'LunaLucru': inv.issue_date.month,
  29. 'TotalFacturi': len(invoices),
  30. 'documents': invoices,
  31. 'Code': 'LSERV',
  32. 'SerieCarnet': 'GS'
  33. }
  34. data2 = {'partners': invoices}
  35. output = template.render(data)
  36. output2 = template2.render(data2)
  37. print(output)
  38. home = os.path.expanduser('~')
  39. downloads_dir = os.path.join(home, 'Downloads', 'mentor', 'import', 'GERM')
  40. output_file = os.path.join(downloads_dir, 'facturi.txt')
  41. output_file2 = os.path.join(downloads_dir, 'PARTNER.txt')
  42. with open(output_file, 'w') as text, open(output_file2, 'w') as text2:
  43. text.write(output)
  44. text2.write(output2)
  45. if __name__ == "__main__":
  46. Test().test_en16931()