-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
181 lines (145 loc) · 5.84 KB
/
helpers.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
from flask import request, flash, redirect, g
import sqls
def product_edit_get(pid):
g.cur.execute("SELECT * FROM `Product` WHERE `product_id` = %s;", [pid])
data = { 'pid': pid, 'count': g.cur.rowcount }
if g.cur.rowcount == 0:
flash("No such product id")
else:
row = g.cur.fetchone()
if row is not None:
data['name'] = row['name']
return data
def product_edit_post(pid):
return add_edit_product("edit", pid)
def add_edit_product(mysql, crud="add", pid=0):
try:
if "name" in request.form:
name = request.form['name']
if name:
if crud == "add":
g.cur.execute("INSERT INTO `Product` (`product_id`, `name`) VALUES (NULL, %s); ", [name])
elif crud == "edit":
g.cur.execute("UPDATE `Product` SET `name` = '%s' WHERE `product_id` = %s;" % (name, pid))
mysql.connection.commit()
flash("Successfully {} product".format("added" if crud == "add" else "updated"), "success")
return redirect("/product/view/%s" % (g.cur.lastrowid if crud == "add" else pid), code=302) # This is to disable refresh
else:
flash("Product name cannot be empty")
else:
return "No hacking !"
except Exception as e:
flash("Error : {}".format(e), "danger")
return None
def location_edit_get(pid):
g.cur.execute("SELECT * FROM `Location` WHERE `location_id` = %s;", [pid])
data = { 'count': g.cur.rowcount }
if g.cur.rowcount == 0:
flash("No such location id")
else:
row = g.cur.fetchone()
if row is not None:
data['name'] = row['name']
return data
def location_edit_post(mysql, pid):
data = {}
try:
if "name" in request.form:
name = request.form['name']
if name:
g.cur.execute(sqls.location_edit % (name, pid))
mysql.connection.commit()
data['name'] = name
flash("Successfully updated location", "success")
else:
flash("Product name cannot be empty")
return data
else:
return "No hacking !"
except Exception as e:
flash("Error : {}".format(e), "danger")
def move_push_get():
data = {}
g.cur.execute("SELECT * FROM `Product`")
if g.cur.rowcount == 0:
return "No Products yet"
else:
row = g.cur.fetchall()
data['products'] = row
g.cur.execute("SELECT * FROM `Location`")
if g.cur.rowcount == 0:
return "No Locations yet"
else:
row = g.cur.fetchall()
data['locations'] = row
return data
def move_push_post(mysql):
try:
product_id = request.form['product_id']
location_id = request.form['location_id']
qty = request.form['qty']
g.cur.execute(sqls.move_push % (location_id, product_id, qty))
mysql.connection.commit()
flash("Successfully added product to location", "success")
return redirect("/move/list", code=302) # This is to disable refresh
except Exception as e:
flash("Error : {}".format(e), "danger")
def move_move_post(mysql):
data = {}
try:
product_id = request.form['product_id']
from_location_id = request.form['from_location_id']
to_location_id = request.form['to_location_id']
qty = request.form['qty']
# From Vashi to Seawoods, need to look how many products are there in Vashi (from_location_id)
g.cur.execute(sqls.ProductMovement_to_Location % (product_id, from_location_id))
pm_rows = g.cur.fetchall()
data["Product_in_Location"] = 0
for pm_row in pm_rows:
print("Location : %s", data["Product_in_Location"])
if pm_row['from_location'] != 0:
data["Product_in_Location"] -= pm_row['qty']
if pm_row['to_location'] != 0:
data["Product_in_Location"] += pm_row['qty']
print("Product ID %s left in location %s = %s" % (product_id, to_location_id, data["Product_in_Location"]))
if data["Product_in_Location"] <= 0:
return "No Products in Location"
g.cur.execute(sqls.ProductMovement_add % (from_location_id, to_location_id, product_id, qty))
mysql.connection.commit()
flash("Successfully moved product to location", "success")
return redirect("/move/list", code=302) # This is to disable refresh
except Exception as e:
flash("Error : {}".format(e), "danger")
return data
def move_move_get():
data = {}
g.cur.execute("SELECT * FROM `Product`")
if g.cur.rowcount == 0:
return "No Products yet"
else:
row = g.cur.fetchall()
data['products'] = row
g.cur.execute("SELECT * FROM `Location`")
if g.cur.rowcount == 0:
return "No Locations yet"
else:
row = g.cur.fetchall()
data['locations'] = row
return data
def data_movement(product_id, dict_index):
data = {}
g.cur.execute(sqls.data_movement % product_id)
pm_rows = g.cur.fetchall()
data[dict_index] = dict()
for pm_row in pm_rows:
if pm_row['from_location'] != 0:
if pm_row['from_location_name'] not in data[dict_index]:
data[dict_index][pm_row['from_location_name']] = 0
data[dict_index][pm_row['from_location_name']] -= pm_row['qty']
if pm_row['to_location'] != 0:
if pm_row['to_location_name'] not in data[dict_index]:
data[dict_index][pm_row['to_location_name']] = 0
data[dict_index][pm_row['to_location_name']] += pm_row['qty']
print("data_movement = ", data)
print("data_movement[dict_index] = ", data[dict_index])
return data[dict_index]