|
1 | 1 | {% assign draft_order_tag = options.draft_order_tag__required %} |
2 | | -{% assign cycle_start_date = options.cycle_start_date__required %} |
| 2 | +{% assign cycle_start_date = options.cycle_start_date__date_required %} |
3 | 3 | {% assign number_of_days_in_cycle = options.number_of_days_in_cycle__number_required %} |
4 | 4 | {% assign complete_the_order_after_creating = options.complete_the_order_after_creating__boolean %} |
5 | | -{% assign complete_the_order_and_mark_as_paid_after_creating = options.complete_the_order_and_mark_as_paid_after_creating__boolean %} |
6 | 5 | {% assign send_email_invoice_after_creating = options.send_email_invoice_after_creating__boolean %} |
7 | 6 | {% assign email_invoice_subject = options.email_invoice_subject %} |
8 | 7 | {% assign email_invoice_bcc = options.email_invoice_bcc__email_array %} |
9 | 8 | {% assign email_invoice_custom_message = options.email_invoice_custom_message__multiline %} |
10 | 9 |
|
11 | 10 | {% if send_email_invoice_after_creating and complete_the_order_after_creating %} |
12 | 11 | {% error "Choose either an email invoice or completing the order - not both. :)" %} |
13 | | -{% elsif send_email_invoice_after_creating and complete_the_order_and_mark_as_paid_after_creating %} |
14 | | - {% error "Choose either an email invoice or completing the order - not both. :)" %} |
15 | | -{% endif %} |
16 | | - |
17 | | -{% if complete_the_order_after_creating or complete_the_order_and_mark_as_paid_after_creating %} |
18 | | - {% assign autocomplete = true %} |
19 | | -{% endif %} |
20 | | - |
21 | | -{% assign cycle_start = cycle_start_date | date: "%s" %} |
22 | | - |
23 | | -{% if cycle_start == blank or cycle_start == cycle_start_date %} |
24 | | - {% error %} |
25 | | - {{ cycle_start_date | json | prepend: "Mechanic wasn't able to parse " | append: " - try another value." | json }} |
26 | | - {% enderror %} |
27 | 12 | {% endif %} |
28 | 13 |
|
29 | 14 | {% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %} |
| 15 | + {% comment %} |
| 16 | + -- see if today is valid run day based on the task cycle configuration |
| 17 | + {% endcomment %} |
| 18 | + |
30 | 19 | {% assign run_qualifies = false %} |
31 | 20 | {% assign cycle_start_date = cycle_start_date %} |
32 | 21 | {% assign cycle_start_d = cycle_start_date | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %} |
|
42 | 31 | {% endif %} |
43 | 32 |
|
44 | 33 | {% if run_qualifies %} |
| 34 | + {% comment %} |
| 35 | + -- get all payment terms templates available in shop, to support duplicating draft orders with payment terms |
| 36 | + {% endcomment %} |
| 37 | + |
| 38 | + {% capture query %} |
| 39 | + query { |
| 40 | + paymentTermsTemplates { |
| 41 | + id |
| 42 | + name |
| 43 | + description |
| 44 | + paymentTermsType |
| 45 | + dueInDays |
| 46 | + } |
| 47 | + } |
| 48 | + {% endcapture %} |
| 49 | + |
| 50 | + {% assign result = query | shopify %} |
| 51 | + |
| 52 | + {% if event.preview %} |
| 53 | + {% capture result_json %} |
| 54 | + { |
| 55 | + "data": { |
| 56 | + "paymentTermsTemplates": [ |
| 57 | + { |
| 58 | + "id": "gid://shopify/PaymentTermsTemplate/4", |
| 59 | + "name": "Net 30", |
| 60 | + "description": "Within 30 days", |
| 61 | + "paymentTermsType": "NET", |
| 62 | + "dueInDays": 30 |
| 63 | + } |
| 64 | + ] |
| 65 | + } |
| 66 | + } |
| 67 | + {% endcapture %} |
| 68 | + |
| 69 | + {% assign result = result_json | parse_json %} |
| 70 | + {% endif %} |
| 71 | + |
| 72 | + {% assign payment_terms_templates = result.data.paymentTermsTemplates %} |
| 73 | + |
| 74 | + {% comment %} |
| 75 | + -- get all draft orders with the configured tag |
| 76 | + {% endcomment %} |
| 77 | + |
45 | 78 | {% assign cursor = nil %} |
46 | 79 |
|
47 | 80 | {% for n in (0..10) %} |
|
132 | 165 | } |
133 | 166 | } |
134 | 167 | note2 |
| 168 | + paymentTerms { |
| 169 | + id |
| 170 | + paymentTermsName |
| 171 | + paymentTermsType |
| 172 | + paymentSchedules(first: 1) { |
| 173 | + nodes { |
| 174 | + id |
| 175 | + } |
| 176 | + } |
| 177 | + } |
135 | 178 | shippingAddress { |
136 | 179 | address1 |
137 | 180 | address2 |
|
206 | 249 | } |
207 | 250 | ] |
208 | 251 | }, |
| 252 | + "paymentTerms": { |
| 253 | + "paymentTermsName": "Net 30", |
| 254 | + "paymentTermsType": "NET" |
| 255 | + }, |
209 | 256 | "tags": {{ array | push: draft_order_tag, "preview-tag" | json }} |
210 | 257 | } |
211 | 258 | ] |
|
220 | 267 | {% for draft_order in result.data.draftOrders.nodes %} |
221 | 268 | {% log |
222 | 269 | message: "Draft order qualifies to be duplicated", |
223 | | - original_draft_order_data: draft_order |
| 270 | + original_draft_order: draft_order |
224 | 271 | %} |
225 | 272 |
|
| 273 | + {% comment %} |
| 274 | + -- marking draft orders as pending upon completion has been deprecated; added support for NET and EVENT payment terms |
| 275 | + {% endcomment %} |
| 276 | + |
| 277 | + {% assign payment_terms_input = nil %} |
| 278 | + |
| 279 | + {% if draft_order.paymentTerms != blank %} |
| 280 | + {% assign payment_terms_template_id |
| 281 | + = payment_terms_templates |
| 282 | + | where: "name", draft_order.paymentTerms.paymentTermsName |
| 283 | + | map: "id" |
| 284 | + | first |
| 285 | + %} |
| 286 | + |
| 287 | + {% if draft_order.paymentTerms.paymentTermsType == "FIXED" %} |
| 288 | + {% log |
| 289 | + message: "FIXED payment terms are not supported by this task because arbitrary due dates cannot be calculated. The new draft order will not be created with any payment terms.", |
| 290 | + original_draft_order_name: draft_order.name |
| 291 | + %} |
| 292 | + |
| 293 | + {% elsif payment_terms_template_id == blank %} |
| 294 | + {% log |
| 295 | + message: "Payment terms on original draft order could not be matched to available payment terms in shop.", |
| 296 | + payment_terms_templates: payment_terms_templates, |
| 297 | + original_draft_order: draft_order |
| 298 | + %} |
| 299 | + |
| 300 | + {% else %} |
| 301 | + {% capture payment_terms_input %} |
| 302 | + paymentTerms: { |
| 303 | + paymentTermsTemplateId: {{ payment_terms_template_id | json }} |
| 304 | + {% if draft_order.paymentTerms.paymentTermsType == "NET" %} |
| 305 | + paymentSchedules: { |
| 306 | + issuedAt: {{ "now" | date: "%Y-%m-%dT%H:%M:%SZ", tz: "UTC" | json }} |
| 307 | + } |
| 308 | + {% endif %} |
| 309 | + } |
| 310 | + {% endcapture %} |
| 311 | + {% endif %} |
| 312 | + {% endif %} |
| 313 | + |
| 314 | + {% comment %} |
| 315 | + -- create new recurring draft order with as much of the original draft order parameters as possible |
| 316 | + {% endcomment %} |
| 317 | + |
226 | 318 | {% action "shopify" %} |
227 | 319 | mutation { |
228 | 320 | draftOrderCreate( |
|
320 | 412 | } |
321 | 413 | {% endfor %} |
322 | 414 | ] |
323 | | - {% if send_email_invoice_after_creating or autocomplete %} |
| 415 | + {% if send_email_invoice_after_creating or complete_the_order_after_creating %} |
324 | 416 | metafields: [ |
325 | 417 | { |
326 | 418 | namespace: "mechanic" |
|
330 | 422 | } |
331 | 423 | ] |
332 | 424 | {% endif %} |
| 425 | + {% if payment_terms_input != blank %} |
| 426 | + {{ payment_terms_input }} |
| 427 | + {% endif %} |
333 | 428 | {% if draft_order.note2 != blank %} |
334 | 429 | note: {{ draft_order.note2 | json }} |
335 | 430 | {% endif %} |
|
364 | 459 | ) { |
365 | 460 | draftOrder { |
366 | 461 | id |
| 462 | + name |
367 | 463 | } |
368 | 464 | userErrors { |
369 | 465 | field |
|
383 | 479 | {% endif %} |
384 | 480 |
|
385 | 481 | {% elsif event.topic == "shopify/draft_orders/create" and send_email_invoice_after_creating %} |
| 482 | + {% comment %} |
| 483 | + -- check to see if this newly created draft order has the Mechanic autoinvoice metafield |
| 484 | + {% endcomment %} |
| 485 | + |
386 | 486 | {% capture query %} |
387 | 487 | query { |
388 | 488 | draftOrder( |
|
427 | 527 | } |
428 | 528 | {% endaction %} |
429 | 529 |
|
430 | | -{% elsif event.topic == "shopify/draft_orders/create" and autocomplete %} |
| 530 | +{% elsif event.topic == "shopify/draft_orders/create" and complete_the_order_after_creating %} |
| 531 | + {% comment %} |
| 532 | + -- check to see if this newly created draft order has the Mechanic autocomplete metafield |
| 533 | + {% endcomment %} |
| 534 | + |
431 | 535 | {% capture query %} |
432 | 536 | query { |
433 | 537 | draftOrder( |
|
445 | 549 |
|
446 | 550 | {% assign result = query | shopify %} |
447 | 551 |
|
448 | | - {% if event.preview or result.data.draftOrder.metafield %} |
449 | | - {% action "shopify" %} |
450 | | - mutation { |
451 | | - draftOrderComplete( |
452 | | - id: {{ draft_order.admin_graphql_api_id | json }} |
453 | | - paymentPending: {% if complete_the_order_and_mark_as_paid_after_creating %}false{% else %}true{% endif %} |
454 | | - ) { |
455 | | - draftOrder { |
456 | | - order { |
457 | | - id |
458 | | - } |
459 | | - } |
460 | | - userErrors { |
461 | | - field |
462 | | - message |
| 552 | + {% if result.data.draftOrder.metafield == blank %} |
| 553 | + {% unless event.preview %} |
| 554 | + {% log "Draft order does not contain the Mechanic autocomplete metafield." %} |
| 555 | + {% break %} |
| 556 | + {% endunless %} |
| 557 | + {% endif %} |
| 558 | + |
| 559 | + {% action "shopify" %} |
| 560 | + mutation { |
| 561 | + draftOrderComplete( |
| 562 | + id: {{ draft_order.admin_graphql_api_id | json }} |
| 563 | + ) { |
| 564 | + draftOrder { |
| 565 | + order { |
| 566 | + id |
463 | 567 | } |
464 | 568 | } |
| 569 | + userErrors { |
| 570 | + field |
| 571 | + message |
| 572 | + } |
465 | 573 | } |
466 | | - {% endaction %} |
467 | | - {% endif %} |
| 574 | + } |
| 575 | + {% endaction %} |
468 | 576 | {% endif %} |
0 commit comments