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: posting transactions for invoiced part with no po line

Brad Barbin64285106f2f3
diff --git a/apps/carbon/app/modules/inventory/inventory.models.ts b/apps/carbon/app/modules/inventory/inventory.models.ts
index 225115ff93..d49d73bb58 100644
--- a/apps/carbon/app/modules/inventory/inventory.models.ts
+++ b/apps/carbon/app/modules/inventory/inventory.models.ts
@@ -4,8 +4,10 @@ import { zfd } from "zod-form-data";
export const receiptSourceDocumentType = [
// "Sales Order",
+ // "Sales Invoice",
// "Sales Return Order",
"Purchase Order",
+ "Purchase Invoice",
// "Purchase Return Order",
// "Inbound Transfer",
// "Outbound Transfer",
diff --git a/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptsTable/ReceiptsTable.tsx b/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptsTable/ReceiptsTable.tsx
index dfb44e8cdc..d425696a52 100644
--- a/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptsTable/ReceiptsTable.tsx
+++ b/apps/carbon/app/modules/inventory/ui/Receipts/ReceiptsTable/ReceiptsTable.tsx
@@ -59,6 +59,18 @@ const ReceiptsTable = memo(({ data, count }: ReceiptsTableProps) => {
{row.original.sourceDocumentReadableId}
</Link>
);
+ case "Purchase Invoice":
+ return (
+ <Link
+ onClick={() =>
+ navigate(
+ path.to.purchaseInvoice(row.original.sourceDocumentId!)
+ )
+ }
+ >
+ {row.original.sourceDocumentReadableId}
+ </Link>
+ );
default:
return null;
}
diff --git a/packages/database/SCHEMA.md b/packages/database/SCHEMA.md
index b09bacf914..165b563fd1 100644
--- a/packages/database/SCHEMA.md
+++ b/packages/database/SCHEMA.md
@@ -5533,8 +5533,10 @@ $$;
```sql
CREATE TYPE "receiptSourceDocument" AS ENUM (
'Sales Order',
+ 'Sales Invoice',
'Sales Return Order',
'Purchase Order',
+ 'Purchase Invoice',
'Purchase Return Order',
'Inbound Transfer',
'Outbound Transfer',
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 9dcb42e187..92910b2095 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -8962,8 +8962,10 @@ export interface Database {
purchaseOrderType: "Purchase" | "Return"
receiptSourceDocument:
| "Sales Order"
+ | "Sales Invoice"
| "Sales Return Order"
| "Purchase Order"
+ | "Purchase Invoice"
| "Purchase Return Order"
| "Inbound Transfer"
| "Outbound Transfer"
diff --git a/packages/database/supabase/functions/post-purchase-invoice/index.ts b/packages/database/supabase/functions/post-purchase-invoice/index.ts
index c4e5ca8fb7..b16bed7818 100644
--- a/packages/database/supabase/functions/post-purchase-invoice/index.ts
+++ b/packages/database/supabase/functions/post-purchase-invoice/index.ts
@@ -7,6 +7,7 @@ import { corsHeaders } from "../lib/headers.ts";
import { getSupabaseServiceRole } from "../lib/supabase.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";
import {
getInventoryPostingGroup,
getPurchasingPostingGroup,
@@ -103,6 +104,14 @@ serve(async (req: Request) => {
"journalId"
>[] = [];
+ const receiptLineInserts: Omit<
+ Database["public"]["Tables"]["receiptLine"]["Insert"],
+ "receiptId"
+ >[] = [];
+
+ const partLedgerInserts: Database["public"]["Tables"]["partLedger"]["Insert"][] =
+ [];
+
const purchaseInvoiceLinesByPurchaseOrderLine =
purchaseInvoiceLines.data.reduce<
Record<
@@ -363,138 +372,37 @@ serve(async (req: Request) => {
throw new Error("No purchasing posting group found");
}
- // determine the journal lines that should be reversed
- const existingJournalLines = invoiceLine.purchaseOrderLineId
- ? journalLinesByPurchaseOrderLine[
- invoiceLine.purchaseOrderLineId
- ] ?? []
- : [];
-
- let previousJournalId: number | null = null;
- let previousAccrual: boolean | null = null;
- let currentGroup = 0;
-
- const existingJournalLineGroups = existingJournalLines.reduce<
- Database["public"]["Tables"]["journalLine"]["Row"][][]
- >((acc, entry) => {
- const { journalId, accrual } = entry;
-
- if (
- journalId === previousJournalId &&
- accrual === previousAccrual
- ) {
- acc[currentGroup - 1].push(entry);
- } else {
- acc.push([entry]);
- currentGroup++;
- }
-
- previousJournalId = journalId;
- previousAccrual = accrual;
- return acc;
- }, []);
-
- const purchaseOrderLine = purchaseOrderLines.data.find(
- (line) => line.id === invoiceLine.purchaseOrderLineId
- );
-
- const quantityReceived = purchaseOrderLine?.quantityReceived ?? 0;
- const quantityInvoiced = purchaseOrderLine?.quantityInvoiced ?? 0;
- const quantityToReverse = Math.max(
- 0,
- Math.min(
- invoiceLine.quantity ?? 0,
- quantityReceived - quantityInvoiced
- )
- );
-
- const quantityAlreadyReversed =
- quantityReceived > quantityInvoiced ? quantityInvoiced : 0;
-
- if (quantityToReverse > 0) {
- let counted = 0;
- let reversed = 0;
-
- existingJournalLineGroups.forEach((entry) => {
- if (entry[0].quantity) {
- const unitCostForEntry =
- (entry[0].amount ?? 0) / entry[0].quantity;
-
- // we don't want to reverse an entry twice, so we need to keep track of what's been previously reversed
-
- // akin to supply
- const quantityAvailableToReverseForEntry =
- quantityAlreadyReversed > counted
- ? entry[0].quantity + counted - quantityAlreadyReversed
- : entry[0].quantity;
-
- // akin to demand
- const quantityRequiredToReverse = quantityToReverse - reversed;
-
- // we can't reverse more than what's available or what's required
- const quantityToReverseForEntry = Math.max(
- 0,
- Math.min(
- quantityAvailableToReverseForEntry,
- quantityRequiredToReverse
- )
- );
-
- if (quantityToReverseForEntry > 0) {
- // create the reversal entries
- journalLineInserts.push({
- accountNumber: entry[0].accountNumber!,
- description: entry[0].description,
- amount:
- entry[0].description === "Interim Inventory Accrual"
- ? credit(
- "asset",
- quantityToReverseForEntry * unitCostForEntry
- )
- : debit(
- "liability",
- quantityToReverseForEntry * unitCostForEntry
- ),
- quantity: quantityToReverseForEntry,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice?.data.supplierReference,
- reference: invoiceLine.purchaseOrderLineId
- ? journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId
- )
- : null,
- });
-
- journalLineInserts.push({
- accountNumber: entry[1].accountNumber!,
- description: entry[1].description,
- amount:
- entry[1].description === "Interim Inventory Accrual"
- ? credit(
- "asset",
- quantityToReverseForEntry * unitCostForEntry
- )
- : debit(
- "liability",
- quantityToReverseForEntry * unitCostForEntry
- ),
- quantity: quantityToReverseForEntry,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice?.data.supplierReference,
- reference: journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId!
- ),
- });
- }
+ // if the purchase order line is null, we receive the part, do the normal entries and do not use accrual/reversing
+ if (invoiceLine.purchaseOrderLineId === null) {
+ // create the receipt line
+ receiptLineInserts.push({
+ partId: invoiceLine.partId!,
+ lineId: invoiceLine.id,
+ orderQuantity: invoiceLine.quantity,
+ outstandingQuantity: invoiceLine.quantity,
+ receivedQuantity: invoiceLine.quantity,
+ locationId: invoiceLine.locationId,
+ shelfId: invoiceLine.shelfId,
+ unitOfMeasure: invoiceLine.unitOfMeasureCode ?? "EA",
+ unitPrice: invoiceLine.unitPrice,
+ createdBy: invoiceLine.createdBy,
+ });
- counted += entry[0].quantity;
- reversed += quantityToReverseForEntry;
- }
+ // 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 entry
+ // create the cost ledger line
costLedgerInserts.push({
partLedgerType: "Purchase",
costLedgerType: "Direct Cost",
@@ -504,9 +412,9 @@ serve(async (req: Request) => {
externalDocumentId:
purchaseInvoice.data?.supplierReference ?? undefined,
partId: invoiceLine.partId,
- quantity: quantityToReverse,
- cost: quantityToReverse * invoiceLine.unitPrice,
- costPostedToGL: quantityToReverse * invoiceLine.unitPrice,
+ quantity: invoiceLine.quantity,
+ cost: invoiceLine.quantity * invoiceLine.unitPrice,
+ costPostedToGL: invoiceLine.quantity * invoiceLine.unitPrice,
});
// create the normal GL entries
@@ -515,14 +423,14 @@ serve(async (req: Request) => {
journalLineInserts.push({
accountNumber: postingGroupInventory.inventoryAccount,
description: "Inventory Account",
- amount: debit("asset", quantityToReverse * invoiceLine.unitPrice),
- quantity: quantityToReverse,
+ amount: debit(
+ "asset",
+ invoiceLine.quantity * invoiceLine.unitPrice
+ ),
+ quantity: invoiceLine.quantity,
documentType: "Invoice",
documentId: purchaseInvoice.data?.id,
externalDocumentId: purchaseInvoice.data?.supplierReference,
- reference: journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId!
- ),
});
// creidt the direct cost applied account
@@ -531,15 +439,12 @@ serve(async (req: Request) => {
description: "Direct Cost Applied",
amount: credit(
"expense",
- quantityToReverse * invoiceLine.unitPrice
+ invoiceLine.quantity * invoiceLine.unitPrice
),
- quantity: quantityToReverse,
+ quantity: invoiceLine.quantity,
documentType: "Invoice",
documentId: purchaseInvoice.data?.id,
externalDocumentId: purchaseInvoice.data?.supplierReference,
- reference: journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId!
- ),
});
// debit the purchase account
@@ -548,9 +453,9 @@ serve(async (req: Request) => {
description: "Purchase Account",
amount: debit(
"expense",
- quantityToReverse * invoiceLine.unitPrice
+ invoiceLine.quantity * invoiceLine.unitPrice
),
- quantity: quantityToReverse,
+ quantity: invoiceLine.quantity,
documentType: "Invoice",
documentId: purchaseInvoice.data?.id,
externalDocumentId: purchaseInvoice.data?.supplierReference,
@@ -565,9 +470,9 @@ serve(async (req: Request) => {
description: "Accounts Payable",
amount: credit(
"liability",
- quantityToReverse * invoiceLine.unitPrice
+ invoiceLine.quantity * invoiceLine.unitPrice
),
- quantity: quantityToReverse,
+ quantity: invoiceLine.quantity,
documentType: "Invoice",
documentId: purchaseInvoice.data?.id,
externalDocumentId: purchaseInvoice.data?.supplierReference,
@@ -575,48 +480,276 @@ serve(async (req: Request) => {
invoiceLine.purchaseOrderLineId!
),
});
- }
+ } // if the line is associated with a purchase order line, we do accrual/reversing
+ else {
+ // determine the journal lines that should be reversed
+ const existingJournalLines = invoiceLine.purchaseOrderLineId
+ ? journalLinesByPurchaseOrderLine[
+ invoiceLine.purchaseOrderLineId
+ ] ?? []
+ : [];
+
+ let previousJournalId: number | null = null;
+ let previousAccrual: boolean | null = null;
+ let currentGroup = 0;
+
+ const existingJournalLineGroups = existingJournalLines.reduce<
+ Database["public"]["Tables"]["journalLine"]["Row"][][]
+ >((acc, entry) => {
+ const { journalId, accrual } = entry;
+
+ if (
+ journalId === previousJournalId &&
+ accrual === previousAccrual
+ ) {
+ acc[currentGroup - 1].push(entry);
+ } else {
+ acc.push([entry]);
+ currentGroup++;
+ }
- if (invoiceLine.quantity > quantityToReverse) {
- // create the accrual entries for invoiced not received
- const quantityToAccrue = invoiceLine.quantity - quantityToReverse;
+ previousJournalId = journalId;
+ previousAccrual = accrual;
+ return acc;
+ }, []);
- // debit the inventory invoiced not received account
- journalLineInserts.push({
- accountNumber:
- postingGroupInventory.inventoryInvoicedNotReceivedAccount,
- description: "Inventory Invoiced Not Received",
- accrual: true,
- amount: debit("asset", quantityToAccrue * invoiceLine.unitPrice),
- quantity: quantityToAccrue,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice.data?.supplierReference,
- reference: invoiceLine.purchaseOrderLineId
- ? journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId
- )
- : null,
- });
+ const purchaseOrderLine = purchaseOrderLines.data.find(
+ (line) => line.id === invoiceLine.purchaseOrderLineId
+ );
- // credit the inventory interim accrual account
- journalLineInserts.push({
- accountNumber:
- postingGroupInventory.inventoryInterimAccrualAccount,
- accrual: true,
- description: "Interim Inventory Accrual",
- amount: credit("asset", quantityToAccrue * invoiceLine.unitPrice),
- quantity: quantityToAccrue,
- documentType: "Invoice",
- documentId: purchaseInvoice.data?.id,
- externalDocumentId: purchaseInvoice.data?.supplierReference,
- reference: invoiceLine.purchaseOrderLineId
- ? journalReference.to.purchaseInvoice(
- invoiceLine.purchaseOrderLineId
- )
- : null,
- });
+ const quantityReceived = purchaseOrderLine?.quantityReceived ?? 0;
+ const quantityInvoiced = purchaseOrderLine?.quantityInvoiced ?? 0;
+ const quantityToReverse = Math.max(
+ 0,
+ Math.min(
+ invoiceLine.quantity ?? 0,
+ quantityReceived - quantityInvoiced
+ )
+ );
+
+ const quantityAlreadyReversed =
+ quantityReceived > quantityInvoiced ? quantityInvoiced : 0;
+
+ if (quantityToReverse > 0) {
+ let counted = 0;
+ let reversed = 0;
+
+ existingJournalLineGroups.forEach((entry) => {
+ if (entry[0].quantity) {
+ const unitCostForEntry =
+ (entry[0].amount ?? 0) / entry[0].quantity;
+
+ // we don't want to reverse an entry twice, so we need to keep track of what's been previously reversed
+
+ // akin to supply
+ const quantityAvailableToReverseForEntry =
+ quantityAlreadyReversed > counted
+ ? entry[0].quantity + counted - quantityAlreadyReversed
+ : entry[0].quantity;
+
+ // akin to demand
+ const quantityRequiredToReverse =
+ quantityToReverse - reversed;
+
+ // we can't reverse more than what's available or what's required
+ const quantityToReverseForEntry = Math.max(
+ 0,
+ Math.min(
+ quantityAvailableToReverseForEntry,
+ quantityRequiredToReverse
+ )
+ );
+
+ if (quantityToReverseForEntry > 0) {
+ // create the reversal entries
+ journalLineInserts.push({
+ accountNumber: entry[0].accountNumber!,
+ description: entry[0].description,
+ amount:
+ entry[0].description === "Interim Inventory Accrual"
+ ? credit(
+ "asset",
+ quantityToReverseForEntry * unitCostForEntry
+ )
+ : debit(
+ "liability",
+ quantityToReverseForEntry * unitCostForEntry
+ ),
+ quantity: quantityToReverseForEntry,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId:
+ purchaseInvoice?.data.supplierReference,
+ reference: invoiceLine.purchaseOrderLineId
+ ? journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId
+ )
+ : null,
+ });
+
+ journalLineInserts.push({
+ accountNumber: entry[1].accountNumber!,
+ description: entry[1].description,
+ amount:
+ entry[1].description === "Interim Inventory Accrual"
+ ? credit(
+ "asset",
+ quantityToReverseForEntry * unitCostForEntry
+ )
+ : debit(
+ "liability",
+ quantityToReverseForEntry * unitCostForEntry
+ ),
+ quantity: quantityToReverseForEntry,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId:
+ purchaseInvoice?.data.supplierReference,
+ reference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ });
+ }
+
+ counted += entry[0].quantity;
+ reversed += quantityToReverseForEntry;
+ }
+ });
+
+ // create the cost entry
+ costLedgerInserts.push({
+ partLedgerType: "Purchase",
+ costLedgerType: "Direct Cost",
+ adjustment: false,
+ documentType: "Purchase Invoice",
+ documentId: purchaseInvoice.data?.id ?? undefined,
+ externalDocumentId:
+ purchaseInvoice.data?.supplierReference ?? undefined,
+ partId: invoiceLine.partId,
+ quantity: quantityToReverse,
+ cost: quantityToReverse * invoiceLine.unitPrice,
+ costPostedToGL: quantityToReverse * invoiceLine.unitPrice,
+ });
+
+ // create the normal GL entries
+
+ // 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,
+ reference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ });
+
+ // 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,
+ reference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ });
+
+ // debit the purchase account
+ journalLineInserts.push({
+ accountNumber: postingGroupPurchasing.purchaseAccount,
+ description: "Purchase Account",
+ amount: debit(
+ "expense",
+ quantityToReverse * invoiceLine.unitPrice
+ ),
+ quantity: quantityToReverse,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ reference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ });
+
+ // credit the accounts payable account
+ journalLineInserts.push({
+ accountNumber: postingGroupPurchasing.payablesAccount,
+ description: "Accounts Payable",
+ amount: credit(
+ "liability",
+ quantityToReverse * invoiceLine.unitPrice
+ ),
+ quantity: quantityToReverse,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ reference: journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId!
+ ),
+ });
+ }
+
+ if (invoiceLine.quantity > quantityToReverse) {
+ // create the accrual entries for invoiced not received
+ const quantityToAccrue = invoiceLine.quantity - quantityToReverse;
+
+ // debit the inventory invoiced not received account
+ journalLineInserts.push({
+ accountNumber:
+ postingGroupInventory.inventoryInvoicedNotReceivedAccount,
+ description: "Inventory Invoiced Not Received",
+ accrual: true,
+ amount: debit(
+ "asset",
+ quantityToAccrue * invoiceLine.unitPrice
+ ),
+ quantity: quantityToAccrue,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ reference: invoiceLine.purchaseOrderLineId
+ ? journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId
+ )
+ : null,
+ });
+
+ // credit the inventory interim accrual account
+ journalLineInserts.push({
+ accountNumber:
+ postingGroupInventory.inventoryInterimAccrualAccount,
+ accrual: true,
+ description: "Interim Inventory Accrual",
+ amount: credit(
+ "asset",
+ quantityToAccrue * invoiceLine.unitPrice
+ ),
+ quantity: quantityToAccrue,
+ documentType: "Invoice",
+ documentId: purchaseInvoice.data?.id,
+ externalDocumentId: purchaseInvoice.data?.supplierReference,
+ reference: invoiceLine.purchaseOrderLineId
+ ? journalReference.to.purchaseInvoice(
+ invoiceLine.purchaseOrderLineId
+ )
+ : null,
+ });
+ }
}
+
break;
case "Fixed Asset":
// TODO: fixed assets
@@ -631,6 +764,59 @@ serve(async (req: Request) => {
const accountingPeriodId = await getCurrentAccountingPeriod(client, db);
await db.transaction().execute(async (trx) => {
+ if (receiptLineInserts.length > 0 && partLedgerInserts.length > 0) {
+ const receiptLinesGroupedByLocationId = receiptLineInserts.reduce<
+ Record<string, typeof receiptLineInserts>
+ >((acc, line) => {
+ if (line.locationId) {
+ if (line.locationId in acc) {
+ acc[line.locationId].push(line);
+ } else {
+ acc[line.locationId] = [line];
+ }
+ }
+
+ return acc;
+ }, {});
+
+ for await (const [locationId, receiptLines] of Object.entries(
+ receiptLinesGroupedByLocationId
+ )) {
+ const readableReceiptId = await getNextSequence(trx, "receipt");
+ const receipt = await trx
+ .insertInto("receipt")
+ .values({
+ receiptId: readableReceiptId,
+ locationId,
+ sourceDocument: "Purchase Invoice",
+ sourceDocumentId: purchaseInvoice.data.id,
+ sourceDocumentReadableId: purchaseInvoice.data.invoiceId,
+ externalDocumentId: purchaseInvoice.data.supplierReference,
+ supplierId: purchaseInvoice.data.supplierId,
+ status: "Posted",
+ postingDate: today,
+ invoiced: true,
+ createdBy: purchaseInvoice.data.createdBy,
+ })
+ .returning(["id"])
+ .execute();
+
+ const receiptId = receipt[0].id;
+ if (!receiptId) throw new Error("Failed to insert receipt");
+
+ await trx
+ .insertInto("receiptLine")
+ .values(
+ receiptLines.map((r) => ({
+ ...r,
+ receiptId: receiptId,
+ }))
+ )
+ .returning(["id"])
+ .execute();
+ }
+ }
+
for await (const [purchaseOrderLineId, update] of Object.entries(
purchaseOrderLineUpdates
)) {
@@ -710,12 +896,19 @@ serve(async (req: Request) => {
.execute();
if (costLedgerInserts.length > 0) {
+ await trx
+ .insertInto("partLedger")
+ .values(partLedgerInserts)
+ .returning(["id"])
+ .execute();
+
const costLedgerIds = await trx
.insertInto("costLedger")
.values(costLedgerInserts)
.returning(["id"])
.execute();
+ // TODO: this won't work -- it assumes each line is the same -- we need better grouping further up in process
const journalLinesPerCostEntry =
journalLineIds.length / costLedgerIds.length;
const costLedgerJournalLineRelationInserts = journalLineIds.map<
diff --git a/packages/database/supabase/functions/post-receipt/index.ts b/packages/database/supabase/functions/post-receipt/index.ts
index 14625a3fbc..1983285566 100644
--- a/packages/database/supabase/functions/post-receipt/index.ts
+++ b/packages/database/supabase/functions/post-receipt/index.ts
@@ -593,7 +593,8 @@ serve(async (req: Request) => {
.returning(["id"])
.execute();
- // insert relationship between journal entry and value entry
+ // TODO: this won't work -- it assumes each line is the same -- we need better grouping further up in process
+ // insert relationship between journal line and cost line
const journalLinesPerCostEntry =
journalLineIds.length / costLedgerIds.length;
const costLedgerJournalLineRelationInserts = journalLineIds.map<
diff --git a/packages/database/supabase/functions/shared/get-next-sequence.ts b/packages/database/supabase/functions/shared/get-next-sequence.ts
index 427e199f5e..c2fbea619c 100644
--- a/packages/database/supabase/functions/shared/get-next-sequence.ts
+++ b/packages/database/supabase/functions/shared/get-next-sequence.ts
@@ -26,7 +26,7 @@ export async function getNextSequence(trx: Transaction<DB>, tableName: string) {
next: nextValue,
updatedBy: "system",
})
- .where("table", "=", "purchaseInvoice")
+ .where("table", "=", tableName)
.execute();
return `${derivedPrefix}${nextSequence}${derivedSuffix}`;
diff --git a/packages/database/supabase/migrations/20230728025201_receipts.sql b/packages/database/supabase/migrations/20230728025201_receipts.sql
index 55c2ae0e36..ae1fa5c5d3 100644
--- a/packages/database/supabase/migrations/20230728025201_receipts.sql
+++ b/packages/database/supabase/migrations/20230728025201_receipts.sql
@@ -1,7 +1,9 @@
CREATE TYPE "receiptSourceDocument" AS ENUM (
'Sales Order',
+ 'Sales Invoice',
'Sales Return Order',
'Purchase Order',
+ 'Purchase Invoice',
'Purchase Return Order',
'Inbound Transfer',
'Outbound Transfer',
diff --git a/www/pages/learn/accounting/purchasing.mdx b/www/pages/learn/accounting/purchasing.mdx
index ba60934fc9..fb14dda9f1 100644
--- a/www/pages/learn/accounting/purchasing.mdx
+++ b/www/pages/learn/accounting/purchasing.mdx
@@ -69,7 +69,7 @@ Here are the basic entries that occur depending on the order of the transactions
- **Payment:**
- Debit the liability Accounts Payable (from Purchasing posting group)
- Credit the asset of a Cash or Bank Account (from Bank Account posting group)
-- **Goods Received and Invoiced at Same Time**
+- **Goods Received and Invoiced at Same Time (no purchase order)**
- **Goods and Invoiced and Received**
- **Inventory Entry:**
- Debit the asset Inventory (from Inventory posting group)