forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
455 lines (437 loc) · 16.3 KB
/
script.liquid
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
{% assign draft_order_tag = options.draft_order_tag__required %}
{% assign cycle_start_date = options.cycle_start_date__required %}
{% assign number_of_days_in_cycle = options.number_of_days_in_cycle__number_required %}
{% assign complete_the_order_after_creating = options.complete_the_order_after_creating__boolean %}
{% assign complete_the_order_and_mark_as_paid_after_creating = options.complete_the_order_and_mark_as_paid_after_creating__boolean %}
{% assign send_email_invoice_after_creating = options.send_email_invoice_after_creating__boolean %}
{% assign email_invoice_subject = options.email_invoice_subject %}
{% assign email_invoice_bcc = options.email_invoice_bcc__email_array %}
{% assign email_invoice_custom_message = options.email_invoice_custom_message__multiline %}
{% if send_email_invoice_after_creating and complete_the_order_after_creating %}
{% error "Choose either an email invoice or completing the order - not both. :)" %}
{% elsif send_email_invoice_after_creating and complete_the_order_and_mark_as_paid_after_creating %}
{% error "Choose either an email invoice or completing the order - not both. :)" %}
{% endif %}
{% if complete_the_order_after_creating or complete_the_order_and_mark_as_paid_after_creating %}
{% assign autocomplete = true %}
{% endif %}
{% assign cycle_start = cycle_start_date | date: "%s" %}
{% if cycle_start == blank or cycle_start == cycle_start_date %}
{% error %}
{{ cycle_start_date | json | prepend: "Mechanic wasn't able to parse " | append: " - try another value." | json }}
{% enderror %}
{% endif %}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% assign run_qualifies = false %}
{% if event.preview %}
{% action "shopify" %}
mutation {
draftOrderCreate(
input: {
billingAddress: {
address1: "123 State St"
city: "Everyville"
company: "Widgets Inc"
countryCode: EE
firstName: "Victor"
lastName: "Juarez"
provinceCode: "PK"
zip: "54321"
}
purchasingEntity: {
customerId: "gid://shopify/Customer/1234567890"
}
email: "[email protected]"
lineItems: [
{
weight: {
unit: GRAMS
value: 5
}
originalUnitPriceWithCurrency: {
amount: "5.0"
currencyCode: USD
}
quantity: 2
requiresShipping: false
sku: "123ABC"
taxable: true
title: "Widget, Our Best"
}
]
tags: []
taxExempt: false
useCustomerDefaultAddress: false
}
) {
draftOrder {
id
}
userErrors {
field
message
}
}
}
{% endaction %}
{% else %}
{% assign cycle_start_date = cycle_start_date %}
{% assign cycle_start_d = cycle_start_date | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}
{% assign now_d = "now" | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}
{% assign day_gap = now_d | minus: cycle_start_d | modulo: number_of_days_in_cycle %}
{% if day_gap == 0 %}
{% assign run_qualifies = true %}
{% else %}
{% log %}
{{ number_of_days_in_cycle | minus: day_gap | prepend: "Waiting another " | append: " day(s) for cycle to complete" | json }}
{% endlog %}
{% endif %}
{% endif %}
{% if run_qualifies %}
{% for n in (0..10) %}
{% capture query %}
query {
draftOrders(
first: 100
after: {{ cursor | json }}
query: {{ "tag:" | append: draft_order_tag | json }}
sortKey: NUMBER
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
appliedDiscount {
amountSet {
shopMoney {
amount
currencyCode
}
}
description
title
value
valueType
}
billingAddress {
address1
address2
city
company
countryCodeV2
firstName
lastName
phone
provinceCode
zip
}
customer {
id
}
customAttributes {
key
value
}
email
lineItems(first: 150) {
nodes {
appliedDiscount {
amountSet {
shopMoney {
amount
currencyCode
}
}
description
title
value
valueType
}
customAttributes {
key
value
}
originalUnitPriceWithCurrency {
amount
currencyCode
}
quantity
sku
taxable
title
variant {
id
}
weight {
unit
value
}
}
}
note2
shippingAddress {
address1
address2
city
company
countryCodeV2
firstName
lastName
phone
provinceCode
zip
}
shippingLine {
title
shippingRateHandle
originalPriceSet {
shopMoney {
amount
currencyCode
}
}
}
tags
taxExempt
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% for draft_order in result.data.draftOrders.nodes %}
{% action "shopify" %}
mutation {
draftOrderCreate(
input: {
{% if draft_order.appliedDiscount %}
appliedDiscount: {
amountWithCurrency: {
amount: {{ draft_order.appliedDiscount.amountSet.shopMoney.amount | json }}
currencyCode: {{ draft_order.appliedDiscount.amountSet.shopMoney.currencyCode }}
}
description: {{ draft_order.appliedDiscount.description | json }}
title: {{ draft_order.appliedDiscount.title | json }}
value: {{ draft_order.appliedDiscount.value }}
valueType: {{ draft_order.appliedDiscount.valueType }}
}
{% endif %}
{% if draft_order.billingAddress %}
billingAddress: {
address1: {{ draft_order.billingAddress.address1 | json }}
address2: {{ draft_order.billingAddress.address2 | json }}
city: {{ draft_order.billingAddress.city | json }}
company: {{ draft_order.billingAddress.company | json }}
countryCode: {{ draft_order.billingAddress.countryCodeV2 }}
firstName: {{ draft_order.billingAddress.firstName | json }}
lastName: {{ draft_order.billingAddress.lastName | json }}
phone: {{ draft_order.billingAddress.phone | json }}
provinceCode: {{ draft_order.billingAddress.provinceCode | json }}
zip: {{ draft_order.billingAddress.zip | json }}
}
{% endif %}
{% if draft_order.customer %}
purchasingEntity: {
customerId: {{ draft_order.customer.id | json }}
}
{% endif %}
{% if draft_order.customAttributes != empty %}
customAttributes: [
{% for custom_attribute in draft_order.customAttributes %}
{
key: {{ custom_attribute.key | json }}
value: {{ custom_attribute.value | json }}
}
{% endfor %}
]
{% endif %}
{% if draft_order.email %}
email: {{ draft_order.email | json }}
{% endif %}
lineItems: [
{% for line_item in draft_order.lineItems.nodes %}
{
{% if line_item.appliedDiscount %}
appliedDiscount: {
amountWithCurrency: {
amount: {{ line_item.appliedDiscount.amountSet.shopMoney.amount | json }}
currencyCode: {{ line_item.appliedDiscount.amountSet.shopMoney.currencyCode }}
}
description: {{ line_item.appliedDiscount.description | json }}
title: {{ line_item.appliedDiscount.title | json }}
value: {{ line_item.appliedDiscount.value | json }}
valueType: {{ line_item.appliedDiscount.valueType }}
}
{% endif %}
{% if line_item.customAttributes != empty %}
customAttributes: [
{% for custom_attribute in line_item.customAttributes %}
{
key: {{ custom_attribute.key | json }}
value: {{ custom_attribute.value | json }}
}
{% endfor %}
]
{% endif %}
{% if line_item.originalUnitPriceWithCurrency.amount %}
originalUnitPriceWithCurrency: {
amount: {{ line_item.originalUnitPriceWithCurrency.amount | json }}
currencyCode: {{ line_item.originalUnitPriceWithCurrency.currencyCode }}
}
{% endif %}
quantity: {{ line_item.quantity | json }}
{% if line_item.requiresShipping != nil %}
requiresShipping: {{ line_item.requiresShipping | json }}
{% endif %}
{% if line_item.sku %}
sku: {{ line_item.sku | json }}
{% endif %}
taxable: {{ line_item.taxable | json }}
title: {{ line_item.title | json }}
{% if line_item.variant %}
variantId: {{ line_item.variant.id | json }}
{% endif %}
{% if line_item.weight and line_item.weight.value != 0 %}
weight: {
value: {{ line_item.weight.value | json }}
unit: {{ line_item.weight.unit }}
}
{% endif %}
}
{% endfor %}
]
{% if send_email_invoice_after_creating or autocomplete %}
metafields: [
{
namespace: "mechanic"
key: {% if send_email_invoice_after_creating %}"autoinvoice"{% else %}"autocomplete"{% endif %}
value: "true"
type: "boolean"
}
]
{% endif %}
{% if draft_order.note2 != blank %}
note: {{ draft_order.note2 | json }}
{% endif %}
{% if draft_order.shippingAddress %}
shippingAddress: {
address1: {{ draft_order.shippingAddress.address1 | json }}
address2: {{ draft_order.shippingAddress.address2 | json }}
city: {{ draft_order.shippingAddress.city | json }}
company: {{ draft_order.shippingAddress.company | json }}
countryCode: {{ draft_order.shippingAddress.countryCodeV2 }}
firstName: {{ draft_order.shippingAddress.firstName | json }}
lastName: {{ draft_order.shippingAddress.lastName | json }}
phone: {{ draft_order.shippingAddress.phone | json }}
provinceCode: {{ draft_order.shippingAddress.provinceCode | json }}
zip: {{ draft_order.shippingAddress.zip | json }}
}
{% endif %}
{% if draft_order.shippingLine %}
shippingLine: {
priceWithCurrency: {
amount: {{ draft_order.shippingLine.originalPriceSet.shopMoney.amount | json }}
currencyCode: {{ draft_order.shippingLine.originalPriceSet.shopMoney.currencyCode }}
}
shippingRateHandle: {{ draft_order.shippingLine.shippingRateHandle | json }}
title: {{ draft_order.shippingLine.title | json }}
}
{% endif %}
tags: {{ draft_order.tags | remove_tag: draft_order_tag | json }}
taxExempt: {{ draft_order.taxExempt | json }}
useCustomerDefaultAddress: false
}
) {
draftOrder {
id
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% if result.data.draftOrders.pageInfo.hasNextPage %}
{% assign cursor = result.data.draftOrders.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% elsif event.topic == "shopify/draft_orders/create" and send_email_invoice_after_creating %}
{% capture query %}
query {
draftOrder(
id: {{ draft_order.admin_graphql_api_id | json }}
) {
metafield(
namespace: "mechanic"
key: "autoinvoice"
) {
value
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview or result.data.draftOrder.metafield %}
{% action "shopify" %}
mutation {
draftOrderInvoiceSend(
id: {{ draft_order.admin_graphql_api_id | json }}
email: {
{% if email_invoice_subject != blank %}
subject: {{ email_invoice_subject | json }}
{% endif %}
bcc: {{ email_invoice_bcc | json }}
customMessage: {{ email_invoice_custom_message | json }}
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% elsif event.topic == "shopify/draft_orders/create" and autocomplete %}
{% capture query %}
query {
draftOrder(
id: {{ draft_order.admin_graphql_api_id | json }}
) {
metafield(
namespace: "mechanic"
key: "autocomplete"
) {
value
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview or result.data.draftOrder.metafield %}
{% action "shopify" %}
mutation {
draftOrderComplete(
id: {{ draft_order.admin_graphql_api_id | json }}
paymentPending: {% if complete_the_order_and_mark_as_paid_after_creating %}false{% else %}true{% endif %}
) {
draftOrder {
order {
id
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}