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(invoicing): handle End of Month and Day of Month payment terms in due date calc

sidgaikwad4db46aa4ac9d
diff --git a/apps/erp/app/modules/invoicing/invoicing.service.ts b/apps/erp/app/modules/invoicing/invoicing.service.ts
index 7914256a84..da5eb4717c 100644
--- a/apps/erp/app/modules/invoicing/invoicing.service.ts
+++ b/apps/erp/app/modules/invoicing/invoicing.service.ts
@@ -1,6 +1,11 @@
import type { Database, Json } from "@carbon/database";
import type { Kysely, KyselyDatabase } from "@carbon/database/client";
-import { getLocalTimeZone, parseDate, today } from "@internationalized/date";
+import {
+ endOfMonth,
+ getLocalTimeZone,
+ parseDate,
+ today
+} from "@internationalized/date";
import type { SupabaseClient } from "@supabase/supabase-js";
import type { z } from "zod";
import {
@@ -41,6 +46,12 @@ import type {
* stale dateDue. The read is scoped by companyId for tenant isolation (defense
* in depth alongside RLS) and uses maybeSingle so an absent row is data: null
* (not an error) — keeping "missing" distinguishable from "failed".
+ *
+ * The term's calculationMethod decides the anchor for daysDue:
+ * - "Net": daysDue days after the issue date.
+ * - "End of Month": daysDue days after the end of the issue month.
+ * - "Day of Month": due on day daysDue of the month — the first occurrence on
+ * or after the issue date, clamped to the month's length (31 → Feb 28).
*/
export async function computeInvoiceDateDue(
client: SupabaseClient<Database>,
@@ -55,7 +66,7 @@ export async function computeInvoiceDateDue(
const paymentTerm = await client
.from("paymentTerm")
- .select("daysDue")
+ .select("daysDue, calculationMethod")
.eq("id", paymentTermId)
.eq("companyId", companyId)
.maybeSingle();
@@ -67,10 +78,25 @@ export async function computeInvoiceDateDue(
}
if (!paymentTerm.data) return null;
+ const { daysDue, calculationMethod } = paymentTerm.data;
+
try {
- return parseDate(dateIssued)
- .add({ days: paymentTerm.data.daysDue })
- .toString();
+ const issued = parseDate(dateIssued);
+ switch (calculationMethod) {
+ case "End of Month":
+ return endOfMonth(issued).add({ days: daysDue }).toString();
+ case "Day of Month": {
+ // set() clamps daysDue to the month's length
+ const sameMonth = issued.set({ day: daysDue });
+ return (
+ sameMonth.compare(issued) >= 0
+ ? sameMonth
+ : issued.add({ months: 1 }).set({ day: daysDue })
+ ).toString();
+ }
+ default:
+ return issued.add({ days: daysDue }).toString();
+ }
} catch {
return null;
}
diff --git a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
index 9914d472d7..d975d51fdf 100644
--- a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
@@ -1,6 +1,6 @@
{
- "generated": "2026-08-03T05:37:07.055Z",
- "totalTools": 1410,
+ "generated": "2026-08-04T13:12:13.628Z",
+ "totalTools": 1411,
"modules": 15,
"tools": [
{
@@ -8044,6 +8044,31 @@
]
}
},
+ {
+ "name": "invoicing_computeInvoiceDateDue",
+ "module": "invoicing",
+ "classification": "WRITE",
+ "description": "compute invoice date due",
+ "paramCount": 2,
+ "serviceParams": [
+ "client",
+ "args"
+ ],
+ "injectAuth": [
+ "companyId"
+ ],
+ "schema": {
+ "type": "object",
+ "properties": {
+ "dateIssued": {},
+ "paymentTermId": {}
+ },
+ "required": [
+ "dateIssued",
+ "paymentTermId"
+ ]
+ }
+ },
{
"name": "invoicing_createPurchaseInvoiceFromPurchaseOrder",
"module": "invoicing",