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 historical pricing for other quotes to the quote line pricing view

Rob Carrington03c115fd5b8e
diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.ts
index 8de8e1d29a..cd1dec125b 100644
--- a/apps/erp/app/modules/sales/sales.service.ts
+++ b/apps/erp/app/modules/sales/sales.service.ts
@@ -916,6 +916,20 @@ export async function getQuoteLinePricesByQuoteId(
.order("quoteLineId", { ascending: true });
}
+export async function getHistoricalQuoteLinePricesByItemId(
+ client: SupabaseClient<Database>,
+ itemId: string,
+ currentQuoteId: string
+) {
+ return client
+ .from("quoteLinePrices")
+ .select("*")
+ .eq("itemId", itemId)
+ .neq("quoteId", currentQuoteId)
+ .order("quoteCreatedAt", { ascending: false })
+ .order("qty", { ascending: true });
+}
+
export async function getQuoteMaterials(
client: SupabaseClient<Database>,
quoteId: string
diff --git a/apps/erp/app/modules/sales/types.ts b/apps/erp/app/modules/sales/types.ts
index 257e5ea7ca..9dab66445a 100644
--- a/apps/erp/app/modules/sales/types.ts
+++ b/apps/erp/app/modules/sales/types.ts
@@ -8,6 +8,7 @@ import type {
getCustomers,
getCustomerStatuses,
getCustomerTypes,
+ getHistoricalQuoteLinePricesByItemId,
getNoQuoteReasons,
getOpportunity,
getQuoteLinePrices,
@@ -117,6 +118,10 @@ export type QuotationPrice = NonNullable<
Awaited<ReturnType<typeof getQuoteLinePrices>>["data"]
>[number];
+export type HistoricalQuotationPrice = NonNullable<
+ Awaited<ReturnType<typeof getHistoricalQuoteLinePricesByItemId>>["data"]
+>[number];
+
export type QuotationStatusType = Database["public"]["Enums"]["quoteStatus"];
export type QuotationPayment = NonNullable<
diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
index f5fb3d14aa..d4db040565 100644
--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
@@ -61,12 +61,14 @@ const QuoteLinePricing = ({
exchangeRate,
getLineCosts,
relatedSalesOrderLines,
+ historicalQuoteLinePrices,
}: {
line: QuotationLine;
pricesByQuantity: Record<number, QuotationPrice>;
exchangeRate: number;
getLineCosts: (quantity: number) => Costs;
relatedSalesOrderLines: SalesOrderLine[];
+ historicalQuoteLinePrices: QuotationPrice[];
}) => {
const permissions = usePermissions();
@@ -402,6 +404,7 @@ const QuoteLinePricing = ({
{relatedSalesOrderLines.length > 0 && (
<QuoteLinePricingHistory
relatedSalesOrderLines={relatedSalesOrderLines}
+ historicalQuoteLinePrices={historicalQuoteLinePrices}
baseCurrency={baseCurrency}
/>
)}
diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricingHistory.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricingHistory.tsx
index 99fd454ad7..3451fc1d19 100644
--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricingHistory.tsx
+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricingHistory.tsx
@@ -30,18 +30,36 @@ import {
TabsTrigger,
} from "../../../../../../../packages/react/src/Tabs";
import { cn } from "../../../../../../../packages/react/src/utils/cn";
-import type { SalesOrderLine } from "../../types";
+import type { HistoricalQuotationPrice, SalesOrderLine } from "../../types";
const QuoteLinePricingHistory = ({
baseCurrency,
relatedSalesOrderLines,
+ historicalQuoteLinePrices,
}: {
baseCurrency: string;
relatedSalesOrderLines: SalesOrderLine[];
+ historicalQuoteLinePrices: HistoricalQuotationPrice[];
}) => {
const [isOpen, setIsOpen] = useState(false);
- const historyCount = relatedSalesOrderLines.length;
+ const historicalQuoteLines = historicalQuoteLinePrices.reduce<
+ Record<
+ string,
+ HistoricalQuotationPrice & { quantities: Record<number, number> }
+ >
+ >((acc, linePrice) => {
+ if (!acc[linePrice.id!]) {
+ acc[linePrice.id!] = { ...linePrice, quantities: {} };
+ }
+ if (linePrice.qty && linePrice.unitPrice) {
+ acc[linePrice.id!].quantities[linePrice.qty] = linePrice.unitPrice;
+ }
+ return acc;
+ }, {});
+
+ const historyCount =
+ relatedSalesOrderLines.length + Object.keys(historicalQuoteLines).length;
return (
<div>
@@ -141,7 +159,79 @@ const QuoteLinePricingHistory = ({
</div>
</TabsContent>
<TabsContent value="quoteLines">
- <Empty className="py-6" />
+ <div className="flex overflow-x-auto space-x-4 pb-4">
+ {Object.keys(historicalQuoteLines).length === 0 && (
+ <Empty className="py-6" />
+ )}
+ <Carousel className="w-full">
+ <CarouselContent className="-ml-4">
+ {Object.values(historicalQuoteLines).map((line) => (
+ <CarouselItem
+ key={line.id}
+ className="pl-4 basis-full md:basis-1/2 lg:basis-1/3"
+ >
+ <Card className="w-full">
+ <CardContent className="p-4">
+ <HStack className="flex justify-between mb-2">
+ <Link
+ to={path.to.quoteLine(
+ line.quoteId!,
+ line.id!
+ )}
+ className="text-sm font-medium hover:underline"
+ >
+ {line.quoteReadableId}
+ </Link>
+ <div>
+ <span className="text-sm text-muted-foreground">
+ {new Date(
+ line.quoteCreatedAt!
+ ).toLocaleDateString()}
+ </span>
+ </div>
+ </HStack>
+ <div className="space-y-2">
+ <div className="flex justify-between">
+ <span className="text-sm font-medium">
+ <CustomerAvatar
+ customerId={line.customerId}
+ />
+ </span>
+ </div>
+
+ <div className="grid grid-cols-2">
+ <div className="text-sm font-medium border p-2">
+ Quantity
+ </div>
+ <div className="text-sm font-medium border p-2">
+ Unit Price
+ </div>
+ {Object.entries(line.quantities).map(
+ ([quantity, price]) => (
+ <>
+ <div className="text-sm text-muted-foreground border p-2">
+ {quantity}
+ </div>
+ <div className="text-sm text-muted-foreground border p-2">
+ {new Intl.NumberFormat("en-US", {
+ style: "currency",
+ currency: baseCurrency,
+ }).format(price)}
+ </div>
+ </>
+ )
+ )}
+ </div>
+ </div>
+ </CardContent>
+ </Card>
+ </CarouselItem>
+ ))}
+ </CarouselContent>
+ <CarouselPrevious />
+ <CarouselNext />
+ </Carousel>
+ </div>
</TabsContent>
</Tabs>
</div>
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 bd6a9068d2..5b8ad9ceef 100644
--- a/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
+++ b/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
@@ -18,6 +18,7 @@ import type {
QuoteMethod,
} from "~/modules/sales";
import {
+ getHistoricalQuoteLinePricesByItemId,
getOpportunityLineDocuments,
getQuoteLine,
getQuoteLinePrices,
@@ -78,6 +79,12 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
);
}
+ const historicalQuoteLinePrices = await getHistoricalQuoteLinePricesByItemId(
+ serviceRole,
+ itemId,
+ quoteId
+ );
+
return defer({
line: line.data,
operations: operations?.data ?? [],
@@ -89,6 +96,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
return acc;
}, {}),
relatedSalesOrderLines: relatedSalesOrderLines?.data ?? [],
+ historicalQuoteLinePrices: historicalQuoteLinePrices?.data ?? [],
});
};
@@ -133,8 +141,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
export default function QuoteLine() {
- const { line, operations, files, pricesByQuantity, relatedSalesOrderLines } =
- useLoaderData<typeof loader>();
+ const {
+ line,
+ operations,
+ files,
+ pricesByQuantity,
+ relatedSalesOrderLines,
+ historicalQuoteLinePrices,
+ } = useLoaderData<typeof loader>();
const permissions = usePermissions();
const { quoteId, lineId } = useParams();
if (!quoteId) throw new Error("Could not find quoteId");
@@ -199,6 +213,7 @@ export default function QuoteLine() {
pricesByQuantity={pricesByQuantity}
getLineCosts={getLineCosts}
relatedSalesOrderLines={relatedSalesOrderLines}
+ historicalQuoteLinePrices={historicalQuoteLinePrices}
/>
)}
<OpportunityLineNotes
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index 00b270ca71..eb18523a53 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -40407,6 +40407,12 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLinePrices.quoteReadableId",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrices.quoteCreatedAt",
+ },
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrices.customerId",
+ },
{
$ref: "#/parameters/select",
},
@@ -66943,6 +66949,16 @@ export default {
format: "text",
type: "string",
},
+ quoteCreatedAt: {
+ format: "timestamp with time zone",
+ type: "string",
+ },
+ customerId: {
+ description:
+ "Note:\nThis is a Foreign Key to `customer.id`.<fk table='customer' column='id'/>",
+ format: "text",
+ type: "string",
+ },
},
type: "object",
},
@@ -93230,6 +93246,20 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.quoteLinePrices.quoteCreatedAt": {
+ name: "quoteCreatedAt",
+ required: false,
+ format: "timestamp with time zone",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.quoteLinePrices.customerId": {
+ name: "customerId",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
"body.salesRfqFavorite": {
name: "salesRfqFavorite",
description: "salesRfqFavorite",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 065a7ae71e..57ffaee485 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -32187,6 +32187,7 @@ export type Database = {
companyId: string | null
configuration: Json | null
createdBy: string | null
+ customerId: string | null
customerPartId: string | null
customerPartRevision: string | null
customFields: Json | null
@@ -32208,6 +32209,7 @@ export type Database = {
noQuoteReason: string | null
qty: number | null
quantity: number[] | null
+ quoteCreatedAt: string | null
quoteId: string | null
quoteReadableId: string | null
quoteRevisionId: number | null
@@ -32223,6 +32225,27 @@ export type Database = {
updatedBy: string | null
}
Relationships: [
+ {
+ foreignKeyName: "quote_customerId_fkey"
+ columns: ["customerId"]
+ isOneToOne: false
+ referencedRelation: "customer"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quote_customerId_fkey"
+ columns: ["customerId"]
+ isOneToOne: false
+ referencedRelation: "customers"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quote_customerId_fkey"
+ columns: ["customerId"]
+ isOneToOne: false
+ referencedRelation: "salesOrderCustomers"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "quoteLine_companyId_fkey"
columns: ["companyId"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 065a7ae71e..57ffaee485 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -32187,6 +32187,7 @@ export type Database = {
companyId: string | null
configuration: Json | null
createdBy: string | null
+ customerId: string | null
customerPartId: string | null
customerPartRevision: string | null
customFields: Json | null
@@ -32208,6 +32209,7 @@ export type Database = {
noQuoteReason: string | null
qty: number | null
quantity: number[] | null
+ quoteCreatedAt: string | null
quoteId: string | null
quoteReadableId: string | null
quoteRevisionId: number | null
@@ -32223,6 +32225,27 @@ export type Database = {
updatedBy: string | null
}
Relationships: [
+ {
+ foreignKeyName: "quote_customerId_fkey"
+ columns: ["customerId"]
+ isOneToOne: false
+ referencedRelation: "customer"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quote_customerId_fkey"
+ columns: ["customerId"]
+ isOneToOne: false
+ referencedRelation: "customers"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quote_customerId_fkey"
+ columns: ["customerId"]
+ isOneToOne: false
+ referencedRelation: "salesOrderCustomers"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "quoteLine_companyId_fkey"
columns: ["companyId"]
diff --git a/packages/database/supabase/migrations/20250317181045_sales-order-lines-order-date.sql b/packages/database/supabase/migrations/20250317181045_sales-order-lines-order-date.sql
index 08f6a94b3c..71f6611ee9 100644
--- a/packages/database/supabase/migrations/20250317181045_sales-order-lines-order-date.sql
+++ b/packages/database/supabase/migrations/20250317181045_sales-order-lines-order-date.sql
@@ -43,7 +43,9 @@ CREATE OR REPLACE VIEW "quoteLinePrices" WITH(SECURITY_INVOKER=true) AS (
ic."unitCost" as "unitCost",
qlp."quantity" as "qty",
qlp."unitPrice",
- q."quoteId" as "quoteReadableId"
+ q."quoteId" as "quoteReadableId",
+ q."createdAt" as "quoteCreatedAt",
+ q."customerId"
FROM "quoteLine" ql
INNER JOIN "quote" q ON q.id = ql."quoteId"
LEFT JOIN "modelUpload" mu ON ql."modelUploadId" = mu."id"