Refactor invoice generation

This commit is contained in:
Kuba Winnicki 2025-01-11 13:55:00 +01:00
parent 4ee3a99310
commit d1f3de9ebd

View File

@ -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,