Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: Fix invoice settlement source amount fallback

Samay Gandhi092eeee63810
diff --git a/packages/database/supabase/migrations/20260916143022_invoice-settlement-source-amount-fallback.sql b/packages/database/supabase/migrations/20260916143022_invoice-settlement-source-amount-fallback.sql
new file mode 100644
index 0000000000..899184b32b
--- /dev/null
+++ b/packages/database/supabase/migrations/20260916143022_invoice-settlement-source-amount-fallback.sql
@@ -0,0 +1,253 @@
+-- ============================================================
+-- Fix invoice settlement source amount fallback
+--
+-- The views in 20260908030026_accounting_balances_and_reports.sql
+-- use COALESCE(sourceAmount, 0), which defaults legacy rows (where
+-- sourceAmount IS NULL) to $0 settled instead of their appliedAmount.
+-- This caused COD invoices to flip to Overdue after the posting day.
+--
+-- The fix: use COALESCE(sourceAmount, appliedAmount, 0) in the settled
+-- CTE so that legacy rows fall back to their stored appliedAmount.
+-- ============================================================
+
+CREATE OR REPLACE VIEW "salesInvoices" WITH(SECURITY_INVOKER=true) AS
+ WITH settled AS (
+ SELECT s."targetSalesInvoiceId", s."companyId",
+ SUM(COALESCE(s."sourceAmount", s."appliedAmount", 0) + round(
+ (s."discountAmount" + s."writeOffAmount") * target."exchangeRate",
+ target_currency."decimalPlaces")) AS amount_document,
+ MAX(s."appliedDate") AS "lastSettlementDate"
+ FROM "invoiceSettlement" s
+ JOIN "salesInvoice" target ON target."id" = s."targetSalesInvoiceId"
+ AND target."companyId" = s."companyId"
+ LEFT JOIN "company" target_company ON target_company."id" = target."companyId"
+ LEFT JOIN "currency" target_currency ON target_currency."code" = target."currencyCode"
+ AND target_currency."companyGroupId" = target_company."companyGroupId"
+ LEFT JOIN "payment" p ON p."id" = s."paymentId" AND p."companyId" = s."companyId"
+ LEFT JOIN "memo" m ON m."id" = s."memoId" AND m."companyId" = s."companyId"
+ LEFT JOIN "payment" vp ON vp."id" = s."appliedViaPaymentId" AND vp."companyId" = s."companyId"
+ WHERE s."targetSalesInvoiceId" IS NOT NULL
+ AND ((s."paymentId" IS NOT NULL AND p."status" = 'Posted')
+ OR (s."memoId" IS NOT NULL AND m."status" = 'Posted'
+ AND (s."appliedViaPaymentId" IS NULL OR vp."status" = 'Posted')))
+ GROUP BY s."targetSalesInvoiceId", s."companyId"
+ )
+ SELECT
+ si."id",
+ si."invoiceId",
+ CASE
+ WHEN si."status" IN ('Draft','Pending','Voided','Return','Credit Note Issued') THEN si."status"::TEXT
+ WHEN si."status" = 'Paid' THEN 'Paid'
+ WHEN COALESCE(s.amount_document, 0) > 0
+ AND amounts.total_document > 0 AND remaining.amount_document <= 0 THEN 'Paid'
+ WHEN COALESCE(s.amount_document, 0) > 0 THEN 'Partially Paid'
+ WHEN si."dateDue" < CURRENT_DATE AND si."status" = 'Submitted' THEN 'Overdue'
+ ELSE si."status"::TEXT
+ END AS status,
+ si."customerId",
+ si."customerReference",
+ si."invoiceCustomerId",
+ si."invoiceCustomerLocationId",
+ si."invoiceCustomerContactId",
+ si."paymentTermId",
+ si."postingDate",
+ si."dateIssued",
+ si."dateDue",
+ CASE
+ WHEN si."status" = 'Paid' THEN si."datePaid"
+ WHEN COALESCE(s.amount_document, 0) > 0
+ AND amounts.total_document > 0 AND remaining.amount_document <= 0
+ THEN COALESCE(s."lastSettlementDate", si."datePaid")
+ ELSE si."datePaid"
+ END AS "datePaid",
+ si."locationId",
+ si."currencyCode",
+ COALESCE(sil."subtotal", 0) AS "subtotal",
+ si."totalDiscount",
+ COALESCE(sil."subtotal", 0) + COALESCE(sil."totalTax", 0) + COALESCE(ss."shippingCost", 0) AS "totalAmount",
+ COALESCE(sil."totalTax", 0) AS "totalTax",
+ CASE
+ WHEN si."status" = 'Paid' THEN 0
+ ELSE remaining.amount_document / NULLIF(si."exchangeRate", 0)
+ END AS "balance",
+ si."exchangeRate",
+ si."exchangeRateUpdatedAt",
+ si."opportunityId",
+ si."shipmentId",
+ si."assignee",
+ si."companyId",
+ si."customFields",
+ si."internalNotes",
+ si."externalNotes",
+ si."tags",
+ si."createdAt",
+ si."createdBy",
+ si."updatedAt",
+ si."updatedBy",
+ sil."thumbnailPath",
+ sil."itemType",
+ COALESCE(sil."subtotal", 0) + COALESCE(sil."totalTax", 0) + COALESCE(ss."shippingCost", 0) AS "invoiceTotal",
+ sil."lines",
+ pt."name" AS "paymentTermName",
+ si."status" AS "baseStatus"
+ FROM "salesInvoice" si
+ LEFT JOIN (
+ SELECT
+ sil."invoiceId",
+ MIN(CASE
+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
+ ELSE i."thumbnailPath"
+ END) AS "thumbnailPath",
+ SUM(
+ COALESCE(sil."quantity", 0)*COALESCE(sil."unitPrice", 0)
+ + COALESCE(sil."addOnCost", 0)
+ + COALESCE(sil."nonTaxableAddOnCost", 0)
+ + COALESCE(sil."shippingCost", 0)
+ ) AS "subtotal",
+ SUM(
+ COALESCE(sil."taxPercent", 0) * (
+ COALESCE(sil."quantity", 0)*COALESCE(sil."unitPrice", 0)
+ + COALESCE(sil."addOnCost", 0)
+ + COALESCE(sil."shippingCost", 0)
+ )
+ ) AS "totalTax",
+ MIN(i."type") AS "itemType",
+ ARRAY_AGG(
+ json_build_object(
+ 'id', sil.id,
+ 'invoiceLineType', sil."invoiceLineType",
+ 'quantity', sil."quantity",
+ 'unitPrice', sil."unitPrice",
+ 'itemId', sil."itemId"
+ )
+ ) AS "lines"
+ FROM "salesInvoiceLine" sil
+ LEFT JOIN "item" i
+ ON i."id" = sil."itemId"
+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
+ GROUP BY sil."invoiceId"
+ ) sil ON sil."invoiceId" = si."id"
+ LEFT JOIN "salesInvoiceShipment" ss ON ss."id" = si."id"
+ LEFT JOIN "paymentTerm" pt ON pt."id" = si."paymentTermId"
+ LEFT JOIN settled s ON s."targetSalesInvoiceId" = si."id" AND s."companyId" = si."companyId"
+ LEFT JOIN "company" invoice_company ON invoice_company."id" = si."companyId"
+ LEFT JOIN "currency" invoice_currency ON invoice_currency."code" = si."currencyCode"
+ AND invoice_currency."companyGroupId" = invoice_company."companyGroupId"
+ CROSS JOIN LATERAL (
+ SELECT round((COALESCE(sil."subtotal", 0) + COALESCE(sil."totalTax", 0) + COALESCE(ss."shippingCost", 0)) * si."exchangeRate", invoice_currency."decimalPlaces") AS total_document
+ ) amounts
+ CROSS JOIN LATERAL (
+ SELECT amounts.total_document - COALESCE(s.amount_document, 0) AS amount_document
+ ) remaining;
+
+
+CREATE OR REPLACE VIEW "purchaseInvoices" WITH(SECURITY_INVOKER=true) AS
+ WITH settled AS (
+ SELECT s."targetPurchaseInvoiceId", s."companyId",
+ SUM(COALESCE(s."sourceAmount", s."appliedAmount", 0) + round(
+ (s."discountAmount" + s."writeOffAmount") * target."exchangeRate",
+ target_currency."decimalPlaces")) AS amount_document,
+ MAX(s."appliedDate") AS "lastSettlementDate"
+ FROM "invoiceSettlement" s
+ JOIN "purchaseInvoice" target ON target."id" = s."targetPurchaseInvoiceId"
+ AND target."companyId" = s."companyId"
+ LEFT JOIN "company" target_company ON target_company."id" = target."companyId"
+ LEFT JOIN "currency" target_currency ON target_currency."code" = target."currencyCode"
+ AND target_currency."companyGroupId" = target_company."companyGroupId"
+ LEFT JOIN "payment" p ON p."id" = s."paymentId" AND p."companyId" = s."companyId"
+ LEFT JOIN "memo" m ON m."id" = s."memoId" AND m."companyId" = s."companyId"
+ LEFT JOIN "payment" vp ON vp."id" = s."appliedViaPaymentId" AND vp."companyId" = s."companyId"
+ WHERE s."targetPurchaseInvoiceId" IS NOT NULL
+ AND ((s."paymentId" IS NOT NULL AND p."status" = 'Posted')
+ OR (s."memoId" IS NOT NULL AND m."status" = 'Posted'
+ AND (s."appliedViaPaymentId" IS NULL OR vp."status" = 'Posted')))
+ GROUP BY s."targetPurchaseInvoiceId", s."companyId"
+ )
+ SELECT
+ pi."id",
+ pi."invoiceId",
+ pi."supplierId",
+ pi."invoiceSupplierId",
+ pi."supplierInteractionId",
+ pi."supplierReference",
+ pi."invoiceSupplierContactId",
+ pi."invoiceSupplierLocationId",
+ pi."locationId",
+ pi."postingDate",
+ pi."dateIssued",
+ pi."dateDue",
+ CASE
+ WHEN pi."status" = 'Paid' THEN pi."datePaid"
+ WHEN COALESCE(s.amount_document, 0) > 0
+ AND amounts.total_document > 0 AND remaining.amount_document <= 0
+ THEN COALESCE(s."lastSettlementDate", pi."datePaid")
+ ELSE pi."datePaid"
+ END AS "datePaid",
+ pi."paymentTermId",
+ pi."currencyCode",
+ pi."exchangeRate",
+ pi."exchangeRateUpdatedAt",
+ COALESCE(pl."subtotal", 0) AS "subtotal",
+ pi."totalDiscount",
+ (COALESCE(pl."orderTotal", 0) + COALESCE(pid."supplierShippingCost", 0) / CASE WHEN pi."exchangeRate" = 0 THEN 1 ELSE pi."exchangeRate" END) AS "totalAmount",
+ COALESCE(pl."totalTax", 0) AS "totalTax",
+ CASE
+ WHEN pi."status" = 'Paid' THEN 0
+ ELSE remaining.amount_document / NULLIF(pi."exchangeRate", 0)
+ END AS "balance",
+ pi."assignee",
+ pi."createdBy",
+ pi."createdAt",
+ pi."updatedBy",
+ pi."updatedAt",
+ pi."internalNotes",
+ pi."customFields",
+ pi."companyId",
+ pl."thumbnailPath",
+ pl."itemType",
+ COALESCE(pl."orderTotal", 0) + COALESCE(pid."supplierShippingCost", 0) / CASE WHEN pi."exchangeRate" = 0 THEN 1 ELSE pi."exchangeRate" END AS "orderTotal",
+ CASE
+ WHEN pi."status" IN ('Draft','Pending','Voided','Return','Debit Note Issued') THEN pi."status"::TEXT
+ WHEN pi."status" = 'Paid' THEN 'Paid'
+ WHEN COALESCE(s.amount_document, 0) > 0
+ AND amounts.total_document > 0 AND remaining.amount_document <= 0 THEN 'Paid'
+ WHEN COALESCE(s.amount_document, 0) > 0 THEN 'Partially Paid'
+ WHEN pi."dateDue" < CURRENT_DATE AND pi."status" = 'Open' THEN 'Overdue'
+ ELSE pi."status"::TEXT
+ END AS status,
+ pt."name" AS "paymentTermName",
+ pi."status" AS "baseStatus"
+ FROM "purchaseInvoice" pi
+ LEFT JOIN (
+ SELECT
+ pol."invoiceId",
+ MIN(CASE
+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
+ ELSE i."thumbnailPath"
+ END) AS "thumbnailPath",
+ SUM(
+ COALESCE(pol."quantity", 0)*COALESCE(pol."unitPrice", 0) + COALESCE(pol."shippingCost", 0)
+ ) AS "subtotal",
+ SUM(COALESCE(pol."taxAmount", 0)) AS "totalTax",
+ SUM(
+ COALESCE(pol."quantity", 0)*COALESCE(pol."unitPrice", 0) + COALESCE(pol."shippingCost", 0) + COALESCE(pol."taxAmount", 0)
+ ) AS "orderTotal",
+ MIN(i."type") AS "itemType"
+ FROM "purchaseInvoiceLine" pol
+ LEFT JOIN "item" i
+ ON i."id" = pol."itemId"
+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"
+ GROUP BY pol."invoiceId"
+ ) pl ON pl."invoiceId" = pi."id"
+ LEFT JOIN "paymentTerm" pt ON pt."id" = pi."paymentTermId"
+ LEFT JOIN "purchaseInvoiceDelivery" pid ON pid."id" = pi."id"
+ LEFT JOIN settled s ON s."targetPurchaseInvoiceId" = pi."id" AND s."companyId" = pi."companyId"
+ LEFT JOIN "company" invoice_company ON invoice_company."id" = pi."companyId"
+ LEFT JOIN "currency" invoice_currency ON invoice_currency."code" = pi."currencyCode"
+ AND invoice_currency."companyGroupId" = invoice_company."companyGroupId"
+ CROSS JOIN LATERAL (
+ SELECT round((COALESCE(pl."orderTotal", 0) + COALESCE(pid."supplierShippingCost", 0) / CASE WHEN pi."exchangeRate" = 0 THEN 1 ELSE pi."exchangeRate" END) * pi."exchangeRate", invoice_currency."decimalPlaces") AS total_document
+ ) amounts
+ CROSS JOIN LATERAL (
+ SELECT amounts.total_document - COALESCE(s.amount_document, 0) AS amount_document
+ ) remaining;