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.

refactor: purchase order, part ledger, and receipts quantities should use uom conversion

Brad Barbinae0472185b49
diff --git a/packages/database/supabase/functions/post-purchase-invoice/index.ts b/packages/database/supabase/functions/post-purchase-invoice/index.ts
index 200c3c2248..66975652bd 100644
--- a/packages/database/supabase/functions/post-purchase-invoice/index.ts
+++ b/packages/database/supabase/functions/post-purchase-invoice/index.ts
@@ -2,10 +2,10 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { format } from "https://deno.land/[email protected]/datetime/mod.ts";
import { nanoid } from "https://deno.land/x/[email protected]/mod.ts";
-import type { Database } from "../lib/types.ts";
import { DB, getConnectionPool, getDatabaseClient } from "../lib/database.ts";
import { corsHeaders } from "../lib/headers.ts";
import { getSupabaseServiceRole } from "../lib/supabase.ts";
+import type { Database } from "../lib/types.ts";
import { credit, debit, journalReference } from "../lib/utils.ts";
import { getCurrentAccountingPeriod } from "../shared/get-accounting-period.ts";
import { getNextSequence } from "../shared/get-next-sequence.ts";
@@ -220,6 +220,9 @@ serve(async (req: Request) => {
> = {};
for await (const invoiceLine of purchaseInvoiceLines.data) {
+ const invoiceLineQuantityInInventoryUnit =
+ invoiceLine.quantity * (invoiceLine.conversionFactor ?? 1);
+
// declaring shared variables between part and service cases
// outside of the switch case to avoid redeclaring them
let postingGroupInventory:
@@ -554,9 +557,9 @@ serve(async (req: Request) => {
receiptLineInserts.push({
partId: invoiceLine.partId!,
lineId: invoiceLine.id,
- orderQuantity: invoiceLine.quantity,
- outstandingQuantity: invoiceLine.quantity,
- receivedQuantity: invoiceLine.quantity,
+ orderQuantity: invoiceLineQuantityInInventoryUnit,
+ outstandingQuantity: invoiceLineQuantityInInventoryUnit,
+ receivedQuantity: invoiceLineQuantityInInventoryUnit,
locationId: invoiceLine.locationId,
shelfId: invoiceLine.shelfId,
unitOfMeasure: invoiceLine.unitOfMeasureCode ?? "EA",
@@ -569,7 +572,7 @@ serve(async (req: Request) => {
partLedgerInserts.push({
postingDate: today,
partId: invoiceLine.partId!,
- quantity: invoiceLine.quantity,
+ quantity: invoiceLineQuantityInInventoryUnit,
locationId: invoiceLine.locationId,
shelfId: invoiceLine.shelfId,
entryType: "Positive Adjmt.",
@@ -590,7 +593,7 @@ serve(async (req: Request) => {
externalDocumentId:
purchaseInvoice.data?.supplierReference ?? undefined,
partId: invoiceLine.partId,
- quantity: invoiceLine.quantity,
+ quantity: invoiceLineQuantityInInventoryUnit,
cost: invoiceLine.quantity * invoiceLine.unitPrice,
costPostedToGL: invoiceLine.quantity * invoiceLine.unitPrice,
});
diff --git a/packages/database/supabase/functions/post-receipt/index.ts b/packages/database/supabase/functions/post-receipt/index.ts
index d20f72567f..e6e3ef580a 100644
--- a/packages/database/supabase/functions/post-receipt/index.ts
+++ b/packages/database/supabase/functions/post-receipt/index.ts
@@ -110,13 +110,17 @@ serve(async (req: Request) => {
purchaseOrderLine.purchaseQuantity &&
purchaseOrderLine.purchaseQuantity > 0
) {
+ const recivedQuantityInPurchaseUnit =
+ receiptLine.receivedQuantity /
+ (receiptLine.conversionFactor ?? 1);
+
const newQuantityReceived =
(purchaseOrderLine.quantityReceived ?? 0) +
- receiptLine.receivedQuantity;
+ recivedQuantityInPurchaseUnit;
const receivedComplete =
purchaseOrderLine.receivedComplete ||
- receiptLine.receivedQuantity >=
+ recivedQuantityInPurchaseUnit >=
(purchaseOrderLine.quantityToReceive ??
purchaseOrderLine.purchaseQuantity);
@@ -182,6 +186,9 @@ serve(async (req: Request) => {
> = {};
for await (const receiptLine of receiptLines.data) {
+ const receiptLineQuantityInInventoryUnit =
+ receiptLine.receivedQuantity * (receiptLine.conversionFactor ?? 1);
+
let postingGroupInventory:
| Database["public"]["Tables"]["postingGroupInventory"]["Row"]
| null = null;
@@ -557,7 +564,7 @@ serve(async (req: Request) => {
partLedgerInserts.push({
postingDate: today,
partId: receiptLine.partId,
- quantity: receiptLine.receivedQuantity,
+ quantity: receiptLineQuantityInInventoryUnit,
locationId: receiptLine.locationId,
shelfId: receiptLine.shelfId,
entryType: "Positive Adjmt.",