Refactoring ifirma core
This commit is contained in:
parent
8483d1432c
commit
b91ddb6685
@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import six
|
import six
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
@ -9,6 +10,9 @@ from python_ifirma.helpers import Helpers
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
from urllib.parse import urljoin, quote
|
||||||
|
|
||||||
|
|
||||||
class VAT:
|
class VAT:
|
||||||
VAT_0 = 0.00
|
VAT_0 = 0.00
|
||||||
VAT_5 = 0.05
|
VAT_5 = 0.05
|
||||||
@ -50,17 +54,17 @@ class Client:
|
|||||||
"Ulica": self.address.street,
|
"Ulica": self.address.street,
|
||||||
"Miejscowosc": self.address.city,
|
"Miejscowosc": self.address.city,
|
||||||
"Kraj": self.address.country,
|
"Kraj": self.address.country,
|
||||||
#"Email": self.email,
|
# "Email": self.email,
|
||||||
#"Telefon": self.phone_number,
|
# "Telefon": self.phone_number,
|
||||||
"OsobaFizyczna": False,
|
"OsobaFizyczna": False,
|
||||||
}
|
}
|
||||||
if self.export == "eu":
|
if self.export == "eu":
|
||||||
d.update({"PrefiksUE": self.tax_id[:2]})
|
d.update({"PrefiksUE": self.tax_id[:2]})
|
||||||
if self.export == "eu" or self.export == "yes":
|
if self.export == "eu" or self.export == "yes":
|
||||||
d.update({"NIP": self.tax_id[2:]})
|
d.update({"NIP": self.tax_id[2:]})
|
||||||
#if self.export == "eu":
|
# if self.export == "eu":
|
||||||
#d.update({"PrefiksUE": self.tax_id[:2]})
|
# d.update({"PrefiksUE": self.tax_id[:2]})
|
||||||
#elif self.export == "yes":
|
# elif self.export == "yes":
|
||||||
d.update({"Kraj": self.tax_id[:2]})
|
d.update({"Kraj": self.tax_id[:2]})
|
||||||
else:
|
else:
|
||||||
d.update({"NIP": self.tax_id})
|
d.update({"NIP": self.tax_id})
|
||||||
@ -147,7 +151,7 @@ class NewInvoiceParams:
|
|||||||
d.update({
|
d.update({
|
||||||
"DataObowiazkuPodatkowego": self.__get_issue_date(),
|
"DataObowiazkuPodatkowego": self.__get_issue_date(),
|
||||||
"NazwaUslugi": "services",
|
"NazwaUslugi": "services",
|
||||||
"Waluta": self.currency, #"PLN",
|
"Waluta": self.currency,
|
||||||
"Jezyk": "en",
|
"Jezyk": "en",
|
||||||
"KursWalutyWidoczny": False,
|
"KursWalutyWidoczny": False,
|
||||||
"KursWalutyZDniaPoprzedzajacegoDzienWystawieniaFaktury": 1.00,
|
"KursWalutyZDniaPoprzedzajacegoDzienWystawieniaFaktury": 1.00,
|
||||||
@ -164,6 +168,8 @@ class NewInvoiceParams:
|
|||||||
|
|
||||||
|
|
||||||
class iFirmaAPI():
|
class iFirmaAPI():
|
||||||
|
__base_url = "https://www.ifirma.pl/iapi/"
|
||||||
|
|
||||||
__username = None
|
__username = None
|
||||||
__invoice_key_name = 'faktura'
|
__invoice_key_name = 'faktura'
|
||||||
__invoice_key_value = None
|
__invoice_key_value = None
|
||||||
@ -195,17 +201,37 @@ class iFirmaAPI():
|
|||||||
|
|
||||||
return response_dict
|
return response_dict
|
||||||
|
|
||||||
def __create_authentication_header_value(self, request_hash_text, key_value=""):
|
def __get_auth_header(self, request_hash_text, key_value=""):
|
||||||
key_value = key_value or self.__invoice_key_value
|
key_value = key_value or self.__invoice_key_value
|
||||||
return "IAPIS user={}, hmac-sha1={}".format(
|
return "IAPIS user={}, hmac-sha1={}".format(
|
||||||
self.__username,
|
self.__username,
|
||||||
Helpers.get_hmac_of_text(key_value, request_hash_text)
|
Helpers.get_hmac_of_text(key_value, request_hash_text)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __get(self, url_path, key_name, key_value="", params={}):
|
||||||
|
"""Send GET request to IFirma API"""
|
||||||
|
|
||||||
|
url = urljoin(self.__base_url, url_path)
|
||||||
|
request_hash_text = f"{url}{self.__username}{key_name}"
|
||||||
|
headers = {
|
||||||
|
# Content-type not needed in GET requests
|
||||||
|
# "Content-type": "application/json; charset=UTF-8",
|
||||||
|
"Accept": "application/json",
|
||||||
|
"Authentication": self.__get_auth_header(request_hash_text,
|
||||||
|
key_value=key_value)
|
||||||
|
}
|
||||||
|
response = requests.get(url, headers=headers, params=params)
|
||||||
|
return json.loads(response.content.decode('utf-8'))
|
||||||
|
|
||||||
|
def find_partner(self, keyword):
|
||||||
|
return self.__get(f"kontrahenci/{quote(keyword)}.json",
|
||||||
|
self.__invoice_key_name)
|
||||||
|
|
||||||
def __create_invoice_and_return_id(self, invoice, url):
|
def __create_invoice_and_return_id(self, invoice, url):
|
||||||
# from pprint import pprint
|
# from pprint import pprint
|
||||||
# pprint(invoice.get_request_data())
|
# pprint(invoice.get_request_data())
|
||||||
request_content = json.dumps(invoice.get_request_data(), separators=(',', ':'))
|
request_content = json.dumps(invoice.get_request_data(),
|
||||||
|
separators=(',', ':'))
|
||||||
request_hash_text = "{}{}{}{}".format(
|
request_hash_text = "{}{}{}{}".format(
|
||||||
url,
|
url,
|
||||||
self.__username,
|
self.__username,
|
||||||
@ -215,7 +241,8 @@ class iFirmaAPI():
|
|||||||
headers = {
|
headers = {
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-type": "application/json; charset=UTF-8",
|
"Content-type": "application/json; charset=UTF-8",
|
||||||
"Authentication": self.__create_authentication_header_value(request_hash_text)
|
"Authentication": self.__get_auth_header(request_hash_text,
|
||||||
|
key_value=__user_key_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
response_dict = self.__execute_post_request(headers, request_content, url)
|
response_dict = self.__execute_post_request(headers, request_content, url)
|
||||||
@ -243,21 +270,13 @@ class iFirmaAPI():
|
|||||||
self.__post_cost(cost_json, url)
|
self.__post_cost(cost_json, url)
|
||||||
|
|
||||||
def get_accounting_month(self):
|
def get_accounting_month(self):
|
||||||
url = 'https://www.ifirma.pl/iapi/abonent/miesiacksiegowy.json'
|
response = self.__get("abonent/miesiacksiegowy.json",
|
||||||
request_hash_text = "{}{}{}".format(
|
self.__user_key_name,
|
||||||
url,
|
key_value=self.__user_key_value)
|
||||||
self.__username,
|
|
||||||
self.__user_key_name,
|
month = response['response']["MiesiacKsiegowy"]
|
||||||
)
|
year = response['response']['RokKsiegowy']
|
||||||
headers = {
|
return year, month
|
||||||
"Accept": "application/json",
|
|
||||||
"Content-type": "application/json; charset=UTF-8",
|
|
||||||
"Authentication":
|
|
||||||
self.__create_authentication_header_value(request_hash_text, self.__user_key_value)
|
|
||||||
}
|
|
||||||
resp = requests.get(url, headers=headers)
|
|
||||||
content = resp.content
|
|
||||||
return content
|
|
||||||
|
|
||||||
def __post_cost(self, cost_json, url):
|
def __post_cost(self, cost_json, url):
|
||||||
request_content = json.dumps(cost_json, separators=(',', ':'))
|
request_content = json.dumps(cost_json, separators=(',', ':'))
|
||||||
@ -270,7 +289,7 @@ class iFirmaAPI():
|
|||||||
headers = {
|
headers = {
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-type": "application/json; charset=UTF-8",
|
"Content-type": "application/json; charset=UTF-8",
|
||||||
"Authentication": self.__create_authentication_header_value(request_hash_text)
|
"Authentication": self.__get_auth_header(request_hash_text)
|
||||||
}
|
}
|
||||||
|
|
||||||
response_dict = self.__execute_post_request(headers, request_content, url)
|
response_dict = self.__execute_post_request(headers, request_content, url)
|
||||||
@ -281,6 +300,15 @@ class iFirmaAPI():
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def list_invoices(self, date_from="", date_to=""):
|
||||||
|
params = {"dataOd": date_from}
|
||||||
|
if date_to:
|
||||||
|
params.update(dataDo=date_to)
|
||||||
|
|
||||||
|
return self.__get("faktury.json", self.__invoice_key_name,
|
||||||
|
params=params)
|
||||||
|
|
||||||
|
|
||||||
def get_invoice_list(self, limit=0):
|
def get_invoice_list(self, limit=0):
|
||||||
url = "https://www.ifirma.pl/iapi/fakturaeksportuslugue/list.json"
|
url = "https://www.ifirma.pl/iapi/fakturaeksportuslugue/list.json"
|
||||||
request_hash_text = "{}{}{}".format(
|
request_hash_text = "{}{}{}".format(
|
||||||
@ -291,7 +319,7 @@ class iFirmaAPI():
|
|||||||
headers = {
|
headers = {
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-type": "application/json; charset=UTF-8",
|
"Content-type": "application/json; charset=UTF-8",
|
||||||
"Authentication": self.__create_authentication_header_value(request_hash_text)
|
"Authentication": self.__get_auth_header(request_hash_text)
|
||||||
}
|
}
|
||||||
params = {}
|
params = {}
|
||||||
if limit:
|
if limit:
|
||||||
@ -301,7 +329,7 @@ class iFirmaAPI():
|
|||||||
return rj
|
return rj
|
||||||
|
|
||||||
def get_invoice(self, invoice_id):
|
def get_invoice(self, invoice_id):
|
||||||
url = "https://www.ifirma.pl/iapi/fakturaeksportuslug/{}.json".format(invoice_id)
|
url = f"https://www.ifirma.pl/iapi/fakturaeksportuslug/{invoice_id}.json"
|
||||||
request_hash_text = "{}{}{}".format(
|
request_hash_text = "{}{}{}".format(
|
||||||
url,
|
url,
|
||||||
self.__username,
|
self.__username,
|
||||||
@ -310,7 +338,7 @@ class iFirmaAPI():
|
|||||||
headers = {
|
headers = {
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-type": "application/json; charset=UTF-8",
|
"Content-type": "application/json; charset=UTF-8",
|
||||||
"Authentication": self.__create_authentication_header_value(request_hash_text)
|
"Authentication": self.__get_auth_header(request_hash_text)
|
||||||
}
|
}
|
||||||
resp = requests.get(url, headers=headers)
|
resp = requests.get(url, headers=headers)
|
||||||
rj = json.loads(resp.content.decode('utf-8'))
|
rj = json.loads(resp.content.decode('utf-8'))
|
||||||
@ -320,11 +348,11 @@ class iFirmaAPI():
|
|||||||
return rj
|
return rj
|
||||||
|
|
||||||
def get_invoice_pdf(self, invoice_id):
|
def get_invoice_pdf(self, invoice_id):
|
||||||
url = "https://www.ifirma.pl/iapi/fakturakraj/{}.pdf".format(invoice_id)
|
url = f"https://www.ifirma.pl/iapi/fakturakraj/{invoice_id}.pdf"
|
||||||
return self.__download_pdf(url)
|
return self.__download_pdf(url)
|
||||||
|
|
||||||
def __get_invoice_number(self, invoice_id):
|
def __get_invoice_number(self, invoice_id):
|
||||||
url = "https://www.ifirma.pl/iapi/fakturakraj/{}.json".format(invoice_id)
|
url = f"https://www.ifirma.pl/iapi/fakturakraj/{invoice_id}.json"
|
||||||
request_hash_text = "{}{}{}".format(
|
request_hash_text = "{}{}{}".format(
|
||||||
url,
|
url,
|
||||||
self.__username,
|
self.__username,
|
||||||
@ -333,7 +361,7 @@ class iFirmaAPI():
|
|||||||
headers = {
|
headers = {
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-type": "application/json; charset=UTF-8",
|
"Content-type": "application/json; charset=UTF-8",
|
||||||
"Authentication": self.__create_authentication_header_value(request_hash_text)
|
"Authentication": self.__get_auth_header(request_hash_text)
|
||||||
}
|
}
|
||||||
resp = requests.get(url, headers=headers)
|
resp = requests.get(url, headers=headers)
|
||||||
rj = json.loads(resp.content.decode('utf-8'))
|
rj = json.loads(resp.content.decode('utf-8'))
|
||||||
@ -351,7 +379,7 @@ class iFirmaAPI():
|
|||||||
headers = {
|
headers = {
|
||||||
"Accept": "application/pdf",
|
"Accept": "application/pdf",
|
||||||
"Content-type": "application/pdf; charset=UTF-8",
|
"Content-type": "application/pdf; charset=UTF-8",
|
||||||
"Authentication": self.__create_authentication_header_value(request_hash_text)
|
"Authentication": self.__get_auth_header(request_hash_text)
|
||||||
}
|
}
|
||||||
resp = requests.get(url, headers=headers)
|
resp = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
|||||||
4
setup.py
4
setup.py
@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="python-ifirma",
|
name="python-ifirma",
|
||||||
version="0.0.5",
|
version="0.1.0b2",
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'requests',
|
'requests',
|
||||||
@ -12,4 +12,4 @@ setup(
|
|||||||
author_email='dariusz@aniszewski.eu',
|
author_email='dariusz@aniszewski.eu',
|
||||||
license='BSD License',
|
license='BSD License',
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user