-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_comparitron.py
351 lines (282 loc) · 16 KB
/
count_comparitron.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
339
340
341
342
343
344
345
346
347
348
349
350
351
# ---------------------------------------------------------------------------------------------------------------------#
# count_comparitron.py
#
# This module compares a list of outbound items' quantities to the corresponding inventory quantities.
# If the inventory is not sufficient to fill the order, it returns an inbound ordering list.
#
# ---------------------------------------------------------------------------------------------------------------------#
import os
import pyodbc
import re
from dotenv import load_dotenv
def compare_transfers(outbound_list):
# This module starts by opening an ODBC connection to the FileMaker database.
#
# This has some mildly-onerous prerequisites. First, you'll need appropriate ODBC drivers for your version of
# FileMaker. These can be downloaded from Claris' website. (Also, I have them, so if I'm still around just ask and
# I'll hook you up.) You might need to do this on a personal computer and move the files over, but I didn't have any
# trouble installing the drivers once I had them. If you do, bug IT about it.
#
# Run both "FMODBC_Installer_Win64" and "FMODBC_Installer_Win32".
#
# Next, you'll need to ask a FileMaker admin (at time of writing, Trey) for the "fmxdbc" permission.
# I used to have the right to do this, but it has apparently been revoked at some point between me granting myself
# this power and now. Luckily, the power remains, even if I am no longer its broker.
#
# Finally, you will need to create a DSN using the ODBC Data Source Administrator (64-bit), and potentially an
# identically-named one using the 32-bit version. I have mixed information on the usefulness of the 32-bit copy and
# figured "better safe than sorry", but it may not be needed.
#
# Use the following settings (skipped lines are times to click "Next >"):
#
# Name: Heat Transfer Inventory
# Description: [Blank/Optional]
#
# Host: 10.7.2.137
# Remaining Options: Optional
#
# Database: Heat Transfer Inventory
# Remaining Options: Optional
#
# Your FileMaker User ID and Password should be saved in your environment variables as "HTI_UID" and "HTI_PWD".
# For example:
#
# HTI_UID=sellickt
# HTI_PWD=moustache
load_dotenv()
connection_list = ['DSN=Heat Transfer Inventory;Database=Heat Transfer Inventory;UID=',
os.getenv('HTI_UID'),
';PWD=',
os.getenv('HTI_PWD')]
connection = pyodbc.connect(''.join(connection_list))
cursor = connection.cursor()
# outbound_list is a list of HTAs that have been ordered by the customer.
# inbound_list is a list of HTAs that we don't already have in the warehouse, which must be made or ordered.
inbound_list = []
for i in range(len(outbound_list)):
# This block takes the whole name of the HTA and splits it into chunks with which the database is comfortable.
# It also gets the quantity needed from the tuple in question.
hta_tuple = outbound_list[i]
whole_hta_string = str(hta_tuple[0])
whole_hta_list = re.split("HTA", whole_hta_string)
hta_name = whole_hta_list[0]
hta_number = 'HTA' + whole_hta_list[1]
units_needed = int(hta_tuple[1])
# Provided the quantity is greater than zero, this block checks the quantity in stock and compares it to the
# quantity required. If it exceeds the requirement by fifteen or more, the item is considered covered.
# Otherwise, it is added to the inbound_list.
#
# The quantity to be ordered is equal to half-again the quantity required, with a minimum order of twenty.
#
# Due to human error, HTA names occasionally contain unwelcome spaces. In this case, the database (or the
# database guy, not sure) either replaces the spaces with hyphens or just omits them. Both possibilities are
# checked for here.
#
# When in doubt, this module errs on the side of ordering what it doesn't understand.
if units_needed > 0:
try:
units_on_hand = int(cursor.execute('SELECT "Units on Hand" FROM Inventory WHERE Name=? AND "HTA '
'Number"=?',
hta_name, hta_number).fetchval())
if units_on_hand - units_needed < 15:
if units_needed <= 13:
units_to_order = 20
else:
units_to_order = (5 * round((units_needed * 1.5) / 5))
inbound_tuple = (whole_hta_string, units_to_order)
inbound_list.append(inbound_tuple)
except TypeError:
if ' ' in hta_name:
hta_name = hta_name.replace(' ', '-')
try:
units_on_hand = int(
cursor.execute('SELECT "Units on Hand" FROM Inventory WHERE Name=? AND "HTA Number"=?',
hta_name, hta_number).fetchval())
if units_on_hand - units_needed < 15:
if units_needed <= 13:
units_to_order = 20
else:
units_to_order = (5 * round((units_needed * 1.5) / 5))
inbound_tuple = (whole_hta_string, units_to_order)
inbound_list.append(inbound_tuple)
except TypeError:
hta_name = hta_name.replace('-', '')
try:
units_on_hand = int(
cursor.execute('SELECT "Units on Hand" FROM Inventory WHERE Name=? AND "HTA Number"=?',
hta_name, hta_number).fetchval())
if units_on_hand - units_needed < 15:
if units_needed <= 13:
units_to_order = 20
else:
units_to_order = (5 * round((units_needed * 1.5) / 5))
inbound_tuple = (whole_hta_string, units_to_order)
inbound_list.append(inbound_tuple)
except TypeError:
if units_needed <= 13:
units_to_order = 20
else:
units_to_order = (5 * round((units_needed * 1.5) / 5))
inbound_tuple = (whole_hta_string, units_to_order)
inbound_list.append(inbound_tuple)
else:
if units_needed <= 13:
units_to_order = 20
else:
units_to_order = (5 * round((units_needed * 1.5) / 5))
inbound_tuple = (whole_hta_string, units_to_order)
inbound_list.append(inbound_tuple)
if inbound_list:
return inbound_list
else:
return None
def compare_helmets(outbound_list):
# The warehouse folks added helmet decals to the inventory, so this checks the inventory before ordering them.
# For more details on how this all works, please see the compare_transfers function in this module.
# This skips this whole rigamarole if the names/numbers are missing.
if outbound_list == 'Names and numbers not found!':
return 'Names and numbers not found!'
# This establishes the connection to the database.
load_dotenv()
connection_list = ['DSN=Heat Transfer Inventory;Database=Heat Transfer Inventory;UID=',
os.getenv('HTI_UID'),
';PWD=',
os.getenv('HTI_PWD')]
connection = pyodbc.connect(''.join(connection_list))
cursor = connection.cursor()
# outbound_list is a list of HTAs that have been ordered by the customer.
# inbound_list is a list of HTAs that we don't already have in the warehouse, which must be made or ordered.
inbound_list = []
for i in range(len(outbound_list)):
# There's no inventory for regular film, so this will skip non-helmet items.
if 'FILLER TEXT' not in outbound_list[i][2]:
inbound_list.append(outbound_list[i])
continue
# This block takes the whole name of the HTA and splits it into chunks with which the database is comfortable.
# It also gets the quantity needed from the tuple in question.
hta_tuple = outbound_list[i]
whole_hta_string = str(hta_tuple[0])
whole_hta_list = re.split("HTA", whole_hta_string)
hta_name = whole_hta_list[0]
hta_number = 'HTA' + whole_hta_list[1]
units_needed = int(hta_tuple[1])
# Provided the quantity is greater than zero, this block checks the quantity in stock and compares it to the
# quantity required. Since we only make the number of helmet decals we need (no minimum order quantity, no
# rounding up), we just subtract the number in stock from the number required. If the resulting number is
# greater than zero, the item stays on (technically, is added to) the inbound list.
#
# Due to human error, HTA names occasionally contain unwelcome spaces. In this case, the database (or the
# database guy, not sure) either replaces the spaces with hyphens or just omits them. Both possibilities are
# checked for here.
#
# When in doubt, this module errs on the side of ordering what it doesn't understand.
if units_needed > 0:
try:
units_on_hand = int(cursor.execute('SELECT "Units on Hand Helmet Decals" FROM Inventory WHERE Name=? '
'AND "HTA Number"=?',
hta_name, hta_number).fetchval())
# If there's negative inventory, this just orders the original quantity.
if units_on_hand < 0:
inbound_tuple = (whole_hta_string, units_needed, 'Helmet')
inbound_list.append(inbound_tuple)
continue
units_to_order = units_needed - units_on_hand
if units_to_order > 0:
inbound_tuple = (whole_hta_string, units_to_order, 'Helmet')
inbound_list.append(inbound_tuple)
else:
continue
except TypeError:
if ' ' in hta_name:
hta_name = hta_name.replace(' ', '-')
try:
units_on_hand = int(
cursor.execute('SELECT "Units on Hand Helmet Decals" FROM Inventory WHERE Name=? AND "HTA '
'Number"=?',
hta_name, hta_number).fetchval())
if units_on_hand < 0:
inbound_tuple = (whole_hta_string, units_needed, 'Helmet')
inbound_list.append(inbound_tuple)
continue
units_to_order = units_needed - units_on_hand
if units_to_order > 0:
inbound_tuple = (whole_hta_string, units_to_order, 'Helmet')
inbound_list.append(inbound_tuple)
else:
continue
except TypeError:
hta_name = hta_name.replace('-', '')
try:
units_on_hand = int(
cursor.execute('SELECT "Units on Hand Helmet Decals" FROM Inventory WHERE Name=? AND '
'"HTA Number"=?',
hta_name, hta_number).fetchval())
if units_on_hand < 0:
inbound_tuple = (whole_hta_string, units_needed, 'Helmet')
inbound_list.append(inbound_tuple)
continue
units_to_order = units_needed - units_on_hand
if units_to_order > 0:
inbound_tuple = (whole_hta_string, units_to_order, 'Helmet')
inbound_list.append(inbound_tuple)
else:
continue
except TypeError:
inbound_tuple = (whole_hta_string, units_needed, 'Helmet')
inbound_list.append(inbound_tuple)
else:
inbound_tuple = (whole_hta_string, units_needed, 'Helmet')
inbound_list.append(inbound_tuple)
if inbound_list:
return inbound_list
else:
return None
def compare_rdx(outbound_list):
# This checks the inventory for raised (RDX) helmets.
# For more details on how this all works, please see the compare_transfers function in this module.
# This skips this whole rigamarole if the names/numbers are missing.
if outbound_list == 'Names and numbers not found!':
return 'Names and numbers not found!'
# This establishes the connection to the database.
load_dotenv()
connection_list = ['DSN=Heat Transfer Inventory;Database=Heat Transfer Inventory;UID=',
os.getenv('HTI_UID'),
';PWD=',
os.getenv('HTI_PWD')]
connection = pyodbc.connect(''.join(connection_list))
cursor = connection.cursor()
# outbound_list is a list of HTAs that have been ordered by the customer.
# inbound_list is a list of HTAs that we don't already have in the warehouse, which must be made or ordered.
inbound_list = []
for i in range(len(outbound_list)):
# This block unpacks the HTA name and quantity for easy use.
whole_hta_string = str(outbound_list[i][0])
units_needed = int(outbound_list[i][1])
# Provided the quantity is greater than zero, this block checks the quantity in stock and compares it to the
# quantity required. Since we only order the number of helmet decals we need (no minimum order quantity, no
# rounding up), we just subtract the number in stock from the number required. If the resulting number is
# greater than zero, the item stays on (technically, is added to) the inbound list.
#
# When in doubt, this module errs on the side of ordering what it doesn't understand.
if units_needed > 0:
try:
units_on_hand = int(cursor.execute('SELECT "Units on Hand" FROM Inventory WHERE "ITEM ID MATCH FIELD"=?',
whole_hta_string).fetchval())
# If there's negative inventory, this just orders the original quantity.
if units_on_hand < 0:
inbound_tuple = (whole_hta_string, units_needed)
inbound_list.append(inbound_tuple)
continue
units_to_order = units_needed - units_on_hand
if units_to_order > 0:
inbound_tuple = (whole_hta_string, units_to_order)
inbound_list.append(inbound_tuple)
else:
continue
except TypeError:
inbound_tuple = (whole_hta_string, units_needed)
inbound_list.append(inbound_tuple)
if inbound_list:
return inbound_list
else:
return None