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: solve for extended quantities per assembly

Brad Barbinc67dc724319a
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
index 71742e92de..53d248e349 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
@@ -11,18 +11,13 @@ import type { BillOfMaterialNode } from "~/modules/shared";
import { path } from "~/utils/path";
import { useQuotationMenu } from "./useQuotation";
-const QuotationExplorerItem = ({
- type,
- id,
- parentId,
- label,
- meta,
- params,
- pathname,
-}: Omit<BillOfMaterialNode, "children"> & {
- params: Params<string>;
- pathname: string;
-}) => {
+const QuotationExplorerItem = (
+ node: Omit<BillOfMaterialNode, "children"> & {
+ params: Params<string>;
+ pathname: string;
+ }
+) => {
+ const { type, id, parentId, label, meta, params, pathname } = node;
let isActive = false;
switch (type) {
case "assemblies":
@@ -159,7 +154,9 @@ const QuotationExplorerItem = ({
</Button>
);
default:
- throw new Error(`unknown type: ${type}`);
+ throw new Error(
+ `unknown type: ${type} for node: ${JSON.stringify(node, null, 2)}`
+ );
}
};
@@ -187,7 +184,7 @@ const QuotationExplorer = () => {
) => {
return menu.map((node) => {
return (
- <div className="w-full" role="group" key={node.id}>
+ <div className="w-full" role="group" key={`${node.id}-${node.type}`}>
<HStack
className="items-stretch w-full"
spacing={0}
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
index 94b4185af1..30b02467f6 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
@@ -18,14 +18,15 @@ import { IoMdTrash } from "react-icons/io";
import { MdMoreHoriz } from "react-icons/md";
import { EditableNumber } from "~/components/Editable";
import Grid from "~/components/Grid";
-import { usePermissions, useRouteData, useUser } from "~/hooks";
+import { usePermissions, useUser } from "~/hooks";
import { useSupabase } from "~/lib/supabase";
-import type {
- Quotation,
- QuotationLine,
- QuotationLineQuantity,
+import {
+ useQuotation,
+ type QuotationLine,
+ type QuotationLineQuantity,
} from "~/modules/sales";
import { path } from "~/utils/path";
+import { useQuotationLinePriceEffects } from "./useQuotation";
type QuotationLineQuantitiesProps = {
quotationLine: QuotationLine;
@@ -42,9 +43,8 @@ const QuotationLineQuantities = ({
const navigate = useNavigate();
- const routeData = useRouteData<{
- quotation: Quotation;
- }>(path.to.quote(id));
+ const [quotation] = useQuotation();
+ const lineEffects = useQuotationLinePriceEffects();
// TODO: use the currency of the quote
const formatter = new Intl.NumberFormat("en-US", {
@@ -73,7 +73,7 @@ const QuotationLineQuantities = ({
[supabase, userId]
);
- const isEditable = ["Draft"].includes(routeData?.quotation?.status ?? "");
+ const isEditable = ["Draft"].includes(quotation?.quote?.status ?? "");
const isMade = quotationLine.replenishmentSystem === "Make";
const columns = useMemo<ColumnDef<QuotationLineQuantity>[]>(() => {
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
index ea92112315..2625bb839b 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
@@ -19,6 +19,16 @@ type Quote = {
materials: QuotationMaterial[];
};
+type Effect = (quantity: number) => number;
+
+type LinePriceEffects = {
+ materialCost: Effect[];
+ laborCost: Effect[];
+ overheadCost: Effect[];
+ setupHours: Effect[];
+ productionHours: Effect[];
+};
+
const $quotationStore = atom<Quote>({
quote: undefined,
lines: [],
@@ -27,6 +37,90 @@ const $quotationStore = atom<Quote>({
materials: [],
});
+const defaultItems = {
+ assemblies: [],
+ operations: [],
+};
+
+const $quotationLinePriceEffects = computed($quotationStore, (store: Quote) => {
+ // vroom vroom
+ if (!store.quote) return [];
+ const linePriceEffects: Record<string, LinePriceEffects> = {};
+
+ const itemsByLineId: Record<
+ string,
+ {
+ assemblies: QuotationAssembly[];
+ operations: QuotationOperation[];
+ }
+ > = {};
+
+ store.assemblies.forEach((assembly) => {
+ if (!itemsByLineId[assembly.quoteLineId]) {
+ itemsByLineId[assembly.quoteLineId] = defaultItems;
+ }
+ itemsByLineId[assembly.quoteLineId].assemblies.push(assembly);
+ });
+
+ store.operations.forEach((operation) => {
+ if (!itemsByLineId[operation.quoteLineId]) {
+ itemsByLineId[operation.quoteLineId] = defaultItems;
+ }
+ itemsByLineId[operation.quoteLineId].operations.push(operation);
+ });
+
+ const materialsByOperationId = store.materials.reduce<
+ Record<string, QuotationMaterial[]>
+ >((acc, material) => {
+ if (!acc[material.quoteOperationId]) {
+ acc[material.quoteOperationId] = [];
+ }
+ acc[material.quoteOperationId].push(material);
+ return acc;
+ }, {});
+
+ store.lines.forEach((line) => {
+ linePriceEffects[line.id] = {
+ materialCost: [],
+ laborCost: [],
+ overheadCost: [],
+ setupHours: [],
+ productionHours: [],
+ };
+
+ const assembliesById = itemsByLineId[line.id].assemblies.reduce<
+ Record<string, QuotationAssembly>
+ >((acc, assembly) => {
+ acc[assembly.id] = assembly;
+ return acc;
+ }, {});
+
+ const extendedQuantitiesPerAssembly: Record<string, number> = {};
+
+ itemsByLineId[line.id].assemblies.forEach((assembly: QuotationAssembly) => {
+ let quantity = assembly.quantityPerParent ?? 1;
+ let asm = assembliesById[assembly.id];
+ while (asm.parentAssemblyId) {
+ // memoize the results
+ if (extendedQuantitiesPerAssembly[asm.parentAssemblyId]) {
+ quantity *= extendedQuantitiesPerAssembly[asm.parentAssemblyId];
+ break;
+ }
+
+ const parent = assembliesById[asm.parentAssemblyId];
+ quantity *= parent.quantityPerParent ?? 1;
+ asm = parent;
+ }
+
+ extendedQuantitiesPerAssembly[assembly.id] = quantity;
+ });
+
+ console.log({ extendedQuantitiesPerAssembly });
+ });
+
+ return true;
+});
+
const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
if (!store.quote) return [];
@@ -82,10 +176,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
store.assemblies.forEach((assembly) => {
if (!itemsByLineId[assembly.quoteLineId]) {
- itemsByLineId[assembly.quoteLineId] = {
- assemblies: [],
- operations: [],
- };
+ itemsByLineId[assembly.quoteLineId] = defaultItems;
}
itemsByLineId[assembly.quoteLineId].assemblies.push({
id: assembly.id,
@@ -98,10 +189,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
store.operations.forEach((operation) => {
if (!itemsByLineId[operation.quoteLineId]) {
- itemsByLineId[operation.quoteLineId] = {
- assemblies: [],
- operations: [],
- };
+ itemsByLineId[operation.quoteLineId] = defaultItems;
}
itemsByLineId[operation.quoteLineId].operations.push({
id: operation.id,
@@ -202,3 +290,5 @@ function traverseTree(
export const useQuotation = () => useNanoStore<Quote>($quotationStore);
export const useQuotationMenu = () => useValue($quotationMenuStore);
+export const useQuotationLinePriceEffects = () =>
+ useValue($quotationLinePriceEffects);