{
  "name": "POS Invoice to WhatsApp",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "documentId": {
          "__rl": true,
          "value": "1qr_Wh3RyvaKy-AwJhG_nvmwb803yMNxbLNNYRSbvQ0I",
          "mode": "list",
          "cachedResultName": "POS & Billing Automation"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1"
        },
        "event": "rowUpdate",
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "typeVersion": 1,
      "position": [
        -1024,
        -112
      ],
      "id": "c1fc7ca1-cd51-4291-890c-28a35317d499",
      "name": "Google Sheets Trigger",
      "credentials": {
        "googleSheetsTriggerOAuth2Api": {
          "id": "E6WQvM7hYnupdtJo",
          "name": "Google Sheets Trigger account"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -832,
        -112
      ],
      "id": "5b6374d2-d40b-4692-b549-6ac7de8bed11",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "operation": "fromJson",
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        -256,
        -96
      ],
      "id": "3457af49-ffc6-4fe0-afe5-81c844bffef6",
      "name": "Get PDF"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://v2.convertapi.com/convert/html/to/pdf",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer api_key"
            }
          ]
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "name": "StoreFile",
              "value": "true"
            },
            {
              "parameterType": "formBinaryData",
              "name": "File",
              "inputDataFieldName": "data"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "id": "9ce50cf7-7391-48b3-a7a2-0a3a8bc6acb1",
      "name": "PDF Generate",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -432,
        -96
      ],
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "parameters": {
        "jsCode": "// Get first input JSON\nconst data = $input.first().json;\n\n// Create dynamic HTML content with interpolation\nconst htmlContent = `\n<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\" />\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n  <title>Receipt</title>\n  <style>\n    :root{ --bg:#f6f7fb; --card:#ffffff; --accent:#1f6feb; --muted:#6b7280; --success:#10b981; }\n    body{font-family:Inter, ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", Roboto, \"Helvetica Neue\", Arial; background:var(--bg); padding:24px;}\n    .receipt-wrap{max-width:800px; margin:18px auto;}\n    .card{background:var(--card); border-radius:12px; box-shadow:0 8px 30px rgba(17,24,39,0.06); padding:24px;}\n    header{display:flex; justify-content:space-between; align-items:center; gap:12px;}\n    .brand{display:flex; gap:12px; align-items:center}\n    .logo{width:64px; height:64px; border-radius:12px; background:linear-gradient(135deg,var(--accent),#7dd3fc); display:flex; align-items:center; justify-content:center; color:#fff; font-weight:700; font-size:20px}\n    h1{font-size:20px; margin:0}\n    .meta{font-size:13px; color:var(--muted)}\n\n    .customer{display:flex; justify-content:space-between; margin-top:18px; padding:12px; border-radius:8px; background: linear-gradient(180deg, rgba(31,111,235,0.03), transparent);}\n    .customer div{min-width:40%}\n    .items{margin-top:20px; width:100%; border-collapse:collapse}\n    .items th, .items td{padding:12px 10px; text-align:left; border-bottom:1px solid #eef2ff}\n    .items th{font-size:13px; color:var(--muted); font-weight:600}\n    .items td{font-size:15px}\n\n    .totals{margin-top:18px; display:flex; justify-content:flex-end}\n    .totals table{width:360px; border-collapse:collapse}\n    .totals td{padding:8px 10px}\n    .totals .label{color:var(--muted)}\n    .totals .value{text-align:right; font-weight:700}\n\n    .final{margin-top:14px; display:flex; justify-content:space-between; align-items:center; padding:14px; border-radius:8px; background:#fff6ee;}\n    .final .left{font-size:14px; color:var(--muted)}\n    .final .right{font-size:20px; font-weight:800; color:var(--success)}\n\n    footer{margin-top:18px; font-size:13px; color:var(--muted); text-align:center}\n\n    @media print{\n      body{background:white}\n      .card{box-shadow:none}\n    }\n  </style>\n</head>\n<body>\n  <div class=\"receipt-wrap\">\n    <div class=\"card\">\n      <header>\n        <div class=\"brand\">\n          <div class=\"logo\">POS</div>\n          <div>\n            <h1>Store Name</h1>\n            <div class=\"meta\">Receipt · Thank you for shopping with us</div>\n          </div>\n        </div>\n        <div style=\"text-align:right\">\n          <div class=\"meta\">Date: ${data.Date || ''}</div>\n          <div class=\"meta\">Receipt #: ${data.receipt_no || ''}</div>\n        </div>\n      </header>\n\n      <div class=\"customer\">\n        <div>\n          <div style=\"font-weight:700\">Customer</div>\n          <div>${data[\"Guest Name\"] || ''}</div>\n          <div class=\"meta\">${data[\"Phone Number\"] || ''}</div>\n          <div class=\"meta\">${data.Email || ''}</div>\n        </div>\n        <div style=\"text-align:right\">\n          <div style=\"font-weight:700\">Payment</div>\n          <div class=\"meta\">Method: ${data.payment_method || 'Cash'}</div>\n          <div class=\"meta\">Status: ${data.Status || 'Paid'}</div>\n        </div>\n      </div>\n\n      <table class=\"items\" aria-label=\"Items\">\n        <thead>\n          <tr>\n            <th>Item</th>\n            <th>Qty</th>\n            <th>Unit Price</th>\n            <th>Discount</th>\n            <th>Subtotal</th>\n          </tr>\n        </thead>\n        <tbody>\n          <tr>\n            <td>${data[\"item Name\"] || ''}</td>\n            <td>${data.Quantity || 0}</td>\n            <td>$${data[\"Unit Price\"] || 0}</td>\n            <td>${data.Discount || 0}</td>\n            <td>$${data[\"Total Item Amount\"] || 0}</td>\n          </tr>\n        </tbody>\n      </table>\n\n      <div class=\"totals\">\n        <table>\n          <tr>\n            <td class=\"label\">Loyalty Redeem</td>\n            <td class=\"value\">$${data[\"Loyalty Points According Discount\"] || 0}</td>\n          </tr>\n          <tr>\n            <td class=\"label\">Tax (5%)</td>\n            <td class=\"value\">$${data.Tax || 0}</td>\n          </tr>\n          <tr>\n            <td class=\"label\">Total (Before Tax)</td>\n            <td class=\"value\">$${data[\"Total Item Amount\"] || 0}</td>\n          </tr>\n        </table>\n      </div>\n\n      <div class=\"final\">\n        <div class=\"left\">Final Amount (incl. tax)</div>\n        <div class=\"right\">$${data[\"Final Price\"] || 0}</div>\n      </div>\n\n      <footer>\n        <div>Questions? Call us at 1800-000-000</div>\n        <div style=\"margin-top:6px\">This is a computer generated receipt and does not require a signature.</div>\n      </footer>\n    </div>\n  </div>\n</body>\n</html>\n`;\n\n// Convert HTML to binary format for n8n\nconst buffer = Buffer.from(htmlContent, 'utf8');\nconst binaryData = {\n  data: buffer.toString('base64'),\n  mimeType: 'text/html',\n  fileName: 'POS_Receipt.html',\n};\n\n// Attach binary data to the first item\nitems[0].binary = { data: binaryData };\n\nreturn items;"
      },
      "id": "aca1640e-f5eb-49eb-9201-8e6fc035a59c",
      "name": "Set Invoice Data",
      "type": "n8n-nodes-base.code",
      "position": [
        -624,
        -96
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "url": "={{ $json.data.Files[0].Url }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -96,
        -96
      ],
      "id": "26460733-9f12-4ce9-aa1a-0a807cef6834",
      "name": "Download PDF"
    },
    {
      "parameters": {
        "name": "={{ $('Loop Over Items').item.json['Guest Name'] }}-POS Invoice",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "1M3B_K7QLKE_UxVdodgPGIb14KrHmhJlq",
          "mode": "list",
          "cachedResultName": "POC-INVOICE"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        112,
        -160
      ],
      "id": "698b7963-ae86-48fe-a12e-b4174bc04bc5",
      "name": "Upload PDF",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "l2hNJyVE2LZJtuCV",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Data Setup\nCreate your own spreadsheet with these tabs/columns, then select it in the Google Sheets nodes:\n- **POS & Billing Automation → Sheet1**: _(columns as used in the nodes)_\n- **POS & Billing Automation → Order Details**: `Phone Number`, `Status`",
        "height": 96,
        "width": 432
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1008,
        -272
      ],
      "typeVersion": 1,
      "id": "a95366bc-7fc5-4fdb-9140-ee859cfddd79",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "1qr_Wh3RyvaKy-AwJhG_nvmwb803yMNxbLNNYRSbvQ0I",
          "mode": "list",
          "cachedResultName": "POS & Billing Automation"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Order Details"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Status": "Send_Invoice",
            "Phone Number": "={{ $('Loop Over Items').item.json['Phone Number'] }}"
          },
          "matchingColumns": [
            "Phone Number"
          ],
          "schema": [
            {
              "id": "Guest Name",
              "displayName": "Guest Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Phone Number",
              "displayName": "Phone Number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "item Name",
              "displayName": "item Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Quantity",
              "displayName": "Quantity",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Unit Price",
              "displayName": "Unit Price",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Discount",
              "displayName": "Discount",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Loyalty Points According Discount",
              "displayName": "Loyalty Points According Discount",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Tax",
              "displayName": "Tax",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Total Item Amount (before tax)",
              "displayName": "Total Item Amount (before tax)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Final Price",
              "displayName": "Final Price",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        288,
        -16
      ],
      "id": "7689a6c3-3c82-4d27-86f4-23c2e369772f",
      "name": "Append or update row in sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "9rJzpmHDDKlqEEe9",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.twilio.com/2010-04-01/Accounts/twilio_sid/Messages.json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "twilioApi",
        "sendBody": true,
        "contentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "Body",
              "value": "=Hello {{ $('Loop Over Items').item.json['Guest Name'] }}\nThank you for visiting Forgent Store today! 🛍️\nWe truly appreciate your support and hope you enjoyed your shopping experience.\n\nLooking forward to serving you again soon. Have a nice day! 🌟"
            },
            {
              "name": "From",
              "value": "whatsapp:+14155238886"
            },
            {
              "name": "To",
              "value": "=whatsapp:+{{ $('Google Sheets Trigger').item.json['Phone Number'] }}"
            },
            {
              "name": "MediaUrl",
              "value": "={{ $json.data.Files[0].Url }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        112,
        -16
      ],
      "id": "fc8ab4d2-2052-44d9-8f36-35bdb07840a0",
      "name": "Send invoice to Whatsapp",
      "credentials": {
        "twilioApi": {
          "id": "9GDTn7krj5zj5GnI",
          "name": "Twilio account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Google Sheets Trigger": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Set Invoice Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PDF Generate": {
      "main": [
        [
          {
            "node": "Get PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get PDF": {
      "main": [
        [
          {
            "node": "Download PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Invoice Data": {
      "main": [
        [
          {
            "node": "PDF Generate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download PDF": {
      "main": [
        [
          {
            "node": "Upload PDF",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send invoice to Whatsapp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload PDF": {
      "main": [
        []
      ]
    },
    "Append or update row in sheet": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send invoice to Whatsapp": {
      "main": [
        [
          {
            "node": "Append or update row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "6239c130-3ccb-4cbe-96cb-52aa38d40f43",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "oVUt1GIBWPhrZQAA",
  "tags": []
}