# Lease Termination, Finalisation & Archival

Manage the full end-of-lease workflow: notice → finalisation → exit inspection → deposit settlement → termination → archival.

## Quick Example

```bash
# 1. Initiate termination after notice received
curl -X POST https://api.faireplace.com/api/leases/<lease_id>/termination \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"notice_id": "<notice_uuid>"}'
```

```json
{
  "id": "a1b2c3d4-...",
  "lease_id": "...",
  "notice_id": "...",
  "status": "INITIATED",
  "effective_date": "2026-06-30",
  "checklist": [
    {"key": "notice_effective", "label": "Congé effectif", "required_for_completion": true, "completed": true},
    {"key": "exit_condition_report_done", "label": "État des lieux de sortie", "required_for_completion": true, "completed": false},
    {"key": "keys_returned", "label": "Clés rendues", "required_for_completion": true, "completed": false},
    {"key": "meters_read", "label": "Relevés de compteurs", "required_for_completion": false, "completed": false},
    {"key": "charges_regularized", "label": "Charges régularisées", "required_for_completion": true, "completed": false},
    {"key": "deposit_settled", "label": "Dépôt de garantie soldé", "required_for_completion": true, "completed": false},
    {"key": "final_rent_paid", "label": "Dernier loyer payé", "required_for_completion": false, "completed": false}
  ],
  "completion_percentage": 14,
  "can_complete": false
}
```

---

## Termination Workflows

FairePlace supports three termination paths:

### Standard termination (via notice)

```
Notice received → ACTIVE → NOTICE → FINALISATION → TERMINATED → archived
```

1. Tenant or landlord sends a [notice](/leases/leases) (congé)
2. `POST /leases/{id}/termination` — initiates the process, lease moves to `NOTICE`
3. When the effective date is reached: `PUT /leases/{id}/termination/start-finalisation` — moves to `FINALISATION`
4. Complete the exit checklist (EDL, keys, charges, deposit)
5. `PUT /leases/{id}/termination/complete` — moves to `TERMINATED`
6. `PUT /leases/{id}/archive` — removes from default listings (manual or auto after 90 days)

### Natural expiration (fixed-term lease)

```
End date reached → ACTIVE → FINALISATION → TERMINATED → archived
```

For mobility, seasonal, or fixed-term leases with no renewal:

1. `POST /leases/{id}/termination/from-expiration` — skips `NOTICE`, goes directly to `FINALISATION`
2. Same exit checklist as standard (adapted for lease type)
3. `PUT /leases/{id}/termination/complete` → `PUT /leases/{id}/archive`

### Force terminate (out-of-app process)

```
ACTIVE / NOTICE / FINALISATION → TERMINATED (bypass)
```

When the termination was handled outside the application:

```bash
curl -X PUT https://api.faireplace.com/api/leases/<lease_id>/force-terminate \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "reason": "Résiliation amiable gérée par courrier le 15/02/2026",
    "termination_date": "2026-03-31"
  }'
```

No `LeaseTermination` or checklist is created. The reason is stored for audit.

---

## Termination Status

| Status | Description |
|--------|-------------|
| `INITIATED` | Process started (notice effective or expiration detected) |
| `EXIT_PENDING` | Waiting for exit inspection + key return |
| `SETTLEMENT_PENDING` | Waiting for charge settlement + deposit return |
| `COMPLETED` | All done — lease can be `TERMINATED` |

---

## Checklist

The checklist is a JSONB list of items adapted to the lease type. Each item has:

| Field | Type | Description |
|-------|------|-------------|
| `key` | string | Unique identifier (`keys_returned`, `deposit_settled`, etc.) |
| `label` | string | Human-readable label |
| `required_for_completion` | boolean | If `true`, must be checked to complete termination |
| `completed` | boolean | Whether the item is done |
| `completed_at` | datetime | When it was checked |
| `completed_by` | uuid | Who checked it |

### Default checklist by lease type

**Residential** (7 items, 5 required):
`notice_effective`, `exit_condition_report_done`, `keys_returned`, `meters_read`*, `charges_regularized`, `deposit_settled`, `final_rent_paid`*

**Commercial** (9 items):
Residential + `commercial_handover`*, `signage_removed`*

**Seasonal** (4 items):
`exit_condition_report_done`, `keys_returned`, `deposit_settled`, `cleaning_done`*

*\* = optional*

### Check/uncheck items

```bash
# Check an item
curl -X PUT https://api.faireplace.com/api/leases/<lease_id>/termination/checklist/keys_returned \
  -H "Authorization: Bearer $API_KEY"

# Uncheck (correction)
curl -X DELETE https://api.faireplace.com/api/leases/<lease_id>/termination/checklist/keys_returned \
  -H "Authorization: Bearer $API_KEY"
```

---

## Exit Inspection (EDL de sortie)

After the exit condition report is signed:

```bash
curl -X PUT https://api.faireplace.com/api/leases/<lease_id>/termination/exit-report \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "condition_report_id": "<report_uuid>",
    "actual_exit_date": "2026-06-30",
    "keys_returned": true
  }'
```

This automatically:
- Checks `exit_condition_report_done` and `keys_returned` in the checklist
- Calculates the deposit restitution deadline (Art. 22): 1 month if conformity, 2 months otherwise
- Updates the security deposit `restitution_due_date`
- Transitions to `SETTLEMENT_PENDING`

---

## Deposit Settlement

Apply retentions on the security deposit:

```bash
curl -X PUT https://api.faireplace.com/api/leases/<lease_id>/termination/settlement \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "retained_for_charges": 150.00,
    "retained_for_damages": 200.00,
    "retained_for_unpaid_rent": 0
  }'
```

The system validates:
- Co-ownership charges: **max 20%** of deposit (Art. 22)
- Total retentions cannot exceed deposit amount
- Computes `amount_returned = deposit - retentions`
- If restitution deadline passed: **10% monthly rent penalty** per month of delay

See [Security Deposit](/leases/security-deposit) for detailed rules.

---

## Archival

Archived leases are excluded from default listings but remain queryable.

```bash
# Archive a terminated lease
curl -X PUT https://api.faireplace.com/api/leases/<lease_id>/archive \
  -H "Authorization: Bearer $API_KEY"

# List archived leases
curl https://api.faireplace.com/api/leases/archived \
  -H "Authorization: Bearer $API_KEY"
```

**Rules:**
- Only `TERMINATED` or `EXPIRED` leases can be archived
- Cannot archive if security deposit is in `DISPUTED` status
- Idempotent — archiving twice is a no-op
- Auto-archival: 90 days after termination (via scheduler)

---

## `change_lease_status` Behavior

The generic `PUT /leases/{id}/status` endpoint only handles pre-Active transitions:
- `DRAFT → PENDING_SIGNATURE`
- `PENDING_SIGNATURE → ACTIVE`
- `DRAFT → ACTIVE`

Post-Active transitions (`NOTICE`, `FINALISATION`, `TERMINATED`) return a **400 error** with a message indicating the correct endpoint to use.

---

## Endpoints Summary

| Method | Endpoint | Action |
|--------|----------|--------|
| POST | `/leases/{id}/termination` | Initiate from notice |
| POST | `/leases/{id}/termination/from-expiration` | Initiate from expiration |
| GET | `/leases/{id}/termination` | Get termination status |
| PUT | `/leases/{id}/termination/start-finalisation` | Notice → Finalisation |
| PUT | `/leases/{id}/termination/exit-report` | Register exit inspection |
| PUT | `/leases/{id}/termination/checklist/{key}` | Check item |
| DELETE | `/leases/{id}/termination/checklist/{key}` | Uncheck item |
| PUT | `/leases/{id}/termination/settlement` | Deposit settlement |
| PUT | `/leases/{id}/termination/complete` | Complete termination |
| PUT | `/leases/{id}/force-terminate` | Force terminate (bypass) |
| PUT | `/leases/{id}/archive` | Archive lease |
| GET | `/leases/archived` | List archived leases |

---

## Legal Reference

> **Art. 15, Loi n°89-462** : Le congé doit être notifié par LRAR, huissier, ou remise en main propre. Le délai de préavis est de 6 mois pour le bailleur, 3 mois pour le locataire (1 mois en zone tendue ou situations spécifiques).

> **Art. 22, Loi n°89-462** : Le dépôt de garantie est restitué sous 1 mois (EDL conforme) ou 2 mois. Retenue charges copropriété limitée à 20%. Pénalité de retard : 10% du loyer HC par mois.

> **Art. 3-2, Loi n°89-462** : L'état des lieux de sortie est établi contradictoirement entre les parties lors de la remise des clés.

---

## Related

- **[Lease Overview](/leases/overview)** — Full lifecycle
- **[Security Deposit](/leases/security-deposit)** — Deposit rules and settlement
- **[Charges](/leases/charges)** — Charge regularisation at lease end
- **[Amendments](/leases/amendments)** — Modify active leases
