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.
Returns module (customer RMAs + supplier returns) — implementation plan
Spec: .ai/specs/2026-08-07-rma-module.md (merged via PR #1354; approved design — every open question resolved inline)
Research: .ai/research/2026-08-07-rma-module.md
Branch: returns-module (off fresh main)
Drift findings — spec assumptions vs current main (resolved here, not silently)
The spec was verified against main (2026-08-14). Everything pre-plumbed that the spec relies on is real: receiptSourceDocument + shipmentSourceDocument both already contain 'Sales Return Order' and 'Purchase Return Order'; itemLedgerDocumentType contains 'Sales Return Receipt' + 'Purchase Return Shipment'; disposition has 'Return to Supplier' but not 'Return to Customer'; memo has no return-order columns; accountDefault has no salesReturnsAccount; nothing of the module exists yet. The following spec assumptions drifted; each task below already carries the resolution:
nonConformanceAssociationTypeis a TypeScript const array (quality.models.ts:71), NOT a DB enum. The spec'sALTER TYPE "nonConformanceAssociationType" ADD VALUEis impossible. Resolution: add"salesReturnOrderLines"/"purchaseReturnOrderLines"to the TS array; DB work is only the two junction tables.- NC junction tables do not use the spec's composite-PK sketch. The existing 10 use a bare
idPK (id('nc…')), denormalized parent readable-id columns, and all-four-policies-on-quality_*RLS. The spec's stated intent is "mirrors the existing 10", so the new junctions follow the real sibling shape. post-memoderivesreasonAccountitself (post-memo/index.ts~L233-249: hardcodedsalesDiscountAccount/supplierPaymentDiscountAccount, written back to the memo at post). The spec's "zero posting changes" is false. Resolution: one additive branch in the driver's reason-account resolution keyed on the newmemo.salesReturnOrderId/purchaseReturnOrderIdcolumns.build-memo-journal.tsis account-agnostic and stays untouched.id('pro')collides withprocedure.purchaseReturnOrderusesid('pret'), its lineid('pretl'). (sro/srolare free.)sequenceinserts must includesuffixand the PK is("table","companyId")— the spec's column list omittedsuffix.- Ledger identity: shipments post
itemLedger.entryType 'Negative Adjmt.'— NOT'Sale'as the spec's rationale assumed;'Sale'/'Purchase'live oncostLedger.itemLedgerType. Resolution: return receipt postsitemLedgerentryType 'Positive Adjmt.'+documentType 'Sales Return Receipt'withcostLedger.itemLedgerType 'Sale'; supplier-return shipment posts'Negative Adjmt.'+'Purchase Return Shipment'withcostLedger.itemLedgerType 'Purchase'. ThedocumentTypecarries the return identity, exactly as'Sales Shipment'/'Purchase Receipt'do today. - No
NUMERIC(p,s)anywhere — the spec sketch'sNUMERIC(10,5)violates the house ban on NUMERIC precision. All new numeric columns are bareNUMERIC. - Readable id is assigned at insert, not at confirm — the spec's own DDL makes
salesReturnOrderIdNOT NULL, and every sibling (insertSalesOrder,insertPurchaseOrder, memonew.tsx) callsget_next_sequenceat create. Confirm's job is the status flip + PDF. - config.toml is not the edge-function gate for these functions (deploy is a directory-wide sync). No config.toml work exists in this plan — no new functions are created, only branches added to
create,post-receipt,post-shipment,post-memo. - Line-table FKs are single-column (
salesOrderLine/shipmentLine/salesInvoiceLine/purchaseOrderLine/receiptLine/purchaseInvoiceLineall have PK("id")). New FKs reference("id")only. - The PO "short-close" precedent is
receivedComplete+shortClosePurchaseOrderLine(Kysely,purchasing.service.ts:1832) + route$orderId.$lineId.receiving.tsx. The return lines keep the spec's ownclosedCompletecolumn; the mechanics (Kysely txn, intent field, status recompute) copy that precedent. - Working tree contamination:
packages/database/src/{types,swagger-docs-schema}.ts+functions/lib/types.tsare locally modified with another branch's schema (cutList tables, a revertedshowCurrencyTrailingZerosrename). They must be reset before branching and never committed from this state (Task 1). journalEntrySourceTypehas no return values. Add'Sales Return Receipt'/'Purchase Return Shipment'via theADD VALUE IF NOT EXISTSpattern (20260726013204precedent) so return journals aren't mislabeled as'Purchase Receipt'/'Sales Shipment'.- Original-outbound-cost resolution has a working precedent: nothing persists
layersConsumed, but Unscrap resolves historical cost by querying the consumption rows (costLedger WHERE documentId = <ledger doc> …) and averaging (post-inventory-adjustment/resolve-unscrap-cost.ts), then posts atfixedUnitCost. The RMA cost reversal reuses that exact shape per(shipment, item)— consumption rows are per-item per-shipment, not per-line. resolvePriceis app-side only (sales.service.ts) — replacement orders are therefore built app-side (insertSalesOrder/insertPurchaseOrder+ line inserts with rollback-by-delete, theinsertSalesOrderprecedent), not asconvertedge-function cases.
Also carried from the spec verbatim: the customer-RMA-from-NCR direction is OUT of scope (deferred to a follow-on spec). No task below creates a customer RMA from a quality Issue.
Progress
- Task 1: Preflight — reset contaminated generated files, create branch
- Task 2: Migration: sales-side schema (
sales-return-orders) - Task 3: Migration: purchase-side schema (
purchase-return-orders) - Task 4: Apply migrations + regenerate DB types
- Task 5: New-company seeds (returnReason, sequences, salesReturnsAccount)
- Task 6:
sales.models.ts— validators + const arrays - Task 7:
sales.service.ts— CRUD, list, return reasons - Task 8:
sales.service.ts— lifecycle, transactional caps, returnable-lines + shipped-entities queries - Task 9:
createedge fn —receiptFromSalesReturnOrder - Task 10:
post-receipt— Sales Return Order post + void branch (+ cost helper + deno test) - Task 11: Return receipt tracked-entity assignment (route branch + ReceiptLines UI)
- Task 12: Receipts UI activation for "Sales Return Order"
- Task 13: Paths, nav, Return Reasons CRUD quartet
- Task 14: RMA list route +
SalesReturnOrdersTable - Task 15: RMA detail shell + new/update routes
- Task 16: RMA line routes + line form
- Task 17: RMA PDF (template type + blocks + file route)
- Task 18: RMA status actions (confirm/cancel/complete/line short-close)
- Task 19: Credit generation (post-memo branch, service, route, dialog)
- Task 20: Replacement sales order
- Task 21: Issue association type
salesReturnOrderLines - Task 22: RMA dispositions (Use As Is, set-disposition, escalate to Issue)
- Task 23: Return-to-customer shipment (create + post-shipment + UI activation)
- Task 24:
purchasing.models.ts+purchasing.service.ts— supplier returns - Task 25:
create+post-shipment— Purchase Return Order branches (+ void) - Task 26: Purchasing paths, nav, list route + table
- Task 27: Supplier return PDF
- Task 28: Supplier return detail tree + lines + status actions + shipments UI activation
- Task 29: Supplier credit + replacement purchase order
- Task 30: Issue association
purchaseReturnOrderLines+ quality bridge (Create Supplier Return, closeIssue guard + write-off reduction) - Task 31: Docs sync (module AGENTS.md) + i18n translate
- Task 32: Validation gates (lint, scoped typechecks, tests)
- Task 33: Browser verification via /test
Dependencies
- Tasks 2→3→4 are strictly sequential (4 applies both and regenerates types). Task 5 needs 4.
- Sales chain: 6→7→8; 9/10 need 4 (types) and 8 (service reads); 11 needs 10's design + 4; 12 needs 9; 13 needs 6-7; 14-16 need 13; 17 needs 6-7 (data reads) and is required by 18 (confirm imports the pdf loader); 18 needs 8+17; 19 needs 8; 20 needs 8; 21 needs 4; 22 needs 21+18; 23 needs 18 (dispositions set) + 10's post-shipment familiarity.
- Purchase chain: 24 needs 4; 25 needs 24; 26-28 need 24 (28 also needs 25+27); 29 needs 24+19 (post-memo branch is written once in 19, covering both directions); 30 needs 24+25+21's touchpoint map.
- Parallelizable by /execute: {6,24} after 4; {9,10} vs {13,14} after their parents; {17} vs {14-16}; the purchase chain 24-29 is independent of sales UI tasks 13-23 except where noted; 30 is last-but-two. 31-33 are strictly last, in order.
Task 1: Preflight — reset contaminated generated files, create branch
Depends on: none Files:
- Modify (reset only):
packages/database/src/types.ts,packages/database/src/swagger-docs-schema.ts,packages/database/supabase/functions/lib/types.ts
Steps:
- Confirm
git status --porcelainshows exactly the three files above modified (they carry another branch's schema — cutList tables, a revertedshowCurrencyTrailingZerosrename — regenerated from a local DB that had unmerged migrations applied). If OTHER files are also modified, STOP and report — do not discard unknown work. git checkout -- packages/database/src/types.ts packages/database/src/swagger-docs-schema.ts packages/database/supabase/functions/lib/types.tsgit checkout main && git pull(fast-forward only; if the pull brings new migrations, note the newest migration filename — Task 2's timestamp must sort after it).git checkout -b returns-module
Verify:
git status --porcelain && git branch --show-current
# Expected: empty status output, branch "returns-module"
Out of scope: Rebuilding the local DB. If the local DB still carries the foreign cutList schema, that only matters at Task 4 — handle it there, not here.
Task 2: Migration: sales-side schema (sales-return-orders)
Depends on: Task 1 Files:
- Create:
packages/database/supabase/migrations/<timestamp>_sales-return-orders.sql(viapnpm db:migrate:new sales-return-orders— never hand-pick the timestamp; HHMMSS must not be000000; must sort after20260813222930_re-land-sales-order-total-fix.sql) - Copy from (precedent):
packages/database/supabase/migrations/20260630093809_ar-ap-payments.sql(account seed loop, sequence inserts, additive-column discipline),20260609143732_document-template.sql(table + RLS template),20250327140050_ncr.sql(NC junction shape)
Steps:
pnpm db:migrate:new sales-return-orders, then write the SQL below into the created file. All statements idempotent (IF NOT EXISTS/ADD VALUE IF NOT EXISTS) — the deploy runner does not wrap migrations in a transaction.- Enums:
CREATE TYPE "salesReturnOrderStatus" AS ENUM (
'Draft', 'Confirmed', 'Partially Received', 'Received', 'Completed', 'Cancelled'
);
ALTER TYPE "disposition" ADD VALUE IF NOT EXISTS 'Return to Customer';
ALTER TYPE "journalEntrySourceType" ADD VALUE IF NOT EXISTS 'Sales Return Receipt';
(No ALTER of receiptSourceDocument/itemLedgerDocumentType — the values already exist. Do NOT reference the new enum values anywhere else in this migration.)
3. returnReason (shared by both directions; the noQuoteReason pattern but with modern RLS):
CREATE TABLE IF NOT EXISTS "returnReason" (
"id" TEXT NOT NULL DEFAULT xid(),
"name" TEXT NOT NULL,
"inventoryValueZero" BOOLEAN NOT NULL DEFAULT FALSE,
"companyId" TEXT NOT NULL,
"createdBy" TEXT NOT NULL REFERENCES "user"("id"),
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedBy" TEXT REFERENCES "user"("id"),
"updatedAt" TIMESTAMP WITH TIME ZONE,
"customFields" JSONB,
PRIMARY KEY ("id", "companyId"),
FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE
);
ALTER TABLE "returnReason" ADD CONSTRAINT "returnReason_companyId_name_key" UNIQUE ("companyId", "name");
CREATE INDEX IF NOT EXISTS "returnReason_companyId_idx" ON "returnReason" ("companyId");
CREATE INDEX IF NOT EXISTS "returnReason_createdBy_idx" ON "returnReason" ("createdBy");
salesReturnOrderheader:
CREATE TABLE IF NOT EXISTS "salesReturnOrder" (
"id" TEXT NOT NULL DEFAULT id('sro'),
"salesReturnOrderId" TEXT NOT NULL,
"status" "salesReturnOrderStatus" NOT NULL DEFAULT 'Draft',
"customerId" TEXT NOT NULL REFERENCES "customer"("id"),
"customerLocationId" TEXT REFERENCES "customerLocation"("id"),
"customerContactId" TEXT REFERENCES "customerContact"("id"),
"customerReference" TEXT,
"locationId" TEXT REFERENCES "location"("id"),
"salesOrderId" TEXT REFERENCES "salesOrder"("id") ON DELETE SET NULL,
"replacementSalesOrderId" TEXT REFERENCES "salesOrder"("id") ON DELETE SET NULL,
"currencyCode" TEXT NOT NULL,
"exchangeRate" NUMERIC NOT NULL DEFAULT 1,
"orderDate" DATE NOT NULL,
"expirationDate" DATE,
"internalNotes" JSON,
"externalNotes" JSON,
"assignee" TEXT REFERENCES "user"("id"),
"companyId" TEXT NOT NULL,
"createdBy" TEXT NOT NULL REFERENCES "user"("id"),
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedBy" TEXT REFERENCES "user"("id"),
"updatedAt" TIMESTAMP WITH TIME ZONE,
"customFields" JSONB,
PRIMARY KEY ("id", "companyId"),
FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE
);
ALTER TABLE "salesReturnOrder" ADD CONSTRAINT "salesReturnOrder_salesReturnOrderId_companyId_key" UNIQUE ("salesReturnOrderId", "companyId");
CREATE INDEX IF NOT EXISTS "salesReturnOrder_companyId_idx" ON "salesReturnOrder" ("companyId");
CREATE INDEX IF NOT EXISTS "salesReturnOrder_customerId_idx" ON "salesReturnOrder" ("customerId");
CREATE INDEX IF NOT EXISTS "salesReturnOrder_status_idx" ON "salesReturnOrder" ("status");
CREATE INDEX IF NOT EXISTS "salesReturnOrder_createdBy_idx" ON "salesReturnOrder" ("createdBy");
salesReturnOrderLine:
CREATE TABLE IF NOT EXISTS "salesReturnOrderLine" (
"id" TEXT NOT NULL DEFAULT id('srol'),
"salesReturnOrderId" TEXT NOT NULL REFERENCES "salesReturnOrder"("id") ON DELETE CASCADE,
"lineNumber" INTEGER NOT NULL DEFAULT 1,
"itemId" TEXT NOT NULL REFERENCES "item"("id"),
"quantity" NUMERIC NOT NULL,
"quantityReceived" NUMERIC NOT NULL DEFAULT 0,
"unitOfMeasureCode" TEXT,
"unitPrice" NUMERIC NOT NULL DEFAULT 0,
"restockFeePercent" NUMERIC NOT NULL DEFAULT 0,
"returnReasonId" TEXT REFERENCES "returnReason"("id"),
"salesOrderLineId" TEXT REFERENCES "salesOrderLine"("id") ON DELETE SET NULL,
"shipmentLineId" TEXT REFERENCES "shipmentLine"("id") ON DELETE SET NULL,
"salesInvoiceLineId" TEXT REFERENCES "salesInvoiceLine"("id") ON DELETE SET NULL,
"disposition" "disposition" NOT NULL DEFAULT 'Pending',
"closedComplete" BOOLEAN NOT NULL DEFAULT FALSE,
"companyId" TEXT NOT NULL,
"createdBy" TEXT NOT NULL REFERENCES "user"("id"),
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedBy" TEXT REFERENCES "user"("id"),
"updatedAt" TIMESTAMP WITH TIME ZONE,
"customFields" JSONB,
PRIMARY KEY ("id", "companyId"),
FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_companyId_idx" ON "salesReturnOrderLine" ("companyId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_salesReturnOrderId_idx" ON "salesReturnOrderLine" ("salesReturnOrderId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_itemId_idx" ON "salesReturnOrderLine" ("itemId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_salesOrderLineId_idx" ON "salesReturnOrderLine" ("salesOrderLineId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_shipmentLineId_idx" ON "salesReturnOrderLine" ("shipmentLineId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_salesInvoiceLineId_idx" ON "salesReturnOrderLine" ("salesInvoiceLineId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_returnReasonId_idx" ON "salesReturnOrderLine" ("returnReasonId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLine_createdBy_idx" ON "salesReturnOrderLine" ("createdBy");
salesReturnOrderLineTrackedEntity(expected serials/batches; noupdatedByneeded on the siblingnonConformanceItemTrackedEntityprecedent, but include it per the house audit mandate):
CREATE TABLE IF NOT EXISTS "salesReturnOrderLineTrackedEntity" (
"salesReturnOrderLineId" TEXT NOT NULL,
"trackedEntityId" TEXT NOT NULL REFERENCES "trackedEntity"("id") ON DELETE CASCADE,
"quantity" NUMERIC NOT NULL DEFAULT 1,
"companyId" TEXT NOT NULL REFERENCES "company"("id") ON DELETE CASCADE,
"createdBy" TEXT NOT NULL REFERENCES "user"("id"),
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedBy" TEXT REFERENCES "user"("id"),
"updatedAt" TIMESTAMP WITH TIME ZONE,
PRIMARY KEY ("salesReturnOrderLineId", "trackedEntityId", "companyId"),
FOREIGN KEY ("salesReturnOrderLineId", "companyId") REFERENCES "salesReturnOrderLine"("id", "companyId") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "salesReturnOrderLineTrackedEntity_companyId_idx" ON "salesReturnOrderLineTrackedEntity" ("companyId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderLineTrackedEntity_trackedEntityId_idx" ON "salesReturnOrderLineTrackedEntity" ("trackedEntityId");
salesReturnOrderCreditLine:
CREATE TABLE IF NOT EXISTS "salesReturnOrderCreditLine" (
"id" TEXT NOT NULL DEFAULT xid(),
"memoId" TEXT NOT NULL REFERENCES "memo"("id") ON DELETE CASCADE,
"salesReturnOrderLineId" TEXT NOT NULL REFERENCES "salesReturnOrderLine"("id") ON DELETE CASCADE,
"quantity" NUMERIC NOT NULL,
"unitPrice" NUMERIC NOT NULL,
"restockFee" NUMERIC NOT NULL DEFAULT 0,
"companyId" TEXT NOT NULL REFERENCES "company"("id") ON DELETE CASCADE,
"createdBy" TEXT NOT NULL REFERENCES "user"("id"),
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedBy" TEXT REFERENCES "user"("id"),
"updatedAt" TIMESTAMP WITH TIME ZONE,
PRIMARY KEY ("id", "companyId")
);
CREATE INDEX IF NOT EXISTS "salesReturnOrderCreditLine_companyId_idx" ON "salesReturnOrderCreditLine" ("companyId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderCreditLine_memoId_idx" ON "salesReturnOrderCreditLine" ("memoId");
CREATE INDEX IF NOT EXISTS "salesReturnOrderCreditLine_salesReturnOrderLineId_idx" ON "salesReturnOrderCreditLine" ("salesReturnOrderLineId");
- NC junction — the real sibling shape (drift #2), with denormalized parent ids like
nonConformanceReceiptLine:
CREATE TABLE IF NOT EXISTS "nonConformanceSalesReturnOrderLine" (
"id" TEXT NOT NULL DEFAULT id('ncsro'),
"nonConformanceId" TEXT NOT NULL REFERENCES "nonConformance"("id") ON DELETE CASCADE,
"salesReturnOrderLineId" TEXT NOT NULL REFERENCES "salesReturnOrderLine"("id") ON DELETE CASCADE,
"salesReturnOrderId" TEXT NOT NULL,
"salesReturnOrderReadableId" TEXT NOT NULL,
"companyId" TEXT NOT NULL REFERENCES "company"("id") ON DELETE CASCADE,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"createdBy" TEXT NOT NULL REFERENCES "user"("id"),
"updatedAt" TIMESTAMP WITH TIME ZONE,
"updatedBy" TEXT REFERENCES "user"("id"),
PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "nonConformanceSalesReturnOrderLine_nonConformanceId_idx" ON "nonConformanceSalesReturnOrderLine" ("nonConformanceId");
CREATE INDEX IF NOT EXISTS "nonConformanceSalesReturnOrderLine_salesReturnOrderLineId_idx" ON "nonConformanceSalesReturnOrderLine" ("salesReturnOrderLineId");
CREATE INDEX IF NOT EXISTS "nonConformanceSalesReturnOrderLine_companyId_idx" ON "nonConformanceSalesReturnOrderLine" ("companyId");
- Additive columns:
ALTER TABLE "memo" ADD COLUMN IF NOT EXISTS "salesReturnOrderId" TEXT REFERENCES "salesReturnOrder"("id") ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS "memo_salesReturnOrderId_idx" ON "memo" ("salesReturnOrderId");
ALTER TABLE "accountDefault" ADD COLUMN IF NOT EXISTS "salesReturnsAccount" TEXT REFERENCES "account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
salesReturnsAccount stays NULLABLE (spec decision: runtime fallback to salesAccount) — do NOT add the SET NOT NULL phase from the ar-ap precedent.
10. Seed the Sales Returns contra-revenue account for existing companies — the 20260630093809 L45-73 pattern exactly: DO $$ loop over companyGroup, resolve parent via WHERE "companyGroupId" = cg.id AND "isGroup" = TRUE AND name = 'Income', insert number '4900', name 'Sales Returns', isGroup false, class/incomeBalance/accountType matching the seeded salesAccountrow (copy the enum casts from the precedent),createdBy 'system', guarded WHERE NOT EXISTS (… number = '4900' …). Treat a NULL parent as an error (RAISE) — never insert orphaned (lesson). Then backfill accountDefault.salesReturnsAccountper company via thecompany → companyGroup → account number '4900'join (precedent L97-122), with NO fallback needed since the column stays nullable. **Before writing, greppackages/database/supabase/functions/lib/seed.data.tsfor4900 and the exact Income group header name (nameof the group that parentssalesAccount "4010"); if 4900is taken or the group name differs, pick the nearest free 49xx number / correct name — if no Income-class group exists at all, STOP and report.** 11. SeedreturnReasonfor existing companies (idempotent;ON CONFLICT` on the named unique constraint):
INSERT INTO "returnReason" ("name", "inventoryValueZero", "companyId", "createdBy")
SELECT v.name, false, c."id", 'system'
FROM "company" c
CROSS JOIN (VALUES ('Defective'), ('Wrong Item Shipped'), ('Damaged in Transit'), ('No Longer Needed'), ('Warranty'), ('Other')) AS v(name)
ON CONFLICT ("companyId", "name") DO NOTHING;
- Sequence rows (drift #5 — include
suffix;next 0, size 6, step 1per the salesOrder seed shape):
INSERT INTO "sequence" ("table", "name", "prefix", "suffix", "next", "size", "step", "companyId")
SELECT 'salesReturnOrder', 'Sales Return Order', 'RMA', NULL, 0, 6, 1, c."id"
FROM "company" c
ON CONFLICT DO NOTHING;
salesReturnOrdersview — copy thesalesOrderstwo-separate-lateral shape (20260813222930— the aggregate lateral must be separate from any fan-out join so nosum(DISTINCT)is ever needed):
DROP VIEW IF EXISTS "salesReturnOrders";
CREATE VIEW "salesReturnOrders" WITH (security_invoker = true) AS
SELECT
sro.*,
lines."linesCount",
lines."quantityAuthorized",
lines."quantityReceived",
COALESCE(credits."quantityCredited", 0) AS "quantityCredited"
FROM "salesReturnOrder" sro
LEFT JOIN LATERAL (
SELECT COUNT(*) AS "linesCount",
COALESCE(SUM(l."quantity"), 0) AS "quantityAuthorized",
COALESCE(SUM(l."quantityReceived"), 0) AS "quantityReceived"
FROM "salesReturnOrderLine" l
WHERE l."salesReturnOrderId" = sro."id"
) lines ON TRUE
LEFT JOIN LATERAL (
SELECT COALESCE(SUM(cl."quantity"), 0) AS "quantityCredited"
FROM "salesReturnOrderCreditLine" cl
INNER JOIN "memo" m ON m."id" = cl."memoId"
INNER JOIN "salesReturnOrderLine" l ON l."id" = cl."salesReturnOrderLineId"
WHERE l."salesReturnOrderId" = sro."id" AND m."status" = 'Posted'
) credits ON TRUE;
(No customer-name join — the table resolves customer names client-side via useCustomers(), the SalesOrdersTable precedent. This is a deliberate minor deviation from the spec's "header + customer name" wording.)
14. RLS — the four standardized policies per table, schema-qualified, ::text[] casts. SELECT via get_companies_with_employee_role(); writes via get_companies_with_employee_permission('sales_<action>') for returnReason, salesReturnOrder, salesReturnOrderLine, salesReturnOrderLineTrackedEntity; invoicing_<action> writes for salesReturnOrderCreditLine; all four on quality_view/create/update/delete for nonConformanceSalesReturnOrderLine (the NC-junction sibling pattern — SELECT uses the permission helper there, not the role helper). No portal arm.
Verify:
pnpm db:migrate
# Expected: the new migration applies cleanly (no SQL errors); output lists <timestamp>_sales-return-orders.sql as applied
If the local DB is unreachable or carries foreign schema that conflicts, STOP and report (never rebuild the DB — user does that).
Out of scope: purchase-side tables (Task 3); any TS/model change; NOT NULL on salesReturnsAccount; touching receiptSourceDocument, shipmentSourceDocument, or itemLedgerDocumentType (values already exist).
Task 3: Migration: purchase-side schema (purchase-return-orders)
Depends on: Task 2 Files:
- Create:
packages/database/supabase/migrations/<timestamp>_purchase-return-orders.sql(viapnpm db:migrate:new purchase-return-orders) - Copy from (precedent): the Task 2 file (mirror),
20260421130000_nc-item-tracked-entity.sql(quantity-bearing junction)
Steps:
pnpm db:migrate:new purchase-return-orders; write the direction-flipped mirror. Deltas from the sales twin (everything else mirrors Task 2 exactly, including RLS withpurchasing_<action>in place ofsales_<action>):
CREATE TYPE "purchaseReturnOrderStatus" AS ENUM (
'Draft', 'Confirmed', 'Partially Shipped', 'Shipped', 'Completed', 'Cancelled'
);
ALTER TYPE "journalEntrySourceType" ADD VALUE IF NOT EXISTS 'Purchase Return Shipment';
purchaseReturnOrder— assalesReturnOrderwith:"id" TEXT NOT NULL DEFAULT id('pret')(drift #4 —'pro'isprocedure's), readable"purchaseReturnOrderId","supplierId" TEXT NOT NULL REFERENCES "supplier"("id"),"supplierLocationId" TEXT REFERENCES "supplierLocation"("id"),"supplierContactId" TEXT REFERENCES "supplierContact"("id"),"supplierReference" TEXT(the supplier's own RMA number),"purchaseOrderId"/"replacementPurchaseOrderId" TEXT REFERENCES "purchaseOrder"("id") ON DELETE SET NULL,"status" "purchaseReturnOrderStatus". No customer columns. Unique("purchaseReturnOrderId","companyId"); indexes oncompanyId,supplierId,status,createdBy.purchaseReturnOrderLine— assalesReturnOrderLinewith:id('pretl'),"quantityShipped" NUMERIC NOT NULL DEFAULT 0(instead ofquantityReceived), links"purchaseOrderLineId" TEXT REFERENCES "purchaseOrderLine"("id") ON DELETE SET NULL,"receiptLineId" TEXT REFERENCES "receiptLine"("id") ON DELETE SET NULL,"purchaseInvoiceLineId" TEXT REFERENCES "purchaseInvoiceLine"("id") ON DELETE SET NULL, nodispositioncolumn, keepreturnReasonId/restockFeePercent/closedComplete. Quantities andunitPriceare ALWAYS in the item's inventory UOM (conversion happens once at authoring — Task 24).purchaseReturnOrderLineTrackedEntity— mirror of the sales twin (composite PK line+entity+company, composite FK to the line).purchaseReturnOrderCreditLine— mirror;invoicing_<action>RLS writes.nonConformancePurchaseReturnOrderLine— sibling NC-junction shape (id('ncpro'), denormalizedpurchaseReturnOrderId+purchaseReturnOrderReadableId, quality_* RLS) plus"quantity" NUMERIC NOT NULL DEFAULT 0— the per-quantity ownership the spec's bridge requires (each row records the issue quantity it covers).ALTER TABLE "memo" ADD COLUMN IF NOT EXISTS "purchaseReturnOrderId" TEXT REFERENCES "purchaseReturnOrder"("id") ON DELETE SET NULL;+ index.- Sequence rows:
('purchaseReturnOrder', 'Purchase Return Order', 'RTS', NULL, 0, 6, 1, c."id")— same INSERT shape as Task 2 step 12. purchaseReturnOrdersview — mirror ofsalesReturnOrders(lateral 1:linesCount/quantityAuthorized/quantityShipped; lateral 2:quantityCreditedfrompurchaseReturnOrderCreditLine× Posted memos).
Verify:
pnpm db:migrate
# Expected: <timestamp>_purchase-return-orders.sql applies cleanly
Out of scope: any sales-side change; disposition machinery (supplier returns have no disposition stage by design).
Task 4: Apply migrations + regenerate DB types
Depends on: Tasks 2, 3 Files:
- Modify (generated):
packages/database/src/types.ts,packages/database/src/swagger-docs-schema.ts,packages/database/supabase/functions/lib/types.ts
Steps:
- Both migrations were applied by the Task 2/3 verifies; if
pnpm db:migratereported "no pending migrations", that's fine. Runpnpm db:typesto force type regeneration. - Inspect the diff of
packages/database/src/types.ts: it must containsalesReturnOrder,purchaseReturnOrder,returnReason,salesReturnOrders/purchaseReturnOrdersviews, the two new enums — and must NOT containcutList,jobOperationBatch, or ahideCurrencyTrailingZerosrename (contamination check, drift #12). If contamination appears, the local DB carries foreign migrations — STOP and report (the fix is the user'scrbnrebuild orsupabase migration repair, per the local-DB-divergence memory; do not attempt it).
Verify:
git diff --stat packages/database/src/types.ts && grep -c "salesReturnOrder" packages/database/src/types.ts && grep -c "cutList" packages/database/src/types.ts
# Expected: non-trivial diff; salesReturnOrder count > 0; cutList count = 0 (grep exits 1)
pnpm exec turbo run typecheck --filter=@carbon/database
# Expected: exit 0
Out of scope: hand-editing any generated file.
Task 5: New-company seeds (returnReason, sequences, salesReturnsAccount)
Depends on: Task 4 Files:
- Modify:
packages/database/supabase/functions/lib/seed.data.ts— addreturnReasonstemplate array, twosequencesentries, oneaccountsentry, oneaccountDefaultskey - Modify:
packages/database/supabase/functions/seed-company/index.ts— insertreturnReasonrows (new step near the sequences insert, ~L308) - Modify:
packages/database/src/seed-dev/bootstrap.ts— insertreturnReasonrows (near the sequences insert, ~L258) - Copy from (precedent): the
sequencesarray entries atseed.data.ts:223-479(salesOrderentry shape), theaccountsarray (L612) +L763)accountDefaultsmap (
Steps:
seed.data.ts: add tosequences:{ table: "salesReturnOrder", name: "Sales Return Order", prefix: "RMA", suffix: null, next: 0, size: 6, step: 1 }and{ table: "purchaseReturnOrder", name: "Purchase Return Order", prefix: "RTS", suffix: null, next: 0, size: 6, step: 1 }(match the existing entries' exact field shape). Add toaccounts: asales-returnsleaf{ key: "sales-returns", number: "4900", name: "Sales Returns", isGroup: false, parentKey: <the key of the group that parents the "4010" salesAccount entry — read it from the file>, … same accountType/incomeBalance/class as the salesAccount entry }. Add toaccountDefaults:salesReturnsAccount: "4900". Exportconst returnReasons = ["Defective", "Wrong Item Shipped", "Damaged in Transit", "No Longer Needed", "Warranty", "Other"].seed-company/index.ts: after the sequences insert, insertreturnReasons.map((name) => ({ name, inventoryValueZero: false, companyId, createdBy: userId }))intoreturnReason. NoteaccountDefaultspropagation is automatic once the key exists (the insert is built from the map's keys); the subsidiary-joining-existing-group path resolves by number — confirm"4900"resolves there too (it looks up existing ids by number, and the Task 2 migration seeded 4900 for existing groups).seed-dev/bootstrap.ts: samereturnReasoninsert;accountDefaultinsert extends automatically (dynamic over map keys).- Use the same number (
4900) as the Task 2 migration — if Task 2's preflight grep forced a different number, use that everywhere.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/database
# Expected: exit 0 (seed.data.ts + bootstrap.ts compile)
grep -n "salesReturnOrder" packages/database/supabase/functions/lib/seed.data.ts
# Expected: sequence entry present
Out of scope: re-running seeds against the local DB (the migration backfilled existing companies); noQuoteReason (stays unseeded).
Task 6: sales.models.ts — validators + const arrays
Depends on: Task 4 Files:
- Modify:
apps/erp/app/modules/sales/sales.models.ts - Copy from (precedent):
salesOrderValidator(sales.models.ts:726),salesOrderStatusType(:673),noQuoteReasonValidator(:162), the disposition commented-subset technique (apps/erp/app/modules/quality/quality.models.ts:20-31)
Steps:
- Add const arrays:
salesReturnOrderStatusType = ["Draft", "Confirmed", "Partially Received", "Received", "Completed", "Cancelled"] as constsalesReturnDispositionType = ["Pending", "Use As Is", "Return to Customer", "Scrap", "Rework"] as const— a picker subset of the DBdispositionenum, commented-subset style with the unused eight values present-but-commented (mirror the quality.models technique).isSalesReturnOrderLocked = (status) => ["Completed", "Cancelled"].includes(status).
returnReasonValidator = z.object({ id: zfd.text(z.string().optional()), name: z.string().trim().min(1, { message: "Name is required" }), inventoryValueZero: zfd.checkbox() }).salesReturnOrderValidator— mirrorsalesOrderValidatorfields where applicable:id?,salesReturnOrderId?(server-generated),customerId: z.string().min(1),customerLocationId?,customerContactId?,customerReference?,locationId?,salesOrderId?,currencyCode: zfd.text(z.string().optional()),exchangeRate: zfd.numeric(z.number().optional()),orderDate: z.string().min(1),expirationDate: zfd.text(z.string().optional()),assignee?.salesReturnOrderLineValidator:id?,salesReturnOrderId: z.string().min(1),itemId: z.string().min(1),quantity: zfd.numeric(z.number().gt(0)),unitOfMeasureCode?,unitPrice: zfd.numeric(z.number().min(0)),restockFeePercent: zfd.numeric(z.number().min(0).max(1)).optional(),returnReasonId?,salesOrderLineId?,shipmentLineId?,salesInvoiceLineId?,trackedEntityIds: zfd.repeatableOfType(z.string()).optional().salesReturnOrderCreditValidator:lines: repeatable of { salesReturnOrderLineId, quantity (numeric ≥ 0) }— shaped for the credit dialog's per-line quantities (usezfd.repeatableOfTypeon a JSON-encoded field or paired repeatable fields — match how an existing multi-row form in the codebase encodes arrays, e.g.selectedLinesinpurchasing.models.ts:318).salesReturnOrderDispositionValidator:{ lineId: z.string().min(1), disposition: z.enum(salesReturnDispositionType) }.- Export everything from the module barrel
apps/erp/app/modules/sales/index.ts.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: purchasing models (Task 24); UI components.
Task 7: sales.service.ts — CRUD, list, return reasons
Depends on: Task 6 Files:
- Modify:
apps/erp/app/modules/sales/sales.service.ts - Copy from (precedent):
getSalesOrders(:1650),getSalesOrder(:1596),insertSalesOrder(:5180, theget_next_sequencecall at:5211),upsertNoQuoteReason(:3159),getNoQuoteReasons(:1005),deleteSalesOrder(:448)
Steps:
- List:
getSalesReturnOrders(client, companyId, args: GenericQueryFilters & { search: string | null; status: string | null; customerId: string | null })— reads thesalesReturnOrdersview,.select("*", { count: LIST_COUNT }),.eq("companyId", companyId), search via.or("salesReturnOrderId.ilike…,customerReference.ilike…"),setGenericQueryFilters(query, args, [{ column: "createdAt", ascending: false }]). (Plainselect("*")— do NOT add a*_LIST_COLUMNSconstant; that pattern belongs to the ten big legacy views only.) getSalesReturnOrder(client, id)(view,.single()),getSalesReturnOrderLines(client, salesReturnOrderId, companyId)(base table, joinedreturnReason(name)+item(readableIdWithRevision, name)— embed by target-table name, neveralias:fkColumn(...), composite-FK lesson),getSalesReturnOrderLine(client, lineId),getSalesReturnOrderLineTrackedEntities(client, lineIds: string[])(one.in()call).insertSalesReturnOrder(client, input)— mirrorinsertSalesOrder: whensalesReturnOrderIdabsent,client.rpc("get_next_sequence", { sequence_name: "salesReturnOrder", company_id: input.companyId }); resolvecurrencyCode ?? customer.currencyCode ?? company.baseCurrencyCodeandexchangeRateviagetCurrencyByCode(client, companyGroupId, currencyCode)(accounting.service.ts:1722— note the arg iscompanyGroupId); insert, return{ id, salesReturnOrderId }.updateSalesReturnOrder(client, input)(sanitize +updatedAt/updatedBy),upsertSalesReturnOrderLine(client, line)(branch oncreatedByin arg, theupsertCustomerstyle; on insert computelineNumber= max+1 for the order),deleteSalesReturnOrder(client, id),deleteSalesReturnOrderLine(client, lineId).setSalesReturnOrderLineTrackedEntities(client, lineId, companyId, entityIds, userId)— delete-then-insert the junction rows for the line (single delete + single bulk insert).- Return reasons:
getReturnReasons(client, companyId, args)/getReturnReasonsList(client, companyId)/getReturnReason(client, id)/upsertReturnReason/deleteReturnReason— clone the fivenoQuoteReasonfunctions, plus theinventoryValueZerofield. - Related-panel reads:
getSalesReturnOrderReceipts(client, salesReturnOrderId, companyId)(receiptwheresourceDocumentId = id),getSalesReturnOrderCredits(client, salesReturnOrderId, companyId)(memowheresalesReturnOrderId = id),getSalesReturnOrderIssues(client, salesReturnOrderId, companyId)(nonConformanceSalesReturnOrderLinewheresalesReturnOrderId = id). - Every query
.eq("companyId", companyId)-scoped; all return raw{ data, error }; export from the barrel.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: lifecycle/caps (Task 8); routes.
Task 8: sales.service.ts — lifecycle, transactional caps, returnable-lines + shipped-entities queries
Depends on: Task 7 Files:
- Modify:
apps/erp/app/modules/sales/sales.service.ts - Copy from (precedent):
replaceInvoiceSettlements(apps/erp/app/modules/invoicing/invoicing.service.ts:1734— the canonical.forUpdate()cap-validation transaction, with the why-comment at:1729),shortClosePurchaseOrderLine(purchasing.service.ts:1832),getTrackedEntitiesByMakeMethodId(inventory.service.ts:1369— theattributes->>query pattern)
Steps:
confirmSalesReturnOrder(db: Kysely<KyselyDatabase>, { id, companyId, userId })— one transaction: a.selectFrom("salesReturnOrder").where("id","=",id).where("companyId","=",companyId).forUpdate()— must beDraft, must have ≥1 line; throw otherwise. b. Lock the order's lines.forUpdate(); for each linked line re-read the reversible cap under lock: base =shipmentLine.shippedQuantitywhenshipmentLineIdset, elsesalesOrderLine.quantitySent, elsesalesInvoiceLine.quantity; minus Σquantityover OTHERsalesReturnOrderLinerows sharing that link whose header status is notCancelled. If any line'squantity> cap, throw a message naming the line and cap. Blind lines (no links) skip the check. c.updateTable("salesReturnOrder").set({ status: "Confirmed", updatedBy: userId, updatedAt }).cancelSalesReturnOrder(client, { id, userId })— guard: no receipt for this RMA isPosted/Pending(queryreceiptbysourceDocumentId) ANDquantityReceived = 0on all lines; setCancelled.completeSalesReturnOrder(client, { id, companyId, userId })— guard (mirrorscloseIssue's blocker-collection style,quality-disposition.server.ts:668): every line withclosedComplete = falsemust havequantityReceived >= quantity, and every line withquantityReceived > 0must havedisposition != 'Pending'. Collect violations into one joined error message; on pass setCompleted.shortCloseSalesReturnOrderLine(db, { lineId, salesReturnOrderId, companyId, userId, intent: "close" | "reopen" })— Kysely txn settingclosedComplete, then recompute the header status: if every non-closed line hasquantityReceived >= quantityand any receipt happened →Received; theshortClosePurchaseOrderLinemechanic with the RMA ladder.getReturnableLinesForCustomer(client, companyId, customerId, { salesOrderId?: string })— the "from document" picker source: postedshipmentLines of shipments whose source SO belongs to the customer (joinshipment→salesOrder), each with item, shipped quantity, unit price from the SO line, minus already-authorized quantities (one.in()aggregate oversalesReturnOrderLinebyshipmentLineIdon non-Cancelled headers — never a per-row query). Return rows shaped{ itemId, description, shippedQuantity, alreadyReturned, returnableQuantity, unitPrice, unitOfMeasureCode, salesOrderLineId, shipmentLineId, salesInvoiceLineId? }. If assembling this via PostgREST embeds turns unreadable, define an RPCget_returnable_lines_for_customerin a small follow-up migration instead — flag it in the run log rather than hand-rolling N+1.getShippedTrackedEntitiesForCustomer(client, companyId, customerId, itemId)— entity picker source: shipment ids for the customer (shipmentjoined tosalesOrderby sourceDocumentId, orshipment.customerId— verify which column exists onshipment; it carriescustomerIdper the create-fn header insert), thentrackedEntitywhereattributes->>Shipmentin those ids,.eq("itemId", itemId),.eq("status", "Consumed")— thegetTrackedEntitiesByMakeMethodIdattribute pattern. Two queries total.getCreditableQuantities(client, salesReturnOrderId, companyId)— per line:quantityReceivedminus Σ credit-line quantity over memos withstatus != 'Voided'(Draft counts against the cap so two drafts can't double-credit; the VIEW's displayedquantityCreditedstill derives from Posted only — state this in a comment).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: credit memo creation (Task 19); replacement (Task 20); any route.
Task 9: create edge fn — receiptFromSalesReturnOrder
Depends on: Tasks 4, 8 Files:
- Modify:
packages/database/supabase/functions/create/index.ts - Copy from (precedent): case
receiptFromInboundTransfer(create/index.ts:1031-1197— the outstanding-balance quantity shape) andreceiptFromPurchaseOrder(:765— tracking-type resolution, pickMethod storage units)
Steps:
- Add union member to
payloadValidator:{ type: z.literal("receiptFromSalesReturnOrder"), companyId, userId, salesReturnOrderId: z.string(), receiptId: z.string().optional(), locationId: z.string().optional() }(match the sibling literals' exact field style). - Add
receiptFromSalesReturnOrder: { create: "inventory" }topermissionsByType. - New case: read
salesReturnOrder(must beConfirmedorPartially Received; else throw) + its lines +item.itemTrackingTypefor the line items + pickMethod default storage units (copy the PO case's${itemId}::${locationId}map). BuildReceiptLineItems from lines where!closedComplete && quantity - quantityReceived > 0:lineId: line.id,orderQuantity: line.quantity,outstandingQuantity/receivedQuantity:line.quantity - line.quantityReceived,unitPrice: 0(cost is resolved at posting, not from the credit-basis price — comment this),conversionFactor: 1,unitOfMeasure: line.unitOfMeasureCode ?? "EA",locationId(header's, overridable by payload),storageUnitIdfrom pickMethod default,requiresSerialTracking/requiresBatchTrackingfrom tracking type,companyId,createdBy.
- Transaction: existing-receipt path updates header + deletes lines (the inbound-transfer shape); else
getNextSequence(trx, "receipt", companyId)+ insert receipt withsourceDocument: "Sales Return Order",sourceDocumentId: salesReturnOrder.id,sourceDocumentReadableId: salesReturnOrder.salesReturnOrderId,customerIdif the receipt table has such a column (grepreceiptcolumns first; if absent, skip),locationId. Bulk-insertreceiptLines.
Verify:
pnpm exec biome check packages/database/supabase/functions/create --reporter=summary
# Expected: 0 errors (deno check is NOT a gate — it fails on pre-existing shared-graph errors; lesson)
pnpm exec turbo run typecheck --filter=@carbon/database
# Expected: exit 0
Out of scope: posting (Task 10); the shipment cases (Tasks 23, 25); any UI.
Task 10: post-receipt — Sales Return Order post + void branch
Depends on: Tasks 4, 9 Files:
- Modify:
packages/database/supabase/functions/post-receipt/index.ts - Create:
packages/database/supabase/functions/shared/resolve-return-cost.ts+packages/database/supabase/functions/shared/resolve-return-cost.test.ts - Copy from (precedent): the Purchase Order post branch (
post-receipt/index.ts:595-1969) for structure;post-inventory-adjustment/resolve-unscrap-cost.ts(+ its test) for the historical-cost resolver; the void path (:196-588) for reversal mechanics
Steps:
- Cost resolver (
shared/resolve-return-cost.ts): pure functionresolveReturnUnitCost(rows: { quantity: number; cost: number }[]): number | null=Σ|cost| / Σ|quantity|, null on empty/zero — extract theresolve-unscrap-cost.tsmath intoshared/(import it frompost-inventory-adjustmenttoo if trivial, otherwise leave that copy alone — do not refactor its call site beyond an import swap).deno testfile with: single layer, multi-layer average, empty → null, zero-quantity → null. - Post branch — new
case "Sales Return Order":in theswitch (receipt.data?.sourceDocument)(at:594). Per receipt line (line links back viareceiptLine.lineId→salesReturnOrderLine.id): a. Cost resolution: if the RMA line'sreturnReason.inventoryValueZero→ unit cost 0. Else ifshipmentLineIdset → fetch thatshipmentLine'sshipmentIdand querycostLedgerrowsWHERE documentId = <shipmentId> AND documentType = 'Sales Shipment' AND itemId = <itemId> AND companyId = …→resolveReturnUnitCost(drift #14: consumption rows are per shipment+item). If null/unlinked → current cost fromitemCost.unitCost(blind-return fallback; also the flagged-variance fallback the spec's risk row prescribes — add a comment). b. itemLedger:entryType: "Positive Adjmt.",documentType: "Sales Return Receipt",documentId: receipt.id, positive quantity,postingDate: today— the three tracked/untracked insert sites mirror the PO branch (:1274-1361), including per-unit serial rows andtrackedEntityIddiscovery viaattributes["Receipt Line"]/["Receipt Line Index"]. c. costLedger layer:itemLedgerType: "Sale",costLedgerType: "Direct Cost",documentType: "Sales Return Receipt",documentId: receipt.id,quantity,cost = quantity × resolvedUnitCost,remainingQuantity = quantity(consumable layer; zero-value reasons produce a 0-cost layer, still with remainingQuantity). d. Journal (onlyaccountingEnabled && accountDefaults, and skip entirely when resolved cost is 0): paired lines byjournalLineReference— DrresolveInventoryAccount(replenishmentSystem, accountDefaults)/ CraccountDefaults.costOfGoodsSoldAccountat layer value; journal headersourceType: "Sales Return Receipt",documentType: "Receipt"on lines,documentId: receipt.id(the PO-branch journal shape at:1114-1217). e. Tracked entities: ALWAYSstatus: "On Hold"(spec: disposition is the only path to Available — deliberately NOT theassignmentByItemIdAvailable/On-Hold split the PO branch uses),quantityrestored to the received quantity.trackedActivity{ type: "Return Receipt", sourceDocument: "Receipt", sourceDocumentId: receiptId, sourceDocumentReadableId: receipt.receiptId }+ onetrackedActivityOutputper entity (the:1882-1927shape).trackedActivity.typeis free TEXT — no enum work. f. Source bump + ladder:salesReturnOrderLine.quantityReceived += receivedQuantity(inventory units, conversionFactor 1 — no division); then re-read all lines in-transaction and set header status: every non-closedCompletelinequantityReceived >= quantity→Received; else any linequantityReceived > 0→Partially Received; else keepConfirmed. (New ladder — there is no shared helper; drift-verified.) g. Receipt →Posted(same as PO branch). - Void branch: the void guard at
:197-215currently throws forsourceDocument !== "Purchase Order"— extend to allow"Sales Return Order", then branch: sign-flip the originalitemLedgerrows (query bydocumentId+documentType: "Sales Return Receipt"), sign-flip journal lines (VOID description, same references), roll backquantityReceivedper line (max(0, existing − received)), recompute the header ladder downward (may return toConfirmed), flip reactivated entities back tostatus: "Consumed"(their pre-receipt state — NOTAvailableas the PO void does; blind-created entities also goConsumed) with aVoid Receiptactivity, and zero the return's own cost layers (UPDATE costLedger SET remainingQuantity = 0 WHERE documentId = receiptId AND documentType = 'Sales Return Receipt') so FIFO can never consume voided return stock — an improvement scoped to our own rows only (the PO void's layer gap is out of scope). Set receiptVoided. - Keep the entire branch additive — zero edits inside the
"Purchase Order"/"Inbound Transfer"cases.
Verify:
cd packages/database/supabase/functions && deno test shared/resolve-return-cost.test.ts
# Expected: all tests pass
pnpm exec biome check packages/database/supabase/functions/post-receipt packages/database/supabase/functions/shared --reporter=summary
# Expected: 0 errors
Out of scope: the PO/transfer branches (byte-identical); dispositions; post-shipment.
Task 11: Return receipt tracked-entity assignment (route branch + ReceiptLines UI)
Depends on: Tasks 8, 10 Files:
- Modify:
apps/erp/app/routes/x+/receipt+/lines.tracking.tsx— newtrackingType === "returnEntity"branch - Modify:
apps/erp/app/modules/inventory/ui/Receipts/ReceiptLines.tsx— return-source picker path - Copy from (precedent): the existing serial/batch branches in
lines.tracking.tsx;packages/react/src/TrackedEntityPicker.tsx(props: caller suppliesentities,onSelect)
Steps:
- The standard tracking flow (
update_receipt_line_serial_tracking) REJECTS a serial whose entity already carries aReceipt Line Indexattribute — which every returned serial does from its original receipt. So returns need their own branch, not the RPCs: lines.tracking.tsx: add branch fortrackingType === "returnEntity"with fieldstrackedEntityId,receiptLineId,receiptId,quantity. Validate with the RLS client: the receipt'ssourceDocument === "Sales Return Order"; the entity isConsumed; the entity id appears insalesReturnOrderLineTrackedEntityfor the RMA line the receipt line points at (receiptLine.lineId). Then via service role, UPDATE the entity'sattributesmerging{ "Receipt": receiptId, "Receipt Line": receiptLineId }(+"Receipt Line Index"for serials, using the submitted index) — do NOT change status or quantity here; posting owns that (Task 10 discovers entities by these attributes, unchanged). Also support un-assignment (intent: "remove"clears those attribute keys).ReceiptLines.tsx: where the serial/batch entry UI renders, branch on the receipt'ssourceDocument === "Sales Return Order": renderTrackedEntityPickerfed with the RMA line's expected entities (loader/fetcher readinggetSalesReturnOrderLineTrackedEntities+trackedEntitystatus — onlyConsumedones selectable), submitting thereturnEntityform topath.to.receiptLinesTracking. Blind returns (line has no expected entities): keep the existing free-text serial/batch inputs (the standard creation path handles new entities correctly).- Escape hatch: if
ReceiptLines.tsx's internal structure can't host a per-source branch without restructuring unrelated flows, STOP and report with the component's actual shape — do not refactor the PO tracking UI.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: the update_receipt_line_* SQL functions (untouched); shipment-side tracking.
Task 12: Receipts UI activation for "Sales Return Order"
Depends on: Task 9 Files:
- Modify:
apps/erp/app/modules/inventory/inventory.models.ts— uncomment"Sales Return Order"inreceiptSourceDocumentType(line ~75) - Modify:
apps/erp/app/modules/inventory/ui/Receipts/ReceiptForm/useReceiptForm.tsx— newfetchSourceDocumentsbranch - Modify:
apps/erp/app/routes/x+/receipt+/new.tsx— new switch case - Modify:
apps/erp/app/routes/x+/receipt+/$receiptId.details.tsx— new switch case - Copy from (precedent): the
"Purchase Order"branch in each of those four places
Steps:
inventory.models.ts: uncomment ONLY"Sales Return Order"inreceiptSourceDocumentType(leave the other commented values untouched).useReceiptForm.tsx: add case"Sales Return Order"→carbon.from("salesReturnOrder").select("id, salesReturnOrderId").eq("companyId", companyId).in("status", ["Confirmed", "Partially Received"])→ map to{ name: salesReturnOrderId, id }.x+/receipt+/new.tsxswitch: case"Sales Return Order"→ invokecreatewith{ type: "receiptFromSalesReturnOrder", salesReturnOrderId: sourceDocumentId, companyId, userId }, redirect topath.to.receiptDetails(data.id)— mirror the PO case's error handling.$receiptId.details.tsxsource-change switch: same case withreceiptIdincluded.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: shipmentSourceDocumentType (Tasks 23/28); any receipt-posting logic.
Task 13: Paths, nav, Return Reasons CRUD quartet
Depends on: Tasks 6, 7 Files:
- Modify:
apps/erp/app/utils/path.ts— all sales-return path helpers (alphabetical placement) - Modify:
apps/erp/app/modules/sales/ui/useSalesSubmodules.tsx— nav items - Create:
apps/erp/app/routes/x+/sales+/return-reasons.tsx,return-reasons.new.tsx,return-reasons.$id.tsx,return-reasons.delete.$id.tsx - Create:
apps/erp/app/modules/sales/ui/ReturnReasons/ReturnReasonForm.tsx,ReturnReasonsTable.tsx,index.ts - Copy from (precedent): the
no-quote-reasonsquartet (apps/erp/app/routes/x+/sales+/no-quote-reasons*.tsx) +apps/erp/app/modules/sales/ui/NoQuoteReasons/*
Steps:
path.tsadditions (exact URL shapes,generatePathfor parameterized):salesReturnOrders: ${x}/sales/rmas,salesReturnOrder(id): ${x}/sales-return-order/${id},salesReturnOrderDetails(id): …/details,salesReturnOrderLine(orderId, lineId): …/${lineId}/details,newSalesReturnOrder,newSalesReturnOrderLine(orderId),salesReturnOrderConfirm/Cancel/Complete/Status(id),salesReturnOrderCredit(id),salesReturnOrderReplacement(id),salesReturnOrderLineDisposition(orderId, lineId),salesReturnOrderLineIssue(orderId, lineId),salesReturnOrderLineReceiving(orderId, lineId)(short-close),deleteSalesReturnOrder(id),deleteSalesReturnOrderLine(orderId, lineId),returnReason(id),returnReasons,newReturnReason,deleteReturnReason(id), and in thefilegroupsalesReturnOrder(id): ${file}/sales-return-order/${id}.pdf.- Nav (
useSalesSubmodules.tsx):Managegroup gets{ name: t\RMAs`, to: path.to.salesReturnOrders, icon: <pick a lucide return/undo icon>, table: "salesReturnOrder" };Configuregroup gets{ name: t`Return Reasons`, to: path.to.returnReasons, role: "employee" }` — match the exact item shapes already in the file. - Return-reasons quartet: clone the four
no-quote-reasonsroutes 1:1 (loader{ view: "sales", role: "employee" }, actionscreate/update/delete: "sales",?${getParams(request)}redirect preservation,ConfirmDelete+ PG23503special-case). Form adds aBooleanfieldinventoryValueZerowith description "Returned goods re-enter inventory at zero value" — labels via Linguit`…`. - Table: clone
NoQuoteReasonsTable+ aninventoryValueZeroboolean column.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: purchasing paths/nav (Task 26); the RMA routes themselves.
Task 14: RMA list route + SalesReturnOrdersTable
Depends on: Task 13 Files:
- Create:
apps/erp/app/routes/x+/sales+/rmas.tsx - Create:
apps/erp/app/modules/sales/ui/SalesReturnOrders/SalesReturnOrdersTable.tsx,SalesReturnOrderStatus.tsx,index.ts - Copy from (precedent):
apps/erp/app/routes/x+/sales+/orders.tsx(list route loader shape) +apps/erp/app/modules/sales/ui/SalesOrder/SalesOrdersTable.tsx(memo + columns + context menu +Tableprops) +PurchasingStatus.tsx-style status badge components
Steps:
- Route: loader
{ view: "sales" }, parseGenericQueryFilters+search/status/customerIdexactly asorders.tsxdoes, callgetSalesReturnOrders, render<SalesReturnOrdersTable data count />+<Outlet />. - Table:
memo'd component; columns:salesReturnOrderId(Hyperlink topath.to.salesReturnOrderDetails, pinned left), customer (viauseCustomers()store), status badge (SalesReturnOrderStatus— small component mapping the six statuses to badge variants),orderDate, received progress (quantityReceived/quantityAuthorizedfrom the view),quantityCredited, assignee (viausePeople()),createdAt.meta.filterstatic options for status;useCustomColumns<T>("salesReturnOrder")spread; context menu Edit/Delete gated on permissions;primaryAction={<New … to={path.to.newSalesReturnOrder} />};table="salesReturnOrder"+withSavedView;ConfirmDeletewired topath.to.deleteSalesReturnOrder. CSV export is automatic — addmeta.exportValuefor the store-resolved customer column.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: detail routes (Task 15); purchasing table.
Task 15: RMA detail shell + new/update routes
Depends on: Task 13 Files:
- Create:
apps/erp/app/routes/x+/sales-return-order+/_layout.tsx,new.tsx,update.tsx,$id.tsx,$id._index.tsx,$id.details.tsx,$id.delete.tsx - Create:
apps/erp/app/modules/sales/ui/SalesReturnOrders/SalesReturnOrderForm.tsx,SalesReturnOrderHeader.tsx,SalesReturnOrderProperties.tsx,SalesReturnOrderExplorer.tsx - Copy from (precedent):
x+/sales-order+/_layout.tsx(+$orderId.tsxshell:PanelProvider+ResizablePanels+ header),x+/sales-order+/new.tsx(insert flow),SalesOrderExplorer.tsx,SalesOrderProperties.tsx;x+/purchase-order+/new.tsx:72for theuseCompanyToday()order-date default Steps:
_layout.tsx:handle = { breadcrumb: msg\Sales`, to: path.to.sales, module: "sales" }, bare` — the sales-order layout verbatim.new.tsx: action{ create: "sales" }→validator(salesReturnOrderValidator).validate→insertSalesReturnOrder→throw redirect(path.to.salesReturnOrderDetails(id)). Default export rendersSalesReturnOrderFormwithorderDate: useCompanyToday()(the PO precedent — deliberately better than sales-order's empty string), centeredmax-w-4xl.$id.tsx: loader{ view: "sales" }fetching order + lines + related panels (defer the panels); shell =SalesReturnOrderHeader(status badge + Confirm/Cancel/Complete/Credit/Replacement action buttons, permission-gated, wired to the POST routes of Tasks 18-20) aboveResizablePanels{ explorer: SalesReturnOrderExplorer (lines list, add-line button, navigates to line routes), content: <Outlet/>, properties: SalesReturnOrderProperties (customer/location/dates/assignee/customerReference/replacement-link chip) }.handleusesdetailBreadcrumbwith the readable id.$id._index.tsx: redirect to details.$id.details.tsx: order summary card + related panels — Receipts (getSalesReturnOrderReceipts, linking topath.to.receiptDetails), Credits (getSalesReturnOrderCredits, linking topath.to.memo-equivalent inx+/credits+), Issues (getSalesReturnOrderIssues, linking topath.to.issue).update.tsx: header-field edit action (update: "sales",updateSalesReturnOrder), guarded byisSalesReturnOrderLocked.$id.delete.tsx:delete: "sales", only whenDraft/Cancelledwith no received quantity;ConfirmDeletepattern.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: line CRUD routes (Task 16), status actions (Task 18).
Task 16: RMA line routes + line form
Depends on: Tasks 8, 15 Files:
- Create:
apps/erp/app/routes/x+/sales-return-order+/$id.new.tsx(new line),$id.$lineId.details.tsx,$id.$lineId.delete.tsx - Create:
apps/erp/app/modules/sales/ui/SalesReturnOrders/SalesReturnOrderLineForm.tsx,ReturnableLinesModal.tsx - Copy from (precedent):
x+/sales-order+/$orderId.new.tsx+$orderId.$lineId.details.tsx+SalesOrderLineForm.tsx;packages/react/src/TrackedEntityPicker.tsx;requireUnlockedfrom~/utils/lockedGuard.serverSteps:
$id.new.tsx: action-only (create: "sales"),requireUnlockedonisSalesReturnOrderLocked, validatesalesReturnOrderLineValidator,upsertSalesReturnOrderLine+setSalesReturnOrderLineTrackedEntitieswhentrackedEntityIdspresent.SalesReturnOrderLineForm: fields —Itemselector,quantity(Number,INPUT_STEP.quantity),unitPrice(Number withINPUT_FORMAT.rate(currency, decimals)— a per-unit price, rate kind),restockFeePercent(percent-points field: mirror whichever existing form field bindsdiscountPercent, including its format/step — grepdiscountPercentinSalesOrderLineForm.tsxand copy exactly),returnReasonId(Combobox fed bygetReturnReasonsList), read-only linkage chips whensalesOrderLineId/shipmentLineId/salesInvoiceLineIdset, disposition select (subsetsalesReturnDispositionType, disabled untilquantityReceived > 0— submits to the Task 22 disposition route), tracked-entity picker for Serial/Batch items fed bygetShippedTrackedEntitiesForCustomer(blind lines allow skipping).ReturnableLinesModal(the "from document" picker on the new-RMA flow): fetchesgetReturnableLinesForCustomerfor the chosen customer (fetcher route: add a smallx+/sales-return-order+/returnable-lines.tsxloader, or anapi+route — match howSalesOrderExplorerfetches auxiliary data); multi-select rows with editable quantity clamped toreturnableQuantity; submits multiple line creates to$id.new. Client-side clamp is UX only — the authoritative cap is Task 8's confirm-time lock (state this in a comment).$id.$lineId.details.tsx: loader + form withinitialValuesfrom the line (getCustomFields), read-only when locked;$id.$lineId.delete.tsxstandard.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: disposition execution (Task 22); receipt creation.
Task 17: RMA PDF (template type + blocks + file route)
Depends on: Tasks 6, 7 Files:
- Modify:
packages/documents/src/template/schema.ts(documentTemplateTypeSchema+ block union entries if needed),template/defaults.ts(BLOCK_METAunchanged,DEFAULT_TEMPLATES+DOCUMENT_CATALOGentries),packages/documents/src/pdf/index.ts,pdf/preview-documents.tsx - Create:
packages/documents/src/pdf/SalesReturnOrderPDF.tsx,pdf/blocks/salesReturnOrder/{types.ts,vars.ts,registry.tsx,HeaderBlock.tsx,PartiesBlock.tsx,LineItemsBlock.tsx,NotesBlock.tsx,TermsBlock.tsx},pdf/salesReturnOrder.samples.ts - Create:
apps/erp/app/routes/file+/sales-return-order+/$id[.]pdf.tsx - Copy from (precedent):
packages/documents/src/pdf/SalesOrderPDF.tsx+pdf/blocks/salesOrder/*+salesOrder.samples.ts; routeapps/erp/app/routes/file+/sales-order+/$id[.]pdf.tsx
Steps:
- Add
salesReturnOrdertodocumentTemplateTypeSchemaand aDEFAULT_TEMPLATES.salesReturnOrder(blocks: header, parties, lineItems, notes, terms — reusing the existing block types; no new block type, so no registry-breaking union change). Add theDOCUMENT_CATALOGlabel ("RMA"). pdf/blocks/salesReturnOrder/:types.tsdata bag typed off the generatedsalesReturnOrdersview Row + line rows + returnReason names;vars.tsmerge vars (rma number, customer, dates, expiration); block components adapted from the salesOrder ones — LineItems columns: line #, item, description, quantity authorized, unit price, return reason; header shows "Return Merchandise Authorization" +salesReturnOrderId+expirationDate; parties shows return-to (company location) + customer.registry.tsxmust key EVERY block type in the union (unused →() => null).SalesReturnOrderPDF.tsxdriver:resolveTemplate("salesReturnOrder", template)→<Template>loop — the SalesOrderPDF shape. Register inpdf/index.ts+preview-documents.tsx(DOCUMENT_PDFS.salesReturnOrder = { Component, sample }) with a sample fixture.- File route: loader
{ view: "sales" },Promise.allof company/companySettings/salesReturnOrder/lines/returnReasons/location/documentTemplate, thentoDocumentTemplate→resolveTemplate→resolveSections(collectSectionIds(...))→ensureFont→renderToStream→ drain to Buffer →ResponsewithContent-Disposition: inline; filename="{company} - {salesReturnOrderId}.pdf"— the sales-order route verbatim. - Check
x+/templates+/$type.tsx— if the$typeparam validates againstdocumentTemplateTypeSchema, the customizer picks the new type up automatically; if there's a hardcoded type list anywhere in the templates routes orDocumentTemplateEditor/labelConfigs, extend it. If adding the type forces changes in more than these known places (schema, defaults, catalog, registries, preview, editor labels), STOP and report the extra coupling before proceeding.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/documents && pnpm exec turbo run typecheck --filter=erp
# Expected: both exit 0
Out of scope: the purchase-return PDF (Task 27); email sending.
Task 18: RMA status actions (confirm/cancel/complete/line short-close)
Depends on: Tasks 8, 15, 17 Files:
- Create:
apps/erp/app/routes/x+/sales-return-order+/$id.confirm.tsx,$id.status.tsx(cancel + complete via astatusfield),$id.$lineId.receiving.tsx(short-close toggle) - Modify:
apps/erp/app/modules/shared/shared.server.ts—generateAndAttachSalesReturnOrderPdf - Modify:
SalesReturnOrderHeader.tsx(Task 15) — wire the buttons + confirm modal - Copy from (precedent):
x+/sales-order+/$orderId.confirm.tsx(PDF generate-and-attach + service-role companyId assertion),$orderId.status.tsx(status-field branching +requestReferrerredirects — do NOT copy itspath.to.quote(id)fallback bug),generateAndAttachSalesOrderPdf(shared.server.ts:140),x+/purchase-order+/$orderId.$lineId.receiving.tsx
Steps:
$id.confirm.tsx(POST-only,assertIsPost,{ update: "sales", role: "employee" }): callconfirmSalesReturnOrder(getDatabaseClient(), …)in try/catch (Kysely throws; return the thrown cap-violation message viaflash(error(...))) → on successgenerateAndAttachSalesReturnOrderPdf(serviceRole, { companyId, id, userId })→ return{ success }.generateAndAttachSalesReturnOrderPdf: mirror the sales-order version — call the Task 17 pdf loader withparams.id, upload to theprivatebucket at${companyId}/sales-return-order/${id}/${fileName}(no opportunity dir — RMAs have no opportunity),upsertDocument(serviceRole, { sourceDocument: "Sales Return Order", sourceDocumentId: id, … })(the document-type enum already contains the value).$id.status.tsx(POST-only,{ update: "sales" }):statusform field validated againstsalesReturnOrderStatusType;Cancelled→cancelSalesReturnOrder(guard errors → flash);Completed→completeSalesReturnOrder(blocker list → flash, the joined-message style); reject any other submitted status (receipt-driven statuses are never set manually).throw redirect(requestReferrer(request) ?? path.to.salesReturnOrderDetails(id)).$id.$lineId.receiving.tsx:intent: "close" | "reopen"→shortCloseSalesReturnOrderLine(getDatabaseClient(), …)— the PO receiving-route shape, permission{ update: "sales" }.- Header wiring: Confirm visible on
Draft; Cancel onDraft/Confirmed(service re-guards); Complete onPartially Received/Received; buttons use fetchers to the routes above; PDF download link →path.to.file.salesReturnOrder(id)once not Draft.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: email notification on confirm (not in spec — PDF only); receipt-driven transitions (Task 10 owns them).
Task 19: Credit generation (post-memo branch, service, route, dialog)
Depends on: Tasks 8, 15 Files:
- Modify:
packages/database/supabase/functions/post-memo/index.ts— reason-account resolution branch (~L233-249) - Modify:
apps/erp/app/modules/sales/sales.service.ts—createSalesReturnOrderCredit - Create:
apps/erp/app/routes/x+/sales-return-order+/$id.credit.tsx - Create:
apps/erp/app/modules/sales/ui/SalesReturnOrders/SalesReturnOrderCreditModal.tsx - Copy from (precedent): memo creation in
x+/credits+/new.tsx(sequence pick +upsertMemofield shape);replaceInvoiceSettlements(invoicing.service.ts:1734) for the locked cap transaction;MemoForm.tsxfor field conventions
Steps:
post-memobranch (drift #3) — in the reason-account derivation, ahead of the existing party-side default:
const memoRow = /* already loaded */;
const reasonAccountId = memoRow.salesReturnOrderId
? (ad.salesReturnsAccount ?? ad.salesAccount)
: memoRow.purchaseReturnOrderId
? ad.goodsReceivedNotInvoicedAccount
: isAR ? ad.salesDiscountAccount : ad.supplierPaymentDiscountAccount;
Both new columns are on the loaded memo row after Task 4's type regen. The void path mirrors the post journal by re-reading stored lines, so it needs no change — verify that assumption by reading the void branch; if void re-derives accounts instead, apply the same branch there. build-memo-journal.ts untouched.
2. createSalesReturnOrderCredit(db: Kysely, { salesReturnOrderId, companyId, userId, lines: { salesReturnOrderLineId, quantity }[] }) — one transaction:
a. Lock the RMA header + its lines .forUpdate(); recompute per-line creditable = quantityReceived − Σ existing credit-line quantity over memos status != 'Voided' (Draft counts — comment why). Reject any requested quantity above it; reject when the header is Draft/Cancelled.
b. getNextSequence(trx, "creditMemo", companyId); compute per-line amount = quantity × line.unitPrice × (1 − restockFeePercent), restockFee = quantity × unitPrice × restockFeePercent; memo amount = Σ line amounts rounded once at the currency's decimalPlaces via round(total, currency.decimalPlaces) from @carbon/utils (settlement boundary — numeric-precision rule; fetch decimals from the currency table by the RMA's currencyCode).
c. Insert one memo: { memoId: <sequence>, direction: "Credit", status: "Draft", customerId, currencyCode, exchangeRate, memoDate: today, salesReturnOrderId, companyId, createdBy } (match memoValidator/upsertMemo columns exactly — read x+/credits+/new.tsx for required fields), then the salesReturnOrderCreditLine rows with the stored restockFee.
d. Return the memo id.
3. $id.credit.tsx (POST-only, create: "invoicing" — the same permission as x+/credits+/new.tsx): validate salesReturnOrderCreditValidator, call the service in try/catch, throw redirect to the memo detail (/x/credits/{memoId} path helper) with a success flash.
4. SalesReturnOrderCreditModal: per-line rows (item, received, already credited, quantity input defaulted to received − credited, fee preview line, line total) + grand total — quantities via useQuantityFormatter, money via useCurrencyFormatter (no inline fraction digits); submits to $id.credit. Launch from the header's "Issue Credit" button (visible when any creditable quantity > 0, gated permissions.can("create", "invoicing")).
5. Posting/voiding stays entirely in x+/credits+ — zero changes there.
Verify:
pnpm exec turbo run typecheck --filter=erp
pnpm exec biome check packages/database/supabase/functions/post-memo --reporter=summary
# Expected: exit 0 / 0 errors
Out of scope: credit application to invoices (existing payment flow untouched); AP memo columns beyond the branch above (Task 29 reuses this branch).
Task 20: Replacement sales order
Depends on: Tasks 8, 15 Files:
- Modify:
apps/erp/app/modules/sales/sales.service.ts—createReplacementSalesOrder - Create:
apps/erp/app/routes/x+/sales-return-order+/$id.replacement.tsx - Copy from (precedent):
insertSalesOrder(sales.service.ts:5180— creation + rollback-by-delete),resolvePrice(:2064),upsertSalesOrderLineusage inx+/sales-order+/$orderId.new.tsx
Steps:
createReplacementSalesOrder(client, { salesReturnOrderId, companyId, companyGroupId, userId })(app-side — drift #15): guardreplacementSalesOrderIdis null (one replacement per RMA; re-invoke returns the existing id);insertSalesOrderwith the RMA's customer/location/currency andcustomerReference = salesReturnOrder.salesReturnOrderId; for each RMA line insert asalesOrderLine(saleQuantity = quantity,unitPricefromresolvePrice(client, companyId, { customerId, itemId, quantity })— the spec's user-adjusts-for-warranty happens on the draft SO afterwards); on any line failuredeleteSalesOrderthe new order and return the error (the insertSalesOrder rollback pattern); finally setsalesReturnOrder.replacementSalesOrderId.$id.replacement.tsx(POST-only,{ create: "sales" }): call service,throw redirect(path.to.salesOrder(newId))with success flash.- Header button "Create Replacement Order" (hidden once
replacementSalesOrderIdset — then show a link chip to the SO instead; the SO side needs no column, the link lives on the RMA per spec).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: replacement PO (Task 29); pricing UI on the draft SO.
Task 21: Issue association type salesReturnOrderLines
Depends on: Task 4 Files:
- Modify:
apps/erp/app/modules/quality/quality.models.ts(nonConformanceAssociationTypearray at:71;issueAssociationValidatorrefine list:167-186) - Modify:
apps/erp/app/modules/quality/quality.service.ts(getIssueAssociations:485,deleteIssueAssociation:86) - Modify:
apps/erp/app/routes/x+/issue+/$id.association.new.tsx(insert switch:47),$id.tsx(tree literal:110-207) - Modify:
apps/erp/app/modules/quality/ui/Issue/IssueAssociations.tsx(getAssociationIcon:299,getAssociationLink:1043,NewAssociationModalswitch:981, newNewSalesReturnOrderLineAssociationform component) - Copy from (precedent): the
receiptLineswiring in each of those exact places (the 9-touchpoint map)
Steps:
- Add
"salesReturnOrderLines"to the const array; it needslineId(a line-level association) so it does NOT join the.refineexempt list. getIssueAssociations: add the query to thePromise.all(nonConformanceSalesReturnOrderLineselect with denormalizedsalesReturnOrderId/salesReturnOrderReadableId), destructure + map to the{id, documentId, documentReadableId, documentLineId, type}child shape.deleteIssueAssociation: new case.$id.association.new.tsx: new insert case resolving the line'ssalesReturnOrderreadable id for the denormalized columns (thereceiptLinescase shape).IssueAssociations.tsx: icon (a return-arrow lucide icon), link →path.to.salesReturnOrderLine(documentId, documentLineId),NewSalesReturnOrderLineAssociationform (customer-agnostic: an RMA combobox then a line combobox — mirrorNewReceiptLineAssociation's two-step fetch), modal switch entry.$id.tsxtree literal: node{ key: "salesReturnOrderLines", name: "RMA Line", pluralName: "RMA Lines", module: "sales" }— the per-nodemodulegates visibility on sales permissions (existingIssueAssociationItembehavior).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: purchaseReturnOrderLines (Task 30); the escalation action (Task 22).
Task 22: RMA dispositions (Use As Is, set-disposition, escalate to Issue)
Depends on: Tasks 18, 21 Files:
- Create:
apps/erp/app/routes/x+/sales-return-order+/$id.$lineId.disposition.tsx,$id.$lineId.issue.tsx - Modify:
apps/erp/app/modules/sales/sales.service.ts—setSalesReturnOrderLineDisposition - Copy from (precedent):
x+/inspection+/$id.reject.tsx(the create-Issue-from-elsewhere flow:insertIssue→ overwrite item row → pre-associate →createedge fnnonConformanceTasks→ compensating delete);quality-disposition.server.ts:928-989(Disposition activity + entity flip shape)
Steps:
setSalesReturnOrderLineDisposition(client, { lineId, companyId, disposition, userId }): guardquantityReceived > 0; set the line'sdisposition. For"Use As Is": flip this RMA's received entities for the line (entities insalesReturnOrderLineTrackedEntitywhose status isOn Hold— plus blind entities discovered via the receipt-line attribute) toAvailable, and write onetrackedActivity{ type: "Disposition", sourceDocument: "Sales Return Order", sourceDocumentId, sourceDocumentReadableId }+trackedActivityInputrows (the quality-disposition shape). Untracked stock: no entity work, no GL (already on hand). For"Return to Customer": just record it (the shipment executes it — Task 23)."Pending"resets."Scrap"/"Rework"are NOT set here — they arrive via the Issue route below.$id.$lineId.disposition.tsx(POST-only,{ update: "sales" }): validatesalesReturnOrderDispositionValidator; rejectScrap/Reworkwith a flash pointing at "Escalate to Issue"; call the service.$id.$lineId.issue.tsx(POST-only,{ create: "quality" }— creating an Issue is a quality act): the inspection-reject sequence adapted:insertIssue(serviceRole, { items: [line.itemId], … })with a description naming the RMA + reason; overwrite the seedednonConformanceItemrow with the line's received quantity + submitted disposition (ScraporRework); pre-associatenonConformanceSalesReturnOrderLine(with denormalized ids) +nonConformanceTrackedEntity+nonConformanceItemTrackedEntityrows for the line's received entities; invokecreate{ type: "nonConformanceTasks" }, deleting the NCR on failure (the compensating-delete precedent); set the RMA line'sdispositionto the submitted value;throw redirect(path.to.issue(ncrId)). The Issue owns approvals + GL from here (post-nonconformanceviacloseIssue) — the RMA posts nothing.- Line form (Task 16's) wires the disposition select to route 2 and an "Escalate to Issue" button (Scrap/Rework choice) to route 3.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: customer-RMA-FROM-Issue (explicitly deferred); post-nonconformance changes (none needed — it's caller-driven); Return-to-Customer execution (Task 23).
Task 23: Return-to-customer shipment (create + post-shipment + UI activation)
Depends on: Tasks 10, 22 Files:
- Modify:
packages/database/supabase/functions/create/index.ts— caseshipmentFromSalesReturnOrder - Modify:
packages/database/supabase/functions/post-shipment/index.ts— post + void cases for"Sales Return Order" - Modify:
apps/erp/app/modules/inventory/inventory.models.ts— uncomment"Sales Return Order"inshipmentSourceDocumentType(~line 296) - Modify:
apps/erp/app/modules/inventory/ui/Shipments/ShipmentForm/useShipmentForm.tsx,apps/erp/app/routes/x+/shipment+/new.tsx,$shipmentId.details.tsx— new branches - Copy from (precedent):
shipmentFromSalesOrder(create/index.ts:1891) trimmed to the Make-to-Stock path; post-shipment's Sales Order branch (:113-1243) and its void (:1792)
Steps:
createcaseshipmentFromSalesReturnOrder: lines from RMA lines withdisposition = 'Return to Customer'andquantityReceived > 0;shippedQuantity= received quantity not yet shipped back (compute from prior posted shipments of this source — queryshipmentLineof posted shipments with thissourceDocumentIdgrouped bylineId, one query); headersourceDocument: "Sales Return Order",sourceDocumentId,sourceDocumentReadableId,customerId,locationId.unitPrice: 0(no revenue — it's a rejected claim going home).post-shipmentpost case"Sales Return Order": negativeitemLedger(entryType "Negative Adjmt.",documentType "Sales Shipment"— goods leaving to a customer; the return identity lives on the shipment's sourceDocument), cost consumption viacalculateCOGSper item + a consumptioncostLedgerrow (the SO-branch shape), journal DrcostOfGoodsSoldAccount/ CrresolveInventoryAccountat consumed cost, headersourceType: "Sales Shipment"; tracked entities →Consumed+trackedActivity { type: "Return Shipment", … }withtrackedActivityInputrows; NO salesOrderLine/RMA quantity bumps and NO RMA status change (dispositions already recorded; Complete's guard reads dispositions, not shipped-back counters). Shipment →Posted.- Void case: mirror the SO void (rebuild positive ledger entries, sign-flip journals, entities back to
On Hold— their pre-shipment RMA state, NOTAvailable— activityVoid Shipment), shipment →Voided. - UI activation: uncomment the enum value;
useShipmentFormbranch listing RMAs.in("status", ["Partially Received", "Received"])having anyReturn to Customerline;new.tsx+$shipmentId.details.tsxswitch cases invokingshipmentFromSalesReturnOrder.
Verify:
pnpm exec biome check packages/database/supabase/functions/create packages/database/supabase/functions/post-shipment --reporter=summary
pnpm exec turbo run typecheck --filter=erp
# Expected: 0 errors / exit 0
Out of scope: the purchase-return shipment branch (Task 25 — keep the cases separate even though both are in post-shipment); packing-slip PDF changes.
Task 24: purchasing.models.ts + purchasing.service.ts — supplier returns
Depends on: Task 4 Files:
- Modify:
apps/erp/app/modules/purchasing/purchasing.models.ts,purchasing.service.ts, module barrelindex.ts - Copy from (precedent): Tasks 6-8's sales twins;
insertPurchaseOrder(purchasing.service.ts:1395);getTrackedEntitiesByMakeMethodIdattribute pattern
Steps:
- Models:
purchaseReturnOrderStatusType(six values),isPurchaseReturnOrderLocked,purchaseReturnOrderValidator(supplier fields +supplierReference; NO disposition),purchaseReturnOrderLineValidator(linkspurchaseOrderLineId/receiptLineId/purchaseInvoiceLineId; quantities/unitPrice documented inventory-UOM always),purchaseReturnOrderCreditValidator. - Service mirrors of Tasks 7-8 (
getPurchaseReturnOrderson the view,insertPurchaseReturnOrderwithget_next_sequence("purchaseReturnOrder"), lines CRUD, tracked-entity junction setter, related-panel reads — shipments viashipment.sourceDocumentId, credits viamemo.purchaseReturnOrderId): getReturnableLinesForSupplier(client, companyId, supplierId, { purchaseOrderId? })— source lineage per spec: base rows are POSTEDreceiptLines of receipts whosesourceDocument = 'Purchase Order'and whose PO belongs to the supplier; quantities already in inventory units. Reversible =receivedQuantity− already authorized (aggregate overpurchaseReturnOrderLine.receiptLineId, non-Cancelled headers, one.in()).unitPrice(commercial basis) from the linked PO line:unitPrice / conversionFactor→ inventory-unit price, converted ONCE here at authoring (drift-verified:purchaseOrderLine.unitPriceis per purchase unit). Rows carry all three link ids where resolvable. An invoice-only pick that resolves to no posted receipt allocation is returned flaggedblind: true(explicit blind-return fallback per spec).getReturnableEntitiesForSupplier(client, companyId, supplierId, itemId)—trackedEntitywhereattributes->>Supplier = supplierId(written by the receipt tracking functions),.eq("itemId", itemId),.eq("status", "Available").confirmPurchaseReturnOrder(db, …)— the Task 8 confirm with the cap base = linkedreceiptLine.receivedQuantity(or PO linequantityReceived × conversionFactorwhen only PO-linked) minus other non-cancelled authorizations;cancelPurchaseReturnOrder(no posted/pending shipment,quantityShipped = 0);completePurchaseReturnOrder(every non-closed linequantityShipped >= quantity— no disposition guard, there is no disposition);shortClosePurchaseReturnOrderLine;getCreditableQuantitiesForPurchaseReturn(shipped − credited over non-voided memos).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: routes/UI (Tasks 26-28); edge functions (Task 25).
Task 25: create + post-shipment — Purchase Return Order branches
Depends on: Tasks 4, 24 Files:
- Modify:
packages/database/supabase/functions/create/index.ts— caseshipmentFromPurchaseReturnOrder - Modify:
packages/database/supabase/functions/post-shipment/index.ts— post + void cases for"Purchase Return Order" - Copy from (precedent): Task 23's cases; the SO branch's
calculateCOGScall + journal pairing;post-receipt's GRNI journal side (goodsReceivedNotInvoicedAccountusage)
Steps:
createcaseshipmentFromPurchaseReturnOrder: lines from open return lines (!closedComplete,quantity − quantityShipped > 0), quantities in inventory units (conversionFactor 1 on the shipment line — conversion already happened at authoring); headersourceDocument: "Purchase Return Order",supplierId, location from header.post-shipmentpost case"Purchase Return Order":- negative
itemLedger(entryType "Negative Adjmt.",documentType "Purchase Return Shipment"— the pre-existing unused value), per tracked/untracked site shape; - cost:
calculateCOGS(trx, { itemId, quantity, companyId })per item (standard carried-cost consumption — no policy choice) + one consumptioncostLedgerrow per item withitemLedgerType: "Purchase",documentType: "Purchase Return Shipment"; - journal: Cr
resolveInventoryAccount(...)/ DraccountDefaults.goodsReceivedNotInvoicedAccountat consumed cost (reverses the receipt posting), headersourceType: "Purchase Return Shipment". The credit-vs-cost delta topurchaseVarianceAccounthappens at MEMO POSTING time, not here — the shipment relieves at cost only; note this in a comment (post-memo's GRNI reason leg vs the shipment's GRNI debit nets the GRNI account; any residual is the variance, visible on GRNI until credited — matching how invoice-vs-receipt variances already live on GRNI); - tracked entities →
Consumed+ activity{ type: "Return Shipment", sourceDocument: "Shipment", … }with input links; partial-batch quantities go throughbuildBatchSplitRecordsexactly as the SO branch does; purchaseReturnOrderLine.quantityShippedbump + header ladder (Confirmed → Partially Shipped → Shipped, skippingclosedCompletelines); shipment →Posted.
- negative
- Void case: mirror Task 23's void — positive ledger rebuild, journal sign-flip,
quantityShippedrollback + ladder recompute, entities back toAvailable(their pre-shipment state — they were on-hand stock),Void Shipmentactivity, shipment →Voided.
Verify:
pnpm exec biome check packages/database/supabase/functions/create packages/database/supabase/functions/post-shipment --reporter=summary
# Expected: 0 errors
pnpm exec turbo run typecheck --filter=@carbon/database
# Expected: exit 0
Out of scope: GRNI/variance journal at memo posting (Task 19's branch already routes the memo's reason leg to GRNI; no further GL work); sales-side branches.
Task 26: Purchasing paths, nav, list route + table
Depends on: Tasks 13 (path.ts conventions), 24 Files:
- Modify:
apps/erp/app/utils/path.ts,apps/erp/app/modules/purchasing/ui/usePurchasingSubmodules.tsx - Create:
apps/erp/app/routes/x+/purchasing+/supplier-returns.tsx - Create:
apps/erp/app/modules/purchasing/ui/PurchaseReturnOrders/PurchaseReturnOrdersTable.tsx,PurchaseReturnOrderStatus.tsx,index.ts - Copy from (precedent): Task 14's sales twins;
x+/purchasing+/orders.tsx
Steps:
path.ts: the full purchase-return helper set mirroring Task 13 step 1 (purchaseReturnOrders: ${x}/purchasing/supplier-returns, detail tree under${x}/purchase-return-order/…,file.purchaseReturnOrder(id)).- Nav:
Managegroup item{ name: t\Supplier Returns`, to: path.to.purchaseReturnOrders, icon: …, table: "purchaseReturnOrder" }`. - List route (loader
{ view: "purchasing" }) + table mirroring Task 14 (supplier viauseSuppliers()store, shipped progress from the view'squantityShipped).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: detail tree (Task 28).
Task 27: Supplier return PDF
Depends on: Tasks 17 (template plumbing exists), 24 Files:
- Modify:
packages/documents/src/template/schema.ts,template/defaults.ts,pdf/index.ts,pdf/preview-documents.tsx - Create:
packages/documents/src/pdf/PurchaseReturnOrderPDF.tsx,pdf/blocks/purchaseReturnOrder/*,pdf/purchaseReturnOrder.samples.ts - Create:
apps/erp/app/routes/file+/purchase-return-order+/$id[.]pdf.tsx - Copy from (precedent): Task 17's files +
file+/purchase-order+/$orderId[.]pdf.tsx(view permissionpurchasing)
Steps:
- Add
purchaseReturnOrderto the template type schema + defaults + catalog ("Supplier Return"); block set mirrors Task 17 with supplier parties,supplierReference(the supplier's RMA number) prominent in the header block, and ship-to = supplier address. - Driver + registry + samples +
DOCUMENT_PDFSentry; file route loader{ view: "purchasing" }.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/documents && pnpm exec turbo run typecheck --filter=erp
# Expected: both exit 0
Out of scope: email; ZPL.
Task 28: Supplier return detail tree + lines + status actions + shipments UI activation
Depends on: Tasks 24, 25, 26, 27 Files:
- Create:
apps/erp/app/routes/x+/purchase-return-order+/—_layout.tsx,new.tsx,update.tsx,$id.tsx,$id._index.tsx,$id.details.tsx,$id.delete.tsx,$id.new.tsx,$id.$lineId.details.tsx,$id.$lineId.delete.tsx,$id.confirm.tsx,$id.status.tsx,$id.$lineId.receiving.tsx - Create:
apps/erp/app/modules/purchasing/ui/PurchaseReturnOrders/—PurchaseReturnOrderForm.tsx,PurchaseReturnOrderHeader.tsx,PurchaseReturnOrderProperties.tsx,PurchaseReturnOrderExplorer.tsx,PurchaseReturnOrderLineForm.tsx,ReturnableReceiptLinesModal.tsx - Modify:
apps/erp/app/modules/inventory/inventory.models.ts(uncomment"Purchase Return Order"inshipmentSourceDocumentType),useShipmentForm.tsx,x+/shipment+/new.tsx,$shipmentId.details.tsx(new branches) - Modify:
apps/erp/app/modules/shared/shared.server.ts—generateAndAttachPurchaseReturnOrderPdf - Copy from (precedent): Tasks 15/16/18 sales twins (
handle.module: "purchasing", permissionspurchasing_*); Task 12's activation shape for the shipment side
Steps:
- Detail tree + forms: mirror Tasks 15-16 with supplier selectors (
Supplier/SupplierLocation/SupplierContactfrom~/components/Form),supplierReferenceinput, line form with the receipt-lineage picker (ReturnableReceiptLinesModalfed bygetReturnableLinesForSupplier; blind rows visibly flagged), entity picker fed bygetReturnableEntitiesForSupplier, NO disposition field anywhere. - Status actions:
$id.confirm.tsx(Kysely confirm +generateAndAttachPurchaseReturnOrderPdf),$id.status.tsx(Cancelled/Completed only),$id.$lineId.receiving.tsxshort-close — the Task 18 shapes withupdate: "purchasing". - Shipments UI activation: uncomment
"Purchase Return Order";useShipmentFormbranch listing returns.in("status", ["Confirmed", "Partially Shipped"]); the two route switches invokingshipmentFromPurchaseReturnOrder. - Related panels on
$id.details.tsx: Shipments (bysourceDocumentId), Credits (memo.purchaseReturnOrderId), linked Issues (vianonConformancePurchaseReturnOrderLine).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: credit/replacement actions (Task 29); the quality bridge (Task 30).
Task 29: Supplier credit + replacement purchase order
Depends on: Tasks 19 (post-memo branch already live), 24, 28 Files:
- Modify:
apps/erp/app/modules/purchasing/purchasing.service.ts—createPurchaseReturnOrderCredit,createReplacementPurchaseOrder - Create:
apps/erp/app/routes/x+/purchase-return-order+/$id.credit.tsx,$id.replacement.tsx - Create:
apps/erp/app/modules/purchasing/ui/PurchaseReturnOrders/PurchaseReturnOrderCreditModal.tsx - Copy from (precedent): Task 19's service/route/modal (direction-flipped);
insertPurchaseOrder(purchasing.service.ts:1395) + supplierPart pricing read (PurchaseOrderLineForm.tsx:351-418logic, done server-side viagetSupplierPartsfromitems.service.ts:2086)
Steps:
createPurchaseReturnOrderCredit— Task 19's transaction with: cap =quantityShipped− credited (non-voided); memo{ direction: "Credit", supplierId, purchaseReturnOrderId, … }; sequence"creditMemo"(AP credit memos use the same sequence the credits UI picks for direction Credit — verify againstx+/credits+/new.tsx's direction→sequence mapping and use exactly what it uses for an AP credit; if it usesdebitMemofor supplier-side credits, STOP and report the naming mismatch rather than guessing).restockFeePercentreduces our credit (supplier-charged fee).$id.credit.tsx(create: "invoicing") + modal — Task 19 mirrors.post-memoalready routes the reason leg to GRNI via Task 19's branch — no edge-function work here.createReplacementPurchaseOrder(client, …): guard single replacement;insertPurchaseOrderwith supplier/currency from the return; lines priced from the linkedpurchaseOrderLine.unitPrice(purchase-UOM price + the line's own conversionFactor/UOM copied) elsesupplierPart.unitPrice; rollback-by-delete; setreplacementPurchaseOrderId.$id.replacement.tsx(create: "purchasing") redirects to the new PO.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: memo posting/voiding UI; AP payment application.
Task 30: Issue association purchaseReturnOrderLines + quality bridge
Depends on: Tasks 21 (touchpoint map), 24, 25, 28 Files:
- Modify: the Task 21 file set (quality.models/service,
$id.association.new.tsx,$id.tsx,IssueAssociations.tsx) — second association typepurchaseReturnOrderLines - Modify:
apps/erp/app/modules/quality/quality-disposition.server.ts—closeIssueguard + write-off reduction + entity-flip exemption - Create:
apps/erp/app/routes/x+/issue+/$id.supplier-return.tsx - Create:
apps/erp/app/modules/quality/ui/Issue/CreateSupplierReturnModal.tsx - Modify:
apps/erp/app/modules/quality/ui/Issue/IssueHeader.tsx(or$id.details.tsxdisposition row actions — put the button whereAssociatedItemsListrenders theReturn to Supplierdisposition) - Copy from (precedent): Task 21's wiring;
x+/inspection+/$id.reject.tsx(multi-step create with compensating delete);closeIssue's blocker collection (quality-disposition.server.ts:668-708) and movement builder (:759-807)
Steps:
- Association type
purchaseReturnOrderLines: repeat Task 21's nine touchpoints withmodule: "purchasing"on the tree node. - Create Supplier Return (
$id.supplier-return.tsx, POST,{ create: "purchasing" }): a. Load the issue'snonConformanceItemrows with dispositionReturn to Supplier+ theirnonConformanceItemTrackedEntitylinks +nonConformanceSupplierrows +nonConformanceReceiptLineassociations. b. Supplier resolution (spec rule): exactly one distinctnonConformanceSupplier→ that supplier; else derive from the receipt-line associations' parent receipts'supplierId(via the receipts' source POs); multiple/none → the modal REQUIRES an explicitsupplierId+ per-row inclusion (an Issue spanning suppliers yields one draft per invocation, scoped to the chosen supplier). Validate every included row's lineage (receipt→PO→supplier, and each tracked entity'sattributes->>Supplier) against the resolved supplier; reject mismatches naming the row. c. Idempotent coverage: per disposition row, covered = Σquantityon itsnonConformancePurchaseReturnOrderLineassociation rows whose return header is notCancelled; uncovered = row quantity − covered. If nothing is uncovered for the selection → redirect to the newest linked open draft (no new rows). d. Create the draft:insertPurchaseReturnOrder(Draft, resolved supplier) + one line per uncovered disposition row (inventory-unit quantity = uncovered;receiptLineIdfrom the association where resolvable,unitPricefrom the receipt's PO line converted once; blind otherwise) +purchaseReturnOrderLineTrackedEntitypicks (the row's entity links, capped to uncovered) + onenonConformancePurchaseReturnOrderLineassociation row per line withquantity= the covered amount it now owns (+ denormalized ids). Use sequential service calls with a compensatingdeletePurchaseReturnOrderon partial failure (the inspection-reject pattern). Redirect to the draft. closeIssuechanges (all inside the existing structure): a. New preflight + in-lock blocker: any linkedpurchaseReturnOrder(via the association rows) with statusDraft/Confirmed/Partially Shipped→ blocker "Supplier return {readableId} is open — ship, short-close, or cancel it first". b. Movement builder (:759-807): for eachReturn to Supplierrow, reduce the write-off movement bymin(row quantity, Σ over its association rows of shipped-via-return quantity)— shipped-via = for each association row, the covered quantity that the linked return line has actually shipped (allocatequantityShippedacross covering associations; with the one-association-per-line design of 2d this is simplymin(association.quantity, returnLine.quantityShipped)). Cancelled returns contribute 0 (their unshipped coverage re-enters the write-off pool automatically since blockers only count open returns). c. Entity handling: entities that a linked return SHIPPED are alreadyConsumed— exempt them from the guard-3 "must not be Consumed" check and from theRejectedflip (:974-989); they are done. Entities NOT shipped via a return keep today's behavior.- Modal + button: on the disposition row (or header) when any
Return to Supplierdisposition exists, gatedpermissions.can("create", "purchasing"); modal shows resolved supplier (or the explicit picker), per-row uncovered quantities, and linked existing returns.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: creating customer RMAs from Issues (deferred spec); post-nonconformance edge function (unchanged — the caller computes movements); changing existing Return to Supplier close behavior when NO linked return exists (must stay byte-identical).
Task 31: Docs sync + i18n translate
Depends on: Tasks 1-30 Files:
- Modify:
apps/erp/app/modules/sales/AGENTS.md,apps/erp/app/modules/purchasing/AGENTS.md,apps/erp/app/modules/quality/AGENTS.md(new tables/services/routes/bridge),packages/documents/AGENTS.mdif it enumerates document types - Modify:
packages/locale/locales/*/*.po(generated by the translate flow)
Steps:
- Update each AGENTS.md's tables/services/routes sections with the return-order additions — claims traced to the real code written above; while there, fix the stale
closeIssue-location line in quality's AGENTS.md (it moved toquality-disposition.server.ts). - Run
pnpm lingui:extract(or the repo's extract script) then invoke the/translateskill to fill the new msgstrs.
Verify:
grep -rn "salesReturnOrder" apps/erp/app/modules/sales/AGENTS.md | head -1
# Expected: at least one hit
pnpm run lint
# Expected: exit 0
Out of scope: the product docs site (docs/) — a follow-on; .claude/rules/ additions.
Task 32: Validation gates
Depends on: Task 31 Steps:
pnpm run lint— fix anything the branch introduced.- Scoped typechecks (NEVER whole-repo — it OOMs):
pnpm exec turbo run typecheck --filter=erp --filter=@carbon/database --filter=@carbon/documents. pnpm --filter erp test(coverslist-select-columns.test.tsamong others) andcd packages/database/supabase/functions && deno test shared/resolve-return-cost.test.ts shared/short-close.test.ts.pnpm run buildonly if any packages' build outputs changed (documents did — fonts/templates):pnpm --filter @carbon/documents build.
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=@carbon/database --filter=@carbon/documents && pnpm run lint && pnpm --filter erp test
# Expected: all exit 0
Out of scope: committing (per-task commits are /execute's job via /check-and-commit).
Task 33: Browser verification via /test
Depends on: Task 32
Steps: Invoke the /test skill against the running dev stack with this scenario list (maps 1:1 to the spec's acceptance criteria; needs a seeded customer+supplier with a shipped/received order — build fixtures through the UI, not raw SQL, per the lessons):
- RMA from a shipped sales order: pre-filled reversible lines; over-authorization rejected at confirm.
- Blind RMA end-to-end: create → confirm (RMA-prefixed id + PDF downloads) → receipt (source "Sales Return Order") partial →
Partially Received→ remainder →Received→ credit → posted memo →quantityCreditedupdates → void memo → reverts. - Receipt void restores quantities + status.
- Costing spot-check (accounting enabled): linked line re-enters at original cost (inspect the journal + costLedger rows via the UI/ledger screens);
inventoryValueZeroreason → 0-value layer, no journal. - Serialized round-trip: same
trackedEntityid On Hold after receipt,Return Receiptactivity visible in traceability; Use As Is → Available in on-hand; Return to Customer → shipment posts and removes it. - Scrap escalation: Issue opens pre-associated; closing it with Scrap posts the write-off (no GL from the RMA).
- Credit math: 2 of 5 received at 100 with 10% fee → Draft memo 180.
- Replacement order cross-links both documents.
- Complete blocked on Pending disposition / short quantity; line short-close unblocks.
- Supplier return from a posted PO receipt: pre-filled reversible lines; over-authorization rejected; partial ship →
Partially Shipped→Shipped; entitiesConsumedwithReturn Shipmentactivity; shipment void reverses. - Supplier credit: Draft AP memo (GRNI reason account visible after posting), linked via the return; posting/voiding updates credited quantities.
- Quality bridge: Issue with
Return to Supplier→ Create Supplier Return drafts with resolved supplier + entities; re-invoke drafts nothing new; close blocked while open; after shipping, close writes off only the remainder. - Permission spot-checks: RMA routes 403 without
sales; credit route withoutinvoicing; supplier-return routes withoutpurchasing.
Verify: the /test run report — every scenario green or explicitly triaged.
Out of scope: load/perf testing; concurrent-transaction race verification (covered by code review of the .forUpdate() transactions — a browser can't drive it deterministically).