-
Notifications
You must be signed in to change notification settings - Fork 60
/
cart.py
342 lines (261 loc) · 10.9 KB
/
cart.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from connectToDb import connectToDB
import time as t
import gLedgerFunctions as af
import invoiceMaker as im
#import printer as pr
class product:
def __init__ (self, pid, name, qty, price):
self.pid = pid
self.name = name
self.qty = qty
self.price = price
self.origPrice = price
class cart:
def __init__ (self):
self.products = []
def __repr__ (self):
return self.products
def addProduct (self, pid, name, qty, sellingPrice):
prod = product(pid, name, qty, sellingPrice)
self.products.append(prod)
def removeProduct (self, prod):
c = 0
for x in self.products:
if x == prod:
del self.products[c]
c = c +1
def makeEmpty(self):
self.products = []
def computeTotalBill (self):
bill = 0
for p in self.products:
bill = bill + ( p.price * p.qty )
return bill
def computeTotalDiscount (self):
discount = 0
for p in self.products:
discount = discount + ( p.qty * ( p.origPrice - p.price ) )
return discount
class terminal:
def __init__ (self):
self.conn = connectToDB()
self.cart = cart()
self.customerId = 0
self.customerName = ''
self.customerContact = ''
self.supplierId = 0
self.supplierName = ''
self.operatorId = 0
def getCart (self):
return self.cart
def getCartProducts (self):
return self.cart.products
def numberOfItems (self):
return len(self.cart.products)
def refresh (self):
self.customerId = 0
self.supplierId = 0
self.cart.makeEmpty()
def commitInsertQuery (self, qry):
curs = self.conn.cursor()
curs.execute(qry)
insertId = self.conn.insert_id()
self.conn.commit()
return insertId
def findProduct(self, productIdentifier, qty):
qry = 'SELECT p.id FROM products p where p.codeName LIKE "%s"' % (productIdentifier)
curs = self.conn.cursor()
curs.execute(qry)
r = curs.fetchone()
if (r is None):
qry = 'SELECT p.id FROM products p where p.barcode LIKE "%s"' % (productIdentifier)
curs.execute(qry)
r = curs.fetchone()
if r is not None:
for p in self.cart.products:
if r['id'] == p.pid:
return False
x = self.scanProduct(r['id'], qty)
return x
def scanProduct (self, pid, qty):
qry = 'SELECT ci.productId, ci.quantity, p.name, p.sellingPrice from currentinventory ci, products p where ci.productId = %d and ci.productId = p.id' % pid
curs = self.conn.cursor()
curs.execute(qry)
r = curs.fetchone()
if (r['quantity'] < qty):
return r['quantity']
else:
qry = 'UPDATE `currentinventory` SET `quantity`=%d WHERE `productId` = %s' % (int(r['quantity'])-int(qty), r['productId'])
curs.execute(qry)
self.conn.commit()
self.addToCart(pid, r['name'], qty, r['sellingPrice'])
return True
def addToCart (self, pid, name, qty, sellingPrice):
self.cart.addProduct(pid, name, qty, sellingPrice)
def removeFromCart (self, pid):
self.cart.removeProduct(pid)
def computeTotalBill(self):
return self.cart.computeTotalBill()
def computeTotalDiscount(self):
return self.cart.computeTotalDiscount()
'''
# this version of the function changed stock levels as products were punched into the cart
# but this was removed and simpler version was deployed after the client as for negative stock levels
def increaseQty (self, pid, qty):
qry = 'SELECT quantity from currentinventory where productId = %d' % pid
curs = self.conn.cursor()
curs.execute(qry)
r = curs.fetchone()
if (r['quantity'] < qty):
return r['quantity']
else:
qry = 'UPDATE `currentinventory` SET `quantity`=`quantity`-%d WHERE `productId` = %s' % (int(qty), str(pid))
self.commitInsertQuery(qry)
c = 0
for p in self.cart.products:
if (pid == p.pid):
self.cart.products[c].qty = self.cart.products[c].qty + qty
c = c + 1
return True
'''
def increaseQty (self, pid, qty):
c = 0
for p in self.cart.products:
if (pid == p.pid):
self.cart.products[c].qty = self.cart.products[c].qty + qty
c = c + 1
return True
def fetchCustomerId (self, contact):
qry = "SELECT id, name from customer WHERE contact = '%s'" % str( contact )
curs = self.conn.cursor()
curs.execute(qry)
r = curs.fetchone()
if r is not None:
self.customerId = r['id']
self.customerName = r['name']
self.customerContact = str(contact)
return True
return False
def registerNewCustomer (self, name, contact):
qry = "INSERT INTO customer (name, contact) VALUES ('%s', '%s')" % (cName, cust)
self.customerId = self.commitInsertQuery(qry)
def fetchSupplierId (self, contact):
qry = "SELECT id, name from supplier WHERE contact = '%s'" % str( contact )
curs = self.conn.cursor()
curs.execute(qry)
r = curs.fetchone()
if r is not None:
self.supplierId = r['id']
self.supplierName = r['name']
return True
return False
def registerNewSupplier (self, name, contact):
qry = "INSERT INTO supplier (name, contact) VALUES ('%s', '%s')" % (cName, cust)
self.supplierId = self.commitInsertQuery(qry)
# ========== Cash Sale =================================
def checkout (self):
if (len(self.cart.products) == 0):
return
bill = self.computeTotalBill()
discount = self.computeTotalDiscount()
saleId = self.recordSale(bill, discount)
self.recordProductsInSale(saleId)
self.cashSaleJournalEntry(saleId, bill, discount)
#printInvoice(t.strftime("%d-%m-%y", t.localtime()), i)
self.refresh()
def recordSale (self, bill, discount):
qry = 'INSERT INTO `sales` (customer, totalBill, discount, preparedBy) VALUES (%d, %d, %d, %s)' % (self.customerId, bill, discount, self.operatorId)
return self.commitInsertQuery(qry)
def recordProductsInSale (self, saleId):
for p in self.cart.products:
qry = 'INSERT INTO productsale (saleId, product, quantity, price, discount) VALUES (%s, %s, %s, %s, %s)' % (saleId, p.pid, p.qty, p.price, p.origPrice - p.price )
self.commitInsertQuery(qry)
qry = 'UPDATE `currentinventory` SET `quantity`=`quantity`-%s WHERE `productId` = %s' % (p.qty, p.pid)
return self.commitInsertQuery(qry)
def cashSaleJournalEntry (self, saleId, bill, discount):
af.cashSaleEntry(bill, discount, saleId)
def returnProducts (self):
if (len(self.cart.products) == 0):
return
bill = self.computeTotalBill()
discount = self.computeTotalDiscount()
returnId = self.recordReturn(bill, discount)
self.recordProductsInReturn(returnId)
self.returnJournalEntry(returnId, bill, discount)
self.refresh()
def recordReturn (self, bill, discount):
qry = 'INSERT INTO `refunds` (customer, totalBill, discount, preparedBy) VALUES (%d, %d, %d, %s)' % (self.customerId, bill, discount, self.operatorId)
return self.commitInsertQuery(qry)
def recordProductsInReturn (self, saleId):
for p in self.cart.products:
qry = 'INSERT INTO productrefund (refundId, product, quantity, price, discount) VALUES (%s, %s, %s, %s, %s)' % (saleId, p.pid, p.qty, p.price, p.origPrice - p.price )
self.commitInsertQuery(qry)
def returnJournalEntry (self, returnId, bill, discount):
af.returnEntry(bill, discount, returnId)
# ========== Cash Sale =================================
# ========== Invoice =================================
def prepareInvoice (self, amtRecieved):
if (len(self.cart.products) == 0):
return
bill = self.computeTotalBill()
discount = self.computeTotalDiscount()
invoiceId = self.recordInvoice(amtRecieved, bill, discount)
self.recordProductsInInvoice(invoiceId)
self.makeInvoice(invoiceId)
self.invoiceSaleJournalEntry(invoiceId, bill, discount, amtRecieved)
self.refresh()
def recordInvoice (self, amtRecieved, bill, discount):
qry = 'INSERT INTO invoice (employeeId, amount, amountRecieved, discount, buyerId) VALUES (%d, %d, "%s", %d, %d)' % (self.operatorId, bill, amtRecieved, discount, self.customerId)
return self.commitInsertQuery(qry)
def recordProductsInInvoice (self, invoiceId):
for p in self.cart.products:
qry = 'INSERT INTO productinvoice (invoiceId, product, quantity, price, discount) VALUES (%s, %s, %s, %s, %s)' % (invoiceId, p.pid, p.qty, p.price, p.origPrice - p.price )
self.commitInsertQuery(qry)
qry = 'UPDATE `currentinventory` SET `quantity`=`quantity`-%s WHERE `productId` = %s' % (p.qty, p.pid)
return self.commitInsertQuery(qry)
def invoiceSaleJournalEntry (self, invoiceId, bill, discount, amtRecieved):
af.invoiceEntry (bill, discount, amtRecieved, self.customerId, invoiceId, cheque=1)
def makeInvoice (self, invoiceId):
prods = []
for x in range(len(self.cart.products)):
prods.append([self.cart.products[x].name, '', self.cart.products[x].qty, self.cart.products[x].price])
im.imaker(str(invoiceId), invoiceId, self.operatorId, self.customerId, self.customerName, self.customerContact, prods, discountNo=0)
# ========== Invoice =================================
# ========== Quotation =================================
def saveQuote (self, expDate):
if (len(self.cart.products) == 0):
return
bill = self.computeTotalBill()
discount = self.computeTotalDiscount()
quoteId = self.recordQuote(bill, discount, expDate)
self.recordProductsInQuotation(quoteId)
self.refresh()
def recordQuote (self, bill, discount, expDate):
qry = 'INSERT INTO `quotations` (customer, totalBill, discount, preparedBy, expiryDate) VALUES (%s, %d, %d, %s, "%s")' % (self.customerId, bill, discount, self.operatorId, expDate)
return self.commitInsertQuery(qry)
def recordProductsInQuotation (self, quoteId):
for p in self.cart.products:
qry = 'INSERT INTO productquotes (quoteId, product, quantity, price, discount) VALUES (%s, %s, %s, %s, %s)' % (quoteId, p.pid, p.qty, p.price, p.origPrice - p.price)
self.commitInsertQuery(qry)
# ========== Quotation =================================
# ========== Purchase =================================
def purchaseItems (self, amountPaid):
if (len(self.cart.products) == 0):
return
bill = self.computeTotalBill()
discount = self.computeTotalDiscount()
purchaseId = self.recordPurchase(amountPaid, bill, discount)
self.recordProductsInPurchase(purchaseId)
self.purchaseJournalEntry (bill, discount, amountPaid, purchaseId)
self.refresh()
def recordPurchase (self, amountPaid, bill, discount):
qry = 'INSERT INTO `purchase` (supplier, totalBill, discount, amountPaid, preparedBy) VALUES (%s, %s, %s, %s, %s)' % (self.supplierId, bill, discount, amountPaid, self.operatorId)
return self.commitInsertQuery(qry)
def recordProductsInPurchase (self, purchaseId):
for p in self.cart.products:
qry = 'INSERT INTO productpurchase (purchaseId, product, quantity, price, discount) VALUES (%s, %s, %s, %s, %s)' % (purchaseId, p.pid, p.qty, p.price, p.origPrice - p.price)
qry = 'UPDATE `currentinventory` SET `quantity`=`quantity`+%s WHERE `productId` = %s' % (p.qty, p.pid)
self.commitInsertQuery(qry)
def purchaseJournalEntry (self, bill, discount, amountPaid, purchaseId):
af.purchaseEntry (bill, discount, amountPaid, self.supplierId, purchaseId)
# ========== Purchase =================================