test_anafoauth.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = 'c:/Users/levi/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_transform_to_pdf(self):
  45. self.efactoauth.transform_to_pdf('c:/Users/DECEL/FACT1/MZK/2023_01_03/MZK2414.xml')
  46. def test_get_all_messages(self):
  47. self.efactoauth.get_all_messages('17259191', days=30, root_path='17259191', env='prod', extract=True)
  48. def test_pretty_messages(self):
  49. self.efactoauth.pretty_messages('17259191', env='prod')
  50. def test_download_invoice(self):
  51. self.efactoauth.download_invoice('3105899770', path='C:/Users/DECEL/FACT1/MZK/2023_06', env='prod')
  52. def test_getxml_to_erp(self):
  53. # self.efactoauth.getxml_from_zip(path='../efact/3016038776.zip', dest_f='')
  54. # latest = self.efactoauth.get_latest_invoice('efact')
  55. inv_files = utils.get_not_booked('efact')
  56. for latest in inv_files:
  57. if 'booked' not in latest:
  58. inv = self.efactoauth.parse_inv(invoice=latest)
  59. print(inv.seller_party.name, inv.invoice_id, inv.issue_date.date())
  60. # for line in inv.lines:
  61. # print(line.tax)
  62. self.efactoauth.addinvoice_tonextup(invoice=inv,
  63. kind='service',
  64. template=32,
  65. exp_plan=370,
  66. warehouse_code=80)
  67. os.rename(latest, latest.replace('.xml'.lower(), '_booked.xml'))
  68. # os.remove(latest)
  69. def test_verify_invoice(self):
  70. self.efactoauth.verify_invoice('4075641152', env='prod')
  71. def test_isvalid(self):
  72. self.efactoauth.load_token_file(token_file_path.format('efact.json'))
  73. print(self.efactoauth.is_valid)
  74. def test_get_latest_invoice(self):
  75. latest = self.efactoauth.get_latest_invoice('efact')
  76. print(latest)
  77. # os.replace(latest, latest.replace('.xml'.lower(), '_booked.xml'))
  78. def test_validate_invoice(self):
  79. # print(os.listdir('c:/Users/DECEL/FACT1/MZK/2023.01.03/'))
  80. # for root, dir, files in os.walk('c:/Users/DECEL/FACT1/MZK/2023_01_03/'):
  81. # for file in files:
  82. # print(file)
  83. # self.efactoauth.validate_invoice(os.path.join(root, file))
  84. self.efactoauth.validate_invoice('c:/Users/DECEL/FACT1/MZK/2023_01_03/MZK2414.xml')
  85. def test_rename_all(self, dir_path, kind: Union[str, 'seller', 'buyer']): # @UndefinedVariable
  86. if kind == 'seller':
  87. account = 'AccountingCustomerParty'
  88. # print(os.path.basename(dir_path))
  89. dir_path = os.path.join(dir_path, os.path.basename(dir_path))
  90. else:
  91. account = 'AccountingSupplierParty'
  92. # next_subfolder = next(os.walk(dir_path))[1][0]
  93. for root_f, _dirs, files in os.walk(dir_path):
  94. for file in files:
  95. # print(root_f)
  96. if file.endswith('.xml') and file.count('_') < 2:
  97. # print(os.path.join(root, file))
  98. tree = etree.parse(os.path.join(root_f, file))
  99. root = tree.getroot()
  100. parties = root.xpath('//cbc:RegistrationName', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  101. for partie in parties:
  102. parent_p_type = partie.getparent().getparent().getparent()
  103. if etree.QName(parent_p_type).localname == account: # @UndefinedVariable
  104. # print(partie.text)
  105. file_partie = partie.text
  106. is_date = root.xpath('//cbc:IssueDate', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  107. is_date = is_date[0].text
  108. partie_ids = root.xpath('//cbc:CompanyID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  109. for partie_id in partie_ids:
  110. parent_p_type = partie_id.getparent().getparent().getparent()
  111. parent_t_type = partie_id.getparent()
  112. # print(parent_t_type)
  113. if etree.QName(parent_t_type).localname == 'PartyTaxScheme' and etree.QName(parent_p_type).localname == account: # @UndefinedVariable
  114. # print(partie_id.text)
  115. file_id = partie_id.text
  116. new_name = '{}_{}_{}_{}.xml'.format(file.replace('.xml', ''), file_id, file_partie, is_date)
  117. current_path = os.path.join(root_f, file)
  118. new_path = os.path.join(root_f, new_name)
  119. if kind == 'buyer':
  120. path_parts = root_f.split('/')
  121. seventh_part = path_parts[7] if len(path_parts) > 6 else None
  122. # print(seventh_part)
  123. if seventh_part != os.path.basename(dir_path):
  124. #
  125. # !This will rename all files
  126. #
  127. # os.rename(current_path, new_path)
  128. print(new_name)
  129. else:
  130. #
  131. # !This will rename all files
  132. #
  133. # os.rename(current_path, new_path)
  134. print(new_name)
  135. if __name__ == "__main__":
  136. # unittest.main()
  137. # efactTest().test_add_invoice()
  138. efactTest().test_get_all_messages()
  139. # efactTest().test_rename_all('c:/Users/levi/git/anafapi/tests/17259191', kind='seller')
  140. # efactTest().test_rename_all('/home/deeejas/git/anafapi/tests/17259191', kind='buyer')
  141. # anoauth.get_token()