-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
196 lines (185 loc) · 8.16 KB
/
app.py
File metadata and controls
196 lines (185 loc) · 8.16 KB
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
from flask import Flask,render_template,request,redirect,url_for
import mysql.connector
from datetime import datetime
from zoneinfo import ZoneInfo
app=Flask(__name__)
def get_db_connection():
connection = mysql.connector.connect(
host='localhost',
user='root',
password='Kanishkar@6374243181',
database='inventory'
)
return connection
@app.route('/')
def index() :
return render_template('index.html')
@app.route('/addproduct',methods=['GET','POST'])
def add_products() :
if request.method=='POST' :
p_name=request.form.get('pname')
price=request.form.get('price')
connection=get_db_connection()
cur=connection.cursor()
cur.execute("INSERT INTO Product(name,price) VALUES(%s,%s)",(p_name,price))
connection.commit()
cur.close()
connection.close()
return render_template('add_product.html')
@app.route('/viewproduct')
def view_product() :
connection=get_db_connection()
cur=connection.cursor(dictionary=True)
cur.execute("SELECT *FROM Product")
product=cur.fetchall()
cur.close()
connection.close()
return render_template('view_products.html',products=product)
@app.route('/editproduct/<string:id>',methods=['GET','POST'])
def edit_product(id) :
if request.method=='POST' :
p_name=request.form.get('pname')
price=request.form.get('price')
connection=get_db_connection()
cur=connection.cursor()
cur.execute("SET SQL_SAFE_UPDATES = 0")
cur.execute("UPDATE Product SET name=%s, price=%s WHERE product_id=%s",(p_name,price,id))
cur.execute("SET SQL_SAFE_UPDATES = 1")
connection.commit()
cur.close()
connection.close()
return redirect(url_for('view_product'))
connection=get_db_connection()
cur=connection.cursor(dictionary=True)
cur.execute("SELECT *FROM Product WHERE product_id=%s",[id])
res=cur.fetchone()
return render_template('edit_product.html',result=res)
@app.route('/addlocation',methods=['POST','GET'])
def add_location() :
if request.method=='POST' :
l_name=request.form.get('lname')
connection=get_db_connection()
cur=connection.cursor()
cur.execute("INSERT INTO Location(name) VALUES(%s)",[l_name])
connection.commit()
cur.close()
connection.close()
return render_template('add_location.html')
@app.route('/viewlocation')
def view_location() :
connection=get_db_connection()
cur=connection.cursor(dictionary=True)
cur.execute("SELECT *FROM Location")
location=cur.fetchall()
cur.close()
connection.close()
return render_template('view_locations.html',locations=location)
@app.route('/addmovement',methods=['POST','GET'])
def add_movement() :
connection=get_db_connection()
cur=connection.cursor(dictionary=True)
cur.execute("SELECT *FROM Product")
product=cur.fetchall()
cur.execute("SELECT *FROM Location")
location=cur.fetchall()
cur.close()
connection.close()
if request.method=='POST' :
m_id=request.form.get('mid')
p_id=request.form.get('pid')
from_l=request.form.get('froml')
if from_l=="" : from_l=None
to_l=request.form.get('tol')
if to_l=="" : to_l=None
now_ist = datetime.now(ZoneInfo("Asia/Kolkata"))
dt=now_ist.strftime("%Y-%m-%d %H:%M:%S")
quantity=request.form.get('quantity')
if from_l or to_l : #if any one date is given or not
connection=get_db_connection()
cur=connection.cursor()
#------------------------------------
if from_l is None and to_l :
cur.execute("SELECT EXISTS(SELECT 1 FROM Stock WHERE product_id=%s AND location_id=%s)AS product_exist",(p_id,to_l))
val=cur.fetchall()
if val[0][0] :
cur.execute("SELECT quantity FROM Stock WHERE product_id=%s AND location_id=%s",(p_id,to_l))
q=cur.fetchall()
cur.execute("UPDATE Stock SET quantity=%s WHERE product_id=%s AND location_id=%s",(q[0][0]+int(quantity),p_id,to_l))
connection.commit()
else :
cur.execute("INSERT INTO Stock(product_id,location_id,quantity) VALUES(%s,%s,%s)",(p_id,to_l,quantity))
connection.commit()
elif from_l and to_l is None :
cur.execute("SELECT EXISTS(SELECT 1 FROM Stock WHERE product_id=%s AND location_id=%s)AS product_exist",(p_id,from_l))
val=cur.fetchall()
if val[0][0] :
cur.execute("SELECT quantity FROM Stock WHERE product_id=%s AND location_id=%s",(p_id,from_l))
q=cur.fetchall()
cur.execute("UPDATE Stock SET quantity=%s WHERE product_id=%s AND location_id=%s",(q[0][0]-int(quantity),p_id,from_l))
connection.commit()
else :
return render_template('add_movement.html',locations=location,products=product,act="Give the vaild location in From Locaton")
else :# from_l and to_l
#from check
cur.execute("SELECT EXISTS(SELECT 1 FROM Stock WHERE product_id=%s AND location_id=%s)AS product_exist",(p_id,from_l))
val=cur.fetchall()
if val[0][0] :
cur.execute("SELECT quantity FROM Stock WHERE product_id=%s AND location_id=%s",(p_id,from_l))
q=cur.fetchall()
cur.execute("UPDATE Stock SET quantity=%s WHERE product_id=%s AND location_id=%s",(q[0][0]-int(quantity),p_id,from_l))
connection.commit()
else :
return render_template('add_movement.html',locations=location,products=product,act="Give the vaild location in From Locaton")
#to check
cur.execute("SELECT EXISTS(SELECT 1 FROM Stock WHERE product_id=%s AND location_id=%s)AS product_exist",(p_id,to_l))
val=cur.fetchall()
if val[0][0] :
cur.execute("SELECT quantity FROM Stock WHERE product_id=%s AND location_id=%s",(p_id,to_l))
q=cur.fetchall()
cur.execute("UPDATE Stock SET quantity=%s WHERE product_id=%s AND location_id=%s",(q[0][0]+int(quantity),p_id,to_l))
connection.commit()
else :
cur.execute("INSERT INTO Stock(product_id,location_id,quantity) VALUES(%s,%s,%s)",(p_id,to_l,quantity))
connection.commit()
#------------------------------------
cur.execute("INSERT INTO Movement(movement_id,product_id,from_location,to_location,quantity,Date_Time) VALUES(%s,%s,%s,%s,%s,%s)",(m_id,p_id,from_l,to_l,quantity,dt))
connection.commit()
cur.close()
connection.close()
else :
return render_template('add_movement.html',locations=location,products=product,act="Movement is not added, Please enter ony one location")
return render_template('add_movement.html',locations=location,products=product,act="Add the Movement")
@app.route('/productmovement')
def product_movement() :
connection=get_db_connection()
cur=connection.cursor(dictionary=True)
cur.execute('''
SELECT
m.movement_id,
p.name AS product_name,
COALESCE(fl.name,'N/A') AS from_location_name,
COALESCE(tl.name,'N/A') AS to_location_name,
m.quantity,
m.Date_Time
FROM Movement m
JOIN Product p ON m.product_id = p.product_id
LEFT JOIN Location fl ON m.from_location = fl.location_id
LEFT JOIN Location tl ON m.to_location = tl.location_id;
''')
movement=cur.fetchall()
cur.execute('''
SELECT
s.stock_id,
p.name as product_name,
l.name as location_name,
s.quantity
from Stock s
JOIN Product p ON s.product_id = p.product_id
JOIN Location l ON s.location_id = l.location_id ;
''')
stock=cur.fetchall()
cur.close()
connection.close()
return render_template('product_movement.html',movements=movement,stocks=stock)
if __name__=='__main__' :
app.run(debug=True)