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.

2.6 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 { generateProductLabelZPL } from "@carbon/documents/zpl";
import { labelSizes, redirect } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import {
  getCompanyLogoForLabel,
  getCompanySettings,
  getDocumentTemplateConfig,
  resolveLabelLogo
} from "~/modules/settings/settings.service";
import { path } from "~/utils/path";
import { getStockTransferLabelItems } from "./labels.server";

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {
    view: "inventory"
  });

  const { id } = params;
  if (!id) throw new Error("Could not find id");

  const url = new URL(request.url);
  const labelParam = url.searchParams.get("labelSize");
  const lineIdParam = url.searchParams.get("lineId");

  const companySettings = await getCompanySettings(client, companyId);
  const labelSizeId =
    labelParam || companySettings.data?.productLabelSize || "label2x1";

  const labelSize = labelSizes.find((size) => size.id === labelSizeId);

  if (!labelSize) {
    throw new Error("Invalid label size");
  }

  if (!labelSize.zpl) {
    throw redirect(
      path.to.file.stockTransferLabelsPdf(id, {
        labelSize: labelSize.id,
        lineId: lineIdParam ?? undefined
      })
    );
  }

  const items = await getStockTransferLabelItems(
    client,
    companyId,
    id,
    lineIdParam ?? undefined
  );

  if (items.length === 0) {
    return new Response(
      `No tracked items found for stock transfer ${id}${
        lineIdParam ? ` and line ${lineIdParam}` : ""
      }`,
      { status: 404 }
    );
  }

  // Apply the tracking-label template + company logo, same as the other label
  // routes — stock-transfer labels should match the configured layout/branding.
  const template = await getDocumentTemplateConfig(
    client,
    companyId,
    "trackingLabel"
  );
  const companyLogo = await getCompanyLogoForLabel(client, companyId);
  const logo = await resolveLabelLogo(companyLogo, template, labelSize);

  const zplOutput = items
    .map((item) => generateProductLabelZPL(item, labelSize, template, logo))
    .join("\n");

  return new Response(zplOutput, {
    status: 200,
    headers: {
      "Content-Type": "application/zpl",
      "Content-Disposition": `attachment; filename="labels-${id}.zpl"`
    }
  });
}