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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat: convert to async task
Brad Barbin5663dedc0627
diff --git a/apps/carbon/app/modules/sales/ui/Quotation/index.ts b/apps/carbon/app/modules/sales/ui/Quotation/index.tsindex 24293ebb17..6d3ab6d004 100644--- a/apps/carbon/app/modules/sales/ui/Quotation/index.ts+++ b/apps/carbon/app/modules/sales/ui/Quotation/index.ts@@ -9,7 +9,7 @@ import QuotationMaterialForm from "./QuotationMaterialForm";import QuotationMaterialLines from "./QuotationMaterialLines";import QuotationOperationForm from "./QuotationOperationForm";import QuotationStatus from "./QuotationStatus";-import { useQuotation } from "./useQuotation";+import { useQuotation, useQuotationLinePriceEffects } from "./useQuotation";export {QuotationAssemblyForm,QuotationDocuments,@@ -23,4 +23,5 @@ export {QuotationOperationForm,QuotationStatus,useQuotation,+ useQuotationLinePriceEffects,};diff --git a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsxindex eec5db6cfe..efd6c7ccae 100644--- a/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx+++ b/apps/carbon/app/modules/sales/ui/Quotation/useQuotation.tsx@@ -1,6 +1,7 @@import type { Database } from "@carbon/database";+import { useStore as useValue } from "@nanostores/react";import type { SupabaseClient } from "@supabase/supabase-js";-import { atom, computed } from "nanostores";+import { atom, computed, task } from "nanostores";import { useNanoStore } from "~/hooks";import type {Quotation,@@ -38,217 +39,229 @@ const $quotationStore = atom<QuoteStore>({materials: [],});-const $quotationLinePriceEffects = computed($quotationStore, (store) => {- if (!store.quote || !store.client)- return {} as Record<string, LinePriceEffects>;-- const assembliesByLineId = store.assemblies?.reduce<- Record<string, QuotationAssembly[]>- >((acc, assembly) => {- if (!acc[assembly.quoteLineId]) {- acc[assembly.quoteLineId] = [];- }- acc[assembly.quoteLineId].push(assembly);- return acc;- }, {});-- const operationsByLineId = store.operations?.reduce<- Record<string, QuotationOperation[]>- >((acc, operation) => {- if (!acc[operation.quoteLineId]) {- acc[operation.quoteLineId] = [];- }- acc[operation.quoteLineId].push(operation);- return acc;- }, {});-- let materialsByOperationId = store.materials?.reduce<- Record<string, QuotationMaterial[]>- >((acc, material) => {- if (!acc[material.quoteOperationId]) {- acc[material.quoteOperationId] = [];- }- acc[material.quoteOperationId].push(material);- 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>+const $quotationLinePriceEffects = computed($quotationStore, (store) =>+ task(async () => {+ if (!store.quote || !store.client)+ return {} as Record<string, LinePriceEffects>;++ // TODO: fetch quoteLineQuantities++ const assembliesByLineId = store.assemblies?.reduce<+ Record<string, QuotationAssembly[]>>((acc, assembly) => {- acc[assembly.id] = assembly;+ if (!acc[assembly.quoteLineId]) {+ acc[assembly.quoteLineId] = [];+ }+ acc[assembly.quoteLineId].push(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;- }-- let parent = assembliesById[asm.parentAssemblyId];- quantity *= parent.quantityPerParent ?? 1;- asm = parent;+ const operationsByLineId = store.operations?.reduce<+ Record<string, QuotationOperation[]>+ >((acc, operation) => {+ if (!acc[operation.quoteLineId]) {+ acc[operation.quoteLineId] = [];}+ acc[operation.quoteLineId].push(operation);+ return acc;+ }, {});- acc[assembly.id] = quantity;+ let materialsByOperationId = store.materials?.reduce<+ Record<string, QuotationMaterial[]>+ >((acc, material) => {+ if (!acc[material.quoteOperationId]) {+ acc[material.quoteOperationId] = [];+ }+ acc[material.quoteOperationId].push(material);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;- });+ const linePriceEffects = store?.lines?.reduce<+ Record<string, LinePriceEffects>+ >((effects, line) => {+ effects[line.id] = {+ materialCost: [],+ laborCost: [],+ overheadCost: [],+ setupHours: [],+ productionHours: [],+ };- 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);- });- }- }+ let assembliesById = assembliesByLineId[line.id]?.reduce<+ Record<string, QuotationAssembly>+ >((acc, assembly) => {+ acc[assembly.id] = assembly;+ return acc;+ }, {});- 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:+ 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;}- if (- ["Total Hours", "Total Minutes"].includes(operation.standardFactor)- ) {- effects[line.id].productionHours.push((_quantity) => {- return hoursPerProductionStandard;+ 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.setupHours) {+ effects[line.id].setupHours.push((_quantity) => {+ return operation.setupHours;});if (operation.quotingRate) {effects[line.id].overheadCost.push((_quantity) => {- return hoursPerProductionStandard * (operation.quotingRate ?? 0);+ return operation.setupHours * (operation.quotingRate ?? 0);});} else {effects[line.id].laborCost.push((_quantity) => {- return hoursPerProductionStandard * (operation.laborRate ?? 0);+ return operation.setupHours * (operation.laborRate ?? 0);});effects[line.id].overheadCost.push((_quantity) => {- return hoursPerProductionStandard * (operation.overheadRate ?? 0);+ return operation.setupHours * (operation.overheadRate ?? 0);});}- } else {- 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)- );+ }++ 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)+ ) {+ effects[line.id].productionHours.push((_quantity) => {+ return hoursPerProductionStandard;});+ if (operation.quotingRate) {+ effects[line.id].overheadCost.push((_quantity) => {+ return (+ hoursPerProductionStandard * (operation.quotingRate ?? 0)+ );+ });+ } else {+ effects[line.id].laborCost.push((_quantity) => {+ return hoursPerProductionStandard * (operation.laborRate ?? 0);+ });+ effects[line.id].overheadCost.push((_quantity) => {+ return (+ hoursPerProductionStandard * (operation.overheadRate ?? 0)+ );+ });+ }} else {- effects[line.id].laborCost.push((quantity) => {+ 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 linePriceEffects;-});+ // TODO: update quoteLineQuantities with the effects++ return linePriceEffects;+ })+);export const useQuotation = () => useNanoStore<QuoteStore>($quotationStore);+export const useQuotationLinePriceEffects = () =>+ useValue($quotationLinePriceEffects);diff --git a/apps/carbon/app/routes/x+/quote+/$id.tsx b/apps/carbon/app/routes/x+/quote+/$id.tsxindex 39d9434407..fe90a41993 100644--- a/apps/carbon/app/routes/x+/quote+/$id.tsx+++ b/apps/carbon/app/routes/x+/quote+/$id.tsx@@ -34,6 +34,7 @@ import {getQuoteMaterials,getQuoteOperations,useQuotation,+ useQuotationLinePriceEffects,} from "~/modules/sales";import { requirePermissions } from "~/services/auth";@@ -110,7 +111,9 @@ export default function QuotationRoute() {quotationMaterials,quotationOperations,} = useLoaderData<typeof loader>();+const [quote, setQuote] = useQuotation();+ useQuotationLinePriceEffects();useEffect(() => {setQuote({