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
// 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 { assertIsPost, error, notFound, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { validationError, validator } from "@carbon/form";
import { useCloseRoute } from "@carbon/react";
import { redirect } from "@carbon/utils";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { useLoaderData } from "react-router";
import { fixedAssetValidator } from "~/modules/accounting/accounting.models";
import {
getFixedAsset,
updateFixedAsset
} from "~/modules/accounting/accounting.service";
import { FixedAssetForm } from "~/modules/accounting/ui/FixedAssets";
import { path } from "~/utils/path";
export async function loader({ request, params }: LoaderFunctionArgs) {
const { client, companyId } = await requirePermissions(request, {
view: "accounting"
});
const { fixedAssetId } = params;
if (!fixedAssetId) throw notFound("fixedAssetId not found");
const asset = await getFixedAsset(client, fixedAssetId, companyId);
if (asset.error) {
throw redirect(
path.to.fixedAssets,
await flash(request, error(asset.error, "Failed to get fixed asset"))
);
}
return {
asset: asset.data
};
}
export async function action({ request, params }: ActionFunctionArgs) {
assertIsPost(request);
const { client, userId } = await requirePermissions(request, {
update: "accounting"
});
const formData = await request.formData();
const validation = await validator(fixedAssetValidator).validate(formData);
if (validation.error) {
return validationError(validation.error);
}
const { id, ...d } = validation.data;
if (!id) throw notFound("Fixed Asset ID was not found");
const result = await updateFixedAsset(client, {
id,
fixedAssetClassId: d.fixedAssetClassId,
name: d.name,
description: d.description ?? null,
serialNumber: d.serialNumber ?? null,
depreciationMethod: d.depreciationMethod,
usefulLifeMonths: d.usefulLifeMonths,
residualValuePercent: d.residualValuePercent,
assetLifetimeUsage: d.assetLifetimeUsage ?? null,
locationId: d.locationId ?? null,
workCenterId: d.workCenterId ?? null,
taxDepreciationMethod: d.taxDepreciationMethod ?? null,
taxUsefulLifeMonths: d.taxUsefulLifeMonths ?? null,
taxResidualValuePercent: d.taxResidualValuePercent ?? null,
macrsPropertyClass: d.macrsPropertyClass ?? null,
macrsConvention: d.macrsConvention ?? null,
bonusDepreciationPercent: d.bonusDepreciationPercent ?? null,
updatedBy: userId
});
if (result.error) {
throw redirect(
path.to.fixedAssets,
await flash(request, error(result.error, "Failed to update fixed asset"))
);
}
const { fixedAssetId } = params;
throw redirect(
path.to.fixedAsset(fixedAssetId!),
await flash(request, success("Fixed asset updated"))
);
}
export default function FixedAssetDetailsRoute() {
const { asset } = useLoaderData<typeof loader>();
const closeRoute = useCloseRoute();
const initialValues = {
id: asset.id,
fixedAssetClassId: asset.fixedAssetClassId,
name: asset.name,
description: asset.description ?? "",
serialNumber: asset.serialNumber ?? "",
depreciationMethod: asset.depreciationMethod,
usefulLifeMonths: asset.usefulLifeMonths,
residualValuePercent: Number(asset.residualValuePercent),
assetLifetimeUsage: asset.assetLifetimeUsage
? Number(asset.assetLifetimeUsage)
: undefined,
locationId: asset.locationId ?? undefined,
// The action writes `workCenterId ?? null`: unseeded, saving the form
// cleared the asset's work center.
workCenterId: asset.workCenterId ?? undefined,
taxDepreciationMethod: (asset as any).taxDepreciationMethod ?? undefined,
taxUsefulLifeMonths: (asset as any).taxUsefulLifeMonths ?? undefined,
taxResidualValuePercent:
(asset as any).taxResidualValuePercent != null
? Number((asset as any).taxResidualValuePercent)
: undefined,
macrsPropertyClass: (asset as any).macrsPropertyClass ?? undefined,
macrsConvention: (asset as any).macrsConvention ?? undefined,
bonusDepreciationPercent:
(asset as any).bonusDepreciationPercent != null
? Number((asset as any).bonusDepreciationPercent)
: undefined
};
return (
<FixedAssetForm
onClose={() => closeRoute()}
key={initialValues.id}
initialValues={initialValues}
/>
);
}