-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_backup.py
191 lines (170 loc) · 8.37 KB
/
order_backup.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
import requests
from datetime import datetime, timedelta
import json
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
# Airtable API details
base_key = 'appd8lN3nYsIT139L'
order_table_name = 'tbleRUUaL4t4TfPEf'
order_view_name = 'viwSGwefEGPr9fohB'
line_items_table_name = 'tbleRUUaL4t4TfPEf'
token = "patxr7JPJypDemRWQ.00484d9b9fc6dbb0fc4b3f8b0ab838a146a314df809048191d5cff9d6d64da1e"
order_api_url = f'https://api.airtable.com/v0/{base_key}/{order_table_name}'
line_items_api_url = f'https://api.airtable.com/v0/{base_key}/{line_items_table_name}'
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
def get_orders_after_date(date_str):
def fetch_orders_after_date(date_str):
#date_str = "2024-07-01 13:50"
filter_formula = f'AND(IS_AFTER({{Created Time}}, "{date_str}"), {{Fulfillment Location (from Order Line Items)}}="MAZUMA")'
params = {
'filterByFormula': filter_formula,
'view': order_view_name,
'maxRecords': 500,
'sort[0][field]': 'Shopify Order Date',
'sort[0][direction]': 'desc'
}
try:
response = requests.get(order_api_url, headers=headers, params=params, verify=False)
if response.status_code == 200:
print(response.json)
return response.json()
else:
logging.error(f'Failed to fetch orders, Error: {response.text}')
return None
except requests.exceptions.RequestException as e:
logging.error(f'Requests exception: {e}')
return None
def fetch_order_line_items_concurrently(line_item_ids):
with ThreadPoolExecutor(max_workers=10) as executor:
future_to_item = {executor.submit(fetch_single_line_item, line_item_id): line_item_id for line_item_id in line_item_ids}
line_items = []
for future in as_completed(future_to_item):
line_item = future.result()
if line_item:
line_items.append(line_item)
return line_items
def fetch_single_line_item(line_item_id):
try:
response = requests.get(f'{line_items_api_url}/{line_item_id}', headers=headers, verify=False)
if response.status_code == 200:
return response.json().get('fields')
else:
logging.error(f'Failed to fetch line item {line_item_id}, Error: {response.text}')
return None
except requests.exceptions.RequestException as e:
logging.error(f'Requests exception: {e}')
return None
def map_payment_status(status):
status_mapping = {
'paid': 'PAID',
'unpaid': 'UNPAID',
'voided': 'CANCELLED'
}
return status_mapping.get(status.lower(), 'PAID')
def transform_to_linnworks_format(orders):
linnworks_orders = []
print("transform_to_linnworks_format")
print(orders)
for order in orders['records']:
line_item_ids = order['fields'].get('Order Line Items', [])
order_line_items = fetch_order_line_items_concurrently(line_item_ids) if line_item_ids else []
print("orders")
# Prepare order items
order_items = []
for item in order_line_items:
mazuma_sku = item.get("Mazuma Variant SKU", "")
print(mazuma_sku)
if not mazuma_sku:
continue # Ignore items without Mazuma Variant SKU
order_items.append({
"TaxCostInclusive": True,
"UseChannelTax": False,
"IsService": False,
"OrderLineNumber": str(item.get("Line Item ID", "")),
"SKU": mazuma_sku,
"PricePerUnit": item.get("Variant Price", 0),
"Qty": item.get("Quantity", 0),
"TaxRate": item.get("VAT", 0),
"LinePercentDiscount": 0.0,
"ItemTitle": item.get("Variant Full Description", ""),
"Options": []
})
if not order_items:
continue # Ignore orders without valid line items
# Convert date to desired format
received_date = order['fields'].get('Shopify Order Date', '')
paid_on = order['fields'].get('Shopify Order Date', '')
if received_date:
received_date = datetime.strptime(received_date, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y-%m-%d %H:%M:%SZ")
if paid_on:
paid_on = datetime.strptime(paid_on, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y-%m-%d %H:%M:%SZ")
# Prepare the order in Linnworks format
linnworks_order = {
"BillingAddress": {
"FullName": f"Mr {order['fields'].get('Customer First Name', '')} {order['fields'].get('Customer Last Name', '')}",
"Company": "",
"Address1": order['fields'].get('Shipping Address Line 1', ''),
"Address2": "",
"Address3": "",
"Town": order['fields'].get('Shipping Town or City', ''),
"Region": order['fields'].get('Shipping County or Province', ''),
"PostCode": order['fields'].get('Postcode or Zip', ''),
"Country": order['fields'].get('Shipping Country', ''),
"CountryCode": "GB",
"PhoneNumber": order['fields'].get('Recipient Phone Number', ''),
"EmailAddress": order['fields'].get('Customer Email Address', '')
},
"DeliveryAddress": {
"FullName": f"Mr {order['fields'].get('Recipient First Name', '')} {order['fields'].get('Recipient Last Name', '')}",
"Company": "",
"Address1": order['fields'].get('Shipping Address Line 1', ''),
"Address2": "",
"Address3": "",
"Town": order['fields'].get('Shipping Town or City', ''),
"Region": order['fields'].get('Shipping County or Province', ''),
"PostCode": order['fields'].get('Postcode or Zip', ''),
"Country": order['fields'].get('Shipping Country', ''),
"CountryCode": "GB",
"PhoneNumber": order['fields'].get('Recipient Phone Number', ''),
"EmailAddress": order['fields'].get('Customer Email Address', '')
},
"OrderItems": order_items,
"ExtendedProperties": [
{
"Name": "Fulfillment Location",
"Value": "Mazuma"
}
],
"Notes": [],
"Site": "",
"MatchPostalServiceTag": order['fields'].get('Selected Shipping Option', ''),
"MatchPaymentMethodTag": order['fields'].get('Payment Gateway', ''),
"PaymentStatus": map_payment_status(order['fields'].get('Financial Status Latest', 'Paid')),
"ChannelBuyerName": f"{order['fields'].get('Customer First Name', '')} {order['fields'].get('Customer Last Name', '')}",
"ReferenceNumber": order['fields'].get('Order Number', ''),
"ExternalReference": str(order['fields'].get('Order ID', '')),
"SecondaryReferenceNumber": None,
"Currency": "GBP",
"ReceivedDate": received_date,
"DispatchBy": (datetime.now() + timedelta(days=10)).strftime("%Y-%m-%d %H:%M:%SZ"), # Example dispatch by date
"PaidOn": paid_on,
"PostalServiceCost": order['fields'].get('Total Shipping Price', 0),
"PostalServiceTaxRate": 20.0,
"UseChannelTax": False
}
linnworks_orders.append(linnworks_order)
return {
"Error": None,
"HasMorePages": False,
"Orders": linnworks_orders
}
orders = fetch_orders_after_date(date_str)
if orders:
linnworks_data = transform_to_linnworks_format(orders)
return linnworks_data
else:
return {"Error": "No orders fetched or an error occurred."}
#print(get_orders_after_date("2024-07-07 13:50"))