-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (96 loc) · 4.04 KB
/
main.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
import tkinter
from tkinter import ttk
from docxtpl import DocxTemplate
import datetime
from tkinter import messagebox
def clear_item():
qty_spinbox.delete(0, tkinter.END)
qty_spinbox.insert(0, "1")
desc_entry.delete(0, tkinter.END)
price_spinbox.delete(0, tkinter.END)
price_spinbox.insert(0, "0.0")
invoice_list = []
def add_item():
qty = int(qty_spinbox.get())
desc = desc_entry.get()
price = float(price_spinbox.get())
line_total = qty*price
invoice_item = [qty, desc, price, line_total]
if len(desc) > 0 and not (all(i == 0 for i in invoice_item[1:])):
tree.insert('',0, values=invoice_item)
clear_item()
invoice_list.append(invoice_item)
else:
messagebox.showwarning("Warning", "Please fill out all fields.")
def new_invoice():
first_name_entry.delete(0, tkinter.END)
last_name_entry.delete(0, tkinter.END)
phone_entry.delete(0, tkinter.END)
clear_item()
tree.delete(*tree.get_children())
invoice_list.clear()
def generate_invoice():
doc = DocxTemplate("invoice_template.docx")
if len(invoice_list) == 0:
messagebox.showwarning("Error", "No items added to the invoice.")
elif len(first_name_entry.get()) ==0 and len(last_name_entry.get())==0:
messagebox.showwarning("Error", "No Username added to the invoice.")
else:
name = first_name_entry.get()+last_name_entry.get()
phone = phone_entry.get()
subtotal = sum(item[3] for item in invoice_list)
salestax = 0.1
total = subtotal*(1-salestax)
doc.render({"name":name,
"phone":phone,
"date":datetime.datetime.now().strftime("%Y-%m-%d"),
"invoice_list": invoice_list,
"subtotal":subtotal,
"salestax":str(salestax*100)+"%",
"total":total})
doc_name = "new_invoice" + name + datetime.datetime.now().strftime("%Y-%m-%d") + ".docx"
doc.save(doc_name)
messagebox.showinfo("Invoice Complete", "Invoice has been Completed !")
new_invoice()
window = tkinter.Tk()
window.title("Invoice Generator Form")
frame = tkinter.Frame(window)
frame.pack(padx=20, pady=10)
first_name_label = tkinter.Label(frame, text="First Name")
first_name_label.grid(row=0, column=0)
last_name_label = tkinter.Label(frame, text="Last Name")
last_name_label.grid(row=0, column=1)
first_name_entry = tkinter.Entry(frame)
last_name_entry = tkinter.Entry(frame)
first_name_entry.grid(row=1, column=0)
last_name_entry.grid(row=1, column=1)
phone_label = tkinter.Label(frame, text="Phone")
phone_label.grid(row=0, column=2)
phone_entry = tkinter.Entry(frame)
phone_entry.grid(row=1, column=2)
qty_label = tkinter.Label(frame, text="Qty")
qty_label.grid(row=2, column=0)
qty_spinbox = tkinter.Spinbox(frame, from_=1, to=100)
qty_spinbox.grid(row=3, column=0)
desc_label = tkinter.Label(frame, text="Description")
desc_label.grid(row=2, column=1)
desc_entry = tkinter.Entry(frame)
desc_entry.grid(row=3, column=1)
price_label = tkinter.Label(frame, text="Unit Price")
price_label.grid(row=2, column=2)
price_spinbox = tkinter.Spinbox(frame, from_=0.0, to=500, increment=0.5)
price_spinbox.grid(row=3, column=2)
add_item_button = tkinter.Button(frame, text = "Add item", command = add_item)
add_item_button.grid(row=4, column=2, pady=5)
columns = ('qty', 'desc', 'price', 'total')
tree = ttk.Treeview(frame, columns=columns, show="headings")
tree.heading('qty', text='Qty')
tree.heading('desc', text='Description')
tree.heading('price', text='Unit Price')
tree.heading('total', text="Total")
tree.grid(row=5, column=0, columnspan=3, padx=20, pady=10)
save_invoice_button = tkinter.Button(frame, text="Generate Invoice", command=generate_invoice)
save_invoice_button.grid(row=6, column=0, columnspan=3, sticky="news", padx=20, pady=5)
new_invoice_button = tkinter.Button(frame, text="New Invoice", command=new_invoice)
new_invoice_button.grid(row=7, column=0, columnspan=3, sticky="news", padx=20, pady=5)
window.mainloop()