Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat(documents): show line-item shipping cost on purchase order PDF and fix document totals (#1250)
Naveen Kashyap19cf01a12402
diff --git a/.ai/lessons.md b/.ai/lessons.mdindex 6b4ac258cf..ebefdd72ba 100644--- a/.ai/lessons.md+++ b/.ai/lessons.md@@ -528,6 +528,16 @@ Format: `Context → Problem → Rule → Applies to`**Applies to:** all `storage.from(...).upload(...)` call sites in `apps/mes` and `apps/erp`; `share+/customer.$id.$.tsx`; any new externally-shared file route.+## Document totals helpers guarded on truthy qty/price silently drop shipping- or tax-only lines++**Context:** Adding line-level shipping cost display to the purchase order PDF (`packages/documents/src/pdf/blocks/purchaseOrder/`). A PO line with a 0.00 unit price and a 30.00 `supplierShippingCost` rendered Total and Subtotal as 0 on the PDF.++**Problem:** The totals helpers in `packages/documents/src/utils/` (`purchase-order.ts` `getLineTotal`/`getTotal`, and the same pattern in `sales-order.ts`/`sales-invoice.ts` `getLineSubtotal`/`getLineTaxableSubtotal`) wrapped the whole formula in `if (line?.qty && line?.unitPrice)` — so any line whose value was only shipping, add-on, or tax contributed 0 to the document, even though those amounts were displayed elsewhere on the page and included in posting math.++**Rule:** Compute document money formulas unconditionally with `?? 0` per term — never gate the whole formula on one term being truthy. Also keep a document's Subtotal semantics identical to that module's in-app summary (PO PDF Subtotal = qty × price + line shipping, matching `PurchaseOrderSummary.tsx`), and lock the helpers with unit tests (`packages/documents/src/utils/document-totals.test.ts`).++**Applies to:** `packages/documents/src/utils/{purchase-order,sales-order,sales-invoice,quote}.ts`, PDF Summary/LineItems blocks, `email/{PurchaseOrder,SalesOrder,SalesInvoice}Email.tsx`, and any new document totals code.+## Loader-computed "current unit" must match the component's, or per-unit attribution credits the wrong unit**Context:** The MES assembly view resolves the unit on screen twice: the loader (`apps/mes/app/routes/x+/assembly.$operationId.tsx`) computes `unitIndex` from `?unit`/`?trackedEntityId`, and `AssemblyView.currentUnitIndex` computes it again client-side. Per-unit material issue attribution (batch parents) was moved into the loader, keyed off the loader's `unitIndex`.diff --git a/packages/documents/src/email/PurchaseOrderEmail.tsx b/packages/documents/src/email/PurchaseOrderEmail.tsxindex 6a2f506a95..00b2b0ee8c 100644--- a/packages/documents/src/email/PurchaseOrderEmail.tsx+++ b/packages/documents/src/email/PurchaseOrderEmail.tsx@@ -283,7 +283,10 @@ const PurchaseOrderEmail = ({<Column className="border-l border-gray-200 h-12"></Column><Column className="w-[90px] pr-5"><Text className="text-base font-semibold whitespace-nowrap">- {formatter.format(getTotal(purchaseOrderLines))}+ {formatter.format(+ getTotal(purchaseOrderLines) ++ (purchaseOrder.supplierShippingCost ?? 0)+ )}</Text></Column></Row>diff --git a/packages/documents/src/pdf/blocks/purchaseOrder/LineItemsBlock.tsx b/packages/documents/src/pdf/blocks/purchaseOrder/LineItemsBlock.tsxindex 0eed6155ef..8bcf80087f 100644--- a/packages/documents/src/pdf/blocks/purchaseOrder/LineItemsBlock.tsx+++ b/packages/documents/src/pdf/blocks/purchaseOrder/LineItemsBlock.tsx@@ -132,6 +132,12 @@ export function LineItemsBlock({/></View>)}+ {(line.supplierShippingCost ?? 0) > 0 && (+ <Text style={tw("text-[7px] text-gray-600 mt-0.5")}>+ Shipping:{" "}+ {numberFormatter.format(line.supplierShippingCost ?? 0)}+ </Text>+ )}</View><Text style={tw("w-[8%] text-center text-gray-600")}>{line.purchaseOrderLineType === "Comment"diff --git a/packages/documents/src/pdf/blocks/purchaseOrder/SummaryBlock.tsx b/packages/documents/src/pdf/blocks/purchaseOrder/SummaryBlock.tsxindex 5663964b76..1e6a1e209e 100644--- a/packages/documents/src/pdf/blocks/purchaseOrder/SummaryBlock.tsx+++ b/packages/documents/src/pdf/blocks/purchaseOrder/SummaryBlock.tsx@@ -17,6 +17,13 @@ export function SummaryBlock({const taxLabel = block.options?.taxLabel?.trim() || "Tax";const shippingCost = purchaseOrder?.supplierShippingCost ?? 0;+ const subtotal = purchaseOrderLines.reduce(+ (acc, line) =>+ acc ++ (line.purchaseQuantity ?? 0) * (line.supplierUnitPrice ?? 0) ++ (line.supplierShippingCost ?? 0),+ 0+ );const taxAmount = purchaseOrderLines.reduce((acc, line) => acc + (line.supplierTaxAmount ?? 0),0@@ -34,14 +41,7 @@ export function SummaryBlock({Subtotal ({currencyCode})</Text><Text style={tw("w-[13%] text-center text-gray-800")}>- {numberFormatter.format(- purchaseOrderLines.reduce((sum, line) => {- if (line?.purchaseQuantity && line?.supplierUnitPrice) {- return sum + line.purchaseQuantity * line.supplierUnitPrice;- }- return sum;- }, 0)- )}+ {numberFormatter.format(subtotal)}</Text></View>diff --git a/packages/documents/src/pdf/purchaseOrder.samples.ts b/packages/documents/src/pdf/purchaseOrder.samples.tsindex 1d280cecd2..22e28bc30b 100644--- a/packages/documents/src/pdf/purchaseOrder.samples.ts+++ b/packages/documents/src/pdf/purchaseOrder.samples.ts@@ -43,6 +43,7 @@ export const SAMPLE_PURCHASE_ORDER = {purchaseQuantity: 100,purchaseUnitOfMeasureCode: "EA",supplierUnitPrice: 5.22,+ supplierShippingCost: 5,supplierTaxAmount: 78.3,taxPercent: 0.15,requiredDate: "2026-06-20",diff --git a/packages/documents/src/utils/document-totals.test.ts b/packages/documents/src/utils/document-totals.test.tsnew file mode 100644index 0000000000..ce6ead41c3--- /dev/null+++ b/packages/documents/src/utils/document-totals.test.ts@@ -0,0 +1,99 @@+import { describe, expect, it } from "vitest";+import {+ getLineTotal as getPurchaseOrderLineTotal,+ getTotal as getPurchaseOrderTotal+} from "./purchase-order";+import {+ getLineSubtotal as getSalesInvoiceLineSubtotal,+ getLineTaxableSubtotal as getSalesInvoiceLineTaxableSubtotal+} from "./sales-invoice";+import {+ getLineSubtotal as getSalesOrderLineSubtotal,+ getLineTaxableSubtotal as getSalesOrderLineTaxableSubtotal+} from "./sales-order";++describe("purchase order totals", () => {+ it("computes qty * price + shipping + tax", () => {+ const line = {+ purchaseQuantity: 100,+ supplierUnitPrice: 5.22,+ supplierShippingCost: 5,+ supplierTaxAmount: 78.3+ } as never;+ expect(getPurchaseOrderLineTotal(line)).toBeCloseTo(605.3);+ });++ it("does not zero out a shipping-only line (zero unit price)", () => {+ const line = {+ purchaseQuantity: 1,+ supplierUnitPrice: 0,+ supplierShippingCost: 30,+ supplierTaxAmount: 0+ } as never;+ expect(getPurchaseOrderLineTotal(line)).toBe(30);+ });++ it("does not zero out a tax-only line (null price)", () => {+ const line = {+ purchaseQuantity: null,+ supplierUnitPrice: null,+ supplierShippingCost: null,+ supplierTaxAmount: 12.5+ } as never;+ expect(getPurchaseOrderLineTotal(line)).toBe(12.5);+ });++ it("sums line totals across lines, including shipping-only lines", () => {+ const lines = [+ {+ purchaseQuantity: 1,+ supplierUnitPrice: 1,+ supplierShippingCost: 5,+ supplierTaxAmount: 0.6+ },+ {+ purchaseQuantity: 1,+ supplierUnitPrice: 0,+ supplierShippingCost: 30,+ supplierTaxAmount: 0+ }+ ] as never[];+ expect(getPurchaseOrderTotal(lines as never)).toBeCloseTo(36.6);+ });+});++describe("sales order line subtotals", () => {+ it("does not zero out a shipping-only line", () => {+ const line = {+ saleQuantity: 1,+ convertedUnitPrice: 0,+ convertedShippingCost: 30+ } as never;+ expect(getSalesOrderLineSubtotal(line)).toBe(30);+ expect(getSalesOrderLineTaxableSubtotal(line)).toBe(30);+ });++ it("includes add-ons alongside price", () => {+ const line = {+ saleQuantity: 2,+ convertedUnitPrice: 10,+ convertedAddOnCost: 3,+ convertedNonTaxableAddOnCost: 2,+ convertedShippingCost: 5+ } as never;+ expect(getSalesOrderLineSubtotal(line)).toBe(30);+ expect(getSalesOrderLineTaxableSubtotal(line)).toBe(28);+ });+});++describe("sales invoice line subtotals", () => {+ it("does not zero out a shipping-only line", () => {+ const line = {+ quantity: 1,+ convertedUnitPrice: 0,+ convertedShippingCost: 30+ } as never;+ expect(getSalesInvoiceLineSubtotal(line)).toBe(30);+ expect(getSalesInvoiceLineTaxableSubtotal(line)).toBe(30);+ });+});diff --git a/packages/documents/src/utils/purchase-order.ts b/packages/documents/src/utils/purchase-order.tsindex c846532889..b0297e8df3 100644--- a/packages/documents/src/utils/purchase-order.ts+++ b/packages/documents/src/utils/purchase-order.ts@@ -44,30 +44,15 @@ export function getLineDescriptionDetails(export function getLineTotal(line: Database["public"]["Views"]["purchaseOrderLines"]["Row"]) {- if (line?.purchaseQuantity && line?.supplierUnitPrice) {- return (- line.purchaseQuantity * line.supplierUnitPrice +- (line.supplierShippingCost ?? 0) +- (line.supplierTaxAmount ?? 0)- );- }-- return 0;+ return (+ (line?.purchaseQuantity ?? 0) * (line?.supplierUnitPrice ?? 0) ++ (line?.supplierShippingCost ?? 0) ++ (line?.supplierTaxAmount ?? 0)+ );}export function getTotal(lines: Database["public"]["Views"]["purchaseOrderLines"]["Row"][]) {- let total = 0;-- lines.forEach((line) => {- if (line?.purchaseQuantity && line?.supplierUnitPrice) {- total +=- line.purchaseQuantity * line.supplierUnitPrice +- (line?.supplierShippingCost ?? 0) +- (line?.supplierTaxAmount ?? 0);- }- });-- return total;+ return lines.reduce((total, line) => total + getLineTotal(line), 0);}diff --git a/packages/documents/src/utils/sales-invoice.ts b/packages/documents/src/utils/sales-invoice.tsindex 957e9aa388..1fb307d35c 100644--- a/packages/documents/src/utils/sales-invoice.ts+++ b/packages/documents/src/utils/sales-invoice.ts@@ -32,28 +32,22 @@ export function getLineDescriptionDetails(export function getLineSubtotal(line: Database["public"]["Views"]["salesInvoiceLines"]["Row"]) {- if (line?.quantity && line?.convertedUnitPrice) {- return (- line.quantity * line.convertedUnitPrice +- (line.convertedAddOnCost ?? 0) +- (line.convertedNonTaxableAddOnCost ?? 0) +- (line.convertedShippingCost ?? 0)- );- }- return 0;+ return (+ (line?.quantity ?? 0) * (line?.convertedUnitPrice ?? 0) ++ (line?.convertedAddOnCost ?? 0) ++ (line?.convertedNonTaxableAddOnCost ?? 0) ++ (line?.convertedShippingCost ?? 0)+ );}export function getLineTaxableSubtotal(line: Database["public"]["Views"]["salesInvoiceLines"]["Row"]) {- if (line?.quantity && line?.convertedUnitPrice) {- return (- line.quantity * line.convertedUnitPrice +- (line.convertedAddOnCost ?? 0) +- (line.convertedShippingCost ?? 0)- );- }- return 0;+ return (+ (line?.quantity ?? 0) * (line?.convertedUnitPrice ?? 0) ++ (line?.convertedAddOnCost ?? 0) ++ (line?.convertedShippingCost ?? 0)+ );}export function getLineTaxesAndFees(diff --git a/packages/documents/src/utils/sales-order.ts b/packages/documents/src/utils/sales-order.tsindex df5cea09c3..8a20e7e9cf 100644--- a/packages/documents/src/utils/sales-order.ts+++ b/packages/documents/src/utils/sales-order.ts@@ -43,28 +43,22 @@ export function getLineDescriptionDetails(export function getLineSubtotal(line: Database["public"]["Views"]["salesOrderLines"]["Row"]) {- if (line?.saleQuantity && line?.convertedUnitPrice) {- return (- line.saleQuantity * line.convertedUnitPrice +- (line.convertedAddOnCost ?? 0) +- (line.convertedNonTaxableAddOnCost ?? 0) +- (line.convertedShippingCost ?? 0)- );- }- return 0;+ return (+ (line?.saleQuantity ?? 0) * (line?.convertedUnitPrice ?? 0) ++ (line?.convertedAddOnCost ?? 0) ++ (line?.convertedNonTaxableAddOnCost ?? 0) ++ (line?.convertedShippingCost ?? 0)+ );}export function getLineTaxableSubtotal(line: Database["public"]["Views"]["salesOrderLines"]["Row"]) {- if (line?.saleQuantity && line?.convertedUnitPrice) {- return (- line.saleQuantity * line.convertedUnitPrice +- (line.convertedAddOnCost ?? 0) +- (line.convertedShippingCost ?? 0)- );- }- return 0;+ return (+ (line?.saleQuantity ?? 0) * (line?.convertedUnitPrice ?? 0) ++ (line?.convertedAddOnCost ?? 0) ++ (line?.convertedShippingCost ?? 0)+ );}export function getLineTaxesAndFees(