Browse Source

handle rename exc.

marton levente 4 months ago
parent
commit
1be07b33d3
1 changed files with 42 additions and 37 deletions
  1. 42 37
      tests/test_anafoauth.py

+ 42 - 37
tests/test_anafoauth.py

@@ -7,6 +7,7 @@ from typing import Union
 
 import ujson
 from lxml import etree
+from lxml.etree import LxmlSyntaxError
 
 from anafapi.anafoauth import Anafoauth, Efactoauth
 from anafapi import utils
@@ -76,7 +77,7 @@ class efactTest(unittest.TestCase):
         self.efactoauth.transform_to_pdf(r'c:/Users/DECEL/FACT1/MZK/2023_01_03/MZK2414.xml')
 
     def test_bulk_transform(self):
-        for root, _, files in os.walk(r'f:/DECEL/EFACT/KINVEST/2024_01/'):
+        for root, _, files in os.walk(r'c:\Users\levi\git\anafapi\tests\17259191\FACTURA PRIMITA\2024_03\20\40628013'):
             for file in files:
                 if file.endswith('.xml'):
                     inv_file = os.path.join(root, file)
@@ -95,7 +96,7 @@ class efactTest(unittest.TestCase):
         # self.efactoauth.getxml_from_zip(path='../efact/3016038776.zip', dest_f='')
         # latest = self.efactoauth.get_latest_invoice('efact')
         inv_files = utils.get_not_booked('17259191')
-        # inv_files = [r'C:/Users/Levi/git/anafapi/tests/17259191/FACTURA PRIMITA/2024_04/08/16702141/4238612905.xml']
+        # inv_files = [r'C:\Users\Levi\git\anafapi\tests\17259191\FACTURA PRIMITA\2024_05\15\25222010\4268226934.xml']
         for latest in inv_files:
             if 'booked' not in latest:
                 inv = self.efactoauth.parse_inv(invoice=latest)
@@ -103,12 +104,12 @@ class efactTest(unittest.TestCase):
                 # for line in inv.lines:
                 #     print(line.tax)
                 self.efactoauth.addinvoice_tonextup(invoice=inv,
-                                                    kind='stock',
+                                                    kind='service',
                                                     template=32,
-                                                    exp_plan=691,
+                                                    exp_plan=370,
                                                     warehouse_code=23,
-                                                    generic=True,
-                                                    generic_value='1767',
+                                                    generic=False,
+                                                    generic_value=None,
                                                     send=True)
                 os.rename(latest, latest.replace('.xml'.lower(), '_booked.xml'))
             # os.remove(latest)
@@ -137,7 +138,7 @@ class efactTest(unittest.TestCase):
 
     def test_validate_invoice(self):
         # print(os.listdir('c:/Users/DECEL/FACT1/MZK/2023.01.03/'))
-        for root, _dir, files in os.walk(r'c:\Users\levi\FACT1\ALP\2024_04_08'):
+        for root, _dir, files in os.walk(r'c:\Users\levi\FACT1\MZK\2024_05_13'):
             for file in files:
                 # print(file)
                 self.efactoauth.validate_invoice(os.path.join(root, file))
@@ -158,36 +159,40 @@ class efactTest(unittest.TestCase):
                 # print(root_f, file)
                 if file.endswith('.xml') and file.count('_') < 2 and 'cif error' not in root_f:
                     # print(os.path.join(root_f, file))
-                    tree = etree.parse(os.path.join(root_f, file))
-                    root = tree.getroot()
-                    inv_id = root.xpath('//cbc:ID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
-                    inv_id = inv_id[0].text
-                    parties = root.xpath('//cbc:RegistrationName', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
-                    for partie in parties:
-                        parent_p_type = partie.getparent().getparent().getparent()
-                        if etree.QName(parent_p_type).localname == account:  # @UndefinedVariable
-                            # print(partie.text)
-                            file_partie = partie.text
-
-                    is_date = root.xpath('//cbc:IssueDate', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
-                    is_date = is_date[0].text
-
-                    partie_ids = root.xpath('//cbc:CompanyID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
-                    for partie_id in partie_ids:
-                        parent_p_type = partie_id.getparent().getparent().getparent()
-                        parent_t_type = partie_id.getparent()
-                        # print(parent_t_type)
-                        if etree.QName(parent_t_type).localname == 'PartyTaxScheme' and etree.QName(parent_p_type).localname == account:  # @UndefinedVariable
-                            # print(partie_id.text)
-                            file_partn_id = partie_id.text
-                    new_name = '{}_{}_{}_{}.xml'.format(file.replace('.xml', ''), inv_id, file_partie, is_date)
-                    current_path = os.path.join(root_f, file)
-                    new_path = os.path.join(root_f, new_name)
-                    #
-                    # !This will rename all files
-                    #
-                    os.rename(current_path, new_path)
-                    print(new_name)
+                    try:
+                        tree = etree.parse(os.path.join(root_f, file))
+                        root = tree.getroot()
+                        inv_id = root.xpath('//cbc:ID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
+                        inv_id = inv_id[0].text
+                        parties = root.xpath('//cbc:RegistrationName', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
+                        for partie in parties:
+                            parent_p_type = partie.getparent().getparent().getparent()
+                            if etree.QName(parent_p_type).localname == account:  # @UndefinedVariable
+                                # print(partie.text)
+                                file_partie = partie.text
+
+                        is_date = root.xpath('//cbc:IssueDate', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
+                        is_date = is_date[0].text
+
+                        partie_ids = root.xpath('//cbc:CompanyID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
+                        for partie_id in partie_ids:
+                            parent_p_type = partie_id.getparent().getparent().getparent()
+                            parent_t_type = partie_id.getparent()
+                            # print(parent_t_type)
+                            if etree.QName(parent_t_type).localname == 'PartyTaxScheme' and etree.QName(parent_p_type).localname == account:  # @UndefinedVariable
+                                # print(partie_id.text)
+                                file_partn_id = partie_id.text
+                        new_name = '{}_{}_{}_{}.xml'.format(file.replace('.xml', ''), inv_id, file_partie, is_date)
+                        current_path = os.path.join(root_f, file)
+                        new_path = os.path.join(root_f, new_name)
+                        #
+                        # !This will rename all files
+                        #
+                        os.rename(current_path, new_path)
+                        print(new_name)
+                    except LxmlSyntaxError as exc_:
+                        print(f'{root_f}/{file} not valid')
+                        print(str(exc_))
         else:
             print('Nothing to rename')