Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions hostfact_python_client/hostfact_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ def call(self, **kwargs):
raise Exception(f"HostFact error: {reply['errors']}" if 'errors' in reply.keys() else Exception("HostFact error."))
return reply

def make_invoice(self,
debtor_code: str,
invoice_lines: list,
newInvoice: bool = False,
attachment=None):
def make_invoice(
self,
debtor_code: str,
invoice_lines: list,
newInvoice: bool = False,
attachment=None,
invoice_reference: str = None
):
method = HostFactCall(self.url, self.api_key, 'invoice', self.debug)

active_invoices = []
Expand All @@ -73,8 +76,13 @@ def make_invoice(self,
active_invoices = method.list(searchat="DebtorCode", searchfor=debtor_code, status=0, sort="Modified")

if newInvoice or (not newInvoice and active_invoices['totalresults'] == 0):
invoice_reply = method.add(DebtorCode=debtor_code, InvoiceLines=invoice_lines)
invoice_reply = method.add(DebtorCode=debtor_code, ReferenceNumber=invoice_reference, InvoiceLines=invoice_lines)
else:
# If an invoice reference is provided, we set it; this means it will also overwrite any reference already
# set on the existing invoice with the (new) one provided.
if invoice_reference not in (None, ""):
method.edit(Identifier=active_invoices['invoices'][0]['Identifier'], ReferenceNumber=invoice_reference)

invoice_line_method = HostFactCall(self.url, self.api_key, 'invoiceline', self.debug)
invoice_reply = invoice_line_method.add(Identifier=active_invoices['invoices'][0]['Identifier'], InvoiceLines=invoice_lines)

Expand Down
20 changes: 11 additions & 9 deletions tests/test_hostfact_python_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def test_make_invoice_http_request(client):

result = client.invoice.make_invoice(**{
"debtor_code": "DB100",
"invoice_reference": "PO123456",
"invoice_lines": [
{
"Description": "test@test.nl",
Expand All @@ -131,20 +132,20 @@ def test_make_invoice_http_request(client):
}
})

assert (result == {'Identifier': '9300'})
assert (result == {'Identifier': '25683'})

invoice = client.invoice.show(Identifier=result['Identifier'])['invoice']

assert (invoice["Attachments"] == [
{
"Identifier": "49",
"Identifier": "17715",
"Filename": "test.txt"
}
])
assert (invoice["InvoiceLines"] == [
{
"Identifier": 318661,
"Date": "2021-06-18",
"Identifier": 351157,
"Date": "2025-09-17",
"Number": "1",
"NumberSuffix": "",
"ProductCode": "",
Expand All @@ -170,6 +171,7 @@ def test_make_invoice_http_request(client):

result = client.invoice.make_invoice(**{
"debtor_code": "DB100",
"invoice_reference": "PO654321",
"invoice_lines": [
{
"Description": "Big money",
Expand All @@ -182,14 +184,14 @@ def test_make_invoice_http_request(client):

assert (invoice["Attachments"] == [
{
"Identifier": "49",
"Identifier": "17715",
"Filename": "test.txt"
}
])
assert (invoice["InvoiceLines"] == [
{
"Identifier": 318661,
"Date": "2021-06-18",
"Identifier": 351157,
"Date": "2025-09-17",
"Number": "1",
"NumberSuffix": "",
"ProductCode": "",
Expand All @@ -211,8 +213,8 @@ def test_make_invoice_http_request(client):
"DiscountAmountExcl": 0
},
{
"Identifier": 318662,
"Date": "2021-06-18",
"Identifier": 351158,
"Date": "2025-09-17",
"Number": "1",
"NumberSuffix": "",
"ProductCode": "",
Expand Down
Loading