# Invoicing & Payment

## Invoice Creation Workflow

1. **Gather data:**
   - Customer name, address (from contact record or conversation)
   - Line items with descriptions and amounts
   - Payment terms (from memory, default "on receipt")
   - Invoice number: `INV-{YYYYMMDD}-{SLUG}` (SLUG = customer name, uppercased, max 20 chars)

2. **Look up business details from memory:**
   - Company name, address, phone, email
   - Bank details (sort code, account number, account name)

3. **Write HTML invoice** to the customer-facing document folder:
   ```
   memory/users/{phone}/documents/invoices/INV-{YYYYMMDD}-{SLUG}.html
   ```
   Use the template below. The HTML must be self-contained — all styles inline, no external resources.

4. **Convert to PDF:**
   Call `document_to_pdf` with the memory path. The PDF is saved alongside the HTML with a `.pdf` extension.

5. **Present the PDF to the business owner immediately.**
   Do not just announce the file exists — deliver it. Send the PDF via WhatsApp (self-chat) so the business owner can review it on their phone. Include a summary:
   > "Here's the invoice for [customer] — [amount]. I've saved it at [path]. Want me to send it to [customer] on WhatsApp?"

   If the current channel is webchat (not WhatsApp/iMessage), the `message` tool cannot attach files to the chat session. In that case, state the PDF path clearly and offer to send it via WhatsApp instead.

6. **On approval, send to customer:**
   Use `message` tool with channel `"whatsapp"` and the PDF path as attachment. Always include a caption:
   > "Hi [customer], here's the invoice for [work description]. Payment details are on the invoice. Any questions, just let me know!"

7. **Update records:**
   - Update contact record status to `invoiced` via `contact_update`
   - Note the invoice number and amount in memory

## HTML Invoice Template

Use this structure for all invoices. Adjust company details, colours, and content to match the business. All CSS must be inline in a `<style>` block — no external stylesheets.

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
  body { font-family: 'Helvetica Neue', Arial, sans-serif; margin: 0; padding: 40px; color: #333; }
  .invoice-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 40px; border-bottom: 3px solid #2c3e50; padding-bottom: 20px; }
  .company-name { font-size: 28px; font-weight: 700; color: #2c3e50; }
  .company-details { font-size: 12px; color: #666; line-height: 1.6; margin-top: 5px; }
  .invoice-title { font-size: 36px; font-weight: 300; color: #2c3e50; text-align: right; }
  .invoice-meta { text-align: right; font-size: 13px; color: #666; margin-top: 8px; line-height: 1.8; }
  .invoice-meta strong { color: #333; }
  .addresses { display: flex; justify-content: space-between; margin-bottom: 40px; }
  .address-block { width: 45%; }
  .address-label { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: #999; margin-bottom: 8px; font-weight: 600; }
  .address-content { font-size: 13px; line-height: 1.7; }
  table { width: 100%; border-collapse: collapse; margin-bottom: 30px; }
  thead th { background: #2c3e50; color: white; padding: 12px 15px; text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; }
  thead th:last-child { text-align: right; }
  tbody td { padding: 15px; border-bottom: 1px solid #eee; font-size: 13px; }
  tbody td:last-child { text-align: right; font-weight: 500; }
  .item-desc { font-size: 11px; color: #888; margin-top: 4px; }
  .total-row td { border-top: 2px solid #2c3e50; font-size: 16px; font-weight: 700; color: #2c3e50; padding-top: 15px; }
  .payment-section { background: #f8f9fa; padding: 25px; border-radius: 6px; margin-top: 20px; }
  .payment-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; color: #2c3e50; margin-bottom: 12px; }
  .payment-details { font-size: 13px; line-height: 1.8; }
  .footer { margin-top: 40px; text-align: center; font-size: 11px; color: #999; border-top: 1px solid #eee; padding-top: 15px; }

  /* Print / PDF styles */
  @media print {
    body {
      font-size: 12pt;
      line-height: 1.4;
      color: black;
      background: white !important;
      padding: 0;
    }
    @page {
      size: A4;
      margin: 1in;
    }
    .payment-section { page-break-inside: avoid; }
    table { page-break-inside: avoid; }
  }
</style>
</head>
<body>
<div class="invoice-header">
  <div>
    <div class="company-name">[COMPANY NAME]</div>
    <div class="company-details">
      [Address line 1]<br>[Address line 2]<br>[City, Postcode]<br>[Phone]<br>[Email]
    </div>
  </div>
  <div>
    <div class="invoice-title">INVOICE</div>
    <div class="invoice-meta">
      <strong>Invoice No:</strong> [INV-YYYYMMDD-SLUG]<br>
      <strong>Date:</strong> [DD Month YYYY]<br>
      <strong>Due:</strong> [Payment terms]
    </div>
  </div>
</div>

<div class="addresses">
  <div class="address-block">
    <div class="address-label">Bill To</div>
    <div class="address-content">
      <strong>[Customer Name / Company]</strong><br>
      [Attn: Contact Name (if different)]<br>
      [Address]<br>[City, Postcode]
    </div>
  </div>
</div>

<table>
  <thead>
    <tr><th>Description</th><th>Amount</th></tr>
  </thead>
  <tbody>
    <!-- Repeat for each line item -->
    <tr>
      <td>[Item description]<div class="item-desc">[Optional detail]</div></td>
      <td>[Currency symbol][Amount]</td>
    </tr>
    <tr class="total-row">
      <td><strong>Total</strong></td>
      <td><strong>[Currency symbol][Total]</strong></td>
    </tr>
  </tbody>
</table>

<div class="payment-section">
  <div class="payment-title">Payment Details</div>
  <div class="payment-details">
    <strong>Account Name:</strong> [Account name]<br>
    <strong>Sort Code:</strong> [XX-XX-XX]<br>
    <strong>Account Number:</strong> [XXXXXXXX]<br>
    <strong>Reference:</strong> [Invoice number]
  </div>
</div>

<div class="footer">
  [Payment terms message, e.g. "Payment is due on receipt. Thank you for your business."]
</div>
</body>
</html>
```

Fill in all `[placeholders]` from memory and conversation context. Do not leave any placeholders in the final HTML.

## Payment Chase Protocol

1. After a job is completed, prompt business owner: "Job done at [customer]? Shall I send the invoice?"
2. Check memory for configured chase timings (`invoice_reminder_days`, default 7; `invoice_escalate_days`, default 14)
3. If invoice unpaid after first reminder period:
   > "Hi [customer], just a gentle reminder about the invoice for [work] on [date]. The amount was [amount]. Let me know if you have any questions!"
4. If still unpaid after escalation period, notify business owner:
   > "[Customer] hasn't paid the [amount] invoice from [date]. Want me to send another reminder or shall I leave it?"
5. Never be aggressive. Never threaten. Always friendly and professional.
6. Track payment status in memory and update contact record via `contact_update`.
