# n8n Workflows – MC Cars

This folder contains exportable n8n workflow definitions for the MC Cars qualification automation.

## Workflows

### 01 – Qualification Payment Email
**Trigger:** Postgres `NOTIFY` on channel `lead_qualified` (fires when `qualify_lead()` creates a sales order).

**Flow:**
1. Receives notification with `sales_order_id`, `customer_id`, etc.
2. Fetches full order + customer data from Postgres.
3. Sends HTML email to customer with:
   - Booking summary (vehicle, dates, pricing)
   - Kaution bank transfer instructions
   - Payment link for the rental amount

### 02 – Mietvertrag PDF Email
**Trigger:** Same `lead_qualified` Postgres notification.

**Flow:**
1. Checks if `mietvertrag_template_path` is set in `site_settings`.
2. If no template → workflow stops (no error).
3. If template exists:
   - Fetches customer + sales order data
   - Downloads DOCX template from `document-templates` storage bucket
   - Fills placeholders using JSZip (in a Code node)
   - Converts filled DOCX to PDF via Gotenberg
   - Sends PDF as email attachment to customer

## Setup Instructions

### 1. Configure `.env`
The stack now bootstraps n8n credentials/workflow automatically on every `docker compose up`.

Required env variables:
- `POSTGRES_PASSWORD`
- `N8N_POSTGRES_CREDENTIAL_ID`
- `N8N_POSTGRES_CREDENTIAL_NAME`
- `N8N_SMTP_CREDENTIAL_ID`
- `N8N_SMTP_CREDENTIAL_NAME`
- `N8N_SMTP_HOST`
- `N8N_SMTP_USER`
- `N8N_SMTP_PASS`
- `N8N_PAYPAL_KAUTION_LINK`
- `N8N_PAYPAL_MIETE_LINK`
- `N8N_PAYMENT_WORKFLOW_ID`

### 2. Mailbox reference (for future incoming-email workflows)
- **IMAP host:** `heracles.mxrouting.net` (port `993`, SSL/TLS)
- **POP3 host:** `heracles.mxrouting.net` (port `995`, SSL/TLS)
- **Username:** `office@mc-cars.at`
- **Password:** same mailbox password as SMTP

### 3. Import behavior
On startup, n8n runs `/opt/mc-cars/bootstrap/bootstrap-n8n.sh` which:
1. Creates/updates Postgres and SMTP credentials from `.env`
2. Renders `01-qualification-payment-email.json` placeholders
3. Imports the workflow so nodes are always linked to the expected credential IDs
4. Activates the payment workflow automatically (`n8n update:workflow --active=true`)

### 4. Upload Mietvertrag template (optional)
1. Open Admin panel → **Einstellungen** tab
2. Upload a DOCX file in the "Mietvertrag-Vorlage" section
3. The template should contain these placeholders:

| Placeholder | Replaced with |
|---|---|
| `{{KUNDE_NAME}}` | Customer full name |
| `{{KUNDE_EMAIL}}` | Customer email |
| `{{KUNDE_TELEFON}}` | Customer phone |
| `{{BESTELLNUMMER}}` | Sales order number (e.g. SO-2026-0001) |
| `{{FAHRZEUG}}` | Vehicle label (e.g. "Ferrari 488 GTB") |
| `{{DATUM_VON}}` | Rental start date (DD.MM.YYYY) |
| `{{DATUM_BIS}}` | Rental end date (DD.MM.YYYY) |
| `{{TAGE_GESAMT}}` | Total rental days |
| `{{WOCHENTAGE}}` | Number of weekdays |
| `{{WOCHENENDTAGE}}` | Number of weekend days |
| `{{TAGESSATZ}}` | Weekday daily subtotal |
| `{{WOCHENENDZUSCHLAG}}` | Weekend surcharge subtotal |
| `{{NETTO}}` | Net amount (excl. VAT) |
| `{{MWST}}` | VAT amount (19%) |
| `{{GESAMT}}` | Total amount (incl. VAT) |
| `{{KAUTION}}` | Deposit amount |
| `{{DATUM_HEUTE}}` | Today's date (DD.MM.YYYY) |

## Dependencies

- **Gotenberg** (docker service `gotenberg`) — converts DOCX → PDF via LibreOffice
- **n8n** needs the `jszip` npm package (pre-installed in n8n Docker image)

## Domain

All email links and sender addresses use `mc-cars.at`.
