-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathamazon_script_2.py
More file actions
359 lines (324 loc) · 15.1 KB
/
Copy pathamazon_script_2.py
File metadata and controls
359 lines (324 loc) · 15.1 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
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
352
353
354
355
356
357
358
359
from multiprocessing.sharedctypes import Value
from re import A
from openpyxl import load_workbook
import os
from datetime import date
from amazon_script_1 import *
import operator
def main():
# Check if 'POs to Confirm [DATE].xlsx' file exists (needed to run this program)
today = date.today()
pos_to_confirm_filename = (
"POs to Confirm - " + today.strftime("%B %d, %Y") + ".xlsx"
)
# Check if the file exists
check_file(pos_to_confirm_filename)
# Get workbook + sheets from within the workbook
new_wb = load_workbook(filename=pos_to_confirm_filename)
raw_data_sheet = new_wb["PO Raw Data"]
inv_to_confirm_sheet = new_wb["Inv to Confirm"]
pos_to_confirm_sheet = new_wb["POs to Confirm"]
# Create a dict for the units to cancel (from non-cancelled orders / orders over min threshold)
units_to_cancel_dict = get_units_to_cancel(inv_to_confirm_sheet)
# Improve formatting/readability of new workbook
reformat_inv_to_confirm_sheet(inv_to_confirm_sheet)
# Get PO values
po_value_dict = get_po_values(raw_data_sheet)
# Remove out of stock units - if any (from non-cancelled orders / orders over min threshold)
remove_out_of_stock_units(raw_data_sheet, units_to_cancel_dict, po_value_dict)
# Recalculate orders over min threshold
sorted_po_list = get_and_sort_po_values(raw_data_sheet)
orders_to_cancel_list = create_list_pos_to_cancel(sorted_po_list)
# Remove cancelled orders from PO Raw Data sheet
update_raw_data_sheet(raw_data_sheet, orders_to_cancel_list)
# Print data to terminal
print_pos_to_confirm_final_version(raw_data_sheet)
print_inventory_to_confirm_final_version(raw_data_sheet)
# Update newly created sheets (allows for manual entry, if the user wishes, and makes it easy to verify any issues)
update_inventory_to_confirm_sheet(raw_data_sheet, inv_to_confirm_sheet)
update_pos_to_confirm_sheet(raw_data_sheet, pos_to_confirm_sheet)
save_new_wb(new_wb, pos_to_confirm_filename)
# Update Vendor Download Sheets with correct values
po_dict = create_po_dict(raw_data_sheet)
# Update Vendor Download Sheets with correct values
usd_file, cad_file = get_vendor_download_file()
if usd_file != "empty":
update_vendor_download_sheet(usd_file, po_dict)
if cad_file != "empty":
update_vendor_download_sheet(cad_file, po_dict)
def check_file(pos_to_confirm_filename):
if not os.path.exists(pos_to_confirm_filename):
raise Exception(
"Error: The required file does not exist - please make sure you have run the first script properly before running this one."
)
def get_units_to_cancel(inv_to_confirm_sheet):
units_to_cancel_dict = {}
# Iterate through data from 'Inv to Confirm' Sheet:
for row in inv_to_confirm_sheet.iter_rows(
min_row=2, max_row=inv_to_confirm_sheet.max_row
):
if row[0].value == None:
break
else:
model_number = row[0].value
units_from_pos_under_380 = row[2].value
units_in_stock = row[3].value
if units_in_stock < units_from_pos_under_380:
units_to_cancel = units_from_pos_under_380 - units_in_stock
if model_number in units_to_cancel_dict.keys():
units_to_cancel_dict[model_number] = (
units_to_cancel_dict[model_number] + units_to_cancel
)
else:
units_to_cancel_dict[model_number] = units_to_cancel
return units_to_cancel_dict
def reformat_inv_to_confirm_sheet(inv_to_confirm_sheet):
inv_to_confirm_sheet["C1"].fill = PatternFill("solid", start_color="CCE5FF")
inv_to_confirm_sheet["D1"].fill = PatternFill("solid", start_color="CCE5FF")
inv_to_confirm_sheet["E1"].fill = PatternFill("solid", start_color="C1E1C1")
# Remove background color from cells in column D
for row in inv_to_confirm_sheet.iter_cols(
min_row=2, max_row=inv_to_confirm_sheet.max_row, min_col=4, max_col=4
):
for cell in row:
cell.fill = PatternFill(fill_type=None)
cell.font = Font(color="000000")
def get_po_values(raw_data_sheet):
po_value_dict = {}
for row in raw_data_sheet.iter_rows(min_row=2, max_row=(raw_data_sheet.max_row)):
po_number = row[0].value
cost = row[8].value
quantity_confirmed = row[11].value
if po_number in po_value_dict.keys():
po_value_dict[po_number] = po_value_dict[po_number] + (
cost * quantity_confirmed
)
else:
po_value_dict[po_number] = cost * quantity_confirmed
return po_value_dict
def remove_out_of_stock_units(raw_data_sheet, units_to_cancel_dict, po_value_dict):
rows = list(raw_data_sheet.iter_rows(min_row=2, max_row=raw_data_sheet.max_row))
rows = reversed(rows)
for row in rows:
model_number = row[2].value
if model_number in units_to_cancel_dict.keys():
po_number = row[0].value
cost = row[8].value
po_value = po_value_dict[po_number]
units_to_cancel = units_to_cancel_dict[model_number]
for x in range(units_to_cancel):
if (po_value - cost) > min_po_value:
if row[11].value > 0:
po_value_dict[po_number] = po_value_dict[po_number] - cost
po_value = po_value_dict[po_number]
row[11].value = (
row[11].value - 1
) # row[11] = 'Quantity Confirmed'
row[12].value = (
row[12].value + 1
) # row[12] = 'Quantity Canceled'
units_to_cancel_dict[model_number] = (
units_to_cancel_dict[model_number] - 1
)
if units_to_cancel_dict[model_number] == 0:
del units_to_cancel_dict[model_number]
# If there are remaining units to cancel, remove them starting from the PO with the lowest value
if len(units_to_cancel_dict) != 0:
low_to_high_po_value_list = sorted(po_value_dict.items(), key=lambda x: x[1])
for index, tuple in enumerate(low_to_high_po_value_list):
list_po_number = tuple[0]
rows = list(
raw_data_sheet.iter_rows(min_row=2, max_row=raw_data_sheet.max_row)
)
rows = reversed(rows)
for row in rows:
po_number = row[0].value
model_number = row[2].value
cost = row[8].value
po_value = po_value_dict[po_number]
if po_number == list_po_number:
if model_number in units_to_cancel_dict.keys():
units_to_cancel = units_to_cancel_dict[model_number]
for y in range(units_to_cancel):
if row[11].value > 0:
po_value_dict[po_number] = (
po_value_dict[po_number] - cost
)
row[11].value = (
row[11].value - 1
) # row[11] = 'Quantity Ordered'
row[12].value = (
row[12].value + 1
) # row[12] = 'Quantity Canceled'
units_to_cancel_dict[model_number] = (
units_to_cancel_dict[model_number] - 1
)
if units_to_cancel_dict[model_number] == 0:
del units_to_cancel_dict[model_number]
def print_pos_to_confirm_final_version(raw_data_sheet):
po_value_dict = get_po_values(raw_data_sheet)
sorted_po_list = sorted(po_value_dict.items(), key=lambda x: x[1], reverse=True)
cad_po_list = get_cad_pos(raw_data_sheet)
orders_to_cancel_list = create_list_pos_to_cancel(sorted_po_list)
po_heading_title = (
"---------------\nAmazon: POs to Confirm/Cancel (confirmed)\n---------------"
)
print_pos_to_confirm(
sorted_po_list, cad_po_list, orders_to_cancel_list, po_heading_title
)
def print_inventory_to_confirm_final_version(raw_data_sheet):
po_value_dict = get_po_values(raw_data_sheet)
sorted_po_list = sorted(po_value_dict.items(), key=lambda x: x[1], reverse=True)
orders_to_cancel_list = create_list_pos_to_cancel(sorted_po_list)
inventory_dicts = create_inventory_tracker_dicts(
raw_data_sheet, orders_to_cancel_list
)
inv_heading_title = "---------------\nAmazon: Inventory Requested (from non-cancelled POs - confirmed)\n---------------"
print_inventory_to_confirm(inventory_dicts, inv_heading_title)
def update_inventory_to_confirm_sheet(raw_data_sheet, inv_to_confirm_sheet):
confirmed_inventory_dict = {}
for row in raw_data_sheet.iter_rows(
min_row=2, max_row=(inv_to_confirm_sheet.max_row)
):
model_number = row[2].value
quantity_confirmed = row[11].value
if model_number == None:
break
elif model_number in confirmed_inventory_dict.keys():
confirmed_inventory_dict[model_number] = (
confirmed_inventory_dict[model_number] + quantity_confirmed
)
else:
confirmed_inventory_dict[model_number] = quantity_confirmed
for row in inv_to_confirm_sheet.iter_rows(
min_row=2, max_row=(inv_to_confirm_sheet.max_row)
):
model_number = row[0].value
model_number = row[0].value
if model_number == None:
break
else:
row[4].value = confirmed_inventory_dict[model_number]
row[4].fill = PatternFill("solid", start_color="FFFDD0")
def update_pos_to_confirm_sheet(raw_data_sheet, pos_to_confirm_sheet):
sorted_po_list = get_and_sort_po_values(raw_data_sheet)
orders_to_cancel_list = create_list_pos_to_cancel(sorted_po_list)
rows = list(
pos_to_confirm_sheet.iter_rows(min_row=2, max_row=pos_to_confirm_sheet.max_row)
)
for row in rows:
new_po_number = row[0].value
new_model_number = row[3].value
if new_po_number in orders_to_cancel_list:
row[1].value = "CANCELLED"
row[1].font = Font(color="FF0000")
row[2].value = " "
row[5].value = 0
else:
crows = list(
raw_data_sheet.iter_rows(min_row=2, max_row=raw_data_sheet.max_row)
)
for crow in crows:
old_po_number = crow[0].value
old_model_number = crow[2].value
old_quantity_confirmed = crow[11].value
if (new_po_number == old_po_number) and (
new_model_number == old_model_number
):
row[5].value = old_quantity_confirmed
new_quantity_requested = row[4].value
new_quantity_confirmed = row[5].value
if new_quantity_requested != new_quantity_confirmed:
row[5].fill = PatternFill("solid", start_color="FFFDD0")
row[2].value = "NO"
row[2].font = Font(color="FF0000")
# Create dict of pos with dict inside of po line items
def create_po_dict(raw_data_sheet):
rows = list(raw_data_sheet.iter_rows(min_row=2, max_row=raw_data_sheet.max_row + 1))
po_dict = {}
line_items = {}
previous_po = ""
x = 1
for row in rows:
if row[0].value != None:
current_po = row[0].value
if (previous_po == current_po) or (previous_po == ""):
model_number = row[2].value
quantity_confirmed = row[11].value
line_items[model_number] = quantity_confirmed
previous_po = current_po
else:
po_dict[previous_po] = line_items
line_items = {}
model_number = row[2].value
quantity_confirmed = row[11].value
line_items[model_number] = quantity_confirmed
previous_po = current_po
else:
po_dict[previous_po] = line_items
break
return po_dict
def get_vendor_download_file():
my_dir = os.listdir()
amazon_files = []
usd_file = "empty"
cad_file = "empty"
for x in my_dir:
if "VendorDownload" in x:
amazon_file = x
amazon_files.append(amazon_file)
if len(amazon_files) >= 1:
amazon_file_1 = amazon_files[0]
wb_1 = load_workbook(filename=amazon_file_1)
ws_1 = wb_1.active
currency = ws_1["AF4"].value
if currency == "USD":
usd_file = amazon_files[0]
elif currency == "CAD":
cad_file = amazon_files[0]
if len(amazon_files) == 2:
amazon_file_2 = amazon_files[1]
wb_2 = load_workbook(filename=amazon_file_2)
ws_2 = wb_2.active
currency = ws_2["AF4"].value
if currency == "USD":
if usd_file != "empty":
raise Exception("Error: There are 2 USD Vendor Download Files")
usd_file = amazon_files[1]
elif currency == "CAD":
if cad_file != "empty":
raise Exception("Error: There are 2 CAD Vendor Download Files")
cad_file = amazon_files[1]
return usd_file, cad_file
def update_vendor_download_sheet(file, po_dict):
wb = load_workbook(filename=file)
ws = wb.active
rows = list(ws.iter_rows(min_row=4, max_row=ws.max_row))
for row in rows:
# VD Column A (Column #0)= PO Number
# VD Column C (Column #2) = Model Number
# VD Column L (Column #11) = Quantity Confirmed
# VD Column P (Column #15) = Hand Off End
# VD Column R (Column #17) = Expected Hand Off End
# VD Column S (Column #18) = Availability Status (AC/OS)
# VD Column X (Column #23) = Condition (confirmed/unconfirmed)
po = row[0].value
model_number = row[2].value
# Get PO line items
line_items = po_dict[po]
quantity_confirmed = line_items[model_number]
# Update VD sheet w/ actual quantity confirmed
row[11].value = quantity_confirmed
# Update VD sheet "Expected Hand Off Date" with the 'Hand Off End' date
row[17].value = row[15].value
# Update 'Availability Status' if zero quantity is accepted
if quantity_confirmed == 0:
row[18].value = "OS - Cancelled: Out of stock"
# Update 'Condition' to "Confirmed"
row[23].value = "Confirmed"
wb.save(filename=file
)
# Save the workbook
def save_new_wb(new_wb, pos_to_confirm_filename):
new_wb.save(pos_to_confirm_filename)
main()