-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmec_inventory.py
723 lines (626 loc) · 26.8 KB
/
mec_inventory.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
"""
Author:Christopher Holder
"""
def create_tables(connection,cursor):
"""
This function creates the neccessary tables in the database.
"""
cursor.execute("CREATE TABLE IF NOT EXISTS OrdinalNumber(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,num TEXT NOT NULL)")
cursor.execute('CREATE TABLE IF NOT EXISTS OrdinalNumberS(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, num TEXT NOT NULL)')
cursor.execute("""CREATE TABLE IF NOT EXISTS Inventory(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,code TEXT NOT NULL,name TEXT NOT NULL,avail INTEGER NOT NULL,costUni REAL NOT NULL,priceUniSug REAL NOT NULL,groupx TEXT NOT NULL,category TEXT,stockmin INTEGER,stockmax INTEGER)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS Entries(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,dat TEXT,trans TEXT,code TEXT NOT NULL,name TEXT NOT NULL,quantity INTEGER NOT NULL,provider TEXT ,costUni REAL NOT NULL,costItems REAL NOT NULL,groupx TEXT NOT NULL, category TEXT)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS Outs(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,dat TEXT,trans TEXT,code TEXT NOT NULL,name TEXT NOT NULL,quantity INTEGER NOT NULL,groupx TEXT NOT NULL,priceUni REAL,priceItems REAL,tax REAL,revenue REAL,winnings REAL,payment TEXT,client TEXT)""")
cursor.execute('CREATE TABLE IF NOT EXISTS Clients(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,identification TEXT,name TEXT,mail TEXT,num TEXT,cel TEXT,fax TEXT ,direction TEXT,bought INTEGER,money_invested REAL,paid REAL,debt REAL)')
add_client(connection,cursor,'Misc','','','','','','')
connection.commit()
return True
def add_item_entry(connection,cursor,code = '#',name = "",quantity = 0,provider = "",costUni = 0.00,priceUniSug = 100 ,groupx = '',category = "",stockmin = "",stockmax = ""):
"""
This function adds entries to the table Inventory and Entries.
"""
cursor.execute('SELECT code,groupx FROM Inventory WHERE code=? AND groupx = ?',(code,groupx))
data = cursor.fetchone()
if data == None:
transnum = ordinal_generator(connection,cursor)
avail = quantity
costItems = costUni * quantity
costItems = round(costItems,2)
priceUniSug = round(priceUniSug,2)
costUni = round(costUni,2)
b = (code,name,avail,costUni,priceUniSug,groupx,category,stockmin,stockmax)
c = (transnum,code,name,quantity,provider,costUni,costItems,groupx,category)
cursor.execute("INSERT INTO Inventory (code,name,avail,costUni,priceUniSug,groupx,category,stockmin,stockmax) VALUES(?,?,?,?,?,?,?,?,?)",b)
cursor.execute("INSERT INTO Entries (dat,trans,code,name,quantity,provider,costUni,costItems,groupx,category) VALUES(date('now'),?,?,?,?,?,?,?,?,?)",c)
connection.commit()
else:
transnum = ordinal_generator(connection,cursor)
avail = quantity
costItems = costUni * quantity
costItems = round(costItems,2)
c = (transnum,code,name,quantity,provider,round(costUni,2),costItems,groupx,category)
#-------------------------------------------------------------------------------------------------------
increase_stock(cursor,code,groupx,quantity)
update_all(cursor,code,groupx,costUni,priceUniSug,name,category)
#-------------------------------------------------------------------------------------------------------
cursor.execute("INSERT INTO Entries (dat,trans,code,name,quantity,provider,costUni,costItems,groupx,category) VALUES(date('now'),?,?,?,?,?,?,?,?,?)",c)
connection.commit()
return True
def add_item_exit_fixed(connection,cursor,code = "#",quantity = 1,tax = 0.07,pricef = 10.00,discount = 0,payment = 'CRE',client = '',trans='',groupx = ''):
a =(code,groupx)
cursor.execute('SELECT name FROM Inventory WHERE code = ? AND groupx = ?',a)
data0 = cursor.fetchone()
name = str(data0[0])
decrease_stock(cursor,code,groupx,quantity)
priceUni = pricef
taxTot = tax * priceUni * quantity
taxTot = round(taxTot,2)
priceItems = priceUni * (tax + 1) * quantity
if (discount == 0):
priceItems = round(priceItems,2)
else:
discount = priceItems * discount
priceItems = priceItems - discount
priceItems = round(priceItems,2)
cursor.execute('SELECT costUni FROM Inventory WHERE code = ? AND groupx = ?',a)
data2 = cursor.fetchone()
costItems = (float(data2[0]))* quantity
costItems = round(costItems,2)
revenue = priceItems - costItems
revenue = round(revenue,2)
winnings = revenue - taxTot
winnings = round(winnings,2)
auto_del_0(connection,cursor)
b = (trans,code,name,quantity,groupx,priceUni,priceItems,taxTot,revenue,winnings,payment,client)
cursor.execute("INSERT INTO Outs (dat,trans,code,name,quantity,groupx,priceUni,priceItems,tax,revenue,winnings,payment,client) VALUES(date('now'),?,?,?,?,?,?,?,?,?,?,?,?)",b)
update_client_info(connection,cursor,client)
connection.commit()
#-------------------------------------------------------------------------------------------------------
return True
#-------------------------------------------------------------------------------------------------------
def modify(connection,cursor,code,groupx,avail,priceUni,category,smin,smax,costUni, name):
if (groupx == 'Global'):
cursor.execute('UPDATE Inventory SET name = ?,priceUniSug = ?,category = ?, stockmin = ?,stockmax = ? ,costUni = ? WHERE code = ?',(name,priceUni,category,smin,smax,costUni,code))
else:
cursor.execute('UPDATE Inventory SET name = ?,avail = ?,priceUniSug = ?,category = ?, stockmin = ?,stockmax = ? ,costUni = ? WHERE code = ? AND groupx = ?',(name,avail,priceUni,category,smin,smax,costUni,code,groupx))
connection.commit()
def modify_client(connection,cursor,name,identification,mail,num,cel,fax,direction):
sel = (identification,mail,num,cel,fax,direction,name)
cursor.execute('UPDATE Clients SET identification = ?,mail = ?,num = ?,cel = ?,fax = ?,direction = ? WHERE name = ?',sel)
connection.commit()
def move(connection,cursor,code,groupx1,groupx2,quantity):
cursor.execute('SELECT code,name,avail,costUni,priceUniSug,groupx,category,stockmin,stockmax FROM Inventory WHERE code = ? and groupx = ?',(code,groupx1))
data = cursor.fetchone()
decrease_stock(cursor,code,groupx1,quantity)
auto_del_0(connection,cursor)
cursor.execute('SELECT name FROM Inventory WHERE code = ? AND groupx = ?' , (code,groupx2))
data2 = cursor.fetchone()
if (data2 == None):
c = (data[0],data[1],quantity,data[3],data[4],groupx2,data[6],data[7],data[8])
cursor.execute('INSERT INTO Inventory (code,name,avail,costUni,priceUniSug,groupx,category,stockmin,stockmax) VALUES(?,?,?,?,?,?,?,?,?)',c)
else:
increase_stock(cursor,code,groupx2,quantity)
connection.commit()
def shopping_cart(connection,cursor,lista):
"""
This function does multiple sales.lista is a list of lists.
The elements should contain the following arguments. : [code,quantity,tax,pricef,discount,payment,client,groupx]
"""
counter = 0
results =[]
failed = {}
for e in lista:
a = sale_valid2(cursor,e[0],e[1],e[7])
results.append(a)
for el in range(len(results)):
if (results[el] != 0):
failed.setdefault((el+1),results[el])
if (len(failed) > 0):
print(failed)
return failed
t = ordinal_generator2(connection,cursor)
for e in lista:
counter += 1
transa = t + (str(counter).zfill(3))
add_item_exit_fixed(connection,cursor,e[0],e[1],e[2],e[3],e[4],e[5],e[6],transa,e[7])
return True
def sale_valid(cursor,code,client_name,quantity,groupx):
"""
Checks If client ,quantity, or code exists.
0 = Sucessful
1 = does not exists. 2 = reduces below existing units ,
3 = client does not exist
"""
l = []
a = (code,groupx)
b = (client_name,)
cursor.execute('SELECT code,avail FROM Inventory WHERE code = ? AND groupx = ?',a)
data0 = cursor.fetchone()
if (data0 == None):
l.append(1)
if (data0 != None):
if (data0[1] < quantity):
l.append(2)
cursor.execute('SELECT name FROM Clients WHERE name = ?',b)
data2 = cursor.fetchone()
if (data2 == None):
l.append(3)
if (len(l) == 0):
l = 0
return l
def sale_valid2(cursor,code,quantity,groupx):
"""
Checks If client ,quantity, or code exists.
0 = Sucessful
1 = does not exists. 2 = reduces below existing units ,
"""
l = []
a = (code,groupx)
cursor.execute('SELECT code,avail FROM Inventory WHERE code = ? AND groupx = ?',a)
data0 = cursor.fetchone()
if (data0 == None):
l.append(1)
if (data0 != None):
if (data0[1] < quantity):
l.append(2)
if (len(l) == 0):
l = 0
return l
def query_add(cursor,code,groupx):
cursor.execute('SELECT name,costUni,priceUniSug,category,stockmin,stockmax FROM Inventory WHERE code = ? AND groupx = ?',(code,groupx))
data = cursor.fetchone()
if (data == None):
return False
return data
def query_sale(cursor,code,groupx):
"""
Returns list with [name,priceUniSug,costUni]
"""
cursor.execute('SELECT name,priceUniSug,costUni FROM Inventory WHERE code = ? AND groupx = ?',(code,groupx))
data = cursor.fetchone()
if (data == None):
print('No name with that code')
return False
return data
def query_modify(cursor,code,groupx):
"""
Returns [avail,priceUniSug,costUni,category,stockmin,stockmax,name]
"""
cursor.execute('SELECT avail,priceUniSug,costUni,category,stockmin,stockmax, name FROM Inventory WHERE code = ? AND groupx = ?',(code,groupx))
data = cursor.fetchone()
return data
def query_client(cursor,name):
"""
Returns [identification,mail,num,cel,fax,direction,bought,money_invested,paid,debt]
"""
cursor.execute('SELECT identification,mail,num,cel,fax,direction FROM Clients WHERE name = ?',(name,))
data = cursor.fetchone()
return data
#-------------------------------------------------------------------------------------------------------
def add_client(connection,cursor,identification,name,mail,num,cel,fax,direction):
"""
Adds client to client table.
Returns False if the name has been used before.
"""
bought = 0
money_invested = 0.0
paid = 0.0
debt = 0.0
i = (name,)
cursor.execute('SELECT name FROM Clients WHERE name = ?',i)
data = cursor.fetchone()
if (data != None):
print('Name already used.')
return False
t = (identification,name,mail,num,cel,fax,direction,bought,money_invested,paid,debt)
cursor.execute("INSERT INTO Clients (identification,name,mail,num,cel,fax,direction,bought,money_invested,paid,debt) VALUES (?,?,?,?,?,?,?,?,?,?,?)",t)
connection.commit()
return True
def update_client_info(connection,cursor,user):
a = (user,)
money = []
articles = []
cursor.execute('SELECT priceItems,quantity FROM Outs WHERE client = ? ',a)
data2 = cursor.fetchall()
if (data2 == None):
return False
for row2 in data2:
money.append(row2[0])
for row2 in data2:
articles.append(row2[1])
debit = []
credit = []
cursor.execute("SELECT priceItems FROM Outs WHERE client = ? AND payment = 'DEB'",a)
data4 = cursor.fetchall()
for row4 in data4:
debit.append(row4[0])
cursor.execute("SELECT priceItems FROM Outs WHERE client = ? AND payment = 'CRE'",a)
data5 = cursor.fetchall()
for row5 in data5:
credit.append(row5[0])
money = sum(money)
articles = sum(articles)
debit = sum(debit)
credit =sum(credit)
cursor.execute('UPDATE Clients SET bought = ?,money_invested = ?,paid = ?,debt = ? WHERE name = ?',(articles,money,debit,credit,user))
connection.commit()
def del_client_id(connection,cursor,identification):
cursor.execute('DELETE FROM Clients WHERE identification = ?',(identification,))
connection.commit()
return True
def del_client_name(connection,cursor,name):
cursor.execute('DELETE FROM Clients WHERE name = ?',(name,))
connection.commit()
return True
#-------------------------------------------------------------------------------------------------------
def calc_bal_his(cursor):
"""
CalcuLates balances of all exits and entries ever and adds them to the historic balance db.
"""
t = []
cursor.execute('SELECT costItems FROM Entries')
data = cursor.fetchall()
for row0 in data:
t.append(row0[0])
costTot = sum(t)
cursor.execute('SELECT priceItems,revenue,tax,winnings FROM Outs')
query = cursor.fetchall()
#-------------------------------------------------------------------------------------------------------
p = []
for row2 in query:
p.append(row2[0])
priceTot = sum(p)
#-------------------------------------------------------------------------------------------------------
g = []
for row3 in query:
g.append(row3[1])
revenueTot = sum(g)
#-------------------------------------------------------------------------------------------------------
i = []
for row4 in query:
i.append(row4[2])
taxTot = sum(i)
#-------------------------------------------------------------------------------------------------------
x = []
for row5 in query:
x.append(row5[3])
winningsTot = sum(x)
#-------------------------------------------------------------------------------------------------------
cd = calc_deb(cursor)
cc = calc_cre(cursor)
return [costTot,priceTot,cd,cc,round((priceTot - costTot),2),taxTot,round((priceTot - costTot - taxTot),2)]
def calc_bal_mes(cursor,year,month):
if (len(year) != 4) or (int(year) < 2016) or (int(year)> 3000) or (isinstance(year,float)) or (len(month) != 2) or (isinstance(month,float)) or (int(month)< 0) or (int(month)>12) :
print('Bad date')
return False
date = year+'-'+ month
entries = []
#-------------------------------------------------------------------------------------------------------
cursor.execute('SELECT dat,costItems FROM Entries')
data = cursor.fetchall()
for row in data:
if (date in row[0]):
entries.append(row[1])
costTot = sum(entries)
cursor.execute('SELECT dat,priceItems,revenue,tax,winnings FROM Outs ')
query = cursor.fetchall()
#-------------------------------------------------------------------------------------------------------
p = []
for e in query:
if (date in e[0]):
p.append(e[1])
priceTot = sum(p)
#-------------------------------------------------------------------------------------------------------
g = []
for d in query:
if (date in d[0]):
g.append(d[2])
revenueTot = sum(g)
#-------------------------------------------------------------------------------------------------------
i = []
for elem in query:
if (date in elem[0]):
i.append(elem[3])
taxTot = sum(i)
#-------------------------------------------------------------------------------------------------------
x = []
for al in query:
if(date in al[0]):
x.append(al[4])
winningsTot = sum(x)
#-------------------------------------------------------------------------------------------------------
cd = calc_deb(cursor,date)
cc = calc_cre(cursor,date)
return [costTot,priceTot,cd,cc,round((priceTot - costTot),2),taxTot,round((priceTot - costTot - taxTot),2)]
def calc_bal_year(cursor,year):
if (len(year) != 4) or (int(year) < 2016) or (int(year)> 3000) or (isinstance(year,float)) :
print('Not proper date.')
return False
date = year
entries = []
#-------------------------------------------------------------------------------------------------------
cursor.execute('SELECT dat,costItems FROM Entries')
data = cursor.fetchall()
for row in data:
if (date in row[0]):
entries.append(row[1])
costTot = sum(entries)
cursor.execute('SELECT dat,priceItems,revenue,tax,winnings FROM Outs ')
query = cursor.fetchall()
#-------------------------------------------------------------------------------------------------------
p = []
for e in query:
if (date in e[0]):
p.append(e[1])
priceTot = sum(p)
#-------------------------------------------------------------------------------------------------------
g = []
for d in query:
if (date in d[0]):
g.append(d[2])
revenueTot = sum(g)
#-------------------------------------------------------------------------------------------------------
i = []
for elem in query:
if (date in elem[0]):
i.append(elem[3])
taxTot = sum(i)
#-------------------------------------------------------------------------------------------------------
x = []
for al in query:
if(date in al[0]):
x.append(al[4])
winningsTot = sum(x)
#-------------------------------------------------------------------------------------------------------
cd = calc_deb(cursor,date)
cc = calc_cre(cursor,date)
return [costTot,priceTot,cd,cc,round((priceTot - costTot),2),taxTot,round((priceTot - costTot - taxTot),2)]
def calc_bal_day(cursor,year,month,day):
if (len(year) != 4) or (int(year) < 2016) or (int(year)> 3000) or (isinstance(year,float)) or (len(month) != 2) or (isinstance(month,float)) or (int(month)< 0) or (int(month)>12) or (int(day) > 31) or (len(day) != 2):
print('Bad date')
return False
date = year+'-'+ month + '-' + day
entries = []
cursor.execute('SELECT dat,costItems FROM Entries')
data = cursor.fetchall()
for row in data:
if (date in row[0]):
entries.append(row[1])
costTot = sum(entries)
cursor.execute('SELECT dat,priceItems,revenue,tax,winnings FROM Outs ')
query = cursor.fetchall()
#-------------------------------------------------------------------------------------------------------
p = []
for e in query:
if (date in e[0]):
p.append(e[1])
priceTot = sum(p)
#-------------------------------------------------------------------------------------------------------
g = []
for d in query:
if (date in d[0]):
g.append(d[2])
revenueTot = sum(g)
#-------------------------------------------------------------------------------------------------------
i = []
for elem in query:
if (date in elem[0]):
i.append(elem[3])
taxTot = sum(i)
#-------------------------------------------------------------------------------------------------------
x = []
for al in query:
if(date in al[0]):
x.append(al[4])
winningsTot = sum(x)
#-------------------------------------------------------------------------------------------------------
cd = calc_deb(cursor,date)
cc = calc_cre(cursor,date)
return [costTot,priceTot,cd,cc,round((priceTot - costTot),2),taxTot,round((priceTot - costTot - taxTot),2)]
#-------------------------------------------------------------------------------------------------------
def gen_query(cursor,table,column,stri,num):
"""
Returns a list with elements that contain the string.
Returns empty list if it does find one.
"""
list1 = []
list2 = []
query = 'SELECT '+ str(column) +' FROM '+ str(table)
cursor.execute(query)
data = cursor.fetchall()
if (data == None):
return list1
for row in data:
list1.append(row[0])
for e in list1:
if (stri in e ):
list2.append(e)
while (len(list2) > num):
list2.pop()
print(list2)
return list2
def paid(connection,cursor,trans):
"""
Marks an item as paid.
"""
t = (trans,)
cursor.execute("UPDATE Outs SET payment = 'DEB' WHERE trans = ?",(trans,))
cursor.execute("SELECT client FROM Outs WHERE trans = ?",(trans,))
data = cursor.fetchone()
update_client_info(connection,cursor,data[0])
connection.commit()
def move_to_credit(connection,cursor,trans):
"""
Marks an item as not paid.
"""
cursor.execute("UPDATE Outs SET payment = 'CRE' WHERE trans = ?",(trans,))
cursor.execute("SELECT client FROM Outs WHERE trans = ?",(trans,))
data = cursor.fetchone()
update_client_info(connection,cursor,data[0])
connection.commit()
def calc_deb(cursor, date = None):
"""
Calculates liquidity.
"""
deb = []
if (date == None):
cursor.execute("SELECT priceItems FROM Outs WHERE payment = 'DEB'")
data = cursor.fetchall()
for e in data:
deb.append(e[0])
else:
cursor.execute("SELECT priceItems,dat FROM Outs WHERE payment = 'DEB'")
data = cursor.fetchall()
for e in data:
if (date in e[1]):
deb.append(e[0])
deb = round(sum(deb),2)
return deb
def calc_cre(cursor,date = None):
"""
Calculates money customers currently owe.
"""
cre = []
if (date == None):
cursor.execute("SELECT priceItems FROM Outs WHERE payment = 'CRE'")
data = cursor.fetchall()
for e in data:
cre.append(e[0])
else:
cursor.execute("SELECT priceItems,dat FROM Outs WHERE payment = 'CRE'")
data = cursor.fetchall()
for e in data:
if (date in e[1]):
cre.append(e[0])
cre = round(sum(cre),2)
return cre
#-------------------------------------------------------------------------------------------------------
def del_general(connection,cursor,trans):
"""
Generalizes use of delete function.
Clients table delete not included.
"""
try:
if(trans[0] == '1'):
return del_item_entries(connection,cursor,trans)
elif(trans[0] == '2'):
return del_item_salidas(connection,cursor,trans)
else:
print('Unknown transaction number')
return False
except TypeError:
print('Error in cell')
return False
def del_item_entries(connection,cursor,trans):
"""
Deletes items from entries by transaction number.
"""
cursor.execute('DELETE FROM Entries WHERE trans = ?',(trans,))
connection.commit()
return True
def del_item_inventory(connection,cursor,code,groupx):
"""
Deletes items from inventory by code.
"""
cursor.execute('DELETE FROM Inventory WHERE code = ? AND groupx = ?',(code,groupx))
connection.commit()
return True
def del_item_salidas(connection,cursor,trans):
"""
Deletes items by transaction number.
"""
cursor.execute('SELECT quantity FROM Outs WHERE trans = ?',(trans,))
data = cursor.fetchone()
if (data == None):
print('Transaction number not from an Out')
return False
cursor.execute('SELECT priceItems,client FROM Outs WHERE trans = ?',(trans,))
p = cursor.fetchone()
cursor.execute('SELECT money_invested FROM Clients WHERE name = ? ',(p[1],))
d = cursor.fetchone()
f = d[0]- p[0]
cursor.execute('UPDATE Clients SET money_invested = ? WHERE name = ?',(f,p[1]))
cursor.execute('SELECT code,groupx FROM Outs WHERE trans = ?',(trans,))
data2 = cursor.fetchone()
#-------------------------------------------------------------------------------------------------------
g = (data2[0],data2[1])
cursor.execute('SELECT avail FROM Inventory WHERE code = ? AND groupx = ?',g)
data3 = cursor.fetchone()
avail = data3[0] + data[0]
b =(avail,data2[0],data2[1])
cursor.execute('UPDATE Inventory SET avail = ? WHERE code = ? AND groupx = ?',b)
#-------------------------------------------------------------------------------------------------------
cursor.execute('DELETE FROM Outs WHERE trans = ?',(trans,))
connection.commit()
return True
def auto_del_0(connection,cursor):
cursor.execute('SELECT avail FROM Inventory WHERE avail = 0')
data4 = cursor.fetchone()
if data4 != None:
cursor.execute('DELETE FROM Inventory WHERE avail = 0')
def unique(cursor,column,table,key_column = "",key = ""):
if key_column == "":
cursor.execute("SELECT DISTINCT "+ column + " FROM " + table)
else:
cursor.execute("SELECT DISTINCT " + column + " FROM " + table + " WHERE " + key_column + " = ?",(key,))
unique_values = []
data = cursor.fetchall()
if data != None:
for line in data:
unique_values.append(line[0])
return unique_values
#-------------------------------------------------------------------------------------------------------
def ordinal_generator(connection,cursor):
"""
Generates string numbers starting with 1 and makes sure to never
have used them before.It also adds them complementary 0's until it
has a minimum length of 8 characters.
"""
exists = False
trans = ""
cursor.execute('SELECT MAX(ID) FROM OrdinalNumber')
index = cursor.fetchone()
if (index[0] == None):
trans = '00000000'
else:
index = str(index[0])
trans = index.zfill(8)
d = ('a',)
cursor.execute('INSERT INTO OrdinalNumber(num) VALUES (?)',d)
connection.commit()
return ('1' + trans)
def ordinal_generator2(connection,cursor):
exists = False
trans = ""
cursor.execute('SELECT MAX(ID) FROM OrdinalNumberS')
index = cursor.fetchone()
if (index[0] == None):
trans = '000000'
else:
index = str(index[0])
trans = index.zfill(6)
d = ('a',)
cursor.execute('INSERT INTO OrdinalNumberS(num) VALUES (?)',d)
connection.commit()
return ('2' + trans)
def update_all(cursor,code,groupx,cost,price,name,category):
t = (name,price,cost,category,code,groupx)
cursor.execute('UPDATE Inventory SET name = ?,priceUniSug = ?,costUni = ?,category = ? WHERE code = ? AND groupx = ?',t)
def increase_stock(cursor,code,groupx,quantity):
cursor.execute('SELECT avail FROM Inventory WHERE code = ? AND groupx = ?',(code,groupx))
data = cursor.fetchone()
avail = int(data[0]) + quantity
cursor.execute('UPDATE Inventory SET avail = ? WHERE code = ? AND groupx = ?',(avail,code,groupx))
return True
def decrease_stock(cursor,code,groupx,quant):
#Reduce stock
cursor.execute('SELECT avail FROM Inventory WHERE code = ? AND groupx = ?',(code,groupx))
data = cursor.fetchone()
avail = int(data[0]) - quant
cursor.execute('UPDATE Inventory SET avail = ? WHERE code = ? AND groupx = ?',(avail,code,groupx))
return True
def print_(cursor,table):#Print any table.
cursor.execute('SELECT * FROM '+ table)
data = cursor.fetchall()
for row in data:
print(row)
return True