test_anafoauth.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. '''Created 6 Jul 2022 Levi'''
  2. # coding: UTF-8
  3. import platform
  4. import unittest
  5. import os
  6. from lxml import etree
  7. from anafapi.anafoauth import Anafoauth, Efactoauth
  8. from anafapi import utils
  9. if platform.system() == 'Linux':
  10. token_file_path = '/home/deeejas/git/anafapi/anafapi/{}'
  11. elif platform.system() == 'Windows':
  12. token_file_path = 'c:/Users/levi/git/anafapi/anafapi/{}'
  13. class oauthTest(unittest.TestCase):
  14. anoauth = Anafoauth(
  15. client_id='16cbef8754bef0bcf015529353427e8a7e3ee71d56879565',
  16. client_secret='c6aa66d89002f36461f93facd990e6bf5bc706e1df897e8a7e3ee71d56879565',
  17. redirect_uri='https://www.anaf.ro/oauth2callback',
  18. )
  19. def test_is_valid(self):
  20. self.anoauth.load_token_file(token_file_path.format('efact.json'))
  21. # print(self.anoauth.to)
  22. print(self.anoauth.is_valid)
  23. def test_browser_redirect(self):
  24. print(self.anoauth._browser_redirect())
  25. def test_get_token(self):
  26. self.anoauth.get_token()
  27. self.anoauth.save_token(token_file_path.format('efact.json'))
  28. def test_revive_token(self):
  29. self.anoauth.load_token_file(token_file_path.format('efact.json'))
  30. self.anoauth.revive_token()
  31. self.anoauth.save_token(token_file_path.format('efact_refreshed.json'))
  32. class efactTest(unittest.TestCase):
  33. efactoauth = Efactoauth(
  34. client_id='16cbef8754bef0bcf015529353427e8a7e3ee71d56879565',
  35. client_secret='c6aa66d89002f36461f93facd990e6bf5bc706e1df897e8a7e3ee71d56879565',
  36. redirect_uri='https://www.anaf.ro/oauth2callback',
  37. )
  38. efactoauth.load_token_file(token_file_path.format('efact.json'))
  39. efactoauth.load_session()
  40. def test_add_invoice(self):
  41. # self.efactoauth.validate_invoice('C:/Users/DECEL/FACT1/MZK/2023_06/MZK2321602.xml', env='prod')
  42. self.efactoauth.add_invoice('17259191', 'C:/Users/DECEL/FACT1/MZK/2023_06/MZK2321602.xml', env='prod')
  43. def test_transform_to_pdf(self):
  44. self.efactoauth.transform_to_pdf('c:/Users/DECEL/FACT1/MZK/2023_01_03/MZK2414.xml')
  45. def test_get_all_messages(self):
  46. self.efactoauth.get_all_messages('17259191', days=30, root_path='17259191', env='prod', extract=True)
  47. def test_pretty_messages(self):
  48. self.efactoauth.pretty_messages('17259191', env='prod')
  49. def test_download_invoice(self):
  50. self.efactoauth.download_invoice('3105899770', path='C:/Users/DECEL/FACT1/MZK/2023_06', env='prod')
  51. def test_getxml_to_erp(self):
  52. # self.efactoauth.getxml_from_zip(path='../efact/3016038776.zip', dest_f='')
  53. # latest = self.efactoauth.get_latest_invoice('efact')
  54. inv_files = utils.get_not_booked('efact')
  55. for latest in inv_files:
  56. if 'booked' not in latest:
  57. inv = self.efactoauth.parse_inv(invoice=latest)
  58. print(inv.seller_party.name, inv.invoice_id, inv.issue_date.date())
  59. # for line in inv.lines:
  60. # print(line.tax)
  61. self.efactoauth.addinvoice_tonextup(invoice=inv,
  62. kind='service',
  63. template=32,
  64. exp_plan=370,
  65. warehouse_code=80)
  66. os.rename(latest, latest.replace('.xml'.lower(), '_booked.xml'))
  67. # os.remove(latest)
  68. def test_verify_invoice(self):
  69. self.efactoauth.verify_invoice('4075641152', env='prod')
  70. def test_isvalid(self):
  71. self.efactoauth.load_token_file(token_file_path.format('efact.json'))
  72. print(self.efactoauth.is_valid)
  73. def test_get_latest_invoice(self):
  74. latest = self.efactoauth.get_latest_invoice('efact')
  75. print(latest)
  76. # os.replace(latest, latest.replace('.xml'.lower(), '_booked.xml'))
  77. def test_validate_invoice(self):
  78. # print(os.listdir('c:/Users/DECEL/FACT1/MZK/2023.01.03/'))
  79. # for root, dir, files in os.walk('c:/Users/DECEL/FACT1/MZK/2023_01_03/'):
  80. # for file in files:
  81. # print(file)
  82. # self.efactoauth.validate_invoice(os.path.join(root, file))
  83. self.efactoauth.validate_invoice('c:/Users/DECEL/FACT1/MZK/2023_01_03/MZK2414.xml')
  84. def test_renamer(self):
  85. tree = etree.parse('c:/Users/levi/FACT1/MZK/2023_01_03/MZK2414.xml')
  86. root = tree.getroot()
  87. parties = root.xpath('//cbc:RegistrationName', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  88. is_date = root.xpath('//cbc:IssueDate', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  89. partie_id = root.xpath('//cbc:CompanyID', namespaces={'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'})
  90. print(parties[1].text, is_date[0].text, partie_id[3].text)
  91. # for partie in parties:
  92. # print(partie.getparent())
  93. if __name__ == "__main__":
  94. # unittest.main()
  95. # efactTest().test_add_invoice()
  96. efactTest().test_get_all_messages()
  97. # efactTest().test_renamer()
  98. # anoauth.get_token()