-
Notifications
You must be signed in to change notification settings - Fork 2
/
order.py
234 lines (178 loc) · 6 KB
/
order.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""This module defines the class for Order creation"""
import sqlite3
import os
class Order():
"""
Purpose:
This class handles creation of a new Order.
Methods:
__init__(self, customer)
add_product(self, product)
add_payment_option(self, payment_option)
check_if_order_exists(self)
get_customer(self)
get_dir_fix(self)
get_order_id(self)
get_products(self)
save_to_db(self)
set_status(self)
get_total(self)
set_status(self, status)
Author:
@alirk / @mccordgh
"""
def __init__(self, customer, active, payment_option):
self.customer = customer
self.payment_option = payment_option
self.products = []
self.active = active
def get_customer(self):
return self.customer
def add_product(self, product, id):
self.products.append(product)
prod_id = product.get_product_id()
ord_id = id
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
INSERT INTO OrderItems
VALUES (null, "{}", "{}")
""".format(ord_id, prod_id)
try:
cursor.execute(sql_command)
except:
print("************ERROR ADDING TO MANY TO MANY DB**************")
connection.commit()
connection.close()
def get_order_id(self):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
SELECT idOrder
FROM Orders
WHERE idCustomer=1
-- AND active="True"
"""
try:
cursor.execute(sql_command)
except:
print("************ERROR GETTING ORDER ID**************")
order_id = cursor.fetchall()[0][0]
connection.commit()
connection.close()
return order_id
def set_payment_type_id(self, id, order_id):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
UPDATE Orders
SET idPayment='{}'
WHERE idOrder={};
""".format(id, order_id)
cursor.execute(sql_command)
connection.commit()
connection.close()
def get_products(self):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
SELECT p.name
FROM Products p, OrderItems o
WHERE o.idOrder=1
AND p.idProduct=o.idProduct
"""
cursor.execute(sql_command)
returned_fetch = cursor.fetchall()
returned_products = []
for product_tupled in returned_fetch:
for product in product_tupled:
returned_products.append(product)
connection.commit()
connection.close()
return returned_products
def set_status(self, status, order_id):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
UPDATE Orders
SET active='{}'
WHERE idOrder={};
""".format(status, order_id)
cursor.execute(sql_command)
connection.commit()
connection.close()
def get_status(self):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
SELECT active
FROM Orders
WHERE idOrder=1
"""
cursor.execute(sql_command)
returned_fetch = cursor.fetchall()
connection.commit()
connection.close()
return returned_fetch[0][0]
def get_total(self, id):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
SELECT p.price
FROM Products p, OrderItems o
WHERE o.idOrder={}
AND p.idProduct=o.idProduct
""".format(id)
cursor.execute(sql_command)
returned_fetch = cursor.fetchall()
product_total = 0
for prices_tupled in returned_fetch:
for price in prices_tupled:
product_total += price
connection.commit()
connection.close()
return float("{:0.2f}".format(product_total))
def select_payment_option(self, payment_option):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
UPDATE Orders
SET idPayment = 1
WHERE idOrder = 1
"""
try:
cursor.execute(sql_command)
except:
return False
connection.commit()
connection.close()
def save_to_db(self, payment_id, customer_id):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
INSERT OR IGNORE INTO Orders VALUES (null, "{}", "{}", "{}")
""".format(self.active, payment_id, customer_id)
cursor.execute(sql_command)
connection.commit()
connection.close()
def check_if_order_exists(self):
connection = sqlite3.connect('{}bangazon.db'.format(self.get_dir_fix()))
cursor = connection.cursor()
sql_command = """
SELECT idOrder
FROM Orders
WHERE idCustomer=1
""".format(self.acct_number)
try:
cursor.execute(sql_command)
except:
return False
acct_info = cursor.fetchall()
connection.commit()
connection.close()
return True
def get_dir_fix(self):
if os.path.basename(os.getcwd()) == 'tests' or os.path.basename(os.getcwd()) == 'scripts':
return '../'
else:
return ''