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.

9.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { requirePermissions } from "@carbon/auth/auth.server";
import {
  now,
  parseDateTime,
  toCalendarDateTime
} from "@internationalized/date";
import type { LoaderFunctionArgs } from "react-router";
import { KPIs } from "~/modules/production/production.models";
import { makeDurations } from "~/utils/duration";

type ProductionEvent = {
  startTime: string;
  endTime: string;
  workCenterId: string;
};

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {
    view: "production"
  });
  const url = new URL(request.url);
  const searchParams = new URLSearchParams(url.search);

  const start = String(searchParams.get("start"));
  const end = String(searchParams.get("end"));

  const startDate = toCalendarDateTime(parseDateTime(start));
  const endDate = toCalendarDateTime(parseDateTime(end));
  const currentDate = toCalendarDateTime(now("UTC"));

  const daysBetween = endDate.compare(startDate);

  // Calculate previous period dates
  const previousEnd = startDate;
  const previousStart = startDate.add({ days: -daysBetween });

  const interval = searchParams.get("interval");

  const { key } = params;

  if (
    !key ||
    !start ||
    !end ||
    !interval ||
    daysBetween < 1 ||
    daysBetween > 500
  )
    return {
      data: [],
      previousPeriodData: []
    };

  const kpi = KPIs.find((k) => k.key === key);
  if (!kpi)
    return {
      data: [],
      previousPeriodData: []
    };

  switch (kpi.key) {
    case "utilization": {
      const [workCenters, productionEvents, previousProductionEvents] =
        await Promise.all([
          client
            .from("workCenter")
            .select("id, name")
            .eq("companyId", companyId)
            .eq("active", true),
          client
            .from("productionEvent")
            .select("startTime, endTime, workCenterId")
            .eq("companyId", companyId)
            .gte("startTime", start)
            .or(`endTime.lte.${end},endTime.is.null`)
            .order("startTime", { ascending: false })
            .order("endTime", { ascending: false }),
          client
            .from("productionEvent")
            .select("startTime, endTime, workCenterId")
            .eq("companyId", companyId)
            .gte("startTime", previousStart.toString())
            .or(`endTime.lte.${previousEnd.toString()},endTime.is.null`)
            .order("startTime", { ascending: false })
            .order("endTime", { ascending: false })
        ]);

      const [groupedEvents, previousGroupedEvents] = [
        productionEvents.data ?? [],
        previousProductionEvents.data ?? []
      ].map((events) =>
        events.reduce<Record<string, ProductionEvent[]>>((acc, event) => {
          if (!event.workCenterId) return acc;

          if (!acc[event.workCenterId]) {
            acc[event.workCenterId] = [];
          }

          acc[event.workCenterId].push({
            ...event,
            workCenterId: event.workCenterId!,
            endTime:
              event.endTime === null ? currentDate.toString() : event.endTime
          });
          return acc;
        }, {})
      );

      const [data, previousPeriodData] = [
        groupedEvents,
        previousGroupedEvents
      ].map((events) =>
        Object.entries(events)
          .map(([workCenterId, events]) => {
            const workCenter = workCenters.data?.find(
              (wc) => wc.id === workCenterId
            );
            if (!workCenter) return { key: workCenterId, value: 0 };

            // Sort events by start time, then end time if start times are equal
            const sortedEvents = [...events].sort((a, b) => {
              const aStart = new Date(a.startTime).getTime();
              const bStart = new Date(b.startTime).getTime();
              if (aStart !== bStart) return aStart - bStart;
              return (
                new Date(a.endTime).getTime() - new Date(b.endTime).getTime()
              );
            });

            let totalTime = 0;
            let lastEndTime: number | null = null;

            // Calculate non-overlapping time
            for (const event of sortedEvents) {
              const startTime = new Date(event.startTime).getTime();
              const endTime = new Date(event.endTime).getTime();

              if (lastEndTime === null) {
                totalTime += endTime - startTime;
              } else {
                // If this event starts after the last end time, add the full duration
                if (startTime > lastEndTime) {
                  totalTime += endTime - startTime;
                }
                // If this event overlaps but ends later, add the non-overlapping portion
                else if (endTime > lastEndTime) {
                  totalTime += endTime - lastEndTime;
                }
                // If this event is completely contained within the last event, skip it
              }

              // Update lastEndTime if this event ends later
              if (lastEndTime === null || endTime > lastEndTime) {
                lastEndTime = endTime;
              }
            }

            return {
              key: workCenter.name,
              value: totalTime
            };
          })
          .sort((a, b) => b.value - a.value)
      );

      return {
        data,
        previousPeriodData
      };
    }
    case "estimatesVsActuals": {
      const jobs = await client
        .from("job")
        .select(
          "id, jobId, customerId, estimatedTime, actualTime, completedDate"
        )
        .eq("companyId", companyId)
        .gte("completedDate", start)
        .lte("completedDate", end)
        .not("completedDate", "is", null);

      if (jobs.error || !jobs.data || jobs.data.length === 0) {
        return {
          data: [],
          previousPeriodData: []
        };
      }

      const [jobOperations, productionEvents] = await Promise.all([
        client
          .from("jobOperation")
          .select("*")
          .in("jobId", jobs.data?.map((job) => job.id) ?? []),
        client
          .from("productionEvent")
          .select("*, ...jobOperation(jobId)")
          .eq("companyId", companyId)
          .in("jobOperation.jobId", jobs.data?.map((job) => job.id) ?? [])
      ]);

      const jobOperationsByJobId = jobOperations.data?.reduce(
        (acc, operation) => {
          if (!acc[operation.jobId]) {
            acc[operation.jobId] = [];
          }
          acc[operation.jobId].push(operation);
          return acc;
        },
        {} as Record<string, typeof jobOperations.data>
      );

      const productionEventsByJobId = productionEvents.data?.reduce(
        (acc, event) => {
          if (!acc[event.jobId]) {
            acc[event.jobId] = [];
          }
          acc[event.jobId].push(event);
          return acc;
        },
        {} as Record<string, typeof productionEvents.data>
      );

      const data: {
        key: string;
        actual: number;
        estimate: number;
        difference: number;
      }[] = [];

      // Calculate totals for each job
      for (const job of jobs.data) {
        const jobId = job.id;

        // Calculate estimated time from job operations
        const operations = jobOperationsByJobId?.[jobId] || [];
        const estimatedTime = operations.reduce((total, operation) => {
          const withDurations = makeDurations(operation);
          return total + withDurations.duration;
        }, 0);

        // Calculate actual time from production events
        const events = productionEventsByJobId?.[jobId] || [];
        const actualTime = events.reduce((total, event) => {
          const startTime = new Date(event.startTime).getTime();
          const endTime = event.endTime
            ? new Date(event.endTime).getTime()
            : new Date().getTime();
          return total + (endTime - startTime);
        }, 0);

        data.push({
          key: job.jobId,
          actual: actualTime,
          estimate: estimatedTime,
          difference:
            estimatedTime === 0
              ? 0
              : (actualTime - estimatedTime) / estimatedTime
        });
      }

      return {
        data: data.sort((a, b) => a.difference - b.difference),
        previousPeriodData: []
      };
    }

    case "completionTime": {
      const [jobs, previousJobs] = await Promise.all([
        client
          .from("job")
          .select("id, jobId, secondsToComplete")
          .eq("companyId", companyId)
          .gte("completedDate", start)
          .lte("completedDate", end)
          .not("completedDate", "is", null)
          .not("releasedDate", "is", null),
        client
          .from("job")
          .select("id, jobId, secondsToComplete")
          .eq("companyId", companyId)
          .gte("completedDate", previousStart.toString())
          .lte("completedDate", previousEnd.toString())
          .not("completedDate", "is", null)
          .not("releasedDate", "is", null)
      ]);

      const [data, previousPeriodData] = [
        jobs.data ?? [],
        previousJobs.data ?? []
      ].map((jobs) =>
        jobs
          .map((job) => ({
            key: job.jobId,
            value: (job.secondsToComplete ?? 0) * 1000
          }))
          .sort((a, b) => b.value - a.value)
      );

      return {
        data,
        previousPeriodData
      };
    }
    default:
      throw new Error(`Invalid KPI key: ${key}`);
  }
}