Skip to content

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.

refactor: rename inventory account -> raw materials

Brad Barbin11359cbbe4b9
diff --git a/.ai/specs/2026-07-13-raw-materials-finished-goods-accounts.md b/.ai/specs/2026-07-13-raw-materials-finished-goods-accounts.md
index 1f2af5d726..2c6fce8e1a 100644
--- a/.ai/specs/2026-07-13-raw-materials-finished-goods-accounts.md
+++ b/.ai/specs/2026-07-13-raw-materials-finished-goods-accounts.md
@@ -28,7 +28,7 @@ Verified current state:
| Question | Resolution (Brad, 2026-07-13) |
|---|---|
| How does an item map to RM vs FG? | **Derived from `item.replenishmentSystem`**: `Buy` → Raw Materials; `Make` / `Buy and Make` → Finished Goods. Stable per item so debit and credit sides always agree (no drift). No new schema on `item`. Inventory-tracked Tools/Consumables/Fixtures are almost always `Buy` → RM. |
-| How are existing companies migrated? | **Auto-create a "Finished Goods" (1220) account per company group** (precedent: PR #1127 overhead-absorption pattern) and wire `finishedGoodsAccount` to it; `rawMaterialsAccount` takes the old `inventoryAccount` value. Accepted caveat: pre-split on-hand FG value stays in the old account, so FG can go negative until a manual reclass JE. Existing account names untouched. |
+| How are existing companies migrated? | **Auto-create a "Finished Goods" (1220) account per company group** (precedent: PR #1127 overhead-absorption pattern) and wire `finishedGoodsAccount` to it; `rawMaterialsAccount` takes the old `inventoryAccount` value. Accepted caveat: pre-split on-hand FG value stays in the old account, so FG can go negative until a manual reclass JE. ~~Existing account names untouched.~~ **Revised 2026-07-13 (follow-up migration `20260713234852`):** existing account names were NOT untouched enough — leaving the raw-materials account named "Inventory" while new companies seed it as "Raw Materials" left existing charts asymmetric (a "Finished Goods" account but no "Raw Materials"). A follow-up migration renames each company's `rawMaterialsAccount`-designated account to "Raw Materials" regardless of its current name (guarded: posting account only, not also serving as `finishedGoodsAccount`, no same-group collision). |
| Column naming | `rawMaterialsAccount`, `finishedGoodsAccount` — matches `workInProgressAccount` style (inventory accounts without an "Inventory" suffix). |
| Keep old column for compatibility? | No — **drop `inventoryAccount`** so TypeScript surfaces every stale reference. |
@@ -109,3 +109,4 @@ Edge functions `post-receipt`, `post-purchase-invoice`, `post-shipment`, `post-s
## Changelog
- 2026-07-13 — Spec written and approved (classification + migration strategy resolved with Brad).
+- 2026-07-13 — Follow-up migration `20260713234852_rename-inventory-account-to-raw-materials.sql`: the original "leave existing account names untouched" decision left existing companies with an "Inventory" raw-materials account while new companies seed "Raw Materials" — an asymmetric, confusing chart. The follow-up renames the `rawMaterialsAccount`-designated "Inventory" account to "Raw Materials" (guarded + idempotent).
diff --git a/packages/database/supabase/migrations/20260713234852_rename-inventory-account-to-raw-materials.sql b/packages/database/supabase/migrations/20260713234852_rename-inventory-account-to-raw-materials.sql
new file mode 100644
index 0000000000..7acb774737
--- /dev/null
+++ b/packages/database/supabase/migrations/20260713234852_rename-inventory-account-to-raw-materials.sql
@@ -0,0 +1,45 @@
+-- Rename each company's raw-materials account to "Raw Materials" for existing companies.
+--
+-- The raw-materials / finished-goods split (20260713190909) created a new
+-- "Finished Goods" (1220) account per company group and repointed the
+-- rawMaterialsAccount default at the OLD inventory account — but per that spec it
+-- deliberately left existing account NAMES untouched. New companies are seeded
+-- with 1210 named "Raw Materials", so existing companies diverged: they show a
+-- "Finished Goods" account but their raw stock is still labelled "Inventory" and
+-- there is no "Raw Materials" account at all. This aligns existing companies with
+-- the seed by renaming the account each company's rawMaterialsAccount default
+-- points at to "Raw Materials" — whatever it is currently named.
+--
+-- Safe + idempotent:
+-- * Touches only the account referenced by accountDefault.rawMaterialsAccount,
+-- regardless of its current name (a chart an admin renamed still gets aligned).
+-- * Posting accounts only (isGroup = false).
+-- * Skips any account that also serves as finishedGoodsAccount — the customized-
+-- COA fallback where no 1220 could be created and one account does double duty
+-- (Step 3 of the split); those are left for a manual reclass rather than being
+-- mislabelled.
+-- * Skips if a "Raw Materials" account already exists in the same company group,
+-- so the UNIQUE ("name","companyGroupId","isGroup") constraint can never trip.
+-- This also makes the statement a no-op on re-run: once renamed, the account is
+-- itself the group's "Raw Materials" row, so the guard excludes it. (The WHERE
+-- is evaluated against the pre-update snapshot, so the first run still fires.)
+
+UPDATE "account"
+SET "name" = 'Raw Materials',
+ "updatedBy" = 'system',
+ "updatedAt" = NOW()
+WHERE "isGroup" = false
+ AND "id" IN (
+ SELECT "rawMaterialsAccount" FROM "accountDefault"
+ WHERE "rawMaterialsAccount" IS NOT NULL
+ )
+ AND "id" NOT IN (
+ SELECT "finishedGoodsAccount" FROM "accountDefault"
+ WHERE "finishedGoodsAccount" IS NOT NULL
+ )
+ AND NOT EXISTS (
+ SELECT 1 FROM "account" rm
+ WHERE rm."companyGroupId" = "account"."companyGroupId"
+ AND rm."name" = 'Raw Materials'
+ AND rm."isGroup" = false
+ );