-
Notifications
You must be signed in to change notification settings - Fork 23
/
app.py
338 lines (278 loc) · 12.3 KB
/
app.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
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from collections import defaultdict
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///inventory.db'
db = SQLAlchemy(app)
class Product(db.Model):
__tablename__ = 'products'
product_id = db.Column(db.String(200), primary_key=True)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Product %r>' % self.product_id
class Location(db.Model):
__tablename__ = 'locations'
location_id = db.Column(db.String(200), primary_key=True)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Location %r>' % self.location_id
class ProductMovement(db.Model):
__tablename__ = 'productmovements'
movement_id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey('products.product_id'))
qty = db.Column(db.Integer)
from_location = db.Column(db.Integer, db.ForeignKey('locations.location_id'))
to_location = db.Column(db.Integer, db.ForeignKey('locations.location_id'))
movement_time = db.Column(db.DateTime, default=datetime.utcnow)
product = db.relationship('Product', foreign_keys=product_id)
fromLoc = db.relationship('Location', foreign_keys=from_location)
toLoc = db.relationship('Location', foreign_keys=to_location)
def __repr__(self):
return '<ProductMovement %r>' % self.movement_id
@app.route('/', methods=["POST", "GET"])
def index():
if (request.method == "POST") and ('product_name' in request.form):
product_name = request.form["product_name"]
new_product = Product(product_id=product_name)
try:
db.session.add(new_product)
db.session.commit()
return redirect("/")
except:
return "There Was an issue while add a new Product"
if (request.method == "POST") and ('location_name' in request.form):
location_name = request.form["location_name"]
new_location = Location(location_id=location_name)
try:
db.session.add(new_location)
db.session.commit()
return redirect("/")
except:
return "There Was an issue while add a new Location"
else:
products = Product.query.order_by(Product.date_created).all()
locations = Location.query.order_by(Location.date_created).all()
return render_template("index.html", products = products, locations = locations)
@app.route('/locations/', methods=["POST", "GET"])
def viewLocation():
if (request.method == "POST") and ('location_name' in request.form):
location_name = request.form["location_name"]
new_location = Location(location_id=location_name)
try:
db.session.add(new_location)
db.session.commit()
return redirect("/locations/")
except:
locations = Location.query.order_by(Location.date_created).all()
return "There Was an issue while add a new Location"
else:
locations = Location.query.order_by(Location.date_created).all()
return render_template("locations.html", locations=locations)
@app.route('/products/', methods=["POST", "GET"])
def viewProduct():
if (request.method == "POST") and ('product_name' in request.form):
product_name = request.form["product_name"]
new_product = Product(product_id=product_name)
try:
db.session.add(new_product)
db.session.commit()
return redirect("/products/")
except:
products = Product.query.order_by(Product.date_created).all()
return "There Was an issue while add a new Product"
else:
products = Product.query.order_by(Product.date_created).all()
return render_template("products.html", products=products)
@app.route("/update-product/<name>", methods=["POST", "GET"])
def updateProduct(name):
product = Product.query.get_or_404(name)
old_porduct = product.product_id
if request.method == "POST":
product.product_id = request.form['product_name']
try:
db.session.commit()
updateProductInMovements(old_porduct, request.form['product_name'])
return redirect("/products/")
except:
return "There was an issue while updating the Product"
else:
return render_template("update-product.html", product=product)
@app.route("/delete-product/<name>")
def deleteProduct(name):
product_to_delete = Product.query.get_or_404(name)
try:
db.session.delete(product_to_delete)
db.session.commit()
return redirect("/products/")
except:
return "There was an issue while deleteing the Product"
@app.route("/update-location/<name>", methods=["POST", "GET"])
def updateLocation(name):
location = Location.query.get_or_404(name)
old_location = location.location_id
if request.method == "POST":
location.location_id = request.form['location_name']
try:
db.session.commit()
updateLocationInMovements(
old_location, request.form['location_name'])
return redirect("/locations/")
except:
return "There was an issue while updating the Location"
else:
return render_template("update-location.html", location=location)
@app.route("/delete-location/<name>")
def deleteLocation(id):
location_to_delete = Location.query.get_or_404(id)
try:
db.session.delete(location_to_delete)
db.session.commit()
return redirect("/locations/")
except:
return "There was an issue while deleteing the Location"
@app.route("/movements/", methods=["POST", "GET"])
def viewMovements():
if request.method == "POST" :
product_id = request.form["productId"]
qty = request.form["qty"]
fromLocation = request.form["fromLocation"]
toLocation = request.form["toLocation"]
new_movement = ProductMovement(
product_id=product_id, qty=qty, from_location=fromLocation, to_location=toLocation)
try:
db.session.add(new_movement)
db.session.commit()
return redirect("/movements/")
except:
return "There Was an issue while add a new Movement"
else:
products = Product.query.order_by(Product.date_created).all()
locations = Location.query.order_by(Location.date_created).all()
movs = ProductMovement.query\
.join(Product, ProductMovement.product_id == Product.product_id)\
.add_columns(
ProductMovement.movement_id,
ProductMovement.qty,
Product.product_id,
ProductMovement.from_location,
ProductMovement.to_location,
ProductMovement.movement_time)\
.all()
movements = ProductMovement.query.order_by(
ProductMovement.movement_time).all()
return render_template("movements.html", movements=movs, products=products, locations=locations)
@app.route("/update-movement/<int:id>", methods=["POST", "GET"])
def updateMovement(id):
movement = ProductMovement.query.get_or_404(id)
products = Product.query.order_by(Product.date_created).all()
locations = Location.query.order_by(Location.date_created).all()
if request.method == "POST":
movement.product_id = request.form["productId"]
movement.qty = request.form["qty"]
movement.from_location= request.form["fromLocation"]
movement.to_location = request.form["toLocation"]
try:
db.session.commit()
return redirect("/movements/")
except:
return "There was an issue while updating the Product Movement"
else:
return render_template("update-movement.html", movement=movement, locations=locations, products=products)
@app.route("/delete-movement/<int:id>")
def deleteMovement(id):
movement_to_delete = ProductMovement.query.get_or_404(id)
try:
db.session.delete(movement_to_delete)
db.session.commit()
return redirect("/movements/")
except:
return "There was an issue while deleteing the Prodcut Movement"
@app.route("/product-balance/", methods=["POST", "GET"])
def productBalanceReport():
movs = ProductMovement.query.\
join(Product, ProductMovement.product_id == Product.product_id).\
add_columns(
Product.product_id,
ProductMovement.qty,
ProductMovement.from_location,
ProductMovement.to_location,
ProductMovement.movement_time).\
order_by(ProductMovement.product_id).\
order_by(ProductMovement.movement_id).\
all()
balancedDict = defaultdict(lambda: defaultdict(dict))
tempProduct = ''
for mov in movs:
row = mov[0]
if(tempProduct == row.product_id):
if(row.to_location and not "qty" in balancedDict[row.product_id][row.to_location]):
balancedDict[row.product_id][row.to_location]["qty"] = 0
elif (row.from_location and not "qty" in balancedDict[row.product_id][row.from_location]):
balancedDict[row.product_id][row.from_location]["qty"] = 0
if (row.to_location and "qty" in balancedDict[row.product_id][row.to_location]):
balancedDict[row.product_id][row.to_location]["qty"] += row.qty
if (row.from_location and "qty" in balancedDict[row.product_id][row.from_location]):
balancedDict[row.product_id][row.from_location]["qty"] -= row.qty
pass
else :
tempProduct = row.product_id
if(row.to_location and not row.from_location):
if(balancedDict):
balancedDict[row.product_id][row.to_location]["qty"] = row.qty
else:
balancedDict[row.product_id][row.to_location]["qty"] = row.qty
return render_template("product-balance.html", movements=balancedDict)
@app.route("/movements/get-from-locations/", methods=["POST"])
def getLocations():
product = request.form["productId"]
location = request.form["location"]
locationDict = defaultdict(lambda: defaultdict(dict))
locations = ProductMovement.query.\
filter( ProductMovement.product_id == product).\
filter(ProductMovement.to_location != '').\
add_columns(ProductMovement.from_location, ProductMovement.to_location, ProductMovement.qty).\
all()
for key, location in enumerate(locations):
if(locationDict[location.to_location] and locationDict[location.to_location]["qty"]):
locationDict[location.to_location]["qty"] += location.qty
else:
locationDict[location.to_location]["qty"] = location.qty
return locationDict
@app.route("/dub-locations/", methods=["POST", "GET"])
def getDublicate():
location = request.form["location"]
locations = Location.query.\
filter(Location.location_id == location).\
all()
print(locations)
if locations:
return {"output": False}
else:
return {"output": True}
@app.route("/dub-products/", methods=["POST", "GET"])
def getPDublicate():
product_name = request.form["product_name"]
products = Product.query.\
filter(Product.product_id == product_name).\
all()
print(products)
if products:
return {"output": False}
else:
return {"output": True}
def updateLocationInMovements(oldLocation, newLocation):
movement = ProductMovement.query.filter(ProductMovement.from_location == oldLocation).all()
movement2 = ProductMovement.query.filter(ProductMovement.to_location == oldLocation).all()
for mov in movement2:
mov.to_location = newLocation
for mov in movement:
mov.from_location = newLocation
db.session.commit()
def updateProductInMovements(oldProduct, newProduct):
movement = ProductMovement.query.filter(ProductMovement.product_id == oldProduct).all()
for mov in movement:
mov.product_id = newProduct
db.session.commit()
if (__name__ == "__main__"):
app.run(debug=True)