test_anafoauth.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. '''Created 6 Jul 2022 Levi'''
  2. # coding: UTF-8
  3. import platform
  4. import unittest
  5. import os
  6. from typing import Union
  7. from lxml import etree
  8. from anafapi.anafoauth import Anafoauth, Efactoauth
  9. from anafapi import utils
  10. if platform.system() == 'Linux':
  11. token_file_path = '/home/deeejas/git/anafapi/anafapi/{}'
  12. elif platform.system() == 'Windows':
  13. token_file_path = os.path.join(os.path.expanduser('~'), 'git/anafapi/anafapi/{}')
  14. class oauthTest(unittest.TestCase):
  15. anoauth = Anafoauth(
  16. client_id='16cbef8754bef0bcf015529353427e8a7e3ee71d56879565',
  17. client_secret='c6aa66d89002f36461f93facd990e6bf5bc706e1df897e8a7e3ee71d56879565',
  18. redirect_uri='https://www.anaf.ro/oauth2callback',
  19. )
  20. def test_is_valid(self):
  21. self.anoauth.load_token_file(token_file_path.format('efact.json'))
  22. # print(self.anoauth.to)
  23. print(self.anoauth.is_valid)
  24. def test_browser_redirect(self):
  25. print(self.anoauth._browser_redirect())
  26. def test_get_token(self):
  27. self.anoauth.get_token()
  28. self.anoauth.save_token(token_file_path.format('efact.json'))
  29. def test_revive_token(self):
  30. self.anoauth.load_token_file(token_file_path.format('efact.json'))
  31. self.anoauth.revive_token()
  32. self.anoauth.save_token(token_file_path.format('efact_refreshed.json'))
  33. class efactTest(unittest.TestCase):
  34. efactoauth = Efactoauth(
  35. client_id='16cbef8754bef0bcf015529353427e8a7e3ee71d56879565',
  36. client_secret='c6aa66d89002f36461f93facd990e6bf5bc706e1df897e8a7e3ee71d56879565',
  37. redirect_uri='https://www.anaf.ro/oauth2callback',
  38. )
  39. efactoauth.load_token_file(token_file_path.format('efact.json'))
  40. efactoauth.load_session()
  41. def test_add_invoice(self):
  42. # self.efactoauth.validate_invoice('C:/Users/DECEL/FACT1/MZK/2023_06/MZK2321602.xml', env='prod')
  43. self.efactoauth.add_invoice('17259191', 'C:/Users/DECEL/FACT1/MZK/2023_06/MZK2321602.xml', env='prod')
  44. def test_bulk_add_invoice(self):
  45. for root, _, files in os.walk('f:/DECEL/EFACT/KINVEST/2024_01/'):
  46. for file in files:
  47. if file.endswith('.xml'):
  48. inv_file = os.path.join(root, file)
  49. self.efactoauth.add_invoice('31434328', inv_file, env='prod')
  50. def test_transform_to_pdf(self):
  51. self.efactoauth.transform_to_pdf('c:/Users/DECEL/FACT1/MZK/2023_01_03/MZK2414.xml')
  52. def test_bulk_transform(self):
  53. for root, _, files in os.walk('f:/DECEL/EFACT/KINVEST/2024_01/'):
  54. for file in files:
  55. if file.endswith('.xml'):
  56. inv_file = os.path.join(root, file)
  57. self.efactoauth.transform_to_pdf(inv_file, no_validation=True)
  58. def test_get_all_messages(self, vat_id, days, root_path, env, filter_, extract):
  59. self.efactoauth.get_all_messages(vat_id, days=days, env=env, root_path=root_path, filter_=filter_, extract=extract)
  60. def test_pretty_messages(self):
  61. self.efactoauth.pretty_messages('17259191', env='prod')
  62. def test_download_invoice(self):
  63. self.efactoauth.download_invoice('3105899770', path='C:/Users/DECEL/FACT1/MZK/2023_06', env='prod')
  64. def test_getxml_to_erp(self):
  65. # self.efactoauth.getxml_from_zip(path='../efact/3016038776.zip', dest_f='')
  66. # latest = self.efactoauth.get_latest_invoice('efact')
  67. inv_files = utils.get_not_booked('17259191')
  68. # inv_files = ['C:/Users/Levi/git/anafapi/tests/17259191/FACTURA PRIMITA/2024_02/06/17317438/4159771982.xml']
  69. for latest in inv_files:
  70. if 'booked' not in latest:
  71. inv = self.efactoauth.parse_inv(invoice=latest)
  72. print(inv.seller_party.name, inv.invoice_id, inv.issue_date.date())
  73. # for line in inv.lines:
  74. # print(line.tax)
  75. self.efactoauth.addinvoice_tonextup(invoice=inv,
  76. kind='service',
  77. template=32,
  78. exp_plan=691,
  79. warehouse_code=23,
  80. generic=True,
  81. generic_value='27')
  82. os.rename(latest, latest.replace('.xml'.lower(), '_booked.xml'))
  83. # os.remove(latest)
  84. def test_verify_invoice(self):
  85. self.efactoauth.verify_invoice('4075641152', env='prod')
  86. def test_isvalid(self):
  87. self.efactoauth.load_token_file(token_file_path.format('efact.json'))
  88. print(self.efactoauth.is_valid)
  89. def test_get_latest_invoice(self):
  90. latest = self.efactoauth.get_latest_invoice('efact')
  91. print(latest)
  92. # os.replace(latest, latest.replace('.xml'.lower(), '_booked.xml'))
  93. def test_validate_invoice(self):
  94. # print(os.listdir('c:/Users/DECEL/FACT1/MZK/2023.01.03/'))
  95. for root, _dir, files in os.walk('c:/Users/levi/FACT1/MZK/2024_01_23'):
  96. for file in files:
  97. # print(file)
  98. self.efactoauth.validate_invoice(os.path.join(root, file))
  99. # self.efactoauth.validate_invoice('c:/Users/levi/FACT1/MZK/2024_01_23/MZK241157.xml')
  100. def test_rename_all(self, dir_path, kind: Union[str, 'seller', 'buyer']): # @UndefinedVariable
  101. if kind == 'seller':
  102. account = 'AccountingCustomerParty'
  103. # print(os.path.basename(dir_path))
  104. # dir_path = os.path.join(dir_path, os.path.basename(dir_path))
  105. else:
  106. account = 'AccountingSupplierParty'
  107. # next_subfolder = next(os.walk(dir_path))[1][0]
  108. for root_f, _dirs, files in os.walk(dir_path):
  109. for file in files:
  110. # print(root_f.split('/'))
  111. # print(os.path.basename(dir_path))
  112. # print(root_f, file)
  113. if file.endswith('.xml') and file.count('_') < 2 and 'cif error' not in root_f:
  114. # print(os.path.join(root_f, file))
  115. tree = etree.parse(os.path.join(root_f, file))
  116. root = tree.getroot()
  117. inv_id = root.xpath('//cbc:ID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  118. inv_id = inv_id[0].text
  119. parties = root.xpath('//cbc:RegistrationName', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  120. for partie in parties:
  121. parent_p_type = partie.getparent().getparent().getparent()
  122. if etree.QName(parent_p_type).localname == account: # @UndefinedVariable
  123. # print(partie.text)
  124. file_partie = partie.text
  125. is_date = root.xpath('//cbc:IssueDate', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  126. is_date = is_date[0].text
  127. partie_ids = root.xpath('//cbc:CompanyID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  128. for partie_id in partie_ids:
  129. parent_p_type = partie_id.getparent().getparent().getparent()
  130. parent_t_type = partie_id.getparent()
  131. # print(parent_t_type)
  132. if etree.QName(parent_t_type).localname == 'PartyTaxScheme' and etree.QName(parent_p_type).localname == account: # @UndefinedVariable
  133. # print(partie_id.text)
  134. file_partn_id = partie_id.text
  135. new_name = '{}_{}_{}_{}.xml'.format(file.replace('.xml', ''), inv_id, file_partie, is_date)
  136. current_path = os.path.join(root_f, file)
  137. new_path = os.path.join(root_f, new_name)
  138. #
  139. # !This will rename all files
  140. #
  141. os.rename(current_path, new_path)
  142. print(new_name)
  143. else:
  144. print('Nothing to rename')
  145. def test_parse_inv(self):
  146. # inv = self.efactoauth.parse_inv('C:/Users/Levi/git/anafapi/tests/17259191/8262600/FACTURA PRIMITA/01_2024/31/4154212674_booked.xml')
  147. # print(self.efactoauth._get_series(inv.invoice_id))
  148. inv_files = utils.get_not_booked('17259191')
  149. print(inv_files)
  150. if __name__ == "__main__":
  151. # unittest.main()
  152. # efactTest().test_add_invoice()
  153. efactTest().test_get_all_messages('17259191', 10, '17259191', 'prod', 'P', True)
  154. # efactTest().test_rename_all(os.path.join(os.path.expanduser('~'), 'git/anafapi/tests/17259191/FACTURA TRIMISA'), kind='seller')
  155. # efactTest().test_rename_all(os.path.join(os.path.expanduser('~'), 'git/anafapi/tests/17259191/FACTURA PRIMITA'), kind='buyer')
  156. # anoauth.get_token()