Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new task: send tax receipts for donations #288

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Send follow-up emails after sending a draft order invoice](./send-follow-up-emails-after-sending-a-draft-order-invoice)
* [Send new customer signups to IFTTT](./send-new-customer-signups-to-ifttt)
* [Send recurring reminders about unpaid orders](./unpaid-order-reminders)
* [Send tax receipts for donations](./send-tax-receipts-for-donations)
* [Send your customers reorder reminders](./send-your-customers-reorder-reminders)
* [Set a default tracking number for new fulfillments](./set-a-default-tracking-number-for-new-fulfillments)
* [Set product or variant metafields values in bulk](./set-product-or-variant-metafields-in-bulk)
Expand Down Expand Up @@ -1220,6 +1221,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Send email when an order comes in](./send-email-when-an-order-comes-in)
* [Send email when an order is tagged](./send-email-when-an-order-is-tagged)
* [Send recurring reminders about unpaid orders](./unpaid-order-reminders)
* [Send tax receipts for donations](./send-tax-receipts-for-donations)
* [Sync an inverse order tag](./sync-an-inverse-order-tag)
* [Sync order timeline comments to the customer note](./sync-order-timeline-comments-to-the-customer-note)
* [Tag online orders by ?ls= Locksmith secret link used](./tag-orders-by-locksmith-secret-link)
Expand Down Expand Up @@ -1250,6 +1252,7 @@ This directory is built automatically. Each task's documentation is generated fr
* [Report Toaster: Deliver report PDF via email or Slack](./report-toaster-deliver-report-pdf-via-email-or-slack)
* [Send a PDF invoice when an order is created](./send-a-pdf-invoice-when-an-order-is-created)
* [Send a PDF when an order is sent to Mechanic](./send-a-pdf-when-an-order-is-sent-to-mechanic)
* [Send tax receipts for donations](./send-tax-receipts-for-donations)

### Payment

Expand Down Expand Up @@ -1391,6 +1394,10 @@ This directory is built automatically. Each task's documentation is generated fr
* [Publish back-in-stock products](./publish-back-in-stock-products)
* [Scheduled theme publishing](./scheduled-theme-publishing)

### Receipts

* [Send tax receipts for donations](./send-tax-receipts-for-donations)

### Recurring

* [Auto-recurring draft orders](./auto-recurring-draft-orders)
Expand Down Expand Up @@ -1643,6 +1650,7 @@ This directory is built automatically. Each task's documentation is generated fr

### Tax

* [Send tax receipts for donations](./send-tax-receipts-for-donations)
* [Temporarily enable tax-exempt status when a customer is tagged](./temporarily-enable-tax-exempt-status-when-a-customer-is-tagged)

### Template
Expand Down
60 changes: 60 additions & 0 deletions docs/send-tax-receipts-for-donations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Send tax receipts for donations

Tags: Orders, PDF, Receipts, Tax

Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, and it will sum the donation amount from all line items that have a configured product donation tag.

* View in the task library: [tasks.mechanic.dev/send-tax-receipts-for-donations](https://tasks.mechanic.dev/send-tax-receipts-for-donations)
* Task JSON, for direct import: [task.json](../../tasks/send-tax-receipts-for-donations.json)
* Preview task code: [script.liquid](./script.liquid)

## Default options

```json
{
"tax_id__required": null,
"identify_donation_products_with_any_of_these_tags__array_required": [
"donation"
],
"send_cc_to_shop_email__boolean": false,
"email_subject__required": "Receipt {{ order.name }} for donation to {{ shop.name }}",
"email_body__multiline_required": "Thank you for your recent donation!\n\nAttached you will find your official donation receipt.",
"donation_receipt_filename__required": "{{ shop.name | handleize }}-donation-receipt-{{ order.name }}.pdf",
"donation_reciept_html_template__code_multiline_required": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\">\n <style>\n .container {\n width: 960px;\n margin: 0 auto;\n font-size: 16px;\n }\n .header {\n display: flex;\n flex-direction: row;\n border-top: 1px solid #000; \n border-bottom: 1px dashed #000; \n font-size: 18px;\n }\n .main {\n margin-top: 10px;\n display: flex;\n flex-direction: row;\n font-size: 16px;\n }\n .left {\n width: 50%;\n }\n .right {\n width: 50%;\n }\n </style>\n</head>\n<body>\n <div class=\"container\">\n <div class=\"header\">\n <p class=\"left\">\n {{ shop.name }}<br/>\n {{ shop.address1 }}<br/>{% if shop.address2 != blank -%}{{ shop.address2 }}<br/>{%- endif %}\n {{ shop.city }}, {{ shop.province }} {{ shop.zip }}<br/>\n {{ order.billing_address.country }}\n </p>\n <p class=\"right\">\n OFFICIAL DONATION RECEIPT<br/>\n TAX ID #[TAX_ID]<br/>\n </p>\n </div>\n <div class=\"main\">\n <p class=\"left\">\n <strong>Donor:</strong><br/>\n {{ order.billing_address.first_name }} {{ billing_address.last_name }}<br/>\n {{ order.billing_address.address1 }}<br/>{% if order.billing_address.address2 != blank -%}{{ order.billing_address.address2 }}<br/>{%- endif %}\n {{ order.billing_address.city }}, {{ order.billing_address.province }} {{ order.billing_address.zip }}<br/>\n {{ order.billing_address.country }}\n </p>\n <p class=\"right\">\n <strong>Donation Details:</strong><br/>\n Order Number: {{ order.name }}<br/>\n Donation Received: {{ order.created_at | date: \"%F\" }}<br/>\n Amount: [DONATION_AMOUNT]<br/>\n Date Issued: {{ \"now\" | date: \"%F\" }}<br/>\n Place Issued: {{ shop.province }}, {{ shop.country_name }}<br/>\n <br/>\n Donations are tax deductible to the extent permitted by law.<br/>\n <br/>\n Thank you\n </p>\n </div>\n </div>\n</body>\n</html>"
}
```

[Learn about task options in Mechanic](https://learn.mechanic.dev/core/tasks/options)

## Subscriptions

```liquid
shopify/orders/paid
mechanic/user/order
```

[Learn about event subscriptions in Mechanic](https://learn.mechanic.dev/core/tasks/subscriptions)

## Documentation

Use this task to email donation receipts to donors as PDF attachments, with an option to cc the shop email. It runs on new, paid orders, and it will sum the donation amount from all line items that have a configured product donation tag.

This task can also receive orders sent directly via [admin action links](https://learn.mechanic.dev/core/shopify/admin-action-links#link-usage). When doing so, the task will **not** check that the order is paid to allow for flexibility when sending donation receipts manually.

**Notes:**

- The task comes with a sample donation tag, email subject, email body, donation receipt filename, and donation receipt HTML template. The HTML template is used by the PDF file generator, so care must be taken to provide valid HTML if this field is customized.
- The *[TAX_ID]* and *[DONATION_AMOUNT]* variables need to be present in the HTML template in order for those values to be included in the PDF donation receipt.
- Orders with no donation products will be ignored.

## Installing this task

Find this task [in the library at tasks.mechanic.dev](https://tasks.mechanic.dev/send-tax-receipts-for-donations), and use the "Try this task" button. Or, import [this task's JSON export](../../tasks/send-tax-receipts-for-donations.json) – see [Importing and exporting tasks](https://learn.mechanic.dev/core/tasks/import-and-export) to learn how imports work.

## Contributions

Found a bug? Got an improvement to add? Start here: [../../CONTRIBUTING.md](../../CONTRIBUTING.md).

## Task requests

Submit your [task requests](https://mechanic.canny.io/task-requests) for consideration by the Mechanic community, and they may be chosen for development and inclusion in the [task library](https://tasks.mechanic.dev/)!
128 changes: 128 additions & 0 deletions docs/send-tax-receipts-for-donations/script.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{% assign tax_id = options.tax_id__required %}
{% assign donation_product_tags = options.identify_donation_products_with_any_of_these_tags__array_required %}
{% assign send_cc_to_shop_email = options.send_cc_to_shop_email__boolean %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__multiline_required %}
{% assign donation_receipt_filename = options.donation_receipt_filename__required %}
{% assign donation_reciept_html_template = options.donation_reciept_html_template__code_multiline_required %}

{% if event.topic == "shopify/orders/paid" or event.topic == "mechanic/user/order" %}
{% comment %}
-- NOTE: if this order was sent directly via an admin link, then the "paid" financial status will not be checked to allow for flexibility on manual sends
{% endcomment %}

{% if order.email == blank %}
{% log "This order does not have an email associated with it." %}
{% break %}
{% endif %}

{% comment %}
-- get the order data to see if there are any donation products on this order, by product tag
{% endcomment %}

{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
presentmentCurrencyCode
lineItems(first: 100) {
nodes {
product {
tags
}
originalTotalSet {
presentmentMoney {
amount
}
}
}
}
}
}
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"presentmentCurrencyCode": "USD",
"lineItems": {
"nodes": [
{
"product": {
"tags": {{ donation_product_tags.first | json }}
},
"originalTotalSet": {
"presentmentMoney": {
"amount": "100.0"
}
}
}
]
}
}
}
}
{% endcapture %}

{% assign result = result_json | parse_json %}
{% endif %}

{% comment %}
-- for any donation products, add the non-discounted total for that line item to the runnning donation amount
{% endcomment %}

{% assign donation_amount = 0 %}

{% for line_item in result.data.order.lineItems.nodes %}
{% for donation_product_tag in donation_product_tags %}
{% if line_item.product.tags contains donation_product_tag %}
{% assign donation_amount = donation_amount | plus: line_item.originalTotalSet.presentmentMoney.amount %}
{% break %}
{% endif %}
{% endfor %}
{% endfor %}

{% unless donation_amount > 0 %}
{% log "This order has no donation products on it; skipping" %}
{% break %}
{% endunless %}

{% assign donation_with_currency = donation_amount | currency: result.data.order.presentmentCurrencyCode %}

{% comment %}
-- replace variables in the HTML template for the configured tax ID and the calculated donation amount
{% endcomment %}

{% assign donation_html
= donation_reciept_html_template
| replace: "[TAX_ID]", tax_id
| replace: "[DONATION_AMOUNT]", donation_with_currency
%}

{% comment %}
-- generate tax receipt as a PDF and attach to email to donor; optionally cc the shop email address
{% endcomment %}

{% action "email" %}
{
"to": {{ order.email | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | newline_to_br | json }},
{% if send_cc_to_shop_email %}"cc": {{ shop.customer_email | json }},{% endif %}
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }},
"attachments": {
{{ donation_receipt_filename | remove: "#" | json }}: {
"pdf": {
"html": {{ donation_html | json }},
"__force_pdfcrowd": true
}
}
}
}
{% endaction %}
{% endif %}
1 change: 1 addition & 0 deletions lib/task_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"Price",
"Products",
"Publish",
"Receipts",
"Recurring",
"Redirects",
"Referral",
Expand Down
Loading