|
@@ -0,0 +1,90 @@
|
|
|
+'''
|
|
|
+Created on 3 Jun 2024 @author: vnc-console
|
|
|
+'''
|
|
|
+import unittest
|
|
|
+import re
|
|
|
+import os
|
|
|
+import datetime
|
|
|
+from jinja2 import (Environment, PackageLoader)
|
|
|
+import pymupdf
|
|
|
+import pandas as pd
|
|
|
+from py_mentor_imports import (convert_date_format, get_last_month, BoltSaleInvoice, Partner)
|
|
|
+
|
|
|
+class Test(unittest.TestCase):
|
|
|
+
|
|
|
+ templates = Environment(loader=PackageLoader('py_mentor_imports', 'templates'))
|
|
|
+ template = templates.get_template('bolt_sale_invoice.txt')
|
|
|
+ template2 = templates.get_template('partners.txt')
|
|
|
+
|
|
|
+ def test_bolt_inv(self):
|
|
|
+ names = []
|
|
|
+ codes = []
|
|
|
+ values = []
|
|
|
+ numbers = []
|
|
|
+ notes = []
|
|
|
+ dates = []
|
|
|
+ for root, _dir, files in os.walk(r'f:\Gdrive\Misc\Dosar-Contabilitate\Ridewave\2024_06'):
|
|
|
+ for file in files:
|
|
|
+ if file.endswith('.pdf'):
|
|
|
+ with pymupdf.open(os.path.join(root, file)) as doc:
|
|
|
+ page = doc[0]
|
|
|
+ texts = []
|
|
|
+ txt = page.get_text()
|
|
|
+ texts.append(txt)
|
|
|
+ # print(texts)
|
|
|
+ buyer_name = re.search(r'[a-zA-Z0-9. ]*(?=\nRidewave SRL)', txt)
|
|
|
+ buyer_code = re.search(r'(?<=Cod TVA: )[a-zA-Z0-9]*|(?<=Reg. code: )[a-zA-Z0-9]*', txt).group()
|
|
|
+ if buyer_code == '49340170':
|
|
|
+ buyer_code = input(f'enter code manually for {buyer_name.group()}')
|
|
|
+
|
|
|
+ total_value = re.search(r'(?<=otal \(RON\):\n)[a-zA-Z0-9.]*', txt)
|
|
|
+ doc_number = re.search(r'(?<=Factura nr. )[a-zA-Z\n0-9-]*|(?<=Invoice no. )[a-zA-Z\n0-9-]*', txt)
|
|
|
+ doc_number = doc_number.group().strip().replace('\n', '')[-3:]
|
|
|
+ doc_notes = re.search(r'(?<=Pornire: )[a-zA-Z \n,.\(\):0-9-]*|(?<=Start: )[a-zA-Z \n,.\(\):0-9-]*', txt)
|
|
|
+ doc_notes = doc_notes.group().strip().replace('\n', '')
|
|
|
+ doc_data = re.search(r'(?<=Dată: )[0-9.]*|(?<=Date: )[0-9.]*', txt)
|
|
|
+ names.append(buyer_name.group())
|
|
|
+ codes.append(buyer_code)
|
|
|
+ values.append(total_value.group())
|
|
|
+ numbers.append(doc_number)
|
|
|
+ notes.append(doc_notes)
|
|
|
+ dates.append(doc_data.group())
|
|
|
+ # print(total_value.group(), buyer_code.group(), doc_number)
|
|
|
+ data = {'dates': dates, 'names': names, 'codes': codes, 'values': values, 'numbers': numbers, 'notes': notes}
|
|
|
+ df = pd.DataFrame(data)
|
|
|
+ # print(df)
|
|
|
+ invoices = []
|
|
|
+ partners = []
|
|
|
+ for date, name, code, value, number, note in zip(df['dates'], df['names'], df['codes'], df['values'], df['numbers'], df['notes']):
|
|
|
+ invoice = BoltSaleInvoice(doc_data=date, buyer_name=name, buyer_code=code, total_value=value, doc_number=number, doc_notes=note)
|
|
|
+ invoices.append(invoice)
|
|
|
+ partner = Partner(vat_code=code)
|
|
|
+ partners.append(partner)
|
|
|
+ data = {'AnLucru': datetime.date.today().year,
|
|
|
+ 'LunaLucru': get_last_month(0),
|
|
|
+ 'TotalFacturi': len(df),
|
|
|
+ 'documents': invoices,
|
|
|
+ # 'NrDoc': [],
|
|
|
+ # 'NumarBonuri': [],
|
|
|
+ # 'Data': [],
|
|
|
+ # 'Observatii': [],
|
|
|
+ 'Code': 'TRANSPMF'
|
|
|
+ # 'Pret': []
|
|
|
+ }
|
|
|
+ data2 = {'partners': partners}
|
|
|
+ output = self.template.render(data)
|
|
|
+ output2 = self.template2.render(data2)
|
|
|
+ print(output)
|
|
|
+ home = os.path.expanduser('~')
|
|
|
+ downloads_dir = os.path.join(home, 'Downloads', 'mentor', 'import', 'RIDEWAVE')
|
|
|
+ output_file = os.path.join(downloads_dir, 'facturi.txt')
|
|
|
+ output_file2 = os.path.join(downloads_dir, 'PARTNER.txt')
|
|
|
+ with open(output_file, 'w') as text:
|
|
|
+ text.write(output)
|
|
|
+ with open(output_file2, 'w') as text:
|
|
|
+ text.write(output2)
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ # unittest.main()
|
|
|
+ Test().test_bolt_inv()
|