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: include demand forecast in item planning chart

Brad Barbin69a76ea48120
diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.ts
index 3564155c49..24e302abe8 100644
--- a/apps/erp/app/modules/items/items.service.ts
+++ b/apps/erp/app/modules/items/items.service.ts
@@ -480,13 +480,28 @@ export async function getItemDemand(
companyId: string;
}
) {
- return client
- .from("demandActual")
- .select("*")
- .eq("itemId", itemId)
- .eq("locationId", locationId)
- .eq("companyId", companyId)
- .in("periodId", periods);
+ const [actuals, forecasts] = await Promise.all([
+ client
+ .from("demandActual")
+ .select("*")
+ .eq("itemId", itemId)
+ .eq("locationId", locationId)
+ .eq("companyId", companyId)
+ .in("periodId", periods),
+ client
+ .from("demandForecast")
+ .select("*")
+ .eq("itemId", itemId)
+ .eq("locationId", locationId)
+ .eq("companyId", companyId)
+ .in("periodId", periods)
+ .order("periodId"),
+ ]);
+
+ return {
+ actuals: actuals.data ?? [],
+ forecasts: forecasts.data ?? [],
+ };
}
export async function getItemFiles(
diff --git a/apps/erp/app/modules/items/ui/Item/ItemPlanningChart.tsx b/apps/erp/app/modules/items/ui/Item/ItemPlanningChart.tsx
index 89337b44ce..7493671c27 100644
--- a/apps/erp/app/modules/items/ui/Item/ItemPlanningChart.tsx
+++ b/apps/erp/app/modules/items/ui/Item/ItemPlanningChart.tsx
@@ -69,6 +69,7 @@ interface ChartDataPoint {
"Production Order": number;
Planned: number;
Projection: number;
+ "Demand Forecast": number;
}
const chartConfig = {} satisfies ChartConfig;
@@ -109,6 +110,7 @@ export const ItemPlanningChart = ({
const periods = forecastFetcher.data.periods;
const demand = forecastFetcher.data.demand;
+ const demandForecast = forecastFetcher.data.demandForecast ?? [];
const supply = forecastFetcher.data.supply;
let currentQuantity = forecastFetcher.data.quantityOnHand ?? 0;
@@ -124,6 +126,7 @@ export const ItemPlanningChart = ({
"Production Order": 0,
Planned: 0,
Projection: currentQuantity, // Initialize with current quantity
+ "Demand Forecast": 0,
};
return acc;
},
@@ -185,6 +188,15 @@ export const ItemPlanningChart = ({
}
});
+ // Add demand forecast data
+ demandForecast.forEach((forecast) => {
+ if (groupedData[forecast.periodId] && forecast.forecastQuantity) {
+ groupedData[forecast.periodId]["Demand Forecast"] = -(
+ forecast.forecastQuantity ?? 0
+ );
+ }
+ });
+
// Calculate running projection
let runningProjection = currentQuantity;
const sortedData = Object.values(groupedData).sort(
@@ -198,8 +210,11 @@ export const ItemPlanningChart = ({
period["Purchase Order"] +
period["Production Order"] +
period["Planned"];
- // Subtract demand
- runningProjection += period["Sales Order"] + period["Job Material"];
+ // Subtract demand (all values are already negative)
+ runningProjection +=
+ period["Sales Order"] +
+ period["Job Material"] +
+ period["Demand Forecast"];
// Update projection
period.Projection = runningProjection;
return period;
@@ -293,7 +308,8 @@ export const ItemPlanningChart = ({
if (
forecastFetcher.data?.demand.length === 0 &&
- forecastFetcher.data?.supply.length === 0
+ forecastFetcher.data?.supply.length === 0 &&
+ (forecastFetcher.data?.demandForecast?.length ?? 0) === 0
) {
return (
<Card>
@@ -341,6 +357,11 @@ export const ItemPlanningChart = ({
<Legend
payload={[
{ value: "Demand", type: "rect", color: "#14b8a6" },
+ {
+ value: "Demand Forecast",
+ type: "rect",
+ color: "#06b6d4",
+ },
{ value: "Supply", type: "rect", color: "#2563eb" },
{ value: "Planned", type: "rect", color: "#4f46e5" },
{
@@ -387,6 +408,11 @@ export const ItemPlanningChart = ({
className="fill-teal-500"
/>
))}
+ <Bar
+ dataKey="Demand Forecast"
+ stackId="stack"
+ className="fill-cyan-500"
+ />
{supplySourceTypes.map((sourceType) => (
<Bar
key={sourceType}
@@ -462,10 +488,14 @@ export const ItemPlanningChart = ({
<CardTitle className="text-4xl ">
<div className="flex justify-start items-center gap-1">
{`${numberFormatter.format(
- forecastFetcher.data?.demand.reduce(
+ (forecastFetcher.data?.demand.reduce(
(acc, curr) => acc + (curr.actualQuantity ?? 0),
0
- ) ?? 0
+ ) ?? 0) +
+ (forecastFetcher.data?.demandForecast?.reduce(
+ (acc, curr) => acc + (curr.forecastQuantity ?? 0),
+ 0
+ ) ?? 0)
)}`}
<LuMoveDown className="text-red-500 text-lg" />
</div>
diff --git a/apps/erp/app/routes/api+/items.$id.$locationId.forecast.ts b/apps/erp/app/routes/api+/items.$id.$locationId.forecast.ts
index 1b0e597d4e..c22f047033 100644
--- a/apps/erp/app/routes/api+/items.$id.$locationId.forecast.ts
+++ b/apps/erp/app/routes/api+/items.$id.$locationId.forecast.ts
@@ -16,6 +16,7 @@ import { getPeriods } from "~/modules/shared/shared.service";
const defaultResponse = {
demand: [],
+ demandForecast: [],
supply: [],
periods: [],
quantityOnHand: 0,
@@ -78,15 +79,16 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
getOpenPurchaseOrderLines(client, { itemId, companyId, locationId }),
]);
- if (demand.error) {
+ if (demand.actuals.length === 0 && demand.forecasts.length === 0) {
return json(
defaultResponse,
- await flash(request, error(demand.error, "Failed to load demand"))
+ await flash(request, error(null, "Failed to load demand"))
);
}
return json({
- demand: demand.data,
+ demand: demand.actuals,
+ demandForecast: demand.forecasts,
supply: [
...supply.actuals,
...supply.forecasts.map((f) => ({