-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
190 lines (162 loc) · 4.51 KB
/
db.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
import mysql.connector
import datetime
username = 'sql12714086'
host="sql12.freesqldatabase.com"
password = "fAbSxAerrP"
database = "sql12714086"
def update(PO, updated_date = None, updated_qty = None, date = False,qty = False):
mydb = mysql.connector.connect(
username = username,
host = host,
password = password,
database = database
)
cur = mydb.cursor()
flag = False
if date and updated_date != None:
flag = update_date(PO,updated_date, cur)
elif qty and updated_qty != None:
flag = update_qty(PO,updated_qty, cur)
# cur.execute("Select * from purchase_orders;")
# print(cur.fetchall())
mydb.commit()
mydb.close()
return flag
# cur.execute("Use easeworkai;")
# {updated_date.strftime('%d-%m-%Y')}
def update_date(PO, updated_date, cur):
try:
query = f'UPDATE purchase_orders SET DeliveryDate = "{updated_date}" WHERE PONumber = "{PO}";'
cur.execute(query)
return True
except Exception as e:
print(e)
return False
def update_qty(PO, updated_qty, cur):
try:
query = f'UPDATE purchase_orders SET Quantity = {updated_qty} WHERE PONumber = "{PO}";'
cur.execute(query)
return True
except Exception as e:
print(e)
return False
# print(update_date("PO001", "2002-01-01"))
def update_received_docs(PO, attached_doc):
mydb = mysql.connector.connect(
username = username,
host = host,
password = password,
database = database
)
cur = mydb.cursor()
supplier_requested_docs = ["BOL" ,"PFI", "Drawings", "MQIC"]
buyer_requested_docs = ["LOC"]
try:
query = f'UPDATE {"buyer_received_docs" if attached_doc in supplier_requested_docs else "supplier_received_docs" if attached_doc in buyer_requested_docs else ""} SET {attached_doc} = 1 WHERE PONumber = "{PO}";'
# print(query)
cur.execute(query)
mydb.commit()
mydb.close()
return True
except Exception as e:
print(e)
return False
def get_delivery_date():
mydb = mysql.connector.connect(
username = username,
host = host,
password = password,
database = database
)
cur = mydb.cursor()
try:
query = f'SELECT PONumber, DeliveryDate FROM purchase_orders;'
cur.execute(query)
data = cur.fetchall()
return data
# print(data)
# print(type(data))
# datetime.date.today()
except Exception as e:
pass
def get_requestor(PO):
mydb = mysql.connector.connect(
username = username,
host = host,
password = password,
database = database
)
cur = mydb.cursor()
try:
query = f'SELECT requestor FROM requestor_supplier WHERE PONumber = "{PO}";'
cur.execute(query)
data = cur.fetchall()
# print(data)
return data[0][0]
except Exception as e:
print("Error", e)
def get_supplier(PO):
mydb = mysql.connector.connect(
username = username,
host = host,
password = password,
database = database
)
cur = mydb.cursor()
try:
query = f'SELECT supplier FROM requestor_supplier WHERE PONumber = "{PO}";'
cur.execute(query)
data = cur.fetchall()
# print(data)
return data[0][0]
except Exception as e:
print("Error", e)
def get_missing_docs(PO):
mydb = mysql.connector.connect(
username = username,
host = host,
password = password,
database = database
)
cur = mydb.cursor()
supplier_requested_docs = ["BOL" ,"PFI", "Drawings", "MQIC"]
buyer_requested_docs = ["LOC"]
s_missing = []
b_missing = []
try:
query = f'SELECT BOL, PFI, Drawings, MQIC FROM buyer_received_docs WHERE PONumber = "{PO}";'
cur.execute(query)
data = cur.fetchall()
for i,f in enumerate(data[0]):
if(f == 0):
s_missing.append((i,supplier_requested_docs[i]))
query = f'SELECT LOC FROM supplier_received_docs WHERE PONumber = "{PO}";'
cur.execute(query)
data = cur.fetchall()
for i,f in enumerate(data[0]):
if(f == 0):
b_missing.append((i,buyer_requested_docs[i]))
# print(data)
return {"s_missing": s_missing,"b_missing": b_missing}
except Exception as e:
print("Error", e)
def get_incharge(PO,file):
mydb = mysql.connector.connect(
username = username,
host = host,
password = password,
database = database
)
cur = mydb.cursor()
try:
query = f'SELECT {file} FROM incharge WHERE PONumber = "{PO}"'
cur.execute(query)
data = cur.fetchall()
# print(data)
return data[0][0]
except Exception as e:
print("Error", e)
# check_missing_docs('PO002')
# print(get_requestor("PO002"))
# print(get_missing_docs("PO003"))
# print(update_received_docs("PO001","LOC"))