-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendingConfirmation.py
312 lines (234 loc) · 10.2 KB
/
sendingConfirmation.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
########################################################################################################################
#
# INCOMPLETE - PENDING OPTIMIZATION
#
########################################################################################################################
import datetime
import os
import datetime
import pandas as pd
import win32com
import xlwings as xw
from os.path import exists
from PIL import ImageGrab
from emailSend import email
global file_path
global mod_file_paths
########################################################################################################################
#
# EXCEL FILE GENERATION
#
########################################################################################################################
# get the image of the
def getImage(sht, xlrange):
win32c = win32com.client.constants
sht.range(xlrange).api.CopyPicture(Format=win32c.xlBitmap) # 2 copies as when it is printed
img = ImageGrab.grabclipboard()
img.save(r'report.png')
# Format the inventory files by removing blank/unnecessary lines
def formatExcel(hj_path, sap_path):
global mod_file_paths
mod_file_paths = []
#
# Remove unwanted lines from the Excel file
#
with xw.App(visible=False) as app:
wb = app.books.open(hj_path)
# check if this value exists on the first line of the file
# if yes then remove that line
# if no then do nothing
check = '<div'
if check in wb.sheets['Sheet1'].range('A1').value:
wb.sheets['Sheet1'].range('1:1').delete()
wb.save(hj_path)
def toExcel(df_sap, df_hj):
#
# Export the SAP and HJ data to Excel file
#
global file_path
date = datetime.datetime.now()
desktop_path = "D:\Sending Confirmation"
file_path = desktop_path + "\\Sending Confirmation Report " + date.strftime("%d %b %I %M %p") + ".xlsx"
print(file_path)
if exists(file_path):
wb = xw.Book(file_path)
else:
wb = xw.Book()
# define the Excel sheets
sht_sap = wb.sheets['Sheet1']
sht_sap.name = 'MB51'
sht_hj = wb.sheets.add()
sht_hj.name = 'HJ GDN'
df_sap = df_sap.applymap(lambda x: str(x) if isinstance(x, datetime.time) else x)
# paste dataframes in table form
sht_sap.range("A1").options(index=False).value = df_sap
sht_hj.range("A1").options(index=False).value = df_hj
wb.save(file_path)
def toExcelVariance(df_variance, df_variance_2):
#
# Export Variance Identified to Excel
#
global file_path
if exists(file_path):
wb = xw.Book(file_path)
else:
wb = xw.Book()
# define the Excel sheets
sht_var = wb.sheets.add()
sht_var.name = 'Variance'
# paste dataframes in table form
sht_var.range("A2").value = 'Quantity Summary Comparison in YDS'
sht_var.range("A2").expand("right").api.Font.Bold = True
sht_var.range("A4").options(index=False).value = df_variance
table1 = sht_var.tables.add(source=sht_var.range('A4').expand())
# -----------------------------------------------------------------
last_table = sht_var.tables[-1]
last_row = last_table.range.last_cell.row
sht_var.range(last_row + 2, 1).value = 'Quantity Summary Comparison in Rolls'
sht_var.range(last_row + 2, 1).expand("right").api.Font.Bold = True
sht_var.range(last_row + 4, 1).options(index=False).value = df_variance_2
table2 = sht_var.tables.add(source=sht_var.range(last_row + 4, 1).expand(), table_style_name='TableStyleMedium5')
# -----------------------------------------------------------------
last_table = sht_var.tables[-1]
last_row = last_table.range.last_cell.row
sht_var.range(last_row + 2, 1).value = 'Sending Confirmation Summary'
sht_var.range(last_row + 2, 1).expand("right").api.Font.Bold = True
# create summary dataframe and convert it to a table in excel
df_summary = pd.DataFrame()
df_summary['Category'] = ['YDS', 'Rolls']
df_summary['SAP'] = [df_variance['SAP Pick Quantity (YDS)'].sum(),
df_variance_2['SAP Pick Quantity (Roll)'].sum()]
df_summary['HJ'] = [df_variance['HJ GDN Quantity (YDS)'].sum(),
df_variance_2['HJ GDN Quantity (Roll)'].sum()]
df_summary['Variance'] = [df_variance['SAP Pick Quantity (YDS)'].sum() - df_variance['HJ GDN Quantity (YDS)'].sum(),
df_variance_2['SAP Pick Quantity (Roll)'].sum() - df_variance_2[
'HJ GDN Quantity (Roll)'].sum()]
# identifying the larger quantity
qty = []
for var in df_summary['Variance']:
if var > 0:
qty.append("HJ+")
elif var < 0:
qty.append("SAP+")
else:
qty.append("Quantity Tally")
df_summary['Remarks'] = qty
sht_var.range(last_row + 4, 1).options(index=False).value = df_summary
table3 = sht_var.tables.add(source=sht_var.range(last_row + 4, 1).expand(), table_style_name='TableStyleMedium7')
expand_cells = sht_var.range("A1").expand("right")
expand_cells.row_height = 15
expand_cells.column_width = 30
sht_var.range("B:B").number_format = "#,##0.000"
sht_var.range("C:C").number_format = "#,##0.000"
image_range = sht_var.range((1, 1),
(table3.range.last_cell.row, table3.range.last_cell.column))
getImage(sht_var, image_range)
wb.save(file_path)
########################################################################################################################
#
# DATA EXTRACTION
#
########################################################################################################################
# get the SAP and HJ Reports
def sendingConfirmation(file_paths, status, progress_bar):
global mod_file_paths
try:
formatExcel(file_paths[1], file_paths[0])
# get the SAP and HJ Reports
df_sap = pd.read_excel(file_paths[0])
df_hj = pd.read_excel(file_paths[1])
# Creating Material and trimmed load ID code
material = []
load_id = []
references = []
for mat in df_hj['Item Number']:
temp = mat.split('-')
print(temp)
material.append(temp[1])
for load in df_hj['Load Id']:
temp_load = load.split('-')
load_id.append(temp_load[0])
for ref in df_sap['Reference']:
temp_load = str(ref).split('-')
references.append(temp_load[0])
df_hj['Material'] = material
df_hj['Load ID Trimmed'] = load_id
df_sap['Reference'] = references
# get the dataframe for the sap report and re-arrange the ID column
df_sap["Unique"] = df_sap['Material'].astype(str) + "-" + df_sap["Reference"].astype(str)
sap_ids = df_sap['Unique'].to_list()
# remove the decimal, nan values & random '-' IDs
new_sap_ids = [i.replace('.0', '') for i in sap_ids]
newer_sap_ids = [i.replace('nan', '') for i in new_sap_ids]
df_sap.drop('Unique', axis=1, inplace=True)
df_sap['Unique'] = newer_sap_ids
first_column_sap = df_sap.pop('Unique')
df_sap.insert(0, 'Unique', first_column_sap)
df_sap = df_sap[df_sap['Unique'] != '-']
# get the data frame for the HJ report and re-arrange the ID column
df_hj["Unique"] = df_hj['Material'].astype(str) + "-" + df_hj["Load ID Trimmed"].astype(str)
first_column_hj = df_hj.pop('Unique')
df_hj.insert(0, 'Unique', first_column_hj)
toExcel(df_sap, df_hj)
df_sap_cpy = df_sap
df_hj_cpy = df_hj
#
# Quantity Summary Comparison in YDS
#
df_sap = df_sap[['Unique', 'Qty in Un. of Entry']]
df_sap = df_sap.groupby('Unique').sum()
df_sap.reset_index(inplace=True)
df_hj = df_hj[['Unique', 'Quantity']]
df_hj = df_hj.groupby('Unique').sum()
df_hj.reset_index(inplace=True)
# combine IDs and Quantities
df_variance = pd.merge(df_sap, df_hj, on="Unique", how="outer")
df_variance.fillna(0, inplace=True)
df_variance.columns = ['Unique', 'SAP Pick Quantity (YDS)', 'HJ GDN Quantity (YDS)']
df_variance['VARIANCE'] = round(
df_variance['HJ GDN Quantity (YDS)'].astype(float) - df_variance['SAP Pick Quantity (YDS)'].astype(float), 3)
# identifying the larger quantity
qty = []
for var in df_variance['VARIANCE']:
if var > 0:
qty.append("HJ+")
elif var < 0:
qty.append("SAP+")
else:
qty.append("Quantity Tally")
# adding status and comments columns
df_variance['SAP+/HJ+ in QTY Level'] = qty
#
# Quantity Summary Comparison in Rolls
#
df_sap_2 = df_sap_cpy[['Unique', 'Qty in Un. of Entry']]
df_sap_2 = df_sap_2.groupby('Unique').count()
df_sap_2.reset_index(inplace=True)
df_hj_2 = df_hj_cpy[['Unique', 'Quantity']]
df_hj_2 = df_hj_2.groupby('Unique').count()
df_hj_2.reset_index(inplace=True)
# combine IDs and Quantities
df_variancej_2 = pd.merge(df_sap_2, df_hj_2, on="Unique", how="outer")
df_variancej_2.fillna(0, inplace=True)
df_variancej_2.columns = ['Unique', 'SAP Pick Quantity (Roll)', 'HJ GDN Quantity (Roll)']
df_variancej_2['VARIANCE'] = round(
df_variancej_2['SAP Pick Quantity (Roll)'].astype(float) - df_variancej_2['HJ GDN Quantity (Roll)'].astype(
float), 3)
# identifying the larger quantity
qty = []
for var in df_variancej_2['VARIANCE']:
if var > 0:
qty.append("HJ+")
elif var < 0:
qty.append("SAP+")
else:
qty.append("Quantity Tally")
# adding status and comments columns
df_variancej_2['SAP+/HJ+ in QTY Level'] = qty
toExcelVariance(df_variance, df_variancej_2)
email(file_paths)
status.configure(text="Completed")
progress_bar.stop()
except Exception as e:
status.configure(text=e)
progress_bar.stop()