-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrecent_orders.cgi
executable file
·151 lines (123 loc) · 4.54 KB
/
recent_orders.cgi
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
#!/usr/local/bin/python3
import cgi
from pizza_kitchen_helpers import *
def get_order_details(db,cursor):
sql = "select * from customer_order order by order_id desc limit 10"
cursor.execute(sql)
orders = cursor.fetchall()
return orders
def get_customer_details(db,cursor,customer_id):
sql = "select * from customer where customer_id = '{0}'".format(customer_id)
cursor.execute(sql)
customer_details = cursor.fetchall()
return customer_details[0]
def get_order_item_details(db,cursor,order_id):
sql = "select * from order_item where order_id = '{0}'".format(order_id)
cursor.execute(sql)
order_items = cursor.fetchall()
return order_items
def get_pizza_names(db,cursor,order_items):
pizza_names = []
for each in order_items:
sql = "select name from pizza where pizza_id='{0}'".format(each[2])
cursor.execute(sql)
pizza = cursor.fetchall()
pizza_names.append(pizza[0][0])
return pizza_names
def create_customer_table(customer_details):
table = """ <table>
<tr>
<th>First Name</th>
<td>{0}</td>
</tr>
<tr>
<th>Last Name</th>
<td>{1}</td>
</tr>
<tr>
<th>Street</th>
<td>{2}</td>
</tr>
<tr>
<th>Town</th>
<td>{3}</td>
</tr>
<tr>
<th>Post Code</th>
<td>{4}</td>
</tr>
<tr>
<th>E-Mail</th>
<td>{5}</td>
</tr>
</table>
""".format(customer_details[1],customer_details[2],customer_details[3],customer_details[4],customer_details[5],customer_details[6])
return table
def create_pizza_table(pizza_names,order_items):
table = "<table>"
pizza = 0
for each in order_items:
table += """
<tr>
<td>{0} x </td>
<td>{1}</td>
<td> - {2} inches</td>
</tr>
""".format(each[4],pizza_names[pizza],each[3])
pizza += 1
table += "</table>"
return table
def create_order_table(customer_details,pizza_names,order_items):
customer_table = create_customer_table(customer_details)
pizza_table = create_pizza_table(pizza_names,order_items)
total_cost = calculate_order_cost(order_items)
table = """<table>
<tr>
<th>Customer Details</th>
<th>Pizzas</th>
</tr>
<tr>
<td>{0}</td>
<td>{1}</td>
</tr>
<tr>
<td colspan="2">
Order Total = {2}
</td>
</tr>
</table>""".format(customer_table,pizza_table,total_cost)
return table
def calculate_order_cost(order_items):
total_cost = 0
for each in order_items:
sql = "select cost from pizza where pizza_id='{0}'".format(each[2])
cursor.execute(sql)
pizza_cost = cursor.fetchall()
pizza_cost = pizza_cost[0][0]
sql = "select cost from size where inches='{0}'".format(each[3])
cursor.execute(sql)
size_cost = cursor.fetchall()
size_cost = size_cost[0][0]
total_cost += (pizza_cost + size_cost) * each[4]
return total_cost
def create_recent_order_summary(orders):
summary = ""
for each in orders:
customer_details = get_customer_details(db,cursor,each[3])
order_items = get_order_item_details(db,cursor,each[0])
pizza_names = get_pizza_names(db,cursor,order_items)
order_table = create_order_table(customer_details,pizza_names,order_items)
summary += order_table
summary += "<br/>"
return summary
if __name__ == "__main__":
try:
html_top("Recent Orders")
db,cursor = connect_pizza_database()
orders = get_order_details(db,cursor)
summary = create_recent_order_summary(orders)
print("<h1>Pizza Kitchen</h1>")
print("<h2>Order Confirmation</h2>")
print(summary)
except:
cgi.print_exception()