Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
chore(production): re-date serial number timing migrations after main
Raul5725bc29146b
diff --git a/apps/erp/app/modules/production/AGENTS.md b/apps/erp/app/modules/production/AGENTS.mdindex d560903ff8..72eac98bae 100644--- a/apps/erp/app/modules/production/AGENTS.md+++ b/apps/erp/app/modules/production/AGENTS.md@@ -40,7 +40,7 @@ Work orders (jobs), scheduling, routings (operations), bill of materials, proced- It **refuses** a quantity ≤ 0 or below what was already received (stocked items), and a fractional quantity on a serial job. Those raises reach the `jobOperation` UPDATE when they happen under the trigger — `sync_finish_job_operation` therefore clamps with `GREATEST(computed, received)` before calling.- The serial branch receives the delta one numbered unit at a time — shop-floor-finished (`Available`) first, then by `readableId` — skipping `Consumed`/`Rejected`/`Scrapped` and any unit with a `Job Receipt` ledger row for this job, so re-completion never double-receives. An item with **no serial sequence** never reaches `assign-serial-numbers`, so its job keeps one whole-quantity seed entity and the delta is received against that seed instead.- **Make to Asset branch.** A job with `fixedAssetClassId` completes into one new `fixedAsset` per unit (status `Active`, or `Under Construction` when the class `isConstructionInProgress`; `acquisitionCost` = swept WIP ÷ units; one Posted `fixedAssetTransfer` per asset, `type 'Capitalization'`, `sourceType 'Job'`); a job with `fixedAssetId` sweeps its WIP onto that Construction in Progress asset as one `fixedAssetCipCost` row + one transfer. The units never enter stock: no `itemLedger`, `costLedger`, `pickMethod` or `itemCost` write, and the WIP discharge posts Dr class `assetAccountId` / Cr WIP as an `'Asset Transfer'` journal (both lines still `documentId = jobId`, so the per-job WIP balance nets to zero). Each serial unit goes `Consumed` with `attributes["Fixed Asset"]` and a `'Capitalize'` `trackedActivity`; every company writes the asset cost (the journal is Provisional before the accounting cutover). Guards raise before anything moves: `'A job linked to a sales order line cannot complete to a fixed asset'`; `'A job that completes to a fixed asset class needs a serialized item'` (any quantity — every asset a class job makes is a fleet unit followed by its serial); `'Make to Asset needs a serialized item or a quantity of one'` (a job attached to an asset under construction); a job attached to a non-CIP asset is also refused (`'… is not in a Construction in Progress class'`). The item rule is `makeToAssetItemError` (`production.models.ts`), applied by `x+/job+/new.tsx` on save (a field error on the item), by `updateJobDetails` (the details form), by `setJobItem` on a property-panel item change and by `resumeJob` when a paused job goes back to `Ready`, with the sales order line gate. Covered by `packages/database/supabase/tests/job-completion-to-asset.test.sql`. Full flow: `.claude/rules/fixed-asset-lifecycle.md`.-- **Serial number timing** (`companySettings.serialNumberTiming`, Settings → Production → Serial Numbers; an item's serial sequence overrides it with `itemSerialSequence.serialNumberTiming`, NULL follows the company, edited in Settings → Serial Numbers). Every reader resolves the item's own value first: `getSerialNumberTiming(db, companyId, itemId)` (`@carbon/database/sequence`), the SQL `number_serial_units_for_completion`, and the Complete dialog. `jobCreation` (default): `assignJobSerialNumbers` splits and numbers every unit when the job is created. `jobRelease`: the same split and numbering runs when `releaseCheckedJobs` flips the job to Ready (`assignJobSerialNumbersAtRelease`, best-effort: a failure is logged and the release goes on), so every release path (Release dialog, bulk, batch, kanban) numbers it; a unit spawned later is numbered at spawn, as under `jobCreation`. `production`: it is skipped, the job keeps its unnumbered seed, and a unit draws its number at its first touch through `assignSerialNumberIfMissing` (`@carbon/database/sequence`): the MES start (`start.$operationId.tsx`, `x+/event.tsx`, via `services/serial-number.server.ts`), the `issue` posting's serial completion, batch output and operation scrap, and the completion that receives it. A unit the serial completion or a scrap spawns stays unnumbered under `production`. Both completion paths call the SQL `number_serial_units_for_completion` (`20261021103400_serial-number-timing-completion.sql`) before `complete_job_to_inventory`: `completeJob` (through `numberSerialUnitsForCompletion`) and the `sync_finish_job_operation` interceptor (an operation marked Done). It does not read the timing: under `jobCreation` it finds every unit numbered and does nothing, unless the job was created under another timing before the item or company switched back, which would otherwise receive one unnumbered entity holding every unit. It takes the units the RPC will take, in its receipt or Make to Asset order, splits what is missing off the seed and numbers every unit taken, so no unnumbered lump is received. The shop floor's first completion or scrap of the seed leaves it holding one unit (a serial scrap takes one unit, never the whole seed) and spawns the next units one at a time, so the rows can hold fewer units than the job quantity: the function creates the units no row holds yet, up to the job quantity less the units in rows (a scrapped unit does not count), and the Complete dialog counts them as receivable. Every serial is formatted by the SQL `get_next_serial_numbers`, which `getNextSerialNumbers` calls. The Complete dialog unlocks the quantity whenever the item has a serial sequence and units are unnumbered or have no row (`getSerialCompleteChoice`, mode `numberOnReceipt`) and says the serials are drawn at receipt; for an item with no sequence it stays locked to what the shop floor finished.+- **Serial number timing** (`companySettings.serialNumberTiming`, Settings → Production → Serial Numbers; an item's serial sequence overrides it with `itemSerialSequence.serialNumberTiming`, NULL follows the company, edited in Settings → Serial Numbers). Every reader resolves the item's own value first: `getSerialNumberTiming(db, companyId, itemId)` (`@carbon/database/sequence`), the SQL `number_serial_units_for_completion`, and the Complete dialog. `jobCreation` (default): `assignJobSerialNumbers` splits and numbers every unit when the job is created. `jobRelease`: the same split and numbering runs when `releaseCheckedJobs` flips the job to Ready (`assignJobSerialNumbersAtRelease`, best-effort: a failure is logged and the release goes on), so every release path (Release dialog, bulk, batch, kanban) numbers it; a unit spawned later is numbered at spawn, as under `jobCreation`. `production`: it is skipped, the job keeps its unnumbered seed, and a unit draws its number at its first touch through `assignSerialNumberIfMissing` (`@carbon/database/sequence`): the MES start (`start.$operationId.tsx`, `x+/event.tsx`, via `services/serial-number.server.ts`), the `issue` posting's serial completion, batch output and operation scrap, and the completion that receives it. A unit the serial completion or a scrap spawns stays unnumbered under `production`. Both completion paths call the SQL `number_serial_units_for_completion` (`20261022114200_serial-number-timing-completion.sql`) before `complete_job_to_inventory`: `completeJob` (through `numberSerialUnitsForCompletion`) and the `sync_finish_job_operation` interceptor (an operation marked Done). It does not read the timing: under `jobCreation` it finds every unit numbered and does nothing, unless the job was created under another timing before the item or company switched back, which would otherwise receive one unnumbered entity holding every unit. It takes the units the RPC will take, in its receipt or Make to Asset order, splits what is missing off the seed and numbers every unit taken, so no unnumbered lump is received. The shop floor's first completion or scrap of the seed leaves it holding one unit (a serial scrap takes one unit, never the whole seed) and spawns the next units one at a time, so the rows can hold fewer units than the job quantity: the function creates the units no row holds yet, up to the job quantity less the units in rows (a scrapped unit does not count), and the Complete dialog counts them as receivable. Every serial is formatted by the SQL `get_next_serial_numbers`, which `getNextSerialNumbers` calls. The Complete dialog unlocks the quantity whenever the item has a serial sequence and units are unnumbered or have no row (`getSerialCompleteChoice`, mode `numberOnReceipt`) and says the serials are drawn at receipt; for an item with no sequence it stays locked to what the shop floor finished.- Dialog logic lives in `ui/Jobs/job-complete-logic.ts` (pure, no JSX/lingui, unit-tested by `apps/erp/test/job-complete-logic.test.ts`); the SQL is covered by `packages/database/supabase/tests/job-completion-received-quantity.test.sql`, hand-run like its siblings (nothing in CI runs `supabase/tests/`).### Loaded operation and unattributed machine timediff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.tsindex a0658135ab..3535926a1c 100644--- a/packages/database/src/swagger-docs-schema.ts+++ b/packages/database/src/swagger-docs-schema.ts@@ -102432,6 +102432,9 @@ export default {{$ref: "#/parameters/rowFilter.companySettings.receiptQuantitiesStartAtZero"},+ {+ $ref: "#/parameters/rowFilter.companySettings.serialNumberTiming"+ },{$ref: "#/parameters/select"},@@ -102713,6 +102716,9 @@ export default {{$ref: "#/parameters/rowFilter.companySettings.receiptQuantitiesStartAtZero"},+ {+ $ref: "#/parameters/rowFilter.companySettings.serialNumberTiming"+ },{$ref: "#/parameters/preferReturn"}@@ -102948,6 +102954,9 @@ export default {{$ref: "#/parameters/rowFilter.companySettings.receiptQuantitiesStartAtZero"},+ {+ $ref: "#/parameters/rowFilter.companySettings.serialNumberTiming"+ },{$ref: "#/parameters/body.companySettings"},@@ -110303,6 +110312,49 @@ export default {tags: ["(rpc) prevent_posted_sales_invoice_deletion"]}},+ "/rpc/number_serial_units_for_completion": {+ post: {+ parameters: [+ {+ in: "body",+ name: "args",+ required: true,+ schema: {+ properties: {+ p_company_id: {+ format: "text",+ type: "string"+ },+ p_job_id: {+ format: "text",+ type: "string"+ },+ p_quantity_complete: {+ format: "numeric",+ type: "number"+ }+ },+ required: ["p_job_id", "p_quantity_complete", "p_company_id"],+ type: "object"+ }+ },+ {+ $ref: "#/parameters/preferParams"+ }+ ],+ produces: [+ "application/json",+ "application/vnd.pgrst.object+json;nulls=stripped",+ "application/vnd.pgrst.object+json"+ ],+ responses: {+ "200": {+ description: "OK"+ }+ },+ tags: ["(rpc) number_serial_units_for_completion"]+ }+ },"/rpc/purchaseLineDimensionPivot": {post: {parameters: [@@ -113765,6 +113817,85 @@ export default {tags: ["(rpc) sync_job_complete_or_canceled"]}},+ "/rpc/interpolate_serial_segment": {+ get: {+ parameters: [+ {+ format: "text",+ in: "query",+ name: "p_value",+ required: true,+ type: "string"+ },+ {+ format: "timestamp without time zone",+ in: "query",+ name: "p_now",+ required: true,+ type: "string"+ },+ {+ format: "text",+ in: "query",+ name: "p_location",+ required: true,+ type: "string"+ }+ ],+ produces: [+ "application/json",+ "application/vnd.pgrst.object+json;nulls=stripped",+ "application/vnd.pgrst.object+json"+ ],+ responses: {+ "200": {+ description: "OK"+ }+ },+ tags: ["(rpc) interpolate_serial_segment"]+ },+ post: {+ parameters: [+ {+ in: "body",+ name: "args",+ required: true,+ schema: {+ properties: {+ p_location: {+ format: "text",+ type: "string"+ },+ p_now: {+ format: "timestamp without time zone",+ type: "string"+ },+ p_value: {+ format: "text",+ type: "string"+ }+ },+ required: ["p_value", "p_now", "p_location"],+ type: "object"+ }+ },+ {+ $ref: "#/parameters/preferParams"+ }+ ],+ produces: [+ "application/json",+ "application/vnd.pgrst.object+json;nulls=stripped",+ "application/vnd.pgrst.object+json"+ ],+ responses: {+ "200": {+ description: "OK"+ }+ },+ tags: ["(rpc) interpolate_serial_segment"]+ }+ },"/rpc/get_my_claim": {post: {parameters: [@@ -117436,6 +117567,57 @@ export default {tags: ["(rpc) get_ap_open_by_supplier"]}},+ "/rpc/get_next_serial_numbers": {+ post: {+ parameters: [+ {+ in: "body",+ name: "args",+ required: true,+ schema: {+ properties: {+ p_company_id: {+ format: "text",+ type: "string"+ },+ p_count: {+ format: "integer",+ type: "integer"+ },+ p_item_id: {+ format: "text",+ type: "string"+ },+ p_location_code: {+ format: "text",+ type: "string"+ },+ p_location_name: {+ format: "text",+ type: "string"+ }+ },+ required: ["p_item_id", "p_company_id", "p_count"],+ type: "object"+ }+ },+ {+ $ref: "#/parameters/preferParams"+ }+ ],+ produces: [+ "application/json",+ "application/vnd.pgrst.object+json;nulls=stripped",+ "application/vnd.pgrst.object+json"+ ],+ responses: {+ "200": {+ description: "OK"+ }+ },+ tags: ["(rpc) get_next_serial_numbers"]+ }+ },"/rpc/get_job_operation_step_records": {post: {parameters: [@@ -166825,7 +167007,8 @@ export default {"leaseReminderDays","requireBackflushStorageUnit","preventNegativeBackflush",- "receiptQuantitiesStartAtZero"+ "receiptQuantitiesStartAtZero",+ "serialNumberTiming"],properties: {id: {@@ -167228,6 +167411,11 @@ export default {default: false,format: "boolean",type: "boolean"+ },+ serialNumberTiming: {+ default: "jobCreation",+ format: "text",+ type: "string"}},type: "object"@@ -222853,6 +223041,12 @@ export default {in: "query",type: "string"},+ "rowFilter.companySettings.serialNumberTiming": {+ name: "serialNumberTiming",+ required: false,+ in: "query",+ type: "string"+ },"body.part": {name: "part",description: "part",diff --git a/packages/database/supabase/migrations/20261021103300_serial-number-timing.sql b/packages/database/supabase/migrations/20261022114100_serial-number-timing.sqlsimilarity index 100%rename from packages/database/supabase/migrations/20261021103300_serial-number-timing.sqlrename to packages/database/supabase/migrations/20261022114100_serial-number-timing.sqldiff --git a/packages/database/supabase/migrations/20261021103400_serial-number-timing-completion.sql b/packages/database/supabase/migrations/20261022114200_serial-number-timing-completion.sqlsimilarity index 100%rename from packages/database/supabase/migrations/20261021103400_serial-number-timing-completion.sqlrename to packages/database/supabase/migrations/20261022114200_serial-number-timing-completion.sqldiff --git a/packages/database/supabase/migrations/20261021103500_serial-number-timing-finish-operation.sql b/packages/database/supabase/migrations/20261022114300_serial-number-timing-finish-operation.sqlsimilarity index 100%rename from packages/database/supabase/migrations/20261021103500_serial-number-timing-finish-operation.sqlrename to packages/database/supabase/migrations/20261022114300_serial-number-timing-finish-operation.sqldiff --git a/packages/jobs/manifests/schema.json b/packages/jobs/manifests/schema.jsonindex c893edd9ca..918ebcd9ae 100644--- a/packages/jobs/manifests/schema.json+++ b/packages/jobs/manifests/schema.json@@ -1,11 +1,11 @@{"kind": "carbon-company-backup","version": 1,- "schemaVersion": "20261022045258",+ "schemaVersion": "20261022114300","sourceCompanyId": "schema-baseline","sourceCompanyGroupId": null,"sourceCompanyName": null,- "exportedAt": "2026-10-22T06:31:40.888Z",+ "exportedAt": "2026-10-22T11:44:59.436Z","exportedBy": "schema-baseline","label": "schema baseline","includeStorage": "none",@@ -484,7 +484,8 @@"leaseReminderDays","requireBackflushStorageUnit","preventNegativeBackflush",- "receiptQuantitiesStartAtZero"+ "receiptQuantitiesStartAtZero",+ "serialNumberTiming"]},{@@ -3025,7 +3026,8 @@"createdBy","createdAt","updatedBy",- "updatedAt"+ "updatedAt",+ "serialNumberTiming"]},{