test_anafoauth.py 10 KB

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