{
  "name": "ScrapFlow - Customer Invoice (Outbound)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "scrapflow-outbound-completed",
        "responseMode": "onReceived",
        "responseData": "firstEntryJson"
      },
      "id": "4a2b3c4d-0001",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1.1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "event-check",
              "leftValue": "={{ $json.event }}",
              "rightValue": "ticket.outbound.completed",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "email-check",
              "leftValue": "={{ $json.data.customer?.email }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "isNotEmpty"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "4a2b3c4d-0002",
      "name": "Has Customer Email?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "fromEmail": "kwanelerh069@gmail.com",
        "toEmail": "={{ $json.data.customer.email }}",
        "subject": "=ScrapFlow Invoice \u2013 {{ $json.data.invoiceNumber ?? $json.data.ticketNumber }}",
        "emailType": "html",
        "message": "=<!DOCTYPE html><html><body style='font-family:Arial,sans-serif;max-width:600px;margin:0 auto;padding:20px'><h2 style='color:#1a56db'>ScrapFlow SA \u2014 Tax Invoice</h2><table width='100%' style='border-collapse:collapse;margin-bottom:20px'><tr style='background:#f3f4f6'><td style='padding:8px 12px;font-weight:bold'>Invoice Number</td><td style='padding:8px 12px'>{{ $json.data.invoiceNumber ?? $json.data.ticketNumber }}</td></tr><tr><td style='padding:8px 12px;font-weight:bold'>Date</td><td style='padding:8px 12px'>{{ $now.setZone('Africa/Johannesburg').toFormat('dd/MM/yyyy') }}</td></tr><tr style='background:#f3f4f6'><td style='padding:8px 12px;font-weight:bold'>Bill To</td><td style='padding:8px 12px'>{{ $json.data.customer.companyName ?? $json.data.customer.fullName }}</td></tr><tr><td style='padding:8px 12px;font-weight:bold'>Site</td><td style='padding:8px 12px'>{{ $json.data.siteName }}</td></tr><tr style='background:#f3f4f6'><td style='padding:8px 12px;font-weight:bold'>Net Weight</td><td style='padding:8px 12px'>{{ $json.data.netWeight }} kg</td></tr></table><h3>Line Items</h3><table width='100%' style='border-collapse:collapse'><thead><tr style='background:#1a56db;color:white'><th style='padding:8px 12px;text-align:left'>Material</th><th style='padding:8px 12px;text-align:right'>Weight (kg)</th><th style='padding:8px 12px;text-align:right'>Total (R)</th></tr></thead><tbody>{{ $json.data.lineItems.map((li, i) => `<tr style='background:${i%2===0?'#f9fafb':'white'}'><td style='padding:8px 12px'>${li.materialCode}</td><td style='padding:8px 12px;text-align:right'>${li.netWeight}</td><td style='padding:8px 12px;text-align:right'>${li.lineTotal}</td></tr>`).join('') }}</tbody><tfoot><tr style='background:#059669;color:white'><td style='padding:10px 12px;font-weight:bold' colspan='2'>TOTAL</td><td style='padding:10px 12px;font-weight:bold;text-align:right;font-size:16px'>R {{ $json.data.totalPrice }}</td></tr></tfoot></table><br/><p style='color:#9ca3af;font-size:12px'>ScrapFlow SA \u2014 Registered Second-Hand Goods Dealer. This is a system-generated invoice.</p></body></html>",
        "options": {}
      },
      "id": "4a2b3c4d-0003",
      "name": "Send Invoice Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        650,
        200
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "id": "4a2b3c4d-0004",
      "name": "No Email \u2014 Skip",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        650,
        420
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Has Customer Email?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Customer Email?": {
      "main": [
        [
          {
            "node": "Send Invoice Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Email \u2014 Skip",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "04-customer-invoice-v1",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": [
    {
      "createdAt": "2026-03-04T00:00:00Z",
      "updatedAt": "2026-03-04T00:00:00Z",
      "id": "scrapflow-tag",
      "name": "ScrapFlow"
    }
  ]
}