{
  "info": {
    "name": "Billing (Facturation) — Admin",
    "description": "Admin cross-client billing-health console (the /abonnements → Facturation tab). Monitor each client's billing status, configure the suspension policy (global default tolerance + per-client tolerance/auto-lock), reconcile Virement invoices, and manually suspend/reactivate.\n\nStatus rule: 0 unpaid (issued) invoices → À jour · 1 → En retard · 2..(tolerance-1) → Impayé · ≥ tolerance → Suspendu. Suspension is also forced by a manual 'locked' override and never by 'unlocked'. The client-facing pay flow stays in the Fleet /billing portal.\n\nGated by subscriptions.manage; superadmins bypass. Run 'Auth — Login' first. Base URL: https://api.fleet.dadycar.fr. client_id = companies.id; invoice_id = subscription_invoices.id.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "bearer",
    "bearer": [{ "key": "token", "value": "{{token}}", "type": "string" }]
  },
  "variable": [
    { "key": "base_url", "value": "https://api.fleet.dadycar.fr" },
    { "key": "token", "value": "" },
    { "key": "client_id", "value": "123" },
    { "key": "invoice_id", "value": "1" }
  ],
  "item": [
    {
      "name": "Auth",
      "item": [
        {
          "name": "Auth — Login (sets {{token}})",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "let json = pm.response.json();",
                  "let token = json.token || json.data?.token || json.access_token || json.data?.access_token;",
                  "if (token) { pm.collectionVariables.set('token', token); }"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              { "key": "Accept", "value": "application/json" },
              { "key": "Content-Type", "value": "application/json" }
            ],
            "body": { "mode": "raw", "raw": "{\n  \"email\": \"superadmin@dadycar.fr\",\n  \"password\": \"password\"\n}" },
            "url": {
              "raw": "{{base_url}}/api/admin/auth/login",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "auth", "login"]
            }
          }
        }
      ]
    },
    {
      "name": "Overview",
      "item": [
        {
          "name": "Stats",
          "request": {
            "method": "GET",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/stats",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "stats"]
            },
            "description": "Returns { total_due, clients_with_unpaid, at_risk, suspended, currency }."
          }
        },
        {
          "name": "Options (filter dropdowns)",
          "request": {
            "method": "GET",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/options",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "options"]
            },
            "description": "statuses (a_jour|en_retard|impaye|suspendu), payment_modes (card|transfer), lock_overrides (auto|locked|unlocked)."
          }
        }
      ]
    },
    {
      "name": "Settings (Paramètres)",
      "item": [
        {
          "name": "Get settings",
          "request": {
            "method": "GET",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/settings",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "settings"]
            },
            "description": "Returns { default_tolerance_months } — the global default applied to clients without an override."
          }
        },
        {
          "name": "Update settings (default tolerance)",
          "request": {
            "method": "PUT",
            "header": [
              { "key": "Accept", "value": "application/json" },
              { "key": "Content-Type", "value": "application/json" }
            ],
            "body": { "mode": "raw", "raw": "{\n  \"default_tolerance_months\": 3\n}" },
            "url": {
              "raw": "{{base_url}}/api/admin/billing/settings",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "settings"]
            },
            "description": "Body: { default_tolerance_months } (1–60). Takes effect immediately for clients without a per-client override."
          }
        }
      ]
    },
    {
      "name": "Clients",
      "item": [
        {
          "name": "List (search + filters)",
          "request": {
            "method": "GET",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/clients?per_page=15",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "clients"],
              "query": [
                { "key": "per_page", "value": "15" },
                { "key": "search", "value": "", "description": "Company name, contact first/last name, or email.", "disabled": true },
                { "key": "status", "value": "impaye", "description": "a_jour | en_retard | impaye | suspendu.", "disabled": true },
                { "key": "payment_mode", "value": "transfer", "description": "card | transfer.", "disabled": true },
                { "key": "order_by", "value": "amount_due", "description": "name | created_at | unpaid_count | amount_due.", "disabled": true },
                { "key": "order", "value": "DESC", "description": "ASC | DESC.", "disabled": true }
              ]
            },
            "description": "Paginated. Each row: client (name/contact/email), plan (name + monthly_amount), vehicles_count, status, unpaid_count, tolerance_months, amount_due, next_due_date, payment_mode."
          }
        },
        {
          "name": "Show (Gérer detail)",
          "request": {
            "method": "GET",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/clients/{{client_id}}",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "clients", "{{client_id}}"]
            },
            "description": "{{client_id}} = companies.id. Adds auto_lock, lock_override, tolerance_override (null=global default), subscription_started_at, last_payment_at."
          }
        },
        {
          "name": "Update billing config",
          "request": {
            "method": "PUT",
            "header": [
              { "key": "Accept", "value": "application/json" },
              { "key": "Content-Type", "value": "application/json" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"billing_tolerance_months\": 6,\n  \"billing_auto_lock\": true,\n  \"billing_lock_override\": \"auto\",\n  \"billing_payment_mode\": \"transfer\"\n}"
            },
            "url": {
              "raw": "{{base_url}}/api/admin/billing/clients/{{client_id}}",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "clients", "{{client_id}}"]
            },
            "description": "All fields optional. billing_tolerance_months: null = inherit global default, else 1–60. billing_lock_override: auto | locked | unlocked. billing_payment_mode: card | transfer."
          }
        },
        {
          "name": "Suspend now",
          "request": {
            "method": "POST",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/clients/{{client_id}}/suspend",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "clients", "{{client_id}}", "suspend"]
            },
            "description": "Force-locks the client (lock_override = locked) — blocks app access regardless of tolerance. Does NOT remove vehicles from ANTAI."
          }
        },
        {
          "name": "Reactivate",
          "request": {
            "method": "POST",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/clients/{{client_id}}/reactivate",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "clients", "{{client_id}}", "reactivate"]
            },
            "description": "Force-unlocks the client (lock_override = unlocked) — restores access even if still unpaid. Set lock_override back to 'auto' via Update to resume the automatic policy."
          }
        }
      ]
    },
    {
      "name": "Invoices",
      "item": [
        {
          "name": "Mark invoice paid (Virement)",
          "request": {
            "method": "POST",
            "header": [{ "key": "Accept", "value": "application/json" }],
            "url": {
              "raw": "{{base_url}}/api/admin/billing/invoices/{{invoice_id}}/mark-paid",
              "host": ["{{base_url}}"],
              "path": ["api", "admin", "billing", "invoices", "{{invoice_id}}", "mark-paid"]
            },
            "description": "{{invoice_id}} = subscription_invoices.id. Reconciles an off-card (bank transfer) invoice as paid: marks it paid, clears the suspension cache, best-effort reflects into Pennylane. Returns { result, client }."
          }
        }
      ]
    }
  ]
}
