Explorar el Código

due_date handle if None

marton levente hace 3 meses
padre
commit
f23fb607ed
Se han modificado 1 ficheros con 28 adiciones y 24 borrados
  1. 28 24
      anafapi/anafoauth.py

+ 28 - 24
anafapi/anafoauth.py

@@ -8,11 +8,11 @@ import random
 from zipfile import ZipFile
 
 import jwt
-from plyer import notification
+from plyer import notification # noqa
 from selenium import webdriver
 from bs4 import BeautifulSoup
 from requests.auth import HTTPBasicAuth
-from requests_oauthlib import OAuth2Session
+from requests_oauthlib import OAuth2Session # noqa
 from requests.exceptions import JSONDecodeError
 
 import anafapi.nextup
@@ -57,7 +57,7 @@ class Anafoauth(object):
         # print(decoded_token)
         return decoded_token
 
-    def get_token(self) -> ujson:
+    def get_token(self) -> dict:
         # https://chat.openai.com/c/61c5c1c5-298a-4073-88c7-d69ce99a140e
         # oauth = OAuth2Session(self.client_id, redirect_uri=self.redirect_uri)
         authorization_url, _state = self.oauth.authorization_url(self.AUTH_URL)
@@ -88,7 +88,7 @@ class Anafoauth(object):
         print(self.token)
         return self.token
 
-    def get_jwt_token(self) -> ujson:
+    def get_jwt_token(self) -> dict:
         # https://chat.openai.com/c/61c5c1c5-298a-4073-88c7-d69ce99a140e
         # oauth = OAuth2Session(self.client_id, redirect_uri=self.redirect_uri)
         authorization_url, _state = self.oauth.authorization_url(self.AUTH_URL, **self.TYPE_JWT)
@@ -122,7 +122,7 @@ class Anafoauth(object):
         print(self.jwt_token)
         return self.jwt_token
 
-    def get_jwt_token_requests(self) -> ujson:
+    def get_jwt_token_requests(self) -> dict:
         import requests
 
         client_id = self.client_id
@@ -158,7 +158,7 @@ class Anafoauth(object):
         # print(f'Token-ul de acces ANAF de tip JWT: {access_token}')
         return self.jwt_token
 
-    def revive_token(self) -> ujson:
+    def revive_token(self) -> dict:
         refr_token = self.token_dict['refresh_token']
         print(refr_token)
         self.token = self.oauth.refresh_token(
@@ -274,8 +274,8 @@ class Efactoauth(Anafoauth):
             for index_, inv in enumerate(self.invoices['mesaje'], start=1):
                 date_ = datetime.date(int(inv['data_creare'][:4]), int(inv['data_creare'][4:6]), int(inv['data_creare'][6:8]))
                 try:
-                    cif = re.search('(?<=cif_emitent=)[0-9]+', inv['detalii']).group()
-                    cif_ben = re.search('(?<=cif_beneficiar=)[0-9]+', inv['detalii']).group()
+                    cif = re.search('(?<=cif_emitent=)[0-9]+', inv['detalii']).group() # noqa
+                    cif_ben = re.search('(?<=cif_beneficiar=)[0-9]+', inv['detalii']).group() # noqa
                 except AttributeError:
                     cif = 'cif error'
                     cif_ben = 'cif_ben erro'
@@ -310,7 +310,7 @@ class Efactoauth(Anafoauth):
             # print(resp.text, resp.status_code)
             return resp.text
 
-    def download_invoice(self, download_id, path='efact', env='test') -> None:
+    def download_invoice(self, download_id, path='efact', env='test') -> bool: # noqa
         url = self.DOWNLOAD.format(env, download_id)
         resp = self.oauth.get(url=url)
         try:
@@ -339,11 +339,11 @@ class Efactoauth(Anafoauth):
         soup = BeautifulSoup(resp.text, 'xml')
         pretty_xml = soup.prettify()
         print(pretty_xml)
-        print('stare', soup.header.get('stare'))
-        print('id_descarcare', soup.header.get('id_descarcare'))
+        print('stare', soup.header.get('stare')) # noqa
+        print('id_descarcare', soup.header.get('id_descarcare')) # noqa
         return resp.text
 
-    def _extract_zipfile(self, inv_zip, dest_f=None) -> os.path:
+    def _extract_zipfile(self, inv_zip, dest_f=None) -> str:
         for file_ in inv_zip.infolist():
             if not os.path.isfile(os.path.join(dest_f, file_.filename)):
                 if 'semnatura' not in file_.filename:
@@ -354,7 +354,7 @@ class Efactoauth(Anafoauth):
                 xml_inv = os.path.join(dest_f, file_.filename)
         return xml_inv
 
-    def getxml_from_zip(self, path=None, dest_f=None) -> os.path:
+    def getxml_from_zip(self, path=None, dest_f=None) -> str: # noqa
         if path:
             inv_zip = ZipFile(path, mode='r')
             name = [name for name in inv_zip.namelist() if 'semnatura' not in name]
@@ -395,19 +395,19 @@ class Efactoauth(Anafoauth):
                 #     os.remove(self.xml_inv)
                     return self.xml_inv
 
-    def parse_inv(self, invoice: os.path=None) -> Invoice:
+    def parse_inv(self, invoice: str=None) -> Invoice:
         if invoice:
             invoice = Invoice.from_xml(invoice)
         else:
             invoice = Invoice.from_xml(self.xml_inv)
         # print(invoice.seller_party.name, invoice.lines, str(int(invoice.lines[0].tax.percent * 100)), invoice.lines[0].price.amount)
         self.e_invoice = invoice
-        return self.e_invoice
+        return self.e_invoice # noqa
 
     def _parse_body(self, invoice: Invoice) -> str:
         art_list = [(line.quantity, line.item_name, line.price, line.currency) for line in invoice.lines]
-        art_list = map(lambda x: str(x), art_list)
-        art_list = "\n".join(art_list)
+        art_list = map(lambda x: str(x), art_list) # noqa
+        art_list = "\n".join(art_list) # noqa
         body = f'''E-invoice:
         Nr. {invoice.invoice_id}
         date: {invoice.issue_date.strftime("%Y-%m-%d")}
@@ -434,7 +434,7 @@ class Efactoauth(Anafoauth):
             num = 1
             return num
 
-    def addinvoice_tonextup(self, invoice: Invoice=None,
+    def addinvoice_tonextup(self, invoice: Invoice,
                             kind: str='stock',
                             template: int=32,
                             inc_plan: int=None,
@@ -500,11 +500,11 @@ class Efactoauth(Anafoauth):
             for line in invoice.lines:
                 if line.item_name.upper() not in names:
                     code = random.randint(10000000, 99999999)
-                    code = f'EN-{str(code)}'
+                    code = f'EN-{str(code)}' # noqa
                     line.item_code = code
                     print(f'...adding missing article <{line.item_code}>-<{line.item_name}>')
                     lines.append(line)
-                    client.add_article(line.item_code,
+                    client.add_article(line.item_code, # noqa
                                        line.item_name.upper(),
                                        measure_unitid=MEAS_UNITS[line.unit_code],
                                        article_templateid=template)
@@ -516,11 +516,11 @@ class Efactoauth(Anafoauth):
             for line in invoice.lines:
                 if line.item_name.upper() not in names:
                     code = random.randint(10000000, 99999999)
-                    code = f'EN-{str(code)}'
+                    code = f'EN-{str(code)}' # noqa
                     line.item_code = code
                     print(f'...adding missing article <{line.item_code}>-<{line.item_name}>')
                     lines.append(line)
-                    client.add_article(line.item_code,
+                    client.add_article(line.item_code, # noqa
                                        line.item_name.upper(),
                                        measure_unitid=MEAS_UNITS[line.unit_code],
                                        article_templateid=None,
@@ -550,11 +550,15 @@ class Efactoauth(Anafoauth):
         for line in invoice.lines:
             notes_ += ' ' + line.item_name
             notes_ = notes_[:1000]
+        if not (due_date := invoice.due_date): # noqa E231
+            due_date = None
+        else:
+            due_date = invoice.due_date.strftime('%Y-%m-%d')
         post = client.add_invoice2(code_,  # ciel.partners.PARTNERS[invoice.seller_party.name]
                                    lines=document_lines,
                                    date=invoice.issue_date.strftime('%Y-%m-%d'),
-                                   due_date=invoice.due_date.strftime('%Y-%m-%d'),
-                                   warehouse_code=warehouse_code,
+                                   due_date=due_date,
+                                   warehouse_code=warehouse_code, # noqa
                                    nrdoc=num,
                                    series=series,
                                    note=notes_)