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.

feat: use overhead accounts for non-inventory items and don't post to parts ledger

Brad Barbin05de27504e01
diff --git a/packages/database/SCHEMA.md b/packages/database/SCHEMA.md
index 0f9779f08c..f043b999b3 100644
--- a/packages/database/SCHEMA.md
+++ b/packages/database/SCHEMA.md
@@ -1,4 +1,6 @@
-# Carbon Database Schema by `scripts/generate-schema-md.ts` using the migrations in `packages/database/supabase/migrations`.
+# Carbon Database Schema
+
+This file is generated by `scripts/generate-schema-md.ts` using the migrations in `packages/database/supabase/migrations`.
diff --git a/packages/database/supabase/functions/post-purchase-invoice/index.ts b/packages/database/supabase/functions/post-purchase-invoice/index.ts
index e39b8ea583..de956876ab 100644
--- a/packages/database/supabase/functions/post-purchase-invoice/index.ts
+++ b/packages/database/supabase/functions/post-purchase-invoice/index.ts
@@ -40,59 +40,54 @@ serve(async (req: Request) => {
if (purchaseInvoiceLines.error)
throw new Error("Failed to fetch receipt lines");
- const [partGroups, servicePartGroups, purchaseOrderLines, supplier] =
- await Promise.all([
- client
- .from("part")
- .select("id, partGroupId")
- .in(
- "id",
- purchaseInvoiceLines.data.reduce<string[]>((acc, invoiceLine) => {
- if (invoiceLine.partId && !acc.includes(invoiceLine.partId)) {
- acc.push(invoiceLine.partId);
- }
- return acc;
- }, [])
- ),
- client
- .from("service")
- .select("id, partGroupId")
- .in(
- "id",
- purchaseInvoiceLines.data.reduce<string[]>((acc, invoiceLine) => {
- if (
- invoiceLine.serviceId &&
- !acc.includes(invoiceLine.serviceId)
- ) {
- acc.push(invoiceLine.serviceId);
- }
- return acc;
- }, [])
- ),
- client
- .from("purchaseOrderLine")
- .select("*")
- .in(
- "id",
- purchaseInvoiceLines.data.reduce<string[]>((acc, invoiceLine) => {
- if (
- invoiceLine.purchaseOrderLineId &&
- !acc.includes(invoiceLine.purchaseOrderLineId)
- ) {
- acc.push(invoiceLine.purchaseOrderLineId);
- }
- return acc;
- }, [])
- ),
- client
- .from("supplier")
- .select("*")
- .eq("id", purchaseInvoice.data.supplierId ?? "")
- .single(),
- ]);
- if (partGroups.error) throw new Error("Failed to fetch part groups");
- if (servicePartGroups.error)
- throw new Error("Failed to fetch service part groups");
+ const [parts, services, purchaseOrderLines, supplier] = await Promise.all([
+ client
+ .from("part")
+ .select("id, partGroupId, partType")
+ .in(
+ "id",
+ purchaseInvoiceLines.data.reduce<string[]>((acc, invoiceLine) => {
+ if (invoiceLine.partId && !acc.includes(invoiceLine.partId)) {
+ acc.push(invoiceLine.partId);
+ }
+ return acc;
+ }, [])
+ ),
+ client
+ .from("service")
+ .select("id, partGroupId")
+ .in(
+ "id",
+ purchaseInvoiceLines.data.reduce<string[]>((acc, invoiceLine) => {
+ if (invoiceLine.serviceId && !acc.includes(invoiceLine.serviceId)) {
+ acc.push(invoiceLine.serviceId);
+ }
+ return acc;
+ }, [])
+ ),
+ client
+ .from("purchaseOrderLine")
+ .select("*")
+ .in(
+ "id",
+ purchaseInvoiceLines.data.reduce<string[]>((acc, invoiceLine) => {
+ if (
+ invoiceLine.purchaseOrderLineId &&
+ !acc.includes(invoiceLine.purchaseOrderLineId)
+ ) {
+ acc.push(invoiceLine.purchaseOrderLineId);
+ }
+ return acc;
+ }, [])
+ ),
+ client
+ .from("supplier")
+ .select("*")
+ .eq("id", purchaseInvoice.data.supplierId ?? "")
+ .single(),
+ ]);
+ if (parts.error) throw new Error("Failed to fetch part groups");
+ if (services.error) throw new Error("Failed to fetch service part groups");
if (purchaseOrderLines.error)
throw new Error("Failed to fetch purchase order lines");
if (supplier.error) throw new Error("Failed to fetch supplier");
@@ -351,8 +346,8 @@ serve(async (req: Request) => {
break;
case "Service":
partGroupId =
- servicePartGroups.data.find(
- (partGroup) => partGroup.id === invoiceLine.serviceId
+ services.data.find(
+ (service) => service.id === invoiceLine.serviceId
)?.partGroupId ?? null;
// inventory posting group
@@ -494,10 +489,13 @@ serve(async (req: Request) => {
break;
case "Part":
+ const partType =
+ parts.data.find((part) => part.id === invoiceLine.partId)
+ ?.partType ?? "Inventory";
+
partGroupId =
- partGroups.data.find(
- (partGroup) => partGroup.id === invoiceLine.partId
- )?.partGroupId ?? null;
+ parts.data.find((part) => part.id === invoiceLine.partId)
+ ?.partGroupId ?? null;
// inventory posting group
if (`${partGroupId}-${locationId}` in inventoryPostingGroups) {
@@ -566,19 +564,21 @@ serve(async (req: Request) => {
createdBy: invoiceLine.createdBy,
});
- // create the part ledger line
- partLedgerInserts.push({
- postingDate: today,
- partId: invoiceLine.partId!,
- quantity: invoiceLine.quantity,
- locationId: invoiceLine.locationId,
- shelfId: invoiceLine.shelfId,
- entryType: "Positive Adjmt.",
- documentType: "Purchase Receipt",
- documentId: purchaseInvoice.data?.id ?? undefined,
- externalDocumentId:
- purchaseInvoice.data?.supplierReference ?? undefined,
- });
+ if (partType === "Inventory") {
+ // create the part ledger line
+ partLedgerInserts.push({
+ postingDate: today,
+ partId: invoiceLine.partId!,
+ quantity: invoiceLine.quantity,
+ locationId: invoiceLine.locationId,
+ shelfId: invoiceLine.shelfId,
+ entryType: "Positive Adjmt.",
+ documentType: "Purchase Receipt",
+ documentId: purchaseInvoice.data?.id ?? undefined,
+ externalDocumentId:
+ purchaseInvoice.data?.supplierReference ?? undefined,
+ });
+ }
// create the cost ledger line
costLedgerInserts.push({
@@ -599,35 +599,67 @@ serve(async (req: Request) => {
journalLineReference = nanoid();
- // debit the inventory account
- journalLineInserts.push({
- accountNumber: postingGroupInventory.inventoryAccount,
- description: "Inventory Account",
- amount: debit(
- "asset",
- invoiceLine.quantity * invoiceLine.unitPrice
- ),
- quantity: invoiceLine.quantity,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice.data?.supplierReference,
- journalLineReference,
- });
+ if (partType === "Inventory") {
+ // debit the inventory account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.inventoryAccount,
+ description: "Inventory Account",
+ amount: debit(
+ "asset",
+ invoiceLine.quantity * invoiceLine.unitPrice
+ ),
+ quantity: invoiceLine.quantity,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ journalLineReference,
+ });
- // creidt the direct cost applied account
- journalLineInserts.push({
- accountNumber: postingGroupInventory.directCostAppliedAccount,
- description: "Direct Cost Applied",
- amount: credit(
- "expense",
- invoiceLine.quantity * invoiceLine.unitPrice
- ),
- quantity: invoiceLine.quantity,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice.data?.supplierReference,
- journalLineReference,
- });
+ // creidt the direct cost applied account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.directCostAppliedAccount,
+ description: "Direct Cost Applied",
+ amount: credit(
+ "expense",
+ invoiceLine.quantity * invoiceLine.unitPrice
+ ),
+ quantity: invoiceLine.quantity,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ journalLineReference,
+ });
+ } else {
+ // debit the overhead account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.overheadAccount,
+ description: "Overhead Account",
+ amount: debit(
+ "asset",
+ invoiceLine.quantity * invoiceLine.unitPrice
+ ),
+ quantity: invoiceLine.quantity,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ journalLineReference,
+ });
+
+ // creidt the overhead cost applied account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.overheadCostAppliedAccount,
+ description: "Overhead Cost Applied",
+ amount: credit(
+ "expense",
+ invoiceLine.quantity * invoiceLine.unitPrice
+ ),
+ quantity: invoiceLine.quantity,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ journalLineReference,
+ });
+ }
journalLineReference = nanoid();
@@ -822,45 +854,84 @@ serve(async (req: Request) => {
costPostedToGL: quantityToReverse * invoiceLine.unitPrice,
});
- // create the normal GL entries
+ // create the normal GL entries for a part
journalLineReference = nanoid();
- // debit the inventory account
- journalLineInserts.push({
- accountNumber: postingGroupInventory.inventoryAccount,
- description: "Inventory Account",
- amount: debit(
- "asset",
- quantityToReverse * invoiceLine.unitPrice
- ),
- quantity: quantityToReverse,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice.data?.supplierReference,
- documentLineReference: journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId!
- ),
- journalLineReference,
- });
-
- // creidt the direct cost applied account
- journalLineInserts.push({
- accountNumber: postingGroupInventory.directCostAppliedAccount,
- description: "Direct Cost Applied",
- amount: credit(
- "expense",
- quantityToReverse * invoiceLine.unitPrice
- ),
- quantity: quantityToReverse,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice.data?.supplierReference,
- documentLineReference: journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId!
- ),
- journalLineReference,
- });
+ if (partType === "Inventory") {
+ // debit the inventory account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.inventoryAccount,
+ description: "Inventory Account",
+ amount: debit(
+ "asset",
+ quantityToReverse * invoiceLine.unitPrice
+ ),
+ quantity: quantityToReverse,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ documentLineReference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ journalLineReference,
+ });
+
+ // creidt the direct cost applied account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.directCostAppliedAccount,
+ description: "Direct Cost Applied",
+ amount: credit(
+ "expense",
+ quantityToReverse * invoiceLine.unitPrice
+ ),
+ quantity: quantityToReverse,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ documentLineReference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ journalLineReference,
+ });
+ } else {
+ // debit the overhead account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.overheadAccount,
+ description: "Overhead Account",
+ amount: debit(
+ "asset",
+ quantityToReverse * invoiceLine.unitPrice
+ ),
+ quantity: quantityToReverse,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ documentLineReference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ journalLineReference,
+ });
+
+ // creidt the overhead cost applied account
+ journalLineInserts.push({
+ accountNumber:
+ postingGroupInventory.overheadCostAppliedAccount,
+ description: "Overhead Cost Applied",
+ amount: credit(
+ "expense",
+ quantityToReverse * invoiceLine.unitPrice
+ ),
+ quantity: quantityToReverse,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ documentLineReference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ journalLineReference,
+ });
+ }
journalLineReference = nanoid();
@@ -967,7 +1038,7 @@ serve(async (req: Request) => {
const accountingPeriodId = await getCurrentAccountingPeriod(client, db);
await db.transaction().execute(async (trx) => {
- if (receiptLineInserts.length > 0 && partLedgerInserts.length > 0) {
+ if (receiptLineInserts.length > 0) {
const receiptLinesGroupedByLocationId = receiptLineInserts.reduce<
Record<string, typeof receiptLineInserts>
>((acc, line) => {
diff --git a/packages/database/supabase/functions/post-receipt/index.ts b/packages/database/supabase/functions/post-receipt/index.ts
index c53086a9ff..7418cff7a9 100644
--- a/packages/database/supabase/functions/post-receipt/index.ts
+++ b/packages/database/supabase/functions/post-receipt/index.ts
@@ -36,9 +36,9 @@ serve(async (req: Request) => {
if (receipt.error) throw new Error("Failed to fetch receipt");
if (receiptLines.error) throw new Error("Failed to fetch receipt lines");
- const partGroups = await client
+ const parts = await client
.from("part")
- .select("id, partGroupId")
+ .select("id, partGroupId, partType")
.in(
"id",
receiptLines.data.reduce<string[]>((acc, receiptLine) => {
@@ -48,7 +48,7 @@ serve(async (req: Request) => {
return acc;
}, [])
);
- if (partGroups.error) throw new Error("Failed to fetch part groups");
+ if (parts.error) throw new Error("Failed to fetch part groups");
switch (receipt.data?.sourceDocument) {
case "Purchase Order": {
@@ -186,10 +186,13 @@ serve(async (req: Request) => {
| Database["public"]["Tables"]["postingGroupInventory"]["Row"]
| null = null;
+ const partType =
+ parts.data.find((part) => part.id === receiptLine.partId)
+ ?.partType ?? "Inventory";
+
const partGroupId: string | null =
- partGroups.data.find(
- (partGroup) => partGroup.id === receiptLine.partId
- )?.partGroupId ?? null;
+ parts.data.find((part) => part.id === receiptLine.partId)
+ ?.partGroupId ?? null;
const locationId = receiptLine.locationId ?? null;
const supplierTypeId: string | null =
supplier.data.supplierTypeId ?? null;
@@ -413,35 +416,67 @@ serve(async (req: Request) => {
let journalLineReference = nanoid();
- // debit the inventory account
- journalLineInserts.push({
- accountNumber: postingGroupInventory.inventoryAccount,
- description: "Inventory Account",
- amount: debit("asset", value),
- quantity: quantityToReverse,
- documentType: "Receipt",
- documentId: receipt.data?.id,
- externalDocumentId: receipt.data?.externalDocumentId,
- documentLineReference: journalReference.to.receipt(
- receiptLine.lineId!
- ),
- journalLineReference,
- });
-
- // creidt the direct cost applied account
- journalLineInserts.push({
- accountNumber: postingGroupInventory.directCostAppliedAccount,
- description: "Direct Cost Applied",
- amount: credit("expense", value),
- quantity: quantityToReverse,
- documentType: "Receipt",
- documentId: receipt.data?.id,
- externalDocumentId: receipt.data?.externalDocumentId,
- documentLineReference: journalReference.to.receipt(
- receiptLine.lineId!
- ),
- journalLineReference,
- });
+ if (partType === "Inventory") {
+ // debit the inventory account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.inventoryAccount,
+ description: "Inventory Account",
+ amount: debit("asset", value),
+ quantity: quantityToReverse,
+ documentType: "Receipt",
+ documentId: receipt.data?.id,
+ externalDocumentId: receipt.data?.externalDocumentId,
+ documentLineReference: journalReference.to.receipt(
+ receiptLine.lineId!
+ ),
+ journalLineReference,
+ });
+
+ // creidt the direct cost applied account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.directCostAppliedAccount,
+ description: "Direct Cost Applied",
+ amount: credit("expense", value),
+ quantity: quantityToReverse,
+ documentType: "Receipt",
+ documentId: receipt.data?.id,
+ externalDocumentId: receipt.data?.externalDocumentId,
+ documentLineReference: journalReference.to.receipt(
+ receiptLine.lineId!
+ ),
+ journalLineReference,
+ });
+ } else {
+ // debit the overhead account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.overheadAccount,
+ description: "Overhead Account",
+ amount: debit("asset", value),
+ quantity: quantityToReverse,
+ documentType: "Receipt",
+ documentId: receipt.data?.id,
+ externalDocumentId: receipt.data?.externalDocumentId,
+ documentLineReference: journalReference.to.receipt(
+ receiptLine.lineId!
+ ),
+ journalLineReference,
+ });
+
+ // creidt the overhead cost applied account
+ journalLineInserts.push({
+ accountNumber: postingGroupInventory.overheadCostAppliedAccount,
+ description: "Overhead Cost Applied",
+ amount: credit("expense", value),
+ quantity: quantityToReverse,
+ documentType: "Receipt",
+ documentId: receipt.data?.id,
+ externalDocumentId: receipt.data?.externalDocumentId,
+ documentLineReference: journalReference.to.receipt(
+ receiptLine.lineId!
+ ),
+ journalLineReference,
+ });
+ }
journalLineReference = nanoid();
@@ -518,17 +553,19 @@ serve(async (req: Request) => {
});
}
- partLedgerInserts.push({
- postingDate: today,
- partId: receiptLine.partId,
- quantity: receiptLine.receivedQuantity,
- locationId: receiptLine.locationId,
- shelfId: receiptLine.shelfId,
- entryType: "Positive Adjmt.",
- documentType: "Purchase Receipt",
- documentId: receipt.data?.id ?? undefined,
- externalDocumentId: receipt.data?.externalDocumentId ?? undefined,
- });
+ if (partType === "Inventory") {
+ partLedgerInserts.push({
+ postingDate: today,
+ partId: receiptLine.partId,
+ quantity: receiptLine.receivedQuantity,
+ locationId: receiptLine.locationId,
+ shelfId: receiptLine.shelfId,
+ entryType: "Positive Adjmt.",
+ documentType: "Purchase Receipt",
+ documentId: receipt.data?.id ?? undefined,
+ externalDocumentId: receipt.data?.externalDocumentId ?? undefined,
+ });
+ }
}
const accountingPeriodId = await getCurrentAccountingPeriod(client, db);