Skip to content

Commit 324cb2e

Browse files
authored
remove payment pending on draft order complete (#507)
1 parent 10d2f5a commit 324cb2e

File tree

3 files changed

+160
-49
lines changed

3 files changed

+160
-49
lines changed

docs/auto-recurring-draft-orders/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Tags: Draft Orders, Recurring
44

5-
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in a email receipt being sent to the customer on file for the original draft order.)
5+
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the draft order, which will mark it as paid unless the original draft order being duplicated has NET or EVENT payment terms.
66

77
* View in the task library: [tasks.mechanic.dev/auto-recurring-draft-orders](https://tasks.mechanic.dev/auto-recurring-draft-orders)
88
* Task JSON, for direct import: [task.json](../../tasks/auto-recurring-draft-orders.json)
@@ -12,11 +12,10 @@ This task searches for draft orders having the configured tag, and duplicates ea
1212

1313
```json
1414
{
15-
"draft_order_tag__required": "repeat-me",
16-
"cycle_start_date__required": "2024-01-01",
15+
"draft_order_tag__required": null,
16+
"cycle_start_date__date_required": null,
1717
"number_of_days_in_cycle__number_required": "7",
1818
"complete_the_order_after_creating__boolean": null,
19-
"complete_the_order_and_mark_as_paid_after_creating__boolean": null,
2019
"send_email_invoice_after_creating__boolean": false,
2120
"email_invoice_subject": null,
2221
"email_invoice_bcc__email_array": null,
@@ -31,7 +30,7 @@ This task searches for draft orders having the configured tag, and duplicates ea
3130
```liquid
3231
mechanic/user/trigger
3332
mechanic/scheduler/daily
34-
{% if options.send_email_invoice_after_creating__boolean or options.complete_the_order_after_creating__boolean or options.complete_the_order_and_mark_as_paid_after_creating__boolean %}
33+
{% if options.send_email_invoice_after_creating__boolean or options.complete_the_order_after_creating__boolean %}
3534
shopify/draft_orders/create
3635
{% endif %}
3736
```
@@ -40,11 +39,16 @@ mechanic/scheduler/daily
4039

4140
## Documentation
4241

43-
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in a email receipt being sent to the customer on file for the original draft order.)
42+
This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the draft order, which will mark it as paid unless the original draft order being duplicated has NET or EVENT payment terms.
4443

4544
Use the "Cycle start date" and "Number of days in cycle" options to control the frequency of the recurring invoices. The task may also be run manually, but the cycle will still be checked to make sure the current day is valid for the cycle.
4645

47-
**Note**: When duplicating invoices, this task will include most discount applications from the original draft order, including custom order and line item discounts, discount codes, and whether or not automatic discounts can be applied.
46+
**IMPORTANT**:
47+
- When duplicating invoices, this task will include most discount applications from the original draft order, including custom order and line item discounts, discount codes, and whether or not automatic discounts can be applied.
48+
- Auto-completed draft orders which do not have payment terms will result in the standard Shopify order confirmation email being sent to the customer on file for the original draft order.
49+
- EVENT payment terms include "Payment on receipt" and "Payment on fulfillment"
50+
- FIXED payment terms are not supported by this task
51+
4852

4953
## Installing this task
5054

docs/auto-recurring-draft-orders/script.liquid

Lines changed: 144 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
{% 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 %}
33
{% assign number_of_days_in_cycle = options.number_of_days_in_cycle__number_required %}
44
{% 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 %}
65
{% assign send_email_invoice_after_creating = options.send_email_invoice_after_creating__boolean %}
76
{% assign email_invoice_subject = options.email_invoice_subject %}
87
{% assign email_invoice_bcc = options.email_invoice_bcc__email_array %}
98
{% assign email_invoice_custom_message = options.email_invoice_custom_message__multiline %}
109

1110
{% if send_email_invoice_after_creating and complete_the_order_after_creating %}
1211
{% 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 %}
2712
{% endif %}
2813

2914
{% 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+
3019
{% assign run_qualifies = false %}
3120
{% assign cycle_start_date = cycle_start_date %}
3221
{% assign cycle_start_d = cycle_start_date | date: "%s" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}
@@ -42,6 +31,50 @@
4231
{% endif %}
4332

4433
{% 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+
4578
{% assign cursor = nil %}
4679

4780
{% for n in (0..10) %}
@@ -132,6 +165,16 @@
132165
}
133166
}
134167
note2
168+
paymentTerms {
169+
id
170+
paymentTermsName
171+
paymentTermsType
172+
paymentSchedules(first: 1) {
173+
nodes {
174+
id
175+
}
176+
}
177+
}
135178
shippingAddress {
136179
address1
137180
address2
@@ -206,6 +249,10 @@
206249
}
207250
]
208251
},
252+
"paymentTerms": {
253+
"paymentTermsName": "Net 30",
254+
"paymentTermsType": "NET"
255+
},
209256
"tags": {{ array | push: draft_order_tag, "preview-tag" | json }}
210257
}
211258
]
@@ -220,9 +267,54 @@
220267
{% for draft_order in result.data.draftOrders.nodes %}
221268
{% log
222269
message: "Draft order qualifies to be duplicated",
223-
original_draft_order_data: draft_order
270+
original_draft_order: draft_order
224271
%}
225272

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+
226318
{% action "shopify" %}
227319
mutation {
228320
draftOrderCreate(
@@ -320,7 +412,7 @@
320412
}
321413
{% endfor %}
322414
]
323-
{% if send_email_invoice_after_creating or autocomplete %}
415+
{% if send_email_invoice_after_creating or complete_the_order_after_creating %}
324416
metafields: [
325417
{
326418
namespace: "mechanic"
@@ -330,6 +422,9 @@
330422
}
331423
]
332424
{% endif %}
425+
{% if payment_terms_input != blank %}
426+
{{ payment_terms_input }}
427+
{% endif %}
333428
{% if draft_order.note2 != blank %}
334429
note: {{ draft_order.note2 | json }}
335430
{% endif %}
@@ -364,6 +459,7 @@
364459
) {
365460
draftOrder {
366461
id
462+
name
367463
}
368464
userErrors {
369465
field
@@ -383,6 +479,10 @@
383479
{% endif %}
384480

385481
{% 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+
386486
{% capture query %}
387487
query {
388488
draftOrder(
@@ -427,7 +527,11 @@
427527
}
428528
{% endaction %}
429529

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+
431535
{% capture query %}
432536
query {
433537
draftOrder(
@@ -445,24 +549,28 @@
445549

446550
{% assign result = query | shopify %}
447551

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
463567
}
464568
}
569+
userErrors {
570+
field
571+
message
572+
}
465573
}
466-
{% endaction %}
467-
{% endif %}
574+
}
575+
{% endaction %}
468576
{% endif %}

0 commit comments

Comments
 (0)