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: add the ability to move files form the opportunity line to the item

Brad Barbinf82fdeb41aaf
diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.ts
index c3edaf1948..6cd13b2a4f 100644
--- a/apps/erp/app/modules/sales/sales.service.ts
+++ b/apps/erp/app/modules/sales/sales.service.ts
@@ -715,13 +715,27 @@ export async function getOpportunityDocuments(
export async function getOpportunityLineDocuments(
client: SupabaseClient<Database>,
companyId: string,
- lineId: string
-) {
- const result = await client.storage
- .from("private")
- .list(`${companyId}/opportunity-line/${lineId}`);
+ lineId: string,
+ itemId?: string | null
+) {
+ const [opportunityLineResult, itemResult] = await Promise.all([
+ client.storage
+ .from("private")
+ .list(`${companyId}/opportunity-line/${lineId}`),
+ itemId
+ ? client.storage.from("private").list(`${companyId}/parts/${itemId}`)
+ : Promise.resolve({ data: [] }),
+ ]);
+
+ const opportunityLineDocs =
+ opportunityLineResult.data?.map((f) => ({
+ ...f,
+ bucket: "opportunity-line",
+ })) ?? [];
+ const itemDocs =
+ itemResult.data?.map((f) => ({ ...f, bucket: "parts" })) ?? [];
- return result.data?.map((f) => ({ ...f, bucket: "opportunity-line" })) ?? [];
+ return [...opportunityLineDocs, ...itemDocs];
}
export async function getQuote(
diff --git a/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx b/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx
index d3683a23c1..a7cc8dc550 100644
--- a/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx
+++ b/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx
@@ -8,6 +8,11 @@ import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
+ DropdownMenuRadioGroup,
+ DropdownMenuRadioItem,
+ DropdownMenuSub,
+ DropdownMenuSubContent,
+ DropdownMenuSubTrigger,
DropdownMenuTrigger,
File,
HStack,
@@ -34,6 +39,7 @@ import { path } from "~/utils/path";
import { useCarbon } from "@carbon/auth";
import { useCallback } from "react";
+import { Enumerable } from "~/components/Enumerable";
import type { OptimisticFileObject } from "~/modules/shared";
import type { ModelUpload } from "~/types";
import { stripSpecialCharacters } from "~/utils/string";
@@ -41,10 +47,12 @@ import { stripSpecialCharacters } from "~/utils/string";
const useOpportunityLineDocuments = ({
id,
lineId,
+ itemId,
type,
}: {
id: string;
lineId: string;
+ itemId?: string | null;
type: "Request for Quote" | "Sales Order" | "Quote" | "Sales Invoice";
}) => {
const permissions = usePermissions();
@@ -57,19 +65,28 @@ const useOpportunityLineDocuments = ({
const canUpdate = permissions.can("update", "sales");
const getPath = useCallback(
- (file: { name: string }) => {
+ (
+ file: { name: string },
+ bucket: "opportunity-line" | "parts" = "opportunity-line"
+ ) => {
+ if (bucket === "parts" && itemId) {
+ return `${company.id}/parts/${itemId}/${stripSpecialCharacters(
+ file.name
+ )}`;
+ }
return `${company.id}/opportunity-line/${lineId}/${stripSpecialCharacters(
file.name
)}`;
},
- [company.id, lineId]
+ [company.id, lineId, itemId]
);
const deleteFile = useCallback(
- async (file: FileObject) => {
+ async (file: FileObject & { bucket?: string }) => {
+ const bucket = file.bucket === "parts" ? "parts" : "opportunity-line";
const fileDelete = await carbon?.storage
.from("private")
- .remove([getPath(file)]);
+ .remove([getPath(file, bucket as "opportunity-line" | "parts")]);
if (!fileDelete || fileDelete.error) {
toast.error(fileDelete?.error?.message || "Error deleting file");
@@ -165,8 +182,11 @@ const useOpportunityLineDocuments = ({
);
const download = useCallback(
- async (file: FileObject) => {
- const url = path.to.file.previewFile(`private/${getPath(file)}`);
+ async (file: FileObject & { bucket?: string }) => {
+ const bucket = file.bucket === "parts" ? "parts" : "opportunity-line";
+ const url = path.to.file.previewFile(
+ `private/${getPath(file, bucket as "opportunity-line" | "parts")}`
+ );
try {
const response = await fetch(url);
const blob = await response.blob();
@@ -199,10 +219,12 @@ const useOpportunityLineDocuments = ({
path: filePath,
name,
size,
+ bucket = "opportunity-line",
}: {
path: string;
name: string;
size: number;
+ bucket?: "opportunity-line" | "parts";
}) => {
const formData = new FormData();
formData.append("path", filePath);
@@ -222,14 +244,22 @@ const useOpportunityLineDocuments = ({
);
const upload = useCallback(
- async (files: File[]) => {
+ async (
+ files: File[],
+ bucket: "opportunity-line" | "parts" = "opportunity-line"
+ ) => {
if (!carbon) {
toast.error("Carbon client not available");
return;
}
+ if (bucket === "parts" && !itemId) {
+ toast.error("Cannot upload to parts bucket without item ID");
+ return;
+ }
+
for (const file of files) {
- const fileName = getPath(file);
+ const fileName = getPath(file, bucket);
const fileUpload = await carbon.storage
.from("private")
@@ -245,12 +275,86 @@ const useOpportunityLineDocuments = ({
path: fileUpload.data.path,
name: file.name,
size: file.size,
+ bucket,
});
}
}
revalidator.revalidate();
},
- [getPath, createDocumentRecord, carbon, revalidator]
+ [getPath, createDocumentRecord, carbon, revalidator, itemId]
+ );
+
+ const moveFile = useCallback(
+ async (
+ file: FileObject & { bucket?: string },
+ targetBucket: "opportunity-line" | "parts"
+ ) => {
+ if (!carbon) {
+ toast.error("Carbon client not available");
+ return;
+ }
+
+ if (targetBucket === "parts" && !itemId) {
+ toast.error("Cannot move to parts bucket without item ID");
+ return;
+ }
+
+ const currentBucket =
+ file.bucket === "parts" ? "parts" : "opportunity-line";
+
+ if (currentBucket === targetBucket) {
+ toast.error("File is already in the selected bucket");
+ return;
+ }
+
+ try {
+ // Download the file first
+ const sourcePath = getPath(file, currentBucket);
+ const { data: downloadData } = await carbon.storage
+ .from("private")
+ .download(sourcePath);
+
+ if (!downloadData) {
+ toast.error("Failed to download file for moving");
+ return;
+ }
+
+ // Upload to new location
+ const targetPath = getPath(file, targetBucket);
+ const { error: uploadError } = await carbon.storage
+ .from("private")
+ .upload(targetPath, downloadData, {
+ cacheControl: `${12 * 60 * 60}`,
+ upsert: true,
+ });
+
+ if (uploadError) {
+ toast.error("Failed to upload file to new location");
+ return;
+ }
+
+ // Delete from old location
+ const { error: deleteError } = await carbon.storage
+ .from("private")
+ .remove([sourcePath]);
+
+ if (deleteError) {
+ toast.error("Failed to delete file from old location");
+ return;
+ }
+
+ toast.success(
+ `Moved ${file.name} to ${
+ targetBucket === "parts" ? "Parts" : "Opportunity Line"
+ } bucket`
+ );
+ revalidator.revalidate();
+ } catch (error) {
+ toast.error("Error moving file");
+ console.error(error);
+ }
+ },
+ [carbon, itemId, getPath, revalidator]
);
return {
@@ -262,6 +366,7 @@ const useOpportunityLineDocuments = ({
downloadModel,
getPath,
getModelPath,
+ moveFile,
upload,
};
};
@@ -270,6 +375,7 @@ type OpportunityLineDocumentsProps = {
files: FileObject[];
id: string;
lineId: string;
+ itemId?: string | null;
type: "Request for Quote" | "Sales Order" | "Quote" | "Sales Invoice";
modelUpload?: ModelUpload;
};
@@ -278,21 +384,25 @@ const OpportunityLineDocuments = ({
files,
id,
lineId,
+ itemId,
modelUpload,
type,
}: OpportunityLineDocumentsProps) => {
const {
canDelete,
+ canUpdate,
download,
downloadModel,
deleteFile,
deleteModel,
getPath,
getModelPath,
+ moveFile,
upload,
} = useOpportunityLineDocuments({
id,
lineId,
+ itemId,
type,
});
@@ -326,7 +436,12 @@ const OpportunityLineDocuments = ({
<CardDescription>Opportunity line documents</CardDescription>
</CardHeader>
<CardAction>
- <OpportunityLineDocumentForm id={id} type={type} lineId={lineId} />
+ <OpportunityLineDocumentForm
+ id={id}
+ type={type}
+ lineId={lineId}
+ itemId={itemId}
+ />
</CardAction>
</HStack>
<CardContent>
@@ -335,6 +450,7 @@ const OpportunityLineDocuments = ({
<Tr>
<Th>Name</Th>
<Th>Size</Th>
+ <Th>Bucket</Th>
<Th />
</Tr>
</Thead>
@@ -361,6 +477,7 @@ const OpportunityLineDocuments = ({
)
: "--"}
</Td>
+ <Td>Opportunity Line</Td>
<Td>
<div className="flex justify-end w-full">
<DropdownMenu>
@@ -404,9 +521,16 @@ const OpportunityLineDocuments = ({
className="font-medium cursor-pointer"
onClick={() => {
if (["PDF", "Image"].includes(type)) {
+ const bucket =
+ (file as any).bucket === "parts"
+ ? "parts"
+ : "opportunity-line";
window.open(
path.to.file.previewFile(
- `${"private"}/${getPath(file)}`
+ `${"private"}/${getPath(
+ file,
+ bucket as "opportunity-line" | "parts"
+ )}`
),
"_blank"
);
@@ -418,7 +542,12 @@ const OpportunityLineDocuments = ({
{["PDF", "Image"].includes(type) ? (
<DocumentPreview
bucket="private"
- pathToFile={getPath(file)}
+ pathToFile={getPath(
+ file,
+ (file as any).bucket === "parts"
+ ? "parts"
+ : "opportunity-line"
+ )}
// @ts-ignore
type={type}
>
@@ -435,6 +564,15 @@ const OpportunityLineDocuments = ({
Math.floor((file.metadata?.size ?? 0) / 1024)
)}
</Td>
+ <Td>
+ <Enumerable
+ value={
+ (file as any).bucket === "parts"
+ ? "Item Master"
+ : "Opportunity"
+ }
+ />
+ </Td>
<Td>
<div className="flex justify-end w-full">
<DropdownMenu>
@@ -449,6 +587,35 @@ const OpportunityLineDocuments = ({
<DropdownMenuItem onClick={() => download(file)}>
Download
</DropdownMenuItem>
+ {itemId && (
+ <DropdownMenuSub>
+ <DropdownMenuSubTrigger disabled={!canUpdate}>
+ Move to
+ </DropdownMenuSubTrigger>
+ <DropdownMenuSubContent>
+ <DropdownMenuRadioGroup
+ value={
+ (file as any).bucket === "parts"
+ ? "parts"
+ : "opportunity-line"
+ }
+ onValueChange={(value) =>
+ moveFile(
+ file,
+ value as "opportunity-line" | "parts"
+ )
+ }
+ >
+ <DropdownMenuRadioItem value="opportunity-line">
+ Opportunity
+ </DropdownMenuRadioItem>
+ <DropdownMenuRadioItem value="parts">
+ Item Master
+ </DropdownMenuRadioItem>
+ </DropdownMenuRadioGroup>
+ </DropdownMenuSubContent>
+ </DropdownMenuSub>
+ )}
<DropdownMenuItem
destructive
disabled={!canDelete}
@@ -466,7 +633,7 @@ const OpportunityLineDocuments = ({
{allFiles.length === 0 && !modelUpload && (
<Tr>
<Td
- colSpan={24}
+ colSpan={4}
className="py-8 text-muted-foreground text-center"
>
No files
@@ -487,20 +654,25 @@ export default OpportunityLineDocuments;
type OpportunityLineDocumentFormProps = {
id: string;
lineId: string;
+ itemId?: string | null;
type: "Request for Quote" | "Sales Order" | "Quote" | "Sales Invoice";
};
const OpportunityLineDocumentForm = ({
id,
lineId,
+ itemId,
type,
}: OpportunityLineDocumentFormProps) => {
const permissions = usePermissions();
- const { upload } = useOpportunityLineDocuments({ id, lineId, type });
+ const { upload } = useOpportunityLineDocuments({ id, lineId, itemId, type });
- const uploadFiles = async (e: ChangeEvent<HTMLInputElement>) => {
+ const uploadFiles = async (
+ e: ChangeEvent<HTMLInputElement>,
+ bucket: "opportunity-line" | "parts" = "opportunity-line"
+ ) => {
if (e.target.files) {
- upload(Array.from(e.target.files));
+ upload(Array.from(e.target.files), bucket);
}
};
@@ -508,7 +680,7 @@ const OpportunityLineDocumentForm = ({
<File
isDisabled={!permissions.can("update", "sales")}
leftIcon={<LuUpload />}
- onChange={uploadFiles}
+ onChange={(e) => uploadFiles(e, "opportunity-line")}
multiple
>
New
diff --git a/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx b/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
index 4896caa283..8ab4933152 100644
--- a/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
@@ -69,7 +69,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
return defer({
line: line.data,
operations: operations?.data ?? [],
- files: getOpportunityLineDocuments(serviceRole, companyId, lineId),
+ files: getOpportunityLineDocuments(serviceRole, companyId, lineId, itemId),
pricesByQuantity: (prices?.data ?? []).reduce<
Record<number, QuotationPrice>
>((acc, price) => {
@@ -242,6 +242,7 @@ export default function QuoteLine() {
files={resolvedFiles ?? []}
id={quoteId}
lineId={lineId}
+ itemId={line?.itemId}
modelUpload={line ?? undefined}
type="Quote"
/>
diff --git a/apps/erp/app/routes/x+/sales-invoice+/$invoiceId.$lineId.details.tsx b/apps/erp/app/routes/x+/sales-invoice+/$invoiceId.$lineId.details.tsx
index 1f6bd74ca4..9e1af2bb8f 100644
--- a/apps/erp/app/routes/x+/sales-invoice+/$invoiceId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/sales-invoice+/$invoiceId.$lineId.details.tsx
@@ -36,9 +36,11 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const salesInvoiceLine = await getSalesInvoiceLine(client, lineId);
+ const itemId = salesInvoiceLine?.data?.itemId;
+
return defer({
salesInvoiceLine: salesInvoiceLine?.data ?? null,
- files: getOpportunityLineDocuments(client, companyId, lineId),
+ files: getOpportunityLineDocuments(client, companyId, lineId, itemId),
});
}
@@ -157,6 +159,7 @@ export default function EditSalesInvoiceLineRoute() {
files={resolvedFiles ?? []}
id={invoiceId}
lineId={lineId}
+ itemId={salesInvoiceLine?.itemId}
type="Sales Invoice"
/>
)}
diff --git a/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.details.tsx b/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.details.tsx
index f39a2fcdc0..0ed5dffb30 100644
--- a/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/sales-order+/$orderId.$lineId.details.tsx
@@ -66,13 +66,15 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
+ const itemId = line.data.itemId;
+
return defer({
line: line?.data ?? null,
itemReplenishment:
- line.data.itemId && line.data.methodType === "Make"
- ? getItemReplenishment(serviceRole, line.data.itemId, companyId)
+ itemId && line.data.methodType === "Make"
+ ? getItemReplenishment(serviceRole, itemId, companyId)
: Promise.resolve({ data: null }),
- files: getOpportunityLineDocuments(serviceRole, companyId, lineId),
+ files: getOpportunityLineDocuments(serviceRole, companyId, lineId, itemId),
jobs: jobs?.data ?? [],
shipments: shipments?.data ?? [],
});
@@ -239,6 +241,7 @@ export default function EditSalesOrderLineRoute() {
files={resolvedFiles ?? []}
id={orderId}
lineId={lineId}
+ itemId={line?.itemId}
modelUpload={line ?? undefined}
type="Sales Order"
/>
diff --git a/apps/erp/app/routes/x+/sales-rfq+/$rfqId.$lineId.details.tsx b/apps/erp/app/routes/x+/sales-rfq+/$rfqId.$lineId.details.tsx
index 831e1f61a4..167d5eb435 100644
--- a/apps/erp/app/routes/x+/sales-rfq+/$rfqId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/sales-rfq+/$rfqId.$lineId.details.tsx
@@ -44,9 +44,11 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
);
}
+ const itemId = line.data.itemId;
+
return defer({
line: line.data,
- files: getOpportunityLineDocuments(serviceRole, companyId, lineId),
+ files: getOpportunityLineDocuments(serviceRole, companyId, lineId, itemId),
});
};
@@ -138,6 +140,7 @@ export default function SalesRFQLine() {
files={resolvedFiles ?? []}
id={rfqId}
lineId={lineId}
+ itemId={line?.itemId}
modelUpload={line ?? undefined}
type="Request for Quote"
/>