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: line prices for bought parts

Brad Barbinb247fe108a9c
diff --git a/apps/carbon/app/components/Editable/EditableQuotationLineQuantity.tsx b/apps/carbon/app/components/Editable/EditableQuotationLineQuantity.tsx
index d88d3c1c95..8d24d5d5d4 100644
--- a/apps/carbon/app/components/Editable/EditableQuotationLineQuantity.tsx
+++ b/apps/carbon/app/components/Editable/EditableQuotationLineQuantity.tsx
@@ -22,6 +22,7 @@ const EditableQuotationLineQuantity =
options: {
client?: SupabaseClient<Database>;
effects: LinePriceEffects;
+ isMade: boolean;
}
) =>
({
@@ -33,11 +34,17 @@ const EditableQuotationLineQuantity =
}: EditableTableCellComponentProps<QuotationLineQuantity>) => {
const updateNumber = async (newValue: string) => {
const quantity = Number(newValue);
- const { client, effects } = options;
+ const { client, effects, isMade } = options;
- const update = getLinePriceUpdate(quantity, effects);
+ const update = isMade
+ ? getLinePriceUpdate(quantity, effects)
+ : {
+ materialCost:
+ row.quantity === 0
+ ? row.materialCost
+ : (row.materialCost / row.quantity) * quantity,
+ };
- console.log({ quantity, effects, update });
// this is the optimistic update on the FE
onUpdate({ quantity, ...update });
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 48d304016b..61223c4ff3 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -357,7 +357,8 @@ export async function getQuoteLineQuantities(
return client
.from("quoteLineQuantity")
.select("*")
- .eq("quoteLineId", quoteLineId);
+ .eq("quoteLineId", quoteLineId)
+ .order("createdAt");
}
export async function getQuoteMaterials(
@@ -508,6 +509,7 @@ export async function insertQuoteLineQuantity(
quoteId: string;
quoteLineId: string;
quantity?: number;
+ materialCost?: number;
createdBy: string;
}
) {
@@ -516,22 +518,24 @@ export async function insertQuoteLineQuantity(
return quoteLine;
}
- const partId = quoteLine.data?.partId;
- const [partCost] = await Promise.all([
- client.from("partCost").select("unitCost").eq("partId", partId).single(),
- ]);
+ let materialCost = 0;
+ if (quoteLine.data?.replenishmentSystem === "Buy") {
+ const partId = quoteLine.data?.partId;
+ const [partCost] = await Promise.all([
+ client.from("partCost").select("unitCost").eq("partId", partId).single(),
+ ]);
+
+ if (partCost.error) {
+ return partCost;
+ }
- if (partCost.error) {
- return partCost;
+ materialCost = partCost.data?.unitCost;
}
return client.from("quoteLineQuantity").insert([
{
...quoteLineQuantity,
- materialCost:
- quoteLine.data.replenishmentSystem === "Make"
- ? partCost.data?.unitCost
- : 0,
+ materialCost,
},
]);
}
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx b/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
index 9a39ff1dab..f54a0d3acb 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/QuotationLineQuantities.tsx
@@ -48,7 +48,6 @@ const QuotationLineQuantities = ({
const [quotation] = useQuotation();
const linePriceEffects = useQuotationLinePriceEffects();
- console.log("linePriceEffects", linePriceEffects);
// TODO: use the currency of the quote
const formatter = new Intl.NumberFormat("en-US", {
@@ -211,14 +210,18 @@ const QuotationLineQuantities = ({
quantity: EditableQuotationLineQuantity(onCellEdit, {
client: supabase,
effects: linePriceEffects.effects[lineId],
+ isMade,
}),
additionalCost: EditableNumber(onCellEdit, { minValue: 0 }),
discountPercentage: EditableNumber(onCellEdit, { minValue: 0 }),
markupPercentage: EditableNumber(onCellEdit, { minValue: 0 }),
unitTaxAmount: EditableNumber(onCellEdit, { minValue: 0 }),
leadTime: EditableNumber(onCellEdit, { minValue: 0 }),
+ materialCost: isMade
+ ? undefined
+ : EditableNumber(onCellEdit, { minValue: 0 }),
}),
- [lineId, linePriceEffects.effects, onCellEdit, supabase]
+ [isMade, lineId, linePriceEffects.effects, onCellEdit, supabase]
);
const newRowButtonRef = useRef<HTMLButtonElement>(null);
@@ -240,6 +243,7 @@ const QuotationLineQuantities = ({
data={quotationLineQuantities}
canEdit={canEdit && isEditable}
columns={columns}
+ // @ts-ignore
editableComponents={editableComponents}
onNewRow={
canEdit && isEditable
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
index 1d38504cd5..bf9c86b918 100644
--- a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx
@@ -73,157 +73,157 @@ const $quotationLinePriceEffects = computed($quotationStore, (store) => {
return acc;
}, {});
- const linePriceEffects = store?.lines?.reduce<
- Record<string, LinePriceEffects>
- >((effects, line) => {
- effects[line.id] = {
- materialCost: [],
- laborCost: [],
- overheadCost: [],
- setupHours: [],
- productionHours: [],
- };
-
- let assembliesById = assembliesByLineId[line.id]?.reduce<
- Record<string, QuotationAssembly>
- >((acc, assembly) => {
- acc[assembly.id] = assembly;
- return acc;
- }, {});
+ const linePriceEffects = store?.lines
+ ?.filter((line) => line.replenishmentSystem === "Make")
+ .reduce<Record<string, LinePriceEffects>>((effects, line) => {
+ effects[line.id] = {
+ materialCost: [],
+ laborCost: [],
+ overheadCost: [],
+ setupHours: [],
+ productionHours: [],
+ };
+
+ let assembliesById = assembliesByLineId[line.id]?.reduce<
+ Record<string, QuotationAssembly>
+ >((acc, assembly) => {
+ acc[assembly.id] = assembly;
+ return acc;
+ }, {});
+
+ const extendedQuantitiesPerAssembly = assembliesByLineId[line.id]?.reduce<
+ Record<string, number>
+ >((acc, assembly: QuotationAssembly) => {
+ let quantity = assembly.quantityPerParent ?? 1;
+ let asm = assembliesById[assembly.id];
+ while (asm.parentAssemblyId) {
+ // memoize the results
+ if (acc[asm.parentAssemblyId]) {
+ quantity *= acc[asm.parentAssemblyId];
+ break;
+ }
- const extendedQuantitiesPerAssembly = assembliesByLineId[line.id]?.reduce<
- Record<string, number>
- >((acc, assembly: QuotationAssembly) => {
- let quantity = assembly.quantityPerParent ?? 1;
- let asm = assembliesById[assembly.id];
- while (asm.parentAssemblyId) {
- // memoize the results
- if (acc[asm.parentAssemblyId]) {
- quantity *= acc[asm.parentAssemblyId];
- break;
+ let parent = assembliesById[asm.parentAssemblyId];
+ quantity *= parent.quantityPerParent ?? 1;
+ asm = parent;
}
- let parent = assembliesById[asm.parentAssemblyId];
- quantity *= parent.quantityPerParent ?? 1;
- asm = parent;
- }
-
- acc[assembly.id] = quantity;
- return acc;
- }, {});
-
- console.log("extendedQuantitiesPerAssembly", extendedQuantitiesPerAssembly);
-
- operationsByLineId[line.id]?.forEach((operation: QuotationOperation) => {
- let materials = materialsByOperationId[operation.id] ?? [];
- let extendedQuantityPerAssembly = operation.quoteAssemblyId
- ? extendedQuantitiesPerAssembly[operation.quoteAssemblyId] ?? 1
- : 1;
-
- let materialCost = materials.reduce(
- (acc, material: QuotationMaterial) => {
- return acc + (material.quantity ?? 0) * (material.unitCost ?? 0);
- },
- 0
- );
-
- effects[line.id].materialCost.push((quantity) => {
- return materialCost * quantity * extendedQuantityPerAssembly;
- });
-
- if (operation.setupHours) {
- effects[line.id].setupHours.push((_quantity) => {
- return operation.setupHours;
+ acc[assembly.id] = quantity;
+ return acc;
+ }, {});
+
+ operationsByLineId[line.id]?.forEach((operation: QuotationOperation) => {
+ let materials = materialsByOperationId[operation.id] ?? [];
+ let extendedQuantityPerAssembly = operation.quoteAssemblyId
+ ? extendedQuantitiesPerAssembly[operation.quoteAssemblyId] ?? 1
+ : 1;
+
+ let materialCost = materials.reduce(
+ (acc, material: QuotationMaterial) => {
+ return acc + (material.quantity ?? 0) * (material.unitCost ?? 0);
+ },
+ 0
+ );
+
+ effects[line.id].materialCost.push((quantity) => {
+ return materialCost * quantity * extendedQuantityPerAssembly;
});
- if (operation.quotingRate) {
- effects[line.id].overheadCost.push((_quantity) => {
- return operation.setupHours * (operation.quotingRate ?? 0);
- });
- } else {
- effects[line.id].laborCost.push((_quantity) => {
- return operation.setupHours * (operation.laborRate ?? 0);
- });
- effects[line.id].overheadCost.push((_quantity) => {
- return operation.setupHours * (operation.overheadRate ?? 0);
- });
- }
- }
- if (operation.productionStandard) {
- let hoursPerProductionStandard = 0;
- switch (operation.standardFactor) {
- case "Total Hours":
- 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 "Total Minutes":
- 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;
- default:
- break;
+ if (operation.setupHours) {
+ effects[line.id].setupHours.push((_quantity) => {
+ return operation.setupHours;
+ });
+ if (operation.quotingRate) {
+ effects[line.id].overheadCost.push((_quantity) => {
+ return operation.setupHours * (operation.quotingRate ?? 0);
+ });
+ } else {
+ effects[line.id].laborCost.push((_quantity) => {
+ return operation.setupHours * (operation.laborRate ?? 0);
+ });
+ effects[line.id].overheadCost.push((_quantity) => {
+ return operation.setupHours * (operation.overheadRate ?? 0);
+ });
+ }
}
- effects[line.id].productionHours.push((quantity) => {
- return (
- hoursPerProductionStandard * quantity * extendedQuantityPerAssembly
- );
- });
- if (operation.quotingRate) {
- effects[line.id].overheadCost.push((quantity) => {
- return (
- hoursPerProductionStandard *
- quantity *
- extendedQuantityPerAssembly *
- (operation.quotingRate ?? 0)
- );
- });
- } else {
- effects[line.id].laborCost.push((quantity) => {
+ if (operation.productionStandard) {
+ let hoursPerProductionStandard = 0;
+ switch (operation.standardFactor) {
+ case "Total Hours":
+ 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 "Total Minutes":
+ 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;
+ default:
+ break;
+ }
+
+ effects[line.id].productionHours.push((quantity) => {
return (
hoursPerProductionStandard *
quantity *
- extendedQuantityPerAssembly *
- (operation.laborRate ?? 0)
- );
- });
- effects[line.id].overheadCost.push((quantity) => {
- return (
- hoursPerProductionStandard *
- quantity *
- extendedQuantityPerAssembly *
- (operation.overheadRate ?? 0)
+ extendedQuantityPerAssembly
);
});
+ if (operation.quotingRate) {
+ effects[line.id].overheadCost.push((quantity) => {
+ return (
+ hoursPerProductionStandard *
+ quantity *
+ extendedQuantityPerAssembly *
+ (operation.quotingRate ?? 0)
+ );
+ });
+ } else {
+ effects[line.id].laborCost.push((quantity) => {
+ return (
+ hoursPerProductionStandard *
+ quantity *
+ extendedQuantityPerAssembly *
+ (operation.laborRate ?? 0)
+ );
+ });
+ effects[line.id].overheadCost.push((quantity) => {
+ return (
+ hoursPerProductionStandard *
+ quantity *
+ extendedQuantityPerAssembly *
+ (operation.overheadRate ?? 0)
+ );
+ });
+ }
}
- }
- });
- return effects;
- }, {});
+ });
+ return effects;
+ }, {});
return {
client: store.client,
@@ -256,6 +256,9 @@ const $quotationLinePriceEffectsUpdate = computed(
const quantity = lineQuantity.quantity ?? 0;
const effects = store.effects[lineQuantity.quoteLineId];
+ // skip updates for parts that aren't make
+ if (!effects) continue;
+
const update = getLinePriceUpdate(quantity, effects);
const { error } = await store.client