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 attachments to supplier quote email (#430)

GauravOSSd1b0083bbd05
diff --git a/apps/erp/app/modules/purchasing/purchasing.models.ts b/apps/erp/app/modules/purchasing/purchasing.models.ts
index 063c14759c..836a9a0fe3 100644
--- a/apps/erp/app/modules/purchasing/purchasing.models.ts
+++ b/apps/erp/app/modules/purchasing/purchasing.models.ts
@@ -107,8 +107,9 @@ export const purchaseOrderValidator = z.object({
export const supplierQuoteFinalizeValidator = z
.object({
- notification: z.enum(["Email", "None"]).optional(),
- supplierContact: zfd.text(z.string().optional())
+ notification: z.enum(["Email", "Share"]).optional(),
+ supplierContact: zfd.text(z.string().optional()),
+ sendAttachments: zfd.checkbox()
})
.refine(
(data) => (data.notification === "Email" ? data.supplierContact : true),
diff --git a/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteSendModal.tsx b/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteSendModal.tsx
index 75ba9415ff..5b886fc59f 100644
--- a/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteSendModal.tsx
+++ b/apps/erp/app/modules/purchasing/ui/SupplierQuote/SupplierQuoteSendModal.tsx
@@ -1,10 +1,13 @@
import { ValidatedForm } from "@carbon/form";
import {
Button,
+ Checkbox,
Copy,
+ HStack,
Input,
InputGroup,
InputRightElement,
+ Label,
Modal,
ModalBody,
ModalContent,
@@ -39,8 +42,9 @@ const SupplierQuoteSendModal = ({
const canEmail = integrations.has("resend");
const [notificationType, setNotificationType] = useState(
- canEmail ? "Email" : "None"
+ canEmail ? "Email" : "Share"
);
+ const [sendAttachments, setSendAttachments] = useState(true);
const digitalQuoteUrl =
externalLinkId && typeof window !== "undefined"
@@ -100,15 +104,16 @@ const SupplierQuoteSendModal = ({
action={path.to.supplierQuoteSend(quote?.id || "")}
onSubmit={onClose}
defaultValues={{
- notification: notificationType as "Email" | "None",
- supplierContact: quote?.supplierContactId ?? undefined
+ notification: notificationType as "Email" | "Share",
+ supplierContact: quote?.supplierContactId ?? undefined,
+ sendAttachments: sendAttachments
}}
fetcher={fetcher}
>
<ModalHeader>
<ModalTitle>Send {quote?.supplierQuoteId}</ModalTitle>
<ModalDescription>
- Send the supplier quote to the supplier via email.
+ Share the supplier quote via link or email.
</ModalDescription>
</ModalHeader>
<ModalBody>
@@ -119,8 +124,8 @@ const SupplierQuoteSendModal = ({
name="notification"
options={[
{
- label: "None",
- value: "None"
+ label: "Share Link",
+ value: "Share"
},
{
label: "Email",
@@ -133,19 +138,61 @@ const SupplierQuoteSendModal = ({
}}
/>
)}
+ {notificationType === "Share" && (
+ <VStack spacing={2} className="w-full items-start">
+ <Label htmlFor="digitalQuoteUrl">
+ <span className="text-xs font-medium text-muted-foreground">
+ Share Link
+ </span>
+ </Label>
+ <InputGroup>
+ <Input
+ id="digitalQuoteUrl"
+ value={digitalQuoteUrl}
+ isReadOnly
+ />
+ <InputRightElement>
+ <Copy text={digitalQuoteUrl} />
+ </InputRightElement>
+ </InputGroup>
+ </VStack>
+ )}
{notificationType === "Email" && (
- <SupplierContact
- name="supplierContact"
- supplier={quote?.supplierId ?? undefined}
- />
+ <>
+ <SupplierContact
+ name="supplierContact"
+ supplier={quote?.supplierId ?? undefined}
+ />
+ <HStack spacing={2}>
+ <Checkbox
+ id="sendAttachments"
+ name="sendAttachments"
+ isChecked={sendAttachments}
+ onCheckedChange={(checked) =>
+ setSendAttachments(checked === true)
+ }
+ />
+ <label htmlFor="sendAttachments" className="text-sm">
+ Send documents attached to line items
+ </label>
+ </HStack>
+ </>
)}
</VStack>
</ModalBody>
<ModalFooter>
- <Button variant="secondary" onClick={onClose}>
- Cancel
- </Button>
- <Button type="submit">Send</Button>
+ {notificationType === "Share" ? (
+ <Button variant="secondary" onClick={onClose}>
+ Close
+ </Button>
+ ) : (
+ <>
+ <Button variant="secondary" onClick={onClose}>
+ Cancel
+ </Button>
+ <Button type="submit">Send</Button>
+ </>
+ )}
</ModalFooter>
</ValidatedForm>
</ModalContent>
diff --git a/apps/erp/app/routes/x+/supplier-quote+/$id.send.tsx b/apps/erp/app/routes/x+/supplier-quote+/$id.send.tsx
index 12d44b1987..c74677848f 100644
--- a/apps/erp/app/routes/x+/supplier-quote+/$id.send.tsx
+++ b/apps/erp/app/routes/x+/supplier-quote+/$id.send.tsx
@@ -7,11 +7,13 @@ import { tasks } from "@trigger.dev/sdk";
import { type ActionFunctionArgs, redirect } from "react-router";
import {
getSupplierContact,
+ getSupplierInteractionLineDocuments,
getSupplierQuote,
+ getSupplierQuoteLines,
sendSupplierQuote,
supplierQuoteFinalizeValidator
} from "~/modules/purchasing";
-import { getCompany, getCompanySettings } from "~/modules/settings";
+import { getCompany } from "~/modules/settings";
import { upsertExternalLink } from "~/modules/shared";
import { getUser } from "~/modules/users/users.server";
import { path } from "~/utils/path";
@@ -82,31 +84,67 @@ export async function action(args: ActionFunctionArgs) {
return validationError(validation.error);
}
- const { notification, supplierContact: supplierContactId } = validation.data;
+ const {
+ notification,
+ supplierContact: supplierContactId,
+ sendAttachments
+ } = validation.data;
switch (notification) {
case "Email":
try {
if (!supplierContactId) throw new Error("Supplier contact is required");
- const [company, companySettings, supplierContact, supplierQuote, user] =
+ const [company, supplierContact, supplierQuote, user] =
await Promise.all([
getCompany(client, companyId),
- getCompanySettings(client, companyId),
getSupplierContact(client, supplierContactId),
getSupplierQuote(client, id),
getUser(client, userId)
]);
if (!company.data) throw new Error("Failed to get company");
- if (!companySettings.data)
- throw new Error("Failed to get company settings");
if (!supplierContact?.data?.contact)
throw new Error("Failed to get supplier contact");
if (!supplierQuote.data)
throw new Error("Failed to get supplier quote");
if (!user.data) throw new Error("Failed to get user");
+ // Fetch all line items and their attached documents if sendAttachments is enabled
+ const attachments: Array<{ filename: string; content: string }> = [];
+
+ if (sendAttachments) {
+ const lines = await getSupplierQuoteLines(client, id);
+
+ if (lines.data) {
+ for (const line of lines.data) {
+ const docs = await getSupplierInteractionLineDocuments(
+ client,
+ companyId,
+ line.id ?? ""
+ );
+
+ for (const doc of docs) {
+ const { data: fileData } = await client.storage
+ .from("private")
+ .download(
+ `${companyId}/supplier-interaction-line/${line.id}/${doc.name}`
+ );
+
+ if (fileData) {
+ const arrayBuffer = await fileData.arrayBuffer();
+ const base64 = Buffer.from(arrayBuffer).toString("base64");
+
+ attachments.push({
+ filename: doc.name,
+ content: base64
+ });
+ }
+ }
+ }
+ }
+ }
+
const requestUrl = new URL(request.url);
const baseUrl = `${requestUrl.protocol}//${requestUrl.host}`;
const externalQuoteUrl: string = `${baseUrl}${path.to.externalSupplierQuote(
@@ -115,11 +153,11 @@ export async function action(args: ActionFunctionArgs) {
const emailSubject = `Supplier Quote ${supplierQuote.data.supplierQuoteId} from ${company.data.name}`;
- const emailBody = `Hey ${supplierContact.data.contact.firstName},\n\nPlease provide pricing and lead time(s) for the linked quote:`;
+ const emailBody = `Hey ${supplierContact.data.contact.firstName || "there"},\n\nPlease provide pricing and lead time(s) for the linked quote:`;
const emailSignature = `Thanks,\n${user.data.firstName} ${user.data.lastName}\n${company.data.name}`;
await tasks.trigger<typeof sendEmailResendTask>("send-email-resend", {
- to: [user.data.email, supplierContact.data.contact.email],
+ to: [user.data.email, supplierContact.data.contact?.email ?? ""],
from: user.data.email,
subject: emailSubject,
html: `${emailBody.replace(
@@ -130,6 +168,7 @@ export async function action(args: ActionFunctionArgs) {
"<br>"
)}`,
text: `${emailBody}\n\n${externalQuoteUrl}\n\n${emailSignature}`,
+ attachments,
companyId
});
} catch (err) {
@@ -141,7 +180,7 @@ export async function action(args: ActionFunctionArgs) {
break;
case undefined:
- case "None":
+ case "Share":
break;
default:
throw new Error("Invalid notification type");