From d1f3de9ebdf8c1d10c467a50fd998bd4b7be35d2 Mon Sep 17 00:00:00 2001 From: Kuba Winnicki Date: Sat, 11 Jan 2025 13:55:00 +0100 Subject: [PATCH] Refactor invoice generation --- python_ifirma/core.py | 51 ++++++++++++--------------------------------------- 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/python_ifirma/core.py b/python_ifirma/core.py index 482fb35..13b569f 100644 --- a/python_ifirma/core.py +++ b/python_ifirma/core.py @@ -226,8 +226,8 @@ class iFirmaAPI(): real_response_content = response_dict["response"] response_code = real_response_content.get("Kod", -1) - if response_code != 0: - print(real_response_content) + if response_code not in (0, + 202): # 'Numer faktury' nie jest unikalne raise PythonIfirmaExceptionFactory.throw_exception_by_code(response_code) return response_dict @@ -236,16 +236,26 @@ class iFirmaAPI(): return self.__get(f"kontrahenci/{quote(keyword)}.json", self.__invoice_key_name) + # Invoice generation def __create_invoice_and_return_id(self, invoice, url): response_dict = self.__request("POST", url, self.__invoice_key_name, invoice.get_request_data()) + breakpoint() if response_dict["response"].get("Identyfikator"): invoice_id = response_dict["response"]["Identyfikator"] return invoice_id else: return None + def __get_invoice_number(self, invoice_id): + rj = self.__get(f"fakturakraj/{invoice_id}.json", + self.__user_key_name) + + if "Kod" in rj["response"] and rj["response"]["Kod"] != 200: + return None + return rj["response"]["PelnyNumer"] + def generate_invoice(self, invoice, endpoint="fakturakraj.json"): invoice_id = self.__create_invoice_and_return_id(invoice, endpoint) if invoice_id: @@ -301,47 +311,10 @@ class iFirmaAPI(): return self.__get("faktury.json", self.__invoice_key_name, params=params) - def get_invoice(self, invoice_id): - url = f"https://www.ifirma.pl/iapi/fakturaeksportuslug/{invoice_id}.json" - request_hash_text = "{}{}{}".format( - url, - self.__username, - self.__invoice_key_name, - ) - headers = { - "Accept": "application/json", - "Content-type": "application/json; charset=UTF-8", - "Authentication": self.__get_auth_header(request_hash_text) - } - resp = requests.get(url, headers=headers) - rj = json.loads(resp.content.decode('utf-8')) - if "Kod" in rj["response"] and rj["response"]["Kod"] != 200: - print(rj) - else: - return rj - def get_invoice_pdf(self, invoice_id): url = f"https://www.ifirma.pl/iapi/fakturakraj/{invoice_id}.pdf" return self.__download_pdf(url) - def __get_invoice_number(self, invoice_id): - url = f"https://www.ifirma.pl/iapi/fakturakraj/{invoice_id}.json" - request_hash_text = "{}{}{}".format( - url, - self.__username, - self.__invoice_key_name, - ) - headers = { - "Accept": "application/json", - "Authentication": self.__get_auth_header(request_hash_text) - } - resp = requests.get(url, headers=headers) - rj = json.loads(resp.content.decode('utf-8')) - if "Kod" in rj["response"] and rj["response"]["Kod"] != 200: - print(rj) - else: - return rj["response"]["PelnyNumer"] - def __download_pdf(self, url): request_hash_text = "{}{}{}".format( url,