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: remove relationship table between cost ledger and journal ledger

Brad Barbind77783f4cff2
diff --git a/apps/carbon/app/modules/purchasing/purchasing.models.ts b/apps/carbon/app/modules/purchasing/purchasing.models.ts
index 5f573ee451..d399a550c0 100644
--- a/apps/carbon/app/modules/purchasing/purchasing.models.ts
+++ b/apps/carbon/app/modules/purchasing/purchasing.models.ts
@@ -97,7 +97,7 @@ export const purchaseOrderLineValidator = withZod(
accountNumber: zfd.text(z.string().optional()),
assetId: zfd.text(z.string().optional()),
description: zfd.text(z.string().optional()),
- purchaseQuantity: z.number(),
+ purchaseQuantity: zfd.numeric(z.number()),
unitOfMeasureCode: zfd.text(z.string().optional()),
unitPrice: zfd.numeric(z.number()),
setupPrice: zfd.numeric(z.number().optional()),
diff --git a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchasingStatus/PurchasingStatus.tsx b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchasingStatus/PurchasingStatus.tsx
index f34a806003..ec430c1579 100644
--- a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchasingStatus/PurchasingStatus.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchasingStatus/PurchasingStatus.tsx
@@ -8,6 +8,7 @@ type PurchasingStatusProps = {
const PurchasingStatus = ({ status }: PurchasingStatusProps) => {
switch (status) {
case "Draft":
+ return <Status color="gray">{status}</Status>;
case "To Review":
return <Status color="yellow">{status}</Status>;
case "To Receive":
diff --git a/packages/database/SCHEMA.md b/packages/database/SCHEMA.md
index 165b563fd1..1063122ec0 100644
--- a/packages/database/SCHEMA.md
+++ b/packages/database/SCHEMA.md
@@ -5417,23 +5417,23 @@ CREATE POLICY "Employees with accounting_view can view the value ledger" ON "cos
(get_my_claim('role'::text)) = '"employee"'::jsonb
);
-CREATE TABLE "costLedgerJournalLineRelation" (
- "costLedgerId" TEXT NOT NULL,
- "journalLineId" TEXT NOT NULL,
+-- CREATE TABLE "costLedgerJournalLineRelation" (
+-- "costLedgerId" TEXT NOT NULL,
+-- "journalLineId" TEXT NOT NULL,
- CONSTRAINT "costLedgerJournalLineRelation_pkey" PRIMARY KEY ("costLedgerId", "journalLineId"),
- CONSTRAINT "costLedgerJournalLineRelation_costLedgerId_fkey" FOREIGN KEY ("costLedgerId") REFERENCES "costLedger"("id"),
- CONSTRAINT "costLedgerJournalLineRelation_journalLineId_fkey" FOREIGN KEY ("journalLineId") REFERENCES "journalLine"("id")
-);
+-- CONSTRAINT "costLedgerJournalLineRelation_pkey" PRIMARY KEY ("costLedgerId", "journalLineId"),
+-- CONSTRAINT "costLedgerJournalLineRelation_costLedgerId_fkey" FOREIGN KEY ("costLedgerId") REFERENCES "costLedger"("id"),
+-- CONSTRAINT "costLedgerJournalLineRelation_journalLineId_fkey" FOREIGN KEY ("journalLineId") REFERENCES "journalLine"("id")
+-- );
-ALTER TABLE "costLedgerJournalLineRelation" ENABLE ROW LEVEL SECURITY;
+-- ALTER TABLE "costLedgerJournalLineRelation" ENABLE ROW LEVEL SECURITY;
-CREATE POLICY "Employees with accounting_view can view the value ledger/general ledger relations" ON "costLedgerJournalLineRelation"
- FOR SELECT
- USING (
- coalesce(get_my_claim('accounting_view')::boolean, false) = true AND
- (get_my_claim('role'::text)) = '"employee"'::jsonb
- );
+-- CREATE POLICY "Employees with accounting_view can view the value ledger/general ledger relations" ON "costLedgerJournalLineRelation"
+-- FOR SELECT
+-- USING (
+-- coalesce(get_my_claim('accounting_view')::boolean, false) = true AND
+-- (get_my_claim('role'::text)) = '"employee"'::jsonb
+-- );
CREATE TABLE "partLedger" (
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 92910b2095..d19e6f160e 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -1313,34 +1313,6 @@ export interface Database {
}
Relationships: []
}
- costLedgerJournalLineRelation: {
- Row: {
- costLedgerId: string
- journalLineId: string
- }
- Insert: {
- costLedgerId: string
- journalLineId: string
- }
- Update: {
- costLedgerId?: string
- journalLineId?: string
- }
- Relationships: [
- {
- foreignKeyName: "costLedgerJournalLineRelation_costLedgerId_fkey"
- columns: ["costLedgerId"]
- referencedRelation: "costLedger"
- referencedColumns: ["id"]
- },
- {
- foreignKeyName: "costLedgerJournalLineRelation_journalLineId_fkey"
- columns: ["journalLineId"]
- referencedRelation: "journalLine"
- referencedColumns: ["id"]
- }
- ]
- }
country: {
Row: {
code: string
diff --git a/packages/database/supabase/functions/post-purchase-invoice/index.ts b/packages/database/supabase/functions/post-purchase-invoice/index.ts
index b16bed7818..921f1733f6 100644
--- a/packages/database/supabase/functions/post-purchase-invoice/index.ts
+++ b/packages/database/supabase/functions/post-purchase-invoice/index.ts
@@ -884,7 +884,7 @@ serve(async (req: Request) => {
const journalId = journal[0].id;
if (!journalId) throw new Error("Failed to insert journal");
- const journalLineIds = await trx
+ await trx
.insertInto("journalLine")
.values(
journalLineInserts.map((journalLine) => ({
@@ -895,34 +895,20 @@ serve(async (req: Request) => {
.returning(["id"])
.execute();
- if (costLedgerInserts.length > 0) {
+ if (partLedgerInserts.length > 0) {
await trx
.insertInto("partLedger")
.values(partLedgerInserts)
.returning(["id"])
.execute();
+ }
- const costLedgerIds = await trx
+ if (costLedgerInserts.length > 0) {
+ 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<
- Database["public"]["Tables"]["costLedgerJournalLineRelation"]["Insert"]
- >((journalLineId, i) => ({
- journalLineId: journalLineId.id!,
- costLedgerId:
- costLedgerIds[Math.floor(i / journalLinesPerCostEntry)].id!,
- }));
-
- await trx
- .insertInto("costLedgerJournalLineRelation")
- .values(costLedgerJournalLineRelationInserts)
- .execute();
}
await trx
diff --git a/packages/database/supabase/functions/post-receipt/index.ts b/packages/database/supabase/functions/post-receipt/index.ts
index 1983285566..024763bde0 100644
--- a/packages/database/supabase/functions/post-receipt/index.ts
+++ b/packages/database/supabase/functions/post-receipt/index.ts
@@ -556,12 +556,6 @@ serve(async (req: Request) => {
.where("id", "=", receipt.data.sourceDocumentId)
.execute();
- await trx
- .insertInto("partLedger")
- .values(partLedgerInserts)
- .returning(["id"])
- .execute();
-
const journal = await trx
.insertInto("journal")
.values({
@@ -575,7 +569,7 @@ serve(async (req: Request) => {
const journalId = journal[0].id;
if (!journalId) throw new Error("Failed to insert journal");
- const journalLineIds = await trx
+ await trx
.insertInto("journalLine")
.values(
journalLineInserts.map((journalLine) => ({
@@ -586,28 +580,19 @@ serve(async (req: Request) => {
.returning(["id"])
.execute();
- if (costLedgerInserts.length > 0) {
- const costLedgerIds = await trx
- .insertInto("costLedger")
- .values(costLedgerInserts)
+ if (partLedgerInserts.length > 0) {
+ await trx
+ .insertInto("partLedger")
+ .values(partLedgerInserts)
.returning(["id"])
.execute();
+ }
- // 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<
- Database["public"]["Tables"]["costLedgerJournalLineRelation"]["Insert"]
- >((journalLineId, i) => ({
- journalLineId: journalLineId.id!,
- costLedgerId:
- costLedgerIds[Math.floor(i / journalLinesPerCostEntry)].id!,
- }));
-
+ if (costLedgerInserts.length > 0) {
await trx
- .insertInto("costLedgerJournalLineRelation")
- .values(costLedgerJournalLineRelationInserts)
+ .insertInto("costLedger")
+ .values(costLedgerInserts)
+ .returning(["id"])
.execute();
}
diff --git a/packages/database/supabase/migrations/20230705033432_ledgers.sql b/packages/database/supabase/migrations/20230705033432_ledgers.sql
index 4f4aa993f2..4efb30f089 100644
--- a/packages/database/supabase/migrations/20230705033432_ledgers.sql
+++ b/packages/database/supabase/migrations/20230705033432_ledgers.sql
@@ -196,23 +196,23 @@ CREATE POLICY "Employees with accounting_view can view the value ledger" ON "cos
(get_my_claim('role'::text)) = '"employee"'::jsonb
);
-CREATE TABLE "costLedgerJournalLineRelation" (
- "costLedgerId" TEXT NOT NULL,
- "journalLineId" TEXT NOT NULL,
-
- CONSTRAINT "costLedgerJournalLineRelation_pkey" PRIMARY KEY ("costLedgerId", "journalLineId"),
- CONSTRAINT "costLedgerJournalLineRelation_costLedgerId_fkey" FOREIGN KEY ("costLedgerId") REFERENCES "costLedger"("id"),
- CONSTRAINT "costLedgerJournalLineRelation_journalLineId_fkey" FOREIGN KEY ("journalLineId") REFERENCES "journalLine"("id")
-);
-
-ALTER TABLE "costLedgerJournalLineRelation" ENABLE ROW LEVEL SECURITY;
-
-CREATE POLICY "Employees with accounting_view can view the value ledger/general ledger relations" ON "costLedgerJournalLineRelation"
- FOR SELECT
- USING (
- coalesce(get_my_claim('accounting_view')::boolean, false) = true AND
- (get_my_claim('role'::text)) = '"employee"'::jsonb
- );
+-- CREATE TABLE "costLedgerJournalLineRelation" (
+-- "costLedgerId" TEXT NOT NULL,
+-- "journalLineId" TEXT NOT NULL,
+
+-- CONSTRAINT "costLedgerJournalLineRelation_pkey" PRIMARY KEY ("costLedgerId", "journalLineId"),
+-- CONSTRAINT "costLedgerJournalLineRelation_costLedgerId_fkey" FOREIGN KEY ("costLedgerId") REFERENCES "costLedger"("id"),
+-- CONSTRAINT "costLedgerJournalLineRelation_journalLineId_fkey" FOREIGN KEY ("journalLineId") REFERENCES "journalLine"("id")
+-- );
+
+-- ALTER TABLE "costLedgerJournalLineRelation" ENABLE ROW LEVEL SECURITY;
+
+-- CREATE POLICY "Employees with accounting_view can view the value ledger/general ledger relations" ON "costLedgerJournalLineRelation"
+-- FOR SELECT
+-- USING (
+-- coalesce(get_my_claim('accounting_view')::boolean, false) = true AND
+-- (get_my_claim('role'::text)) = '"employee"'::jsonb
+-- );
CREATE TABLE "partLedger" (