/
app.py
230 lines (209 loc) · 9.35 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
from flask import Flask,flash, request,redirect,render_template
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql.functions import func
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///frappeInventory.db'
db = SQLAlchemy(app)
class Product(db.Model):
__tablename__ = 'product'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(50),nullable=False)
def __repr__(self):
return '<Product %r>' %self.id
class Location(db.Model):
__tablename__ = 'location'
id = db.Column(db.Integer,primary_key=True)
name= db.Column(db.String(50),nullable=False)
def __repr__(self):
return '<Location %r>' %self.id
class Movement(db.Model):
movement_id= db.Column(db.Integer,primary_key=True)
product_id= db.Column(db.Integer,db.ForeignKey('product.id'))
product = db.relationship("Product")
to_location_id = db.Column(db.Integer,db.ForeignKey('location.id'))
to_location = db.relationship("Location",primaryjoin=to_location_id==Location.id)
from_location_id = db.Column(db.Integer,db.ForeignKey('location.id'))
from_location = db.relationship("Location",primaryjoin=from_location_id==Location.id)
quantity = db.Column(db.Integer)
def __repr__(self):
return '<Movement %r>' %self.id
@app.route('/')
def index():
return render_template('home.html')
@app.route('/products',methods=["GET","POST"])
def products():
error=None
if request.method == "GET":
products = Product.query.all()
return render_template('products.html',products=products)
if request.method == "POST":
products = Product.query.all()
product_name = request.form["product-name"]
if not product_name.strip():
error ='Please Enter Valid Product!'
return render_template('products.html',error=error , products=products)
if db.session.query(Product).filter(func.lower(Product.name)==func.lower(product_name)).all():
error='Product already exist!'
return render_template('products.html',error=error , products=products)
else:
new_product = Product(name=product_name)
try:
db.session.add(new_product)
db.session.commit()
return redirect('/products')
except:
return "Database error"
@app.route('/products/<int:id>/update',methods=["GET","POST"])
def update_product(id):
product = Product.query.get_or_404(id)
if request.method == "GET":
return render_template("update_product.html",product=product)
if request.method == "POST":
product_name = request.form['product-name']
product = Product.query.get_or_404(id)
if not product_name.strip():
error ='Please Enter Valid Product!'
return render_template('update_product.html',error=error ,product=product )
if db.session.query(Product).filter(func.lower(Product.name)==func.lower(product_name)).all():
error='Product already exist!'
return render_template('update_product.html',error=error ,product=product )
else:
product.name=request.form['product-name']
try:
db.session.commit()
return redirect('/products')
except:
return "Database error"
@app.route('/locations',methods=["GET","POST"])
def locations():
error=None
if request.method == "GET":
locations = Location.query.all()
return render_template('locations.html',locations=locations)
if request.method == "POST":
locations = Location.query.all()
location_name = request.form["location-name"]
if not location_name.strip():
error ='Please Enter Valid Location!'
return render_template('locations.html',error=error , locations=locations)
if db.session.query(Location).filter(func.lower(Location.name)==func.lower(location_name)).all():
error='Location already exist!'
return render_template('locations.html',error=error , locations=locations)
else:
new_location = Location(name=location_name)
try:
db.session.add(new_location)
db.session.commit()
return redirect('/locations')
except:
return "Database error"
@app.route('/locations/<int:id>/update',methods=["GET","POST"])
def update_locations(id):
location = Location.query.get_or_404(id)
if request.method == "GET":
return render_template("update_location.html",location=location)
if request.method == "POST":
location_name = request.form['location-name']
location = Location.query.get_or_404(id)
if not location_name.strip():
error ='Please Enter Valid Location!'
return render_template('update_location.html',error=error ,location=location )
if db.session.query(Location).filter(func.lower(Location.name)==func.lower(location_name)).all():
error='Location already exist!'
return render_template('update_location.html',error=error ,location=location )
else:
location.name=request.form['location-name']
try:
db.session.commit()
return redirect('/locations')
except:
return "Database error"
@app.route('/movements',methods=["GET","POST"])
def movements():
if request.method == "GET":
products = Product.query.all()
locations = Location.query.all()
movements = Movement.query.all()
return render_template('movements.html',products=products,locations=locations,movements=movements)
if request.method == "POST":
products = Product.query.all()
locations = Location.query.all()
movements = Movement.query.all()
to_location = request.form['to-location']
from_location = request.form['from-location']
if from_location==to_location:
error='From location and To location cant be same'
return render_template('movements.html',error=error,products=products,locations=locations,movements=movements)
product = request.form['product']
quantity = request.form['quantity']
if from_location == 'none':
new_movement = Movement(to_location_id=to_location,from_location_id=from_location\
,product_id=product,quantity=quantity)
try:
db.session.add(new_movement)
db.session.commit()
return redirect('/movements')
except:
return "Database error"
else:
quantity_at_location = get_quantity(from_location,product)
if quantity_at_location < int(quantity):
error= "Not enough quantity is available"
return render_template('movements.html',error=error,products=products,locations=locations,movements=movements)
else:
new_movement = Movement(to_location_id=to_location,from_location_id=from_location\
,product_id=product,quantity=quantity)
try:
db.session.add(new_movement)
db.session.commit()
return redirect('/movements')
except:
return "Database error"
@app.route('/report')
def report():
locations = Location.query.all()
products = Product.query.all()
report = []
for location in locations:
for product in products:
row = {}
row["location"] = location.name
row["product"] = product.name
row["quantity"] = get_quantity(location.id,product.id)
report.append(row)
return render_template('reports.html',report=report)
def get_quantity(location,product):
added_products = Movement.query.\
filter(Movement.to_location_id==location,Movement.product_id==product)\
.from_self(func.sum(Movement.quantity,)).all()
added_products = added_products[0][0]
if added_products == None:
added_products = 0
removed_products = Movement.query.\
filter(Movement.from_location_id==location,Movement.product_id==product)\
.from_self(func.sum(Movement.quantity,name="removed")).all()
removed_products = removed_products[0][0]
if removed_products == None:
removed_products = 0
return added_products-removed_products
@app.route('/movements/<int:id>/update',methods=["GET","POST"])
def update_movements(id):
movement_to_be_updated = Movement.query.get_or_404(id)
if request.method == "GET":
products = Product.query.all()
locations = Location.query.all()
movements = Movement.query.all()
return render_template('update_movements.html',products=products,locations=locations,movement=movement_to_be_updated)
if request.method == "POST":
movement_to_be_updated.to_location_id = request.form["to-location"]
movement_to_be_updated.from_location_id = request.form["from-location"]
movement_to_be_updated.product_id = request.form["product"]
movement_to_be_updated.quantity = request.form["quantity"]
try:
db.session.commit()
return redirect('/movements')
except:
return "Database error"
if __name__ == "__main__":
app.run(debug=True)