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.

wip: super borked

Brad Barbined73beeaab9a
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
index 53d248e349..c65b44d2f7 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationExplorer.tsx
@@ -27,7 +27,13 @@ const QuotationExplorerItem = (
className="w-full justify-between text-muted-foreground"
asChild
>
- <Link to={path.to.newQuoteAssembly(params.id!, id, parentId)}>
+ <Link
+ to={path.to.newQuoteAssembly(
+ params.id!,
+ meta.quoteLineId,
+ parentId
+ )}
+ >
<span>{label}</span>
<IoMdAdd />
</Link>
@@ -55,7 +61,13 @@ const QuotationExplorerItem = (
className="w-full justify-between text-muted-foreground"
asChild
>
- <Link to={path.to.newQuoteOperation(params.id!, id, parentId)}>
+ <Link
+ to={path.to.newQuoteOperation(
+ params.id!,
+ meta.quoteLineId,
+ parentId
+ )}
+ >
<span>{label}</span>
<IoMdAdd />
</Link>
@@ -166,6 +178,7 @@ const QuotationExplorer = () => {
if (!params.id) throw new Error("id not found");
const menu = useQuotationMenu() as BillOfMaterialNode[];
+ console.log({ menu });
const [expandedNodes, setExpandedNodes] = useState<Record<string, boolean>>({
[params.id]: true,
@@ -184,7 +197,7 @@ const QuotationExplorer = () => {
) => {
return menu.map((node) => {
return (
- <div className="w-full" role="group" key={`${node.id}-${node.type}`}>
+ <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 30b02467f6..b1f339eeea 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
@@ -26,7 +26,6 @@ import {
type QuotationLineQuantity,
} from "~/modules/sales";
import { path } from "~/utils/path";
-import { useQuotationLinePriceEffects } from "./useQuotation";
type QuotationLineQuantitiesProps = {
quotationLine: QuotationLine;
@@ -44,7 +43,8 @@ const QuotationLineQuantities = ({
const navigate = useNavigate();
const [quotation] = useQuotation();
- const lineEffects = useQuotationLinePriceEffects();
+ // const lineEffects = useQuotationLinePriceEffects();
+ // console.log({ lineEffects });
// TODO: use the currency of the quote
const formatter = new Intl.NumberFormat("en-US", {
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
index 2625bb839b..afd3d178f2 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
@@ -42,6 +42,14 @@ const defaultItems = {
operations: [],
};
+const defaultEffects = {
+ materialCost: [],
+ laborCost: [],
+ overheadCost: [],
+ setupHours: [],
+ productionHours: [],
+};
+
const $quotationLinePriceEffects = computed($quotationStore, (store: Quote) => {
// vroom vroom
if (!store.quote) return [];
@@ -80,13 +88,7 @@ const $quotationLinePriceEffects = computed($quotationStore, (store: Quote) => {
}, {});
store.lines.forEach((line) => {
- linePriceEffects[line.id] = {
- materialCost: [],
- laborCost: [],
- overheadCost: [],
- setupHours: [],
- productionHours: [],
- };
+ linePriceEffects[line.id] = defaultEffects;
const assembliesById = itemsByLineId[line.id].assemblies.reduce<
Record<string, QuotationAssembly>
@@ -115,10 +117,122 @@ const $quotationLinePriceEffects = computed($quotationStore, (store: Quote) => {
extendedQuantitiesPerAssembly[assembly.id] = quantity;
});
- console.log({ extendedQuantitiesPerAssembly });
- });
+ itemsByLineId[line.id].operations.forEach(
+ (operation: QuotationOperation) => {
+ const materials = materialsByOperationId[operation.id] ?? [];
+ const extendedQuantityPerAssembly = operation.quoteAssemblyId
+ ? extendedQuantitiesPerAssembly[operation.quoteAssemblyId] ?? 1
+ : 1;
+
+ const materialCost = materials.reduce(
+ (acc, material: QuotationMaterial) => {
+ return acc + (material.quantity ?? 0) * (material.unitCost ?? 0);
+ },
+ 0
+ );
+
+ linePriceEffects[line.id].materialCost.push((quantity) => {
+ return materialCost * quantity * extendedQuantityPerAssembly;
+ });
+
+ if (operation.setupHours) {
+ linePriceEffects[line.id].setupHours.push((_quantity) => {
+ return operation.setupHours;
+ });
+ linePriceEffects[line.id].laborCost.push((_quantity) => {
+ return operation.setupHours * (operation.laborRate ?? 0);
+ });
+ linePriceEffects[line.id].overheadCost.push((_quantity) => {
+ return operation.setupHours * (operation.overheadRate ?? 0);
+ });
+ }
- return true;
+ if (operation.productionStandard) {
+ let hoursPerProductionStandard = 0;
+ switch (operation.standardFactor) {
+ case "Hours/Piece":
+ hoursPerProductionStandard = operation.productionStandard;
+ break;
+ case "Hours/100 Pieces":
+ hoursPerProductionStandard = operation.productionStandard / 100;
+ break;
+ case "Hours/1000 Pieces":
+ hoursPerProductionStandard = operation.productionStandard / 1000;
+ break;
+ case "Minutes/Piece":
+ hoursPerProductionStandard = operation.productionStandard / 60;
+ break;
+ case "Minutes/100 Pieces":
+ hoursPerProductionStandard =
+ operation.productionStandard / 100 / 60;
+ break;
+ case "Minutes/1000 Pieces":
+ hoursPerProductionStandard =
+ operation.productionStandard / 1000 / 60;
+ break;
+ case "Pieces/Hour":
+ hoursPerProductionStandard = 1 / operation.productionStandard;
+ break;
+ case "Pieces/Minute":
+ hoursPerProductionStandard =
+ 1 / (operation.productionStandard / 60);
+ break;
+ case "Seconds/Piece":
+ hoursPerProductionStandard = operation.productionStandard / 3600;
+ break;
+ case "Total Hours":
+ hoursPerProductionStandard = operation.productionStandard;
+ break;
+ case "Total Minutes":
+ hoursPerProductionStandard = operation.productionStandard / 60;
+ break;
+ default:
+ break;
+ }
+
+ if (
+ ["Total Hours", "Total Minutes"].includes(operation.standardFactor)
+ ) {
+ linePriceEffects[line.id].productionHours.push((_quantity) => {
+ return hoursPerProductionStandard;
+ });
+ linePriceEffects[line.id].laborCost.push((_quantity) => {
+ return hoursPerProductionStandard * (operation.laborRate ?? 0);
+ });
+ linePriceEffects[line.id].overheadCost.push((_quantity) => {
+ return hoursPerProductionStandard * (operation.overheadRate ?? 0);
+ });
+ } else {
+ linePriceEffects[line.id].productionHours.push((quantity) => {
+ return (
+ hoursPerProductionStandard *
+ quantity *
+ extendedQuantityPerAssembly
+ );
+ });
+ linePriceEffects[line.id].laborCost.push((quantity) => {
+ return (
+ hoursPerProductionStandard *
+ quantity *
+ extendedQuantityPerAssembly *
+ (operation.laborRate ?? 0)
+ );
+ });
+ linePriceEffects[line.id].overheadCost.push((quantity) => {
+ return (
+ hoursPerProductionStandard *
+ quantity *
+ extendedQuantityPerAssembly *
+ (operation.overheadRate ?? 0)
+ );
+ });
+ }
+ }
+ }
+ );
+
+ return linePriceEffects;
+ });
});
const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
@@ -199,7 +313,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
meta: operation,
children: [
{
- id: operation.id,
+ id: `${operation.id}-materials`,
label: "Materials",
type: "materials",
meta: operation,
@@ -227,7 +341,7 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
meta: line,
children: [
{
- id: line.id,
+ id: `${line.id}-assemblies`,
label: "Assemblies",
type: "assemblies",
children: itemsByLineId[line.id]?.assemblies
@@ -238,14 +352,16 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
}) as BillOfMaterialNode[]),
]
: [],
+ meta: line,
},
{
- id: line.id,
+ id: `${line.id}-operations`,
label: "Operations",
type: "operations",
children: itemsByLineId[line.id]?.operations.filter(
(operation) => operation.parentId === undefined
) as BillOfMaterialNode[],
+ meta: line,
},
],
})),
@@ -256,23 +372,27 @@ const $quotationMenuStore = computed($quotationStore, (store: Quote) => {
if (node.type === "assembly") {
node.children = [
{
- id: node.meta.quoteLineId,
+ id: `${node.id}-assemblies`,
parentId: node.id,
label: "Assemblies",
type: "assemblies",
- children: node.children,
+ children: node.children ? [...node.children] : [],
+ meta: node.meta,
},
{
- id: node.meta.quoteLineId,
+ id: `${node.id}-operations`,
parentId: node.id,
label: "Operations",
type: "operations",
children: operationsByAssemblyId[node.id] ?? [],
+ meta: node.meta,
},
];
}
});
+ console.log({ store });
+
return menu;
});