test_sale_invoice.py 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. '''
  2. Created on 3 Jun 2024 @author: vnc-console
  3. '''
  4. import unittest
  5. import re
  6. import os
  7. import datetime
  8. from jinja2 import (Environment, PackageLoader)
  9. import pymupdf # noqa
  10. import pandas as pd
  11. from py_mentor_imports import (convert_date_format, get_last_month, BoltSaleInvoice, Partner)
  12. class Test(unittest.TestCase):
  13. templates = Environment(loader=PackageLoader('py_mentor_imports', 'templates'))
  14. template = templates.get_template('bolt_sale_invoice.txt')
  15. template2 = templates.get_template('partners.txt')
  16. code_string = r'(?<=Cod TVA: )[a-zA-Z0-9]*|(?<=VAT no.: )[a-zA-Z0-9]*|(?<=Reg. code: )[a-zA-Z0-9]*|(?<=Cod unic de înregistrare: )[a-zA-Z0-9]*'
  17. reg_string = r'(?<=Reg. code: )[a-zA-Z0-9]*'
  18. name_string = r'[a-zA-Z0-9. ]*(?=\nRidewave SRL)'
  19. value_string = r'(?<=otal \(RON\):\n)[a-zA-Z0-9.]*'
  20. docn_string = r'(?<=Factura nr. )[a-zA-Z\n0-9-]*|(?<=Invoice no. )[a-zA-Z\n0-9-]*'
  21. note_string = r'(?<=Pornire: )[a-zA-Z \n,.\(\):0-9-]*|(?<=Start: )[a-zA-Z \n,.\(\):0-9-]*'
  22. date_string = r'(?<=Dată: )[0-9.]*|(?<=Date: )[0-9.]*'
  23. def test_bolt_inv(self) -> None:
  24. names = []
  25. codes = []
  26. values = []
  27. numbers = []
  28. notes = []
  29. dates = []
  30. for root, _dir, files in os.walk(r'f:\Gdrive\Misc\Dosar-Contabilitate\Ridewave\2024_04\e-Fact'):
  31. for file in files:
  32. if file.endswith('.pdf'):
  33. with pymupdf.open(os.path.join(root, file)) as doc:
  34. page = doc[0]
  35. texts = []
  36. txt = page.get_text()
  37. texts.append(txt)
  38. # print(texts)
  39. buyer_name = re.search(self.name_string, txt)
  40. buyer_code = re.search(self.code_string, txt).group() # noqa E:if match None
  41. if buyer_code == '49340170':
  42. buyer_code = input(f'enter code manually for {buyer_name.group()}') # noqa E:if match None
  43. elif buyer_code.startswith('J'):
  44. buyer_code = re.search(r'(?<=Cod TVA: )[a-zA-Z0-9]*|(?<=VAT no.: )[a-zA-Z0-9]*', txt).group() # noqa E:if match None
  45. elif buyer_code == '14532901':
  46. buyer_code = '42717800'
  47. total_value = re.search(self.value_string, txt)
  48. doc_number = re.search(self.docn_string, txt)
  49. doc_number = doc_number.group().strip().replace('\n', '')[-3:] # noqa E:if match None
  50. doc_notes = re.search(self.note_string, txt)
  51. doc_notes = doc_notes.group().strip().replace('\n', '') # noqa E:if match None
  52. doc_data = re.search(self.date_string, txt)
  53. names.append(buyer_name.group()) # noqa E:if match None
  54. codes.append(buyer_code)
  55. values.append(total_value.group()) # noqa E:if match None
  56. numbers.append(doc_number)
  57. notes.append(doc_notes)
  58. dates.append(doc_data.group()) # noqa E:if match None
  59. # print(total_value.group(), buyer_code.group(), doc_number)
  60. data = {'dates': dates, 'names': names, 'codes': codes, 'values': values, 'numbers': numbers, 'notes': notes}
  61. df = pd.DataFrame(data)
  62. # print(df)
  63. invoices = []
  64. partners = []
  65. for date, name, code, value, number, note in zip(df['dates'], df['names'], df['codes'], df['values'], df['numbers'], df['notes']):
  66. invoice = BoltSaleInvoice(doc_data=date, buyer_name=name, buyer_code=code, total_value=value, doc_number=number, doc_notes=note)
  67. invoices.append(invoice)
  68. partner = Partner(vat_code=code, name=name)
  69. partners.append(partner)
  70. data = {'AnLucru': datetime.date.today().year,
  71. 'LunaLucru': get_last_month(0),
  72. 'TotalFacturi': len(df),
  73. 'documents': invoices,
  74. 'Code': 'TRANSPMF'
  75. }
  76. data2 = {'partners': partners}
  77. output = self.template.render(data)
  78. output2 = self.template2.render(data2)
  79. print(output)
  80. home = os.path.expanduser('~')
  81. downloads_dir = os.path.join(home, 'Downloads', 'mentor', 'import', 'RIDEWAVE')
  82. output_file = os.path.join(downloads_dir, 'facturi.txt')
  83. output_file2 = os.path.join(downloads_dir, 'PARTNER.txt')
  84. with open(output_file, 'w') as text:
  85. text.write(output)
  86. with open(output_file2, 'w') as text:
  87. # output2.encode('utf-8')
  88. text.write(output2)
  89. if __name__ == "__main__":
  90. # unittest.main()
  91. Test().test_bolt_inv()