{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/officina-it/json-schema/officina-it-worksheet-schema.json",
  "title": "Worksheet",
  "description": "A workshop work order. Belongs to one customer and (usually)\none vehicle. Lifecycle: draft → waiting → started →\ncompleted → delivered. Each state has a matching\nnullable `*_at` timestamp populated by the server when the\ntransition fires.\n\nTotals are server-computed from the embedded line items and\nrounded to the working currency precision. They are\nread-only on the wire.\n\n`line_items` is populated on detail / write responses and\nomitted (or `null`) on list responses to keep page payloads\nsmall.\n",
  "x-generated": "2026-11-19",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/officina-it-openapi.json#/components/schemas/Worksheet",
  "type": "object",
  "required": [
    "object",
    "id",
    "work_category_ids",
    "work_categories",
    "seq_number",
    "status",
    "customer_id",
    "created_by",
    "created_at",
    "updated_at"
  ],
  "properties": {
    "version": {
      "type": "integer",
      "x-since": "2026-10-22",
      "description": "Detail, create and write responses only (lists omit it). Send it\nback as `If-Match` on PATCH (and on the line-items batch): a save\nfrom an older copy is refused with 409. Required from revision\n2026-10-22.\n"
    },
    "object": {
      "type": "string",
      "const": "worksheet"
    },
    "id": {
      "type": "string"
    },
    "seq_number": {
      "type": "integer",
      "description": "Per-company human-friendly sequence number. Allocated\nserver-side at creation; never reused.\n"
    },
    "status": {
      "type": "string",
      "enum": [
        "draft",
        "waiting",
        "started",
        "completed",
        "delivered"
      ]
    },
    "title": {
      "type": [
        "string",
        "null"
      ]
    },
    "comments": {
      "type": [
        "string",
        "null"
      ]
    },
    "internal_comments": {
      "type": [
        "string",
        "null"
      ],
      "description": "Workshop-internal notes; surfaced through the mobile\napp for the workshop's own users (no customer-facing\nchannel exposes this field).\n"
    },
    "customer_id": {
      "type": "string"
    },
    "vehicle_id": {
      "type": [
        "string",
        "null"
      ]
    },
    "assigned_to": {
      "type": [
        "string",
        "null"
      ]
    },
    "work_category_id": {
      "type": [
        "string",
        "null"
      ],
      "deprecated": true,
      "description": "The first category's id. Served only to revisions before\n2026-11-02; from 2026-11-02 the key is absent: read\n`work_category_ids`.\n"
    },
    "km": {
      "type": [
        "number",
        "null"
      ],
      "description": "Odometer reading captured when the worksheet was opened.\nFree-form decimal because some workshops record fuel\nmileage or hour-meter values in the same field.\n"
    },
    "created_by": {
      "type": "string"
    },
    "created_by_full_name": {
      "type": "string",
      "description": "Display string of the user who created the worksheet —\ndenormalized for list rendering without a join.\n"
    },
    "customer_name": {
      "type": "string"
    },
    "vehicle_name": {
      "type": [
        "string",
        "null"
      ]
    },
    "vehicle_license": {
      "type": [
        "string",
        "null"
      ]
    },
    "work_category_name": {
      "type": [
        "string",
        "null"
      ],
      "deprecated": true,
      "description": "The first category's name. Served only to revisions before\n2026-11-02; from 2026-11-02 the key is absent: read\n`work_categories`.\n"
    },
    "work_category_ids": {
      "type": "array",
      "items": {
        "type": "string",
        "format": "uuid"
      },
      "x-since": "2026-10-03",
      "description": "Every work category (\"categoria\") of the document, in\nthe order chosen. `work_category_id` (revisions before 2026-11-02)\nalways equals the first. `[]` when the document has none, never\nnull. Available since revision 2026-10-03.\n"
    },
    "work_categories": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/WorkCategory"
      },
      "x-since": "2026-10-03",
      "description": "The same list as `work_category_ids` with each category's\nname, same order. `work_category_name` (revisions before 2026-11-02) is the first one's\nname. Available since revision 2026-10-03.\n"
    },
    "total_before_tax": {
      "type": "number"
    },
    "total_tax": {
      "type": "number"
    },
    "total_after_tax": {
      "type": "number"
    },
    "total_after_discount": {
      "type": "number"
    },
    "total_discount": {
      "type": "number"
    },
    "total_price": {
      "type": "number"
    },
    "total_payments": {
      "type": "number"
    },
    "outstanding_payments": {
      "type": "number"
    },
    "fiscal_document_type": {
      "type": "string",
      "description": "Which fiscal document the worksheet produces: \"fattura\"\n(default) or \"scontrino\".\n"
    },
    "invoice_id": {
      "type": [
        "string",
        "null"
      ],
      "description": "Detail responses only — the first linked invoice, when one\nexists. Drives the Vedi Fattura action.\n"
    },
    "receipt_id": {
      "type": [
        "string",
        "null"
      ],
      "description": "Detail responses only — the linked scontrino for\nscontrino-type worksheets, when one exists.\n"
    },
    "linked_invoices": {
      "type": "array",
      "description": "Detail responses only — compact rows for the Fattura tab\n(fattura-type worksheets).\n",
      "items": {
        "$ref": "#/$defs/WorksheetFiscalDocument"
      }
    },
    "linked_receipt": {
      "description": "Detail responses only — the compact row for the Scontrino\ntab (scontrino-type worksheets), when one exists.\n",
      "allOf": [
        {
          "$ref": "#/$defs/WorksheetFiscalDocument"
        }
      ]
    },
    "has_quote": {
      "type": "boolean",
      "description": "Detail responses only — true when a live preventivo is linked to\nthis worksheet (made from it, or converted into it). False is\nwhen POST /v1/worksheets/{id}/quotes is allowed.\n"
    },
    "quote_id": {
      "type": "string",
      "description": "Detail responses only — the id of that linked preventivo; absent\nwhen `has_quote` is false.\n"
    },
    "quote_seq_number": {
      "type": "integer",
      "description": "Detail responses only — that preventivo's number (with\n`quote_created_at`, its year); absent when `has_quote` is false.\n"
    },
    "quote_created_at": {
      "type": "string",
      "format": "date-time",
      "description": "Detail responses only — that preventivo's creation instant;\nabsent when `has_quote` is false.\n"
    },
    "frozen": {
      "type": "boolean",
      "description": "Detail responses only (since 2026-07-19) — true when a linked\ninvoice has a non-draft electronic invoice; mutations are\nrejected with 409 and the app hides/blocks Modifica.\n"
    },
    "total_work_cost": {
      "type": "number",
      "description": "Internal labour cost ex-VAT (Costi di lavorazione): the value\ntyped by hand when `work_cost_manual` is true, otherwise the sum\nof the lines' `unit_cost × quantity` for unit types h, gg, lav,\nmin. Correct on every revision and on list responses since\n2026-10-15 (lists used to return 0).\n"
    },
    "total_article_cost": {
      "type": "number",
      "description": "Internal parts cost ex-VAT: the value typed by hand when\n`article_cost_manual` is true, otherwise the sum of the other\nlines' `unit_cost × quantity`.\n"
    },
    "work_cost_manual": {
      "type": "boolean",
      "x-since": "2026-10-15",
      "description": "True when `total_work_cost` was typed by hand; line changes no\nlonger move it. Reset with `PATCH` `total_work_cost: null`.\n"
    },
    "article_cost_manual": {
      "type": "boolean",
      "x-since": "2026-10-15",
      "description": "True when `total_article_cost` was typed by hand."
    },
    "waiting_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "started_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "completed_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "delivered_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "created_at": {
      "type": "string",
      "format": "date-time"
    },
    "updated_at": {
      "type": "string",
      "format": "date-time"
    },
    "deleted_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time",
      "description": "Soft-deletion timestamp. Populated on the response from\nDELETE /v1/worksheets/{id}. Worksheets with a non-null\ndeleted_at are not returned by list/detail endpoints.\n"
    },
    "line_items": {
      "type": [
        "array",
        "null"
      ],
      "description": "Embedded line items. Populated on detail / create / patch\n/ delete / transition responses. List responses set this\nto `null` to keep the payload small.\n",
      "items": {
        "$ref": "#/$defs/WorkLineItem"
      }
    },
    "documents": {
      "type": [
        "array",
        "null"
      ],
      "description": "Attachments (Documenti Allegati). Populated on the same\nresponses as `line_items`; null on lists.\n",
      "items": {
        "$ref": "#/$defs/WorksheetDocument"
      }
    }
  },
  "$defs": {
    "WorkCategory": {
      "type": "object",
      "required": [
        "object",
        "id",
        "name"
      ],
      "properties": {
        "object": {
          "type": "string",
          "const": "work_category"
        },
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "name": {
          "type": "string"
        }
      }
    },
    "WorkLineItem": {
      "type": "object",
      "required": [
        "object",
        "id",
        "line_type",
        "description",
        "quantity",
        "unit_type",
        "unit_price",
        "tax_rate",
        "total_price",
        "position",
        "created_at"
      ],
      "description": "A single line on a worksheet. Returned read-only on every\nworksheet response; mutate via\n`POST /v1/worksheets/{id}/line-items/batch`.\n",
      "properties": {
        "object": {
          "type": "string",
          "const": "work_line_item"
        },
        "id": {
          "type": "string"
        },
        "line_type": {
          "type": "string",
          "enum": [
            "item",
            "note",
            "separator"
          ],
          "x-since": "2026-10-14",
          "description": "`item` is a priced line. `note` is a description-only line and\n`separator` an empty spacer row: both print without quantity,\nprice and VAT and carry zero amounts (ignore their numeric\nfields). Lines from documents without types report `item`.\n"
        },
        "article_id": {
          "type": [
            "string",
            "null"
          ],
          "description": "Optional link to a catalog article. When set, edits\nflow through the article inventory bookkeeping.\n"
        },
        "work_category_names": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "x-since": "2026-11-02",
          "description": "Lines with an `article_id` only: the article's work categories\n(\"categorie\"), in the order chosen, read from the article now\n(not a snapshot of when the line was written). `[]` when the\narticle has none. The key is absent on lines without an\narticle (free text, notes, separators) and when the article was\ndeleted.\n"
        },
        "article_code": {
          "type": "string",
          "x-since": "2026-11-02",
          "description": "From 2026-11-11: the line's own code (written with it,\nunchanged when the article changes); absent when the line has\nnone and on notes / separators. `description` no longer\ncontains it.\nBefore 2026-11-11: the article's main code as it is now, on lines\nwith an `article_id` whose article still exists and has a code;\nwhen the shop prints codes, a stored code is also put back in\nfront of `description`.\n"
        },
        "description": {
          "type": "string"
        },
        "quantity": {
          "type": "number"
        },
        "unit_type": {
          "type": "string"
        },
        "unit_price": {
          "type": "number"
        },
        "unit_discount_rate": {
          "type": "number"
        },
        "tax_rate": {
          "type": "number"
        },
        "vat_code": {
          "type": [
            "string",
            "null"
          ]
        },
        "total_price": {
          "type": "number"
        },
        "unit_cost": {
          "type": [
            "number",
            "null"
          ],
          "x-since": "2026-10-15",
          "description": "Internal cost per unit, ex-VAT, never printed on documents. Fixed\nwhen the line was written: the article's purchase price at that\nmoment, or the `unit_cost` sent. `null` when unknown.\n"
        },
        "total_before_tax": {
          "type": "number"
        },
        "total_tax": {
          "type": "number"
        },
        "total_after_tax": {
          "type": "number"
        },
        "position": {
          "type": "integer"
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "WorksheetDocument": {
      "type": "object",
      "required": [
        "object",
        "id",
        "name",
        "content_type",
        "url",
        "file_size_bytes",
        "created_at"
      ],
      "description": "One worksheet attachment; `url` opens the stored file.",
      "properties": {
        "object": {
          "type": "string",
          "const": "worksheet_document"
        },
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "content_type": {
          "type": "string"
        },
        "description": {
          "type": [
            "string",
            "null"
          ]
        },
        "url": {
          "type": "string"
        },
        "file_size_bytes": {
          "type": "integer"
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "WorksheetFiscalDocument": {
      "type": "object",
      "required": [
        "object",
        "id",
        "seq_number",
        "issued_at",
        "created_at",
        "total_after_discount",
        "outstanding_payments"
      ],
      "description": "One compact linked fiscal document on a worksheet detail —\nseq_number 0 means a draft (\"Non emessa\" / \"Bozza\").\n",
      "properties": {
        "object": {
          "type": "string",
          "const": "worksheet_fiscal_document"
        },
        "id": {
          "type": "string"
        },
        "seq_number": {
          "type": "integer"
        },
        "issued_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        },
        "total_after_discount": {
          "type": "number",
          "format": "double"
        },
        "outstanding_payments": {
          "type": "number",
          "format": "double"
        },
        "credited": {
          "type": "boolean",
          "description": "Invoices only — true when a credit note undoes the invoice (\"Stornata con nota di credito\"). Omitted when false."
        }
      }
    }
  }
}