forked from aleaforny/python-invoice-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoice_generator.py
161 lines (143 loc) · 5.79 KB
/
invoice_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import requests
import json
import pytz
import locale
from datetime import datetime
class InvoiceGenerator:
""" API Object for Invoice-Generator tool - https://invoice-generator.com/ """
URL = "https://invoice-generator.com"
API_KEY = "SET_YOUR_API_KEY_HERE"
DATE_FORMAT = "%d %b %Y"
LOCALE = "fr_FR"
TIMEZONE = "Europe/Paris"
# Below are the default template parameters that can be changed (see https://github.com/Invoiced/invoice-generator-api/)
TEMPLATE_PARAMETERS = [
"header",
"to_title",
"ship_to_title",
"invoice_number_title",
"date_title",
"payment_terms_title",
"due_date_title",
"purchase_order_title",
"quantity_header",
"item_header",
"unit_cost_header",
"amount_header",
"subtotal_title",
"discounts_title",
"tax_title",
"shipping_title",
"total_title",
"amount_paid_title",
"balance_title",
"terms_title",
"notes_title",
]
def __init__(self, sender, to,
logo=None,
ship_to=None,
number=None,
payments_terms=None,
due_date=None,
notes=None,
terms=None,
currency="USD",
date=datetime.now(tz=pytz.timezone(TIMEZONE)),
discounts=0,
tax=0,
shipping=0,
amount_paid=0,
):
""" Object constructor """
self.logo = logo
self.sender = sender
self.to = to
self.ship_to = ship_to
self.number = number
self.currency = currency
self.custom_fields = []
self.date = date
self.payment_terms = payments_terms
self.due_date = due_date
self.items = []
self.fields = {"tax": "%", "discounts": False, "shipping": False}
self.discounts = discounts
self.tax = tax
self.shipping = shipping
self.amount_paid = amount_paid
self.notes = notes
self.terms = terms
self.template = {}
def _to_json(self):
"""
Parsing the object as JSON string
Please note we need also to replace the key sender to from, as per expected in the API but incompatible with from keyword inherent to Python
We are formatting here the correct dates
We are also resetting the two list of Objects items and custom_fields so that it can be JSON serializable
Finally, we are handling template customization with its dict
"""
locale.setlocale(locale.LC_ALL, InvoiceGenerator.LOCALE)
object_dict = self.__dict__
object_dict['from'] = object_dict.get('sender')
object_dict['date'] = self.date.strftime(InvoiceGenerator.DATE_FORMAT)
if object_dict['due_date'] is not None:
object_dict['due_date'] = self.due_date.strftime(InvoiceGenerator.DATE_FORMAT)
object_dict.pop('sender')
for index, item in enumerate(object_dict['items']):
object_dict['items'][index] = item.__dict__
for index, custom_field in enumerate(object_dict['custom_fields']):
object_dict['custom_fields'][index] = custom_field.__dict__
for template_parameter, value in self.template.items():
object_dict[template_parameter] = value
object_dict.pop('template')
return json.dumps(object_dict)
def add_custom_field(self, name=None, value=None):
""" Add a custom field to the invoice """
self.custom_fields.append(CustomField(
name=name,
value=value
))
def add_item(self, name=None, quantity=0, unit_cost=0.0, description=None):
""" Add item to the invoice """
self.items.append(Item(
name=name,
quantity=quantity,
unit_cost=unit_cost,
description=description
))
def download(self, file_path):
""" Directly send the request and store the file on path """
json_string = self._to_json()
response = requests.post(InvoiceGenerator.URL, json=json.loads(json_string), stream=True, headers={'Accept-Language': InvoiceGenerator.LOCALE, 'Authorization': f'Bearer {self.API_KEY}'})
if response.status_code == 200:
open(file_path, 'wb').write(response.content)
else:
raise Exception(f"Invoice download request returned the following message:{response.json()} Response code = {response.status_code} ")
def set_template_text(self, template_parameter, value):
""" If you want to change a default value for customising your invoice template, call this method """
if template_parameter in InvoiceGenerator.TEMPLATE_PARAMETERS:
self.template[template_parameter] = value
else:
raise ValueError("The parameter {} is not a valid template parameter. See docs.".format(template_parameter))
def toggle_subtotal(self, tax="%", discounts=False, shipping=False):
""" Toggle lines of subtotal """
self.fields = {
"tax": tax,
"discounts": discounts,
"shipping": shipping
}
class Item:
""" Item object for an invoice """
def __init__(self, name, quantity, unit_cost, description=""):
""" Object constructor """
self.name = name
self.quantity = quantity
self.unit_cost = unit_cost
self.description = description
class CustomField:
""" Custom Field object for an invoice """
def __init__(self, name, value):
""" Object constructor """
self.name = name
self.value = value