-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice_generator.py
209 lines (174 loc) · 6.37 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import argparse
import json
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_RIGHT, TA_LEFT
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
from reportlab.platypus.flowables import HRFlowable
def get_currency_symbol(currency):
currency_symbols = {
'USD': '$',
'GBP': '£',
'EUR': '€'
}
return currency_symbols.get(currency, '$') # Default to $ if currency not found
def generate_invoice(config_file, invoice_number, date, hours):
# Load company and client details from the specified config file
with open(config_file, "r") as f:
config = json.load(f)
company_name = config["company_name"]
company_address = config["company_address"]
bank_details = config["bank_details"]
client_name = config["client_name"]
client_address = config["client_address"]
rate = float(config["rate"])
currency = config.get("currency", "USD")
currency_symbol = get_currency_symbol(currency)
unit_of_work = config.get("unit_of_work", "HOURLY")
if unit_of_work == "DAILY":
unit_header = "Number of Days"
rate_header = "Daily Rate"
elif unit_of_work == "WEEKLY":
unit_header = "Number of Weeks"
rate_header = "Weekly Rate"
else:
unit_header = "Number of Hours"
rate_header = "Hourly Rate"
include_vat = config.get("include_vat", False)
total_amount = hours * rate
file_name = f"invoice_{invoice_number}.pdf"
doc = SimpleDocTemplate(file_name, pagesize=letter)
styles = getSampleStyleSheet()
elements = []
# Define color scheme
blue_color = colors.Color(0, 0.3, 0.5) # Dark blue for title and amount due
# Add company name
title_style = ParagraphStyle(
"Title", parent=styles["Title"], textColor=blue_color, spaceAfter=6
)
elements.append(Paragraph(company_name, title_style))
# Add horizontal line under the title
elements.append(
HRFlowable(width="100%", thickness=1, color=colors.black, spaceAfter=6)
)
# Create a right-aligned style for invoice details
right_style = ParagraphStyle(
"RightAlign", parent=styles["Normal"], alignment=TA_RIGHT
)
# Create a table for company address and invoice details
data = [
[
Paragraph(company_address[0], styles["Normal"]),
Paragraph(f"Invoice Number: {invoice_number}", right_style),
],
[
Paragraph(company_address[1], styles["Normal"]),
Paragraph(f"Date: {date}", right_style),
],
]
# Add remaining address lines if any
for line in company_address[2:]:
data.append([Paragraph(line, styles["Normal"]), ""])
address_table = Table(data, colWidths=[doc.width / 2] * 2)
address_table.setStyle(
TableStyle(
[
("VALIGN", (0, 0), (-1, -1), "TOP"),
]
)
)
# Add the address table to the elements list
elements.append(address_table)
elements.append(Spacer(1, 12))
# Add horizontal line after the company address
elements.append(
HRFlowable(width="100%", thickness=1, color=colors.black, spaceAfter=6)
)
# Add client information
client_info = (
f"<b>Billed to:</b><br/>{client_name}<br/>{'<br/>'.join(client_address)}"
)
elements.append(Paragraph(client_info, styles["Normal"]))
elements.append(Spacer(1, 12))
# Add table with invoice details
headers = ["Description", unit_header, rate_header]
if include_vat:
headers.extend(["Amount", "VAT (20%)", "Total Amount"])
else:
headers.append("Total Amount")
amount_excl_vat = hours * rate
row = [
"Consulting Services",
hours,
f"{currency_symbol}{rate:.2f}",
f"{currency_symbol}{amount_excl_vat:.2f}",
]
if include_vat:
vat_amount = amount_excl_vat * 0.2
total_amount = amount_excl_vat + vat_amount
row.extend([
f"{currency_symbol}{vat_amount:.2f}",
f"{currency_symbol}{total_amount:.2f}"
])
data = [headers, row]
table = Table(data)
table.setStyle(
TableStyle(
[
("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey),
("TEXTCOLOR", (0, 0), (-1, 0), colors.black),
("ALIGN", (0, 0), (-1, -1), "CENTER"),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("BOTTOMPADDING", (0, 0), (-1, 0), 12),
("BACKGROUND", (0, 1), (-1, -1), colors.white),
("GRID", (0, 0), (-1, -1), 1, colors.black),
]
)
)
elements.append(table)
elements.append(Spacer(1, 24))
# Create styles for bank details and amount due
left_style = ParagraphStyle("LeftAlign", parent=styles["Normal"], alignment=TA_LEFT)
right_style_large = ParagraphStyle(
"RightAlignLarge",
parent=styles["Normal"],
alignment=TA_RIGHT,
fontSize=14,
fontName="Helvetica-Bold",
textColor=blue_color,
)
# Create a table for bank details and amount due
bank_details_text = "<br/>".join(bank_details)
amount_due_text = f"Amount Due: {currency_symbol}{total_amount:.2f}"
bottom_table = Table(
[
[
Paragraph(bank_details_text, left_style),
Paragraph(amount_due_text, right_style_large),
]
],
colWidths=[doc.width / 2] * 2,
)
bottom_table.setStyle(TableStyle([("VALIGN", (0, 0), (-1, -1), "TOP")]))
elements.append(bottom_table)
doc.build(elements)
print(f"Invoice {file_name} generated successfully.")
def main():
parser = argparse.ArgumentParser(description="Generate an invoice.")
parser.add_argument(
"-c", "--config", required=True, help="Path to the configuration file"
)
parser.add_argument("-n", "--number", required=True, help="Invoice number")
parser.add_argument("-d", "--date", required=True, help="Date of the invoice")
parser.add_argument(
"-u",
"--units",
type=float,
required=True,
help="Number of units (hours/days/weeks) worked",
)
args = parser.parse_args()
generate_invoice(args.config, args.number, args.date, args.units)
if __name__ == "__main__":
main()