-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderProcessing.py
More file actions
418 lines (374 loc) · 19.9 KB
/
orderProcessing.py
File metadata and controls
418 lines (374 loc) · 19.9 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import requests
import pandas as pd
import os
from dotenv import load_dotenv
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
from datetime import datetime
from email_module import create_and_draft_email
current_date = datetime.now()
# Create date format for orders
year = current_date.strftime('%Y')
month_number = current_date.strftime('%m')
month_name = current_date.strftime('%B')
day = current_date.strftime('%d')
path_prefix = f'./{year}/{month_number} - {month_name}/{month_number}-{day}-{year}/'
class VendorFormatter:
def __init__(self, location_id, output_filename):
self.location_id = location_id
self.output_filename = output_filename
self.columns_order = []
self.mapping = {}
self.default_values = {}
self.location_id_file = f"lastRun_{location_id}.txt"
def set_defaults(self, default_values):
if default_values is not None:
self.default_values = default_values
else:
self.default_values = {}
def set_mapping(self, columns_order, mapping):
self.columns_order = columns_order
self.mapping = mapping
def extract_nested_field(self, node, field_path):
"""
Extract a nested field from the GraphQL response node.
Example: extract_nested_field(order_node, "shippingAddress.name")
"""
fields = field_path.split(".")
value = node
for field in fields:
if field.isdigit(): # Check if the field is a numeric index (indicating an array)
field = int(field)
if isinstance(value, list) and 0 <= field < len(value):
value = value[field]
else:
return None
elif field in value:
value = value[field]
else:
return None # Handle missing fields gracefully
return value
def map_vendor_data(self, order_node):
all_line_items_data = []
for line_item_node in order_node.get("lineItems", {}).get("nodes", []):
if line_item_node.get("requiresShipping", True):
vendor_data = {}
for column_name, details in self.mapping.items():
if details["level"] == "order":
vendor_data[column_name] = self.extract_nested_field(order_node, details["path"])
elif details["level"] == "line_item":
vendor_data[column_name] = line_item_node.get(details["path"])
elif details["level"] == "static":
vendor_data[column_name] = details["value"]
if "order" in order_node:
order_data = order_node["order"]
if "id" in order_data:
vendor_data["PO Number"] = order_data["id"].split("/")[-1]
vendor_data["id"] = order_data["id"].split("/")[-1]
if "customer" in order_data and "id" in order_data["customer"]:
vendor_data["Customer ID"] = order_data["customer"]["id"].split("/")[-1]
# Set default values for columns that need them
for column_name, default_value in self.default_values.items():
vendor_data.setdefault(column_name, default_value)
all_line_items_data.append(vendor_data)
return all_line_items_data
def run_query_and_format(self):
# Your Shopify GraphQL endpoint
graphql_endpoint = os.getenv("ENDPOINT_URL")
# Your Shopify access token
access_token = os.getenv("ACCESS_TOKEN")
# GraphQL query template with a variable
graphql_query_template = """
query FulfillmentOrders($locationId: String!, $after: String) {
fulfillmentOrders(
first: 5
query: $locationId
includeClosed: true
after: $after
) {
nodes {
assignedLocation {
name
}
order {
id
name
billingAddress {
address1
address2
company
city
countryCode
firstName
lastName
name
phone
provinceCode
zip
}
customer {
id
email
}
shippingAddress {
address1
address2
company
city
countryCode
firstName
lastName
name
phone
provinceCode
zip
}
shippingLine {
title
}
}
status
lineItems(first: 25) {
pageInfo {
hasNextPage
}
edges {
cursor
}
nodes {
sku
totalQuantity
productTitle
requiresShipping
}
}
createdAt
}
pageInfo {
endCursor
hasNextPage
}
edges {
cursor
}
}
}
"""
all_flattened_data = []
cursor = None
while True:
if os.path.isfile(self.location_id_file):
with open(self.location_id_file, "r") as file:
cursor = file.read().strip()
response = requests.post(
graphql_endpoint,
json={"query": graphql_query_template, "variables": {"locationId": f"assigned_location_id:{self.location_id}", "after": cursor}},
headers={"Content-Type": "application/json", "X-Shopify-Access-Token": access_token},
)
data = response.json()
for order_node in data["data"]["fulfillmentOrders"]["nodes"]:
line_items_data = self.map_vendor_data(order_node)
all_flattened_data.extend(line_items_data)
# Update cursor for the next page
has_next_page = data["data"]["fulfillmentOrders"]["pageInfo"]["hasNextPage"]
cursor = data["data"]["fulfillmentOrders"]["pageInfo"]["endCursor"]
if has_next_page:
with open(self.location_id_file, "w") as file:
file.write(cursor)
else:
break
if cursor is not None:
with open(self.location_id_file, "w") as file:
file.write(cursor)
if all_flattened_data:
df = pd.DataFrame(all_flattened_data, columns=self.columns_order)
xlsx_file_path = f"{path_prefix}{self.output_filename}.xlsx"
# Create directory structure if it doesn't already exist
os.makedirs(os.path.dirname(xlsx_file_path), exist_ok=True)
df.to_excel(xlsx_file_path, index=False)
# Auto-adjust columns' width
workbook = load_workbook(xlsx_file_path)
worksheet = workbook.active
for column_cells in worksheet.columns:
length = max(len(str(cell.value)) for cell in column_cells) + 2
worksheet.column_dimensions[get_column_letter(column_cells[0].column)].width = length
workbook.save(xlsx_file_path)
#print(f"XLSX file has been created: {xlsx_file_path}")
else:
print("No data to include in the DataFrame.")
# Load environment variables from .env
load_dotenv()
# List of vendors with their formatting details
vendors = [
{
"location_id": "00000000000",
"vendor_name": "vendor1",
"output_filename": "filename1",
"email_addresses": [
"example@example.com",
],
"columns_order": ["Order ID", "Ship-to Name", "Ship-to Address 1", "Ship-to Address 2", "Ship-to City",
"Ship-to State", "Ship-to Zip", "", "SKU Number", "Quantity"],
"mapping": {
"Order ID": {"path": "order.name", "level": "order"},
"Ship-to Name": {"path": "order.shippingAddress.name", "level": "order"},
"Ship-to Address 1": {"path": "order.shippingAddress.address1", "level": "order"},
"Ship-to Address 2": {"path": "order.shippingAddress.address2", "level": "order"},
"Ship-to City": {"path": "order.shippingAddress.city", "level": "order"},
"Ship-to State": {"path": "order.shippingAddress.provinceCode", "level": "order"},
"Ship-to Zip": {"path": "order.shippingAddress.zip", "level": "order"},
"": {"path": "static", "level": "static", "value": "Home"},
"SKU Number": {"path": "sku", "level": "line_item"},
"Quantity": {"path": "totalQuantity", "level": "line_item"},
},
"defaults": {
"": "Home"
}
},
{
"location_id":"00000000000",
"vendor_name": "vendor2",
"output_filename": "filename2",
"email_addresses": [
"example@example.com"
],
"columns_order": ["Name", "Email", "Created At", "Shipping Method", "Lineitem quantity",
"Lineitem Name", "Lineitem sku", "Billing Name", "Billing Address1", "Billing Address2",
"Billing Company", "Billing City", "Billing Zip", "Billing Province", "Billing Country",
"Billing Phone", "Shipping Name", "Shipping Address1", "Shipping Address2", "Shipping Company",
"Shipping City", "Shipping Zip", "Shipping Province", "Shipping Country", "Shipping Phone", "Id"],
"mapping": {
"Name": {"path": "order.name", "level": "order"},
"Email": {"path": "order.customer.email", "level": "order"},
"Created At": {"path": "createdAt", "level": "order"},
"Shipping Method": {"path": "order.shippingLine.title", "level": "order"},
"Lineitem quantity": {"path": "totalQuantity", "level": "line_item"},
"Lineitem Name": {"path": "productTitle", "level": "line_item"},
"Lineitem sku": {"path": "sku", "level": "line_item"},
"Billing Name": {"path": "order.billingAddress.name", "level": "order"},
"Billing Address1": {"path": "order.billingAddress.address1", "level": "order"},
"Billing Address2": {"path": "order.billingAddress.address2", "level": "order"},
"Billing Company": {"path": "order.billingAddress.company", "level": "order"},
"Billing City": {"path": "order.billingAddress.city", "level": "order"},
"Billing Zip": {"path": "order.billingAddress.zip", "level": "order"},
"Billing Province": {"path": "order.billingAddress.provinceCode", "level": "order"},
"Billing Country": {"path": "order.billingAddress.countryCode", "level": "order"},
"Billing Phone": {"path": "order.billingAddress.phone", "level": "order"},
"Shipping Name": {"path": "order.shippingAddress.name", "level": "order"},
"Shipping Address1": {"path": "order.shippingAddress.address1", "level": "order"},
"Shipping Address2": {"path": "order.shippingAddress.address2", "level": "order"},
"Shipping Company": {"path": "order.shippingAddress.company", "level": "order"},
"Shipping City": {"path": "order.shippingAddress.city", "level": "order"},
"Shipping Zip": {"path": "order.shippingAddress.zip", "level": "order"},
"Shipping Province": {"path": "order.shippingAddress.provinceCode", "level": "order"},
"Shipping Country": {"path": "order.shippingAddress.countryCode", "level": "order"},
"Shipping Phone": {"path": "order.shippingAddress.phone", "level": "order"},
"Id": {"path": "order.id", "level": "order"},
},
"defaults": {
}
},
{
"location_id": "00000000000",
"vendor_name": "vendor3",
"output_filename": "filename3",
"email_addresses": [
"example@example.com",
"example2@example.com",
],
"columns_order": ["Order Number", "PO Number", "Order Date", "Customer ID", "Billing Full Name",
"Billing First Name", "Billing Last Name", "Billing Street 1", "Billing Street 2", "Billing City",
"Billing State", "Billing ZipCode", "Billing Email", "Billing Phone",
"Billing Country", "Shipping Full Name", "Shipping First Name", "Shipping Last Name",
"Shipping Street 1", "Shipping Street 2", "Shipping City", "Shipping State",
"Shipping ZipCode", "Shipping Email", "Shipping Phone", "Shipping Country",
"Shipping Charge", "Sales Tax", "Shipping Carrier", "Shipping Method", "Ship By Date",
"Shipping Reference 1", "Shipping Reference 2", "3rd Party Shipping Account Number",
"3rd Party Shipping Name", "3rd Party Shipping Address", "3rd Party Shipping City",
"3rd Party Shipping State", "3rd Party Shipping ZipCode", "3rd Party Shipping Country",
"Lineitem sku", "Item Description", "Item Quantity", "Item Price", "Notes"],
"mapping": {
"Order Number": {"path": "order.name", "level": "order"},
"PO Number": {"path": "order.id", "level": "order"},
"Order Date": {"path": "createdAt", "level": "order"},
"Customer ID": {"path": "order.customer.id", "level": "order"},
"Billing Full Name": {"path": "order.billingAddress.name", "level": "order"},
"Billing First Name": {"path": "order.billingAddress.firstName", "level": "order"},
"Billing Last Name": {"path": "order.billingAddress.lastName", "level": "order"},
"Billing Street 1": {"path": "order.billingAddress.address1", "level": "order"},
"Billing Street 2": {"path": "order.billingAddress.address2", "level": "order"},
"Billing City": {"path": "order.billingAddress.city", "level": "order"},
"Billing State": {"path": "order.billingAddress.provinceCode", "level": "order"},
"Billing ZipCode": {"path": "order.billingAddress.zip", "level": "order"},
"Billing Email": {"path": "order.customer.email", "level": "order"},
"Billing Phone": {"path": "order.billingAddress.phone", "level": "order"},
"Billing Country": {"path": "order.billingAddress.countryCode", "level": "order"},
"Shipping Full Name": {"path": "order.shippingAddress.name", "level": "order"},
"Shipping First Name": {"path": "order.shippingAddress.firstName", "level": "order"},
"Shipping Last Name": {"path": "order.shippingAddress.lastName", "level": "order"},
"Shipping Street 1": {"path": "order.shippingAddress.address1", "level": "order"},
"Shipping Street 2": {"path": "order.shippingAddress.address2", "level": "order"},
"Shipping City": {"path": "order.shippingAddress.city", "level": "order"},
"Shipping State": {"path": "order.shippingAddress.provinceCode", "level": "order"},
"Shipping ZipCode": {"path": "order.shippingAddress.zip", "level": "order"},
"Shipping Email": {"path": "order.customer.email", "level": "order"},
"Shipping Phone": {"path": "order.shippingAddress.phone", "level": "order"},
"Shipping Country": {"path": "order.shippingAddress.countryCode", "level": "order"},
"Shipping Charge": {"path": "static", "level": "static", "value": ""},
"Sales Tax": {"path": "static", "level": "static", "value": ""},
"Shipping Carrier": {"path": "static", "level": "static", "value": "FedEx"},
"Shipping Method": {"path": "order.shippingLine.title", "level": "order"},
"Ship By Date": {"path": "static", "level": "static", "value": ""},
"Shipping Reference 1": {"path": "static", "level": "static", "value": ""},
"Shipping Reference 2": {"path": "static", "level": "static", "value": ""},
"3rd Party Shipping Account Number": {"path": "static", "level": "static", "value": "000000000"},
"3rd Party Shipping Name": {"path": "static", "level": "static", "value": ""},
"3rd Party Shipping Address": {"path": "static", "level": "static", "value": ""},
"3rd Party Shipping City": {"path": "static", "level": "static", "value": ""},
"3rd Party Shipping State": {"path": "static", "level": "static", "value": ""},
"3rd Party Shipping ZipCode": {"path": "static", "level": "static", "value": ""},
"3rd Party Shipping Country": {"path": "static", "level": "static", "value": ""},
"Lineitem sku": {"path": "sku", "level": "line_item"},
"Item Description": {"path": "productTitle", "level": "line_item"},
"Item Quantity": {"path": "totalQuantity", "level": "line_item"},
"Item Price": {"path": "static", "level": "static", "value": ""},
"Notes": {"path": "static", "level": "static", "value": ""}
},
"defaults": {
"Shipping Charge": "",
"Sales Tax": "",
"Shipping Carrier": "FedEx",
"Ship By Date": "",
"Shipping Reference 1": "",
"Shipping Reference 2": "",
"3rd Party Shipping Account Number": "000000000",
"3rd Party Shipping Name": "",
"3rd Party Shipping Address": "",
"3rd Party Shipping City": "",
"3rd Party Shipping State": "",
"3rd Party Shipping ZipCode": "",
"3rd Party Shipping Country": "",
"Item Price": "",
"Notes": ""
}
}
# Add similar entries for other vendors
]
# Loop through vendors
for vendor_details in vendors:
vendor_formatter = VendorFormatter(vendor_details["location_id"], vendor_details["output_filename"])
vendor_formatter.set_mapping(vendor_details["columns_order"], vendor_details["mapping"])
vendor_formatter.set_defaults(vendor_details.get("defaults",{}))
vendor_formatter.run_query_and_format()
# Email the generated file(s)
vendor_name = f"{vendor_details['vendor_name']}"
email_subject = f"Orders {month_number}-{day}"
email_recipient = vendor_details.get("email_addresses", [])
attachment_path = f"{path_prefix}{vendor_details['output_filename']}.xlsx"
cc_emails = ["cc@example.com", "cc2@example.com"]
email_body = "Place your email text here, using \n for line breaks"
email_signature_html = """
If you have an HTML/formatted/styled email signature, you can paste the code here.
"""
create_and_draft_email(email_recipient, email_subject, email_body, email_signature_html, attachment_path, cc_emails, vendor_name)
input("Finished processing orders, press Enter to exit...")