# 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. Create Postgres credential in n8n
- **Name:** `MC Cars Postgres`
- **Host:** `db`
- **Port:** `5432`
- **Database:** `postgres`
- **User:** `postgres`
- **Password:** (value of `POSTGRES_PASSWORD` from `.env`)

### 2. Create SMTP credential in n8n
- **Name:** `MC Cars SMTP`
- **Host:** your SMTP server (e.g. `smtp.mailgun.org`, `mail.mc-cars.at`)
- **Port:** `587` (TLS) or `465` (SSL)
- **User:** your SMTP username
- **Password:** your SMTP password
- **From:** `info@mc-cars.at`

### 3. Import workflows
1. Open n8n at http://localhost:55590
2. Go to **Workflows** → **Import from file**
3. Import `01-qualification-payment-email.json`
4. Import `02-mietvertrag-pdf-email.json`
5. Open each workflow → assign the credentials created above → **Activate**

### 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`.
