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: show discount on quotes

Brad Barbin7ce0d4cc6485
diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteSummary.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteSummary.tsx
index 56cd2b4b32..35ca13a6bc 100644
--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteSummary.tsx
+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteSummary.tsx
@@ -46,6 +46,9 @@ type SelectedLine = {
shippingCost: number;
convertedShippingCost: number;
taxPercent: number;
+ discountPercent: number;
+ unitPrice: number;
+ convertedUnitPrice: number;
};
const deselectedLine: SelectedLine = {
@@ -58,6 +61,9 @@ const deselectedLine: SelectedLine = {
shippingCost: 0,
convertedShippingCost: 0,
taxPercent: 0,
+ discountPercent: 0,
+ unitPrice: 0,
+ convertedUnitPrice: 0,
};
const LineItems = ({
@@ -343,6 +349,9 @@ const LinePricingOptions = ({
convertedShippingCost:
selectedOption.convertedShippingCost ?? 0,
taxPercent: line.taxPercent ?? 0,
+ discountPercent: selectedOption.discountPercent ?? 0,
+ unitPrice: selectedOption.unitPrice ?? 0,
+ convertedUnitPrice: selectedOption.convertedUnitPrice ?? 0,
},
}));
setSelectedValue(value);
@@ -355,6 +364,7 @@ const LinePricingOptions = ({
<Th />
<Th>Quantity</Th>
<Th>Unit Price</Th>
+ <Th>Discount</Th>
<Th>Add-Ons</Th>
<Th>Lead Time</Th>
<Th>Subtotal</Th>
@@ -387,7 +397,12 @@ const LinePricingOptions = ({
</Td>
<Td>{option.quantity}</Td>
<Td>
- {formatter.format(option.convertedNetUnitPrice ?? 0)}
+ {formatter.format(option.convertedUnitPrice ?? 0)}
+ </Td>
+ <Td>
+ {option.discountPercent > 0
+ ? percentFormatter.format(option.discountPercent)
+ : "-"}
</Td>
<Td>
{formatter.format(
@@ -429,7 +444,7 @@ const LinePricingOptions = ({
<Td className="text-right">
<MotionNumber
value={
- (selectedLine.convertedNetUnitPrice ?? 0) *
+ (selectedLine.convertedUnitPrice ?? 0) *
selectedLine.quantity
}
format={{ style: "currency", currency: quoteCurrency }}
@@ -438,6 +453,27 @@ const LinePricingOptions = ({
</Td>
</Tr>
+ {selectedLine.discountPercent > 0 && (
+ <Tr key="discount" className="border-b border-border">
+ <Td>
+ Discount (
+ {percentFormatter.format(selectedLine.discountPercent)})
+ </Td>
+ <Td className="text-right">
+ -
+ <MotionNumber
+ value={
+ (selectedLine.convertedUnitPrice ?? 0) *
+ selectedLine.quantity *
+ selectedLine.discountPercent
+ }
+ format={{ style: "currency", currency: quoteCurrency }}
+ locales={locale}
+ />
+ </Td>
+ </Tr>
+ )}
+
{additionalCharges.length > 0 &&
additionalCharges.map((charge) => (
<Tr
@@ -627,6 +663,9 @@ const QuoteSummary = ({
shippingCost: price.shippingCost ?? 0,
convertedShippingCost: price.convertedShippingCost ?? 0,
taxPercent: line.taxPercent ?? 0,
+ discountPercent: price.discountPercent ?? 0,
+ unitPrice: price.unitPrice ?? 0,
+ convertedUnitPrice: price.convertedUnitPrice ?? 0,
};
return acc;
}, {}) ?? {}
@@ -641,6 +680,14 @@ const QuoteSummary = ({
(line.convertedShippingCost ?? 0)
);
}, 0);
+ const totalDiscount = Object.values(selectedLines).reduce((acc, line) => {
+ return (
+ acc +
+ (line.convertedUnitPrice ?? 0) *
+ line.quantity *
+ (line.discountPercent ?? 0)
+ );
+ }, 0);
const tax = Object.values(selectedLines).reduce((acc, line) => {
return (
acc +
@@ -694,7 +741,7 @@ const QuoteSummary = ({
<HStack className="justify-between text-base text-muted-foreground w-full">
<span>Subtotal:</span>
<MotionNumber
- value={subtotal}
+ value={subtotal + totalDiscount}
format={{
style: "currency",
currency: routeData?.quote?.currencyCode ?? "USD",
@@ -702,6 +749,22 @@ const QuoteSummary = ({
locales={locale}
/>
</HStack>
+ {totalDiscount > 0 && (
+ <HStack className="justify-between text-base text-muted-foreground w-full">
+ <span>Discount:</span>
+ <span className="text-muted-foreground">
+ -
+ <MotionNumber
+ value={totalDiscount}
+ format={{
+ style: "currency",
+ currency: routeData?.quote?.currencyCode ?? "USD",
+ }}
+ locales={locale}
+ />
+ </span>
+ </HStack>
+ )}
<HStack className="justify-between text-base text-muted-foreground w-full">
<span>Tax:</span>
<MotionNumber
diff --git a/apps/erp/app/routes/share+/quote.$id.tsx b/apps/erp/app/routes/share+/quote.$id.tsx
index d09ecea62c..584ffa4d9b 100644
--- a/apps/erp/app/routes/share+/quote.$id.tsx
+++ b/apps/erp/app/routes/share+/quote.$id.tsx
@@ -423,6 +423,9 @@ type SelectedLine = {
shippingCost: number;
convertedShippingCost: number;
taxPercent: number;
+ discountPercent: number;
+ unitPrice: number;
+ convertedUnitPrice: number;
};
const deselectedLine: SelectedLine = {
@@ -435,6 +438,9 @@ const deselectedLine: SelectedLine = {
shippingCost: 0,
convertedShippingCost: 0,
taxPercent: 0,
+ discountPercent: 0,
+ unitPrice: 0,
+ convertedUnitPrice: 0,
};
const LineItems = ({
@@ -721,6 +727,9 @@ const LinePricingOptions = ({
convertedShippingCost:
selectedOption.convertedShippingCost ?? 0,
taxPercent: line.taxPercent ?? 0,
+ discountPercent: selectedOption.discountPercent ?? 0,
+ unitPrice: selectedOption.unitPrice ?? 0,
+ convertedUnitPrice: selectedOption.convertedUnitPrice ?? 0,
},
}));
setSelectedValue(value);
@@ -733,6 +742,7 @@ const LinePricingOptions = ({
<Th />
<Th>{strings.Quantity}</Th>
<Th>{strings["Unit Price"]}</Th>
+ <Th>Discount</Th>
<Th>{strings["Add-Ons"]}</Th>
<Th>{strings["Lead Time"]}</Th>
<Th>{strings.Subtotal}</Th>
@@ -766,9 +776,14 @@ const LinePricingOptions = ({
<Td>{option.quantity}</Td>
<Td>
{unitPriceformatter.format(
- option.convertedNetUnitPrice ?? 0
+ option.convertedUnitPrice ?? 0
)}
</Td>
+ <Td>
+ {option.discountPercent > 0
+ ? percentFormatter.format(option.discountPercent)
+ : "-"}
+ </Td>
<Td>
{formatter.format(
convertedAdditionalChargesByQuantity[
@@ -809,7 +824,7 @@ const LinePricingOptions = ({
<Td className="text-right">
<MotionNumber
value={
- (selectedLine.convertedNetUnitPrice ?? 0) *
+ (selectedLine.convertedUnitPrice ?? 0) *
selectedLine.quantity
}
format={{ style: "currency", currency: quoteCurrency }}
@@ -818,6 +833,27 @@ const LinePricingOptions = ({
</Td>
</Tr>
+ {selectedLine.discountPercent > 0 && (
+ <Tr key="discount" className="border-b border-border">
+ <Td>
+ Discount (
+ {percentFormatter.format(selectedLine.discountPercent)})
+ </Td>
+ <Td className="text-right">
+ -
+ <MotionNumber
+ value={
+ (selectedLine.convertedUnitPrice ?? 0) *
+ selectedLine.quantity *
+ selectedLine.discountPercent
+ }
+ format={{ style: "currency", currency: quoteCurrency }}
+ locales={locale}
+ />
+ </Td>
+ </Tr>
+ )}
+
{additionalCharges.length > 0 &&
additionalCharges.map((charge) => (
<Tr
@@ -1056,6 +1092,9 @@ const Quote = ({
shippingCost: price.shippingCost ?? 0,
convertedShippingCost: price.convertedShippingCost ?? 0,
taxPercent: line.taxPercent ?? 0,
+ discountPercent: price.discountPercent ?? 0,
+ unitPrice: price.unitPrice ?? 0,
+ convertedUnitPrice: price.convertedUnitPrice ?? 0,
};
return acc;
}, {}) ?? {}
@@ -1070,6 +1109,14 @@ const Quote = ({
line.convertedShippingCost
);
}, 0);
+ const totalDiscount = Object.values(selectedLines).reduce((acc, line) => {
+ return (
+ acc +
+ (line.convertedUnitPrice ?? 0) *
+ line.quantity *
+ (line.discountPercent ?? 0)
+ );
+ }, 0);
const tax = Object.values(selectedLines).reduce((acc, line) => {
return (
acc +
@@ -1150,8 +1197,46 @@ const Quote = ({
</HStack>
)}
{(shippingMethod || paymentTerm) && <Separator />}
+ <HStack className="justify-between text-base w-full">
+ <span>{strings.Subtotal}:</span>
+ <MotionNumber
+ value={subtotal + totalDiscount}
+ format={{
+ style: "currency",
+ currency: quote.currencyCode ?? "USD",
+ }}
+ locales={locale}
+ />
+ </HStack>
+ {totalDiscount > 0 && (
+ <HStack className="justify-between text-base w-full">
+ <span>Discount:</span>
+ <span className="text-muted-foreground">
+ -
+ <MotionNumber
+ value={totalDiscount}
+ format={{
+ style: "currency",
+ currency: quote.currencyCode ?? "USD",
+ }}
+ locales={locale}
+ />
+ </span>
+ </HStack>
+ )}
+ <HStack className="justify-between text-base w-full">
+ <span>{strings.Tax}:</span>
+ <MotionNumber
+ value={tax}
+ format={{
+ style: "currency",
+ currency: quote.currencyCode ?? "USD",
+ }}
+ locales={locale}
+ />
+ </HStack>
{convertedShippingCost > 0 && (
- <HStack className="justify-between text-xl w-full">
+ <HStack className="justify-between text-base w-full">
<span>{strings.Shipping}:</span>
<MotionNumber
value={convertedShippingCost}
@@ -1163,6 +1248,7 @@ const Quote = ({
/>
</HStack>
)}
+ <Separator className="my-2" />
<HStack className="justify-between text-xl font-bold w-full">
<span>{strings.Total}:</span>
<MotionNumber
diff --git a/llm/cache/quote-discount-system.md b/llm/cache/quote-discount-system.md
new file mode 100644
index 0000000000..23cc354d04
--- /dev/null
+++ b/llm/cache/quote-discount-system.md
@@ -0,0 +1,66 @@
+# Quote Discount System
+
+## Overview
+The Carbon ERP system includes discount functionality at the quote line price level, not at the quote header or quote line level directly.
+
+## Database Structure
+
+### Quote Table
+- No discount fields exist on the `quote` table itself
+- Located in migration: `20240715024405_quotes.sql`
+
+### Quote Line Table (`quoteLine`)
+- No discount fields exist on the `quoteLine` table itself
+- Contains: `additionalCharges`, `taxPercent`, but no discount fields
+
+### Quote Line Price Table (`quoteLinePrice`)
+- Contains the discount functionality
+- Located in migration: `20240715134816_quote-pricing.sql`
+- Key fields:
+ - `discountPercent`: NUMERIC(10,5) NOT NULL DEFAULT 0
+ - `unitPrice`: NUMERIC(10,5) NOT NULL DEFAULT 0
+ - `quantity`: NUMERIC(10,5) NOT NULL DEFAULT 1
+ - `leadTime`: NUMERIC(10,5) NOT NULL DEFAULT 0
+ - `shippingCost`: NUMERIC(10,5) NOT NULL DEFAULT 0
+ - `exchangeRate`: NUMERIC(10,5)
+
+## UI Implementation
+
+### QuoteLinePricing Component
+- Location: `/apps/erp/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx`
+- Displays discount as a percentage field
+- Calculates net unit price as: `unitPrice * (1 - discountPercent)`
+- Allows editing discount percent per quantity break
+- Shows the impact of discount on pricing calculations
+
+## Service Layer
+
+### Sales Service
+- Location: `/apps/erp/app/modules/sales/sales.service.ts`
+- Handles discount persistence when updating quote line prices
+- Preserves existing discounts when recalculating prices
+- Functions that handle discounts:
+ - Quote line price updates preserve `discountPercent` and `leadTime`
+ - Recalculation functions maintain existing discount values
+
+## Key Features
+
+1. **Per-Quantity Discounts**: Each quantity break can have its own discount percentage
+2. **Net Price Calculation**: Automatically calculates net price after discount
+3. **Precision Control**: Discount percent uses NUMERIC(10,5) allowing up to 5 decimal places
+4. **UI Integration**: Full editing capabilities in the quote line pricing interface
+
+## Type Definitions
+
+The `QuotationPrice` type (from `sales.service.ts` exports) includes:
+- `discountPercent`: number
+- `unitPrice`: number
+- `leadTime`: number
+- `quantity`: number
+- `shippingCost`: number
+- `exchangeRate`: number | null
+
+## Notes
+- Discounts are applied at the price level per quantity, not at the quote or line level
+- The system calculates net prices dynamically based on unit price and discount percent
+- No global quote-level discount exists; all discounts are line-item and quantity-specific
\ No newline at end of file
diff --git a/packages/documents/src/pdf/QuotePDF.tsx b/packages/documents/src/pdf/QuotePDF.tsx
index a93a3814dc..5324697187 100644
--- a/packages/documents/src/pdf/QuotePDF.tsx
+++ b/packages/documents/src/pdf/QuotePDF.tsx
@@ -203,11 +203,12 @@ const QuotePDF = ({
<Text>Description</Text>
</View>
<View style={tw("w-2/3 flex flex-row")}>
- <Text style={tw("w-1/5 text-right")}>Qty</Text>
- <Text style={tw("w-1/5 text-right")}>Unit Price</Text>
- <Text style={tw("w-1/5 text-right")}>Tax & Fees</Text>
- <Text style={tw("w-1/5 text-right")}>Lead Time</Text>
- <Text style={tw("w-1/5 text-right")}>Total Price</Text>
+ <Text style={tw("w-1/6 text-right")}>Qty</Text>
+ <Text style={tw("w-1/6 text-right")}>Unit Price</Text>
+ <Text style={tw("w-1/6 text-right")}>Discount</Text>
+ <Text style={tw("w-1/6 text-right")}>Tax & Fees</Text>
+ <Text style={tw("w-1/6 text-right")}>Lead Time</Text>
+ <Text style={tw("w-1/6 text-right")}>Total Price</Text>
</View>
</View>
@@ -308,9 +309,11 @@ const QuotePDF = ({
const price = prices.find(
(price) => price.quantity === quantity
);
+ const unitPrice = price?.convertedUnitPrice ?? 0;
const netUnitPrice = price?.convertedNetUnitPrice ?? 0;
const netExtendedPrice =
price?.convertedNetExtendedPrice ?? 0;
+ const discountPercent = price?.discountPercent ?? 0;
const additionalCharge =
additionalChargesByQuantity[index] ?? 0;
@@ -330,23 +333,28 @@ const QuotePDF = ({
return (
<View key={quantity} style={tw("flex flex-row")}>
- <Text style={tw("w-1/5 text-right")}>
+ <Text style={tw("w-1/6 text-right")}>
{quantity}
</Text>
- <Text style={tw("w-1/5 text-right")}>
- {netUnitPrice
- ? unitPriceFormatter.format(netUnitPrice)
+ <Text style={tw("w-1/6 text-right")}>
+ {unitPrice
+ ? unitPriceFormatter.format(unitPrice)
+ : "-"}
+ </Text>
+ <Text style={tw("w-1/6 text-right")}>
+ {discountPercent > 0
+ ? `${(discountPercent * 100).toFixed(1)}%`
: "-"}
</Text>
- <Text style={tw("w-1/5 text-right")}>
+ <Text style={tw("w-1/6 text-right")}>
{totalTaxAndFees > 0
? formatter.format(totalTaxAndFees)
: "-"}
</Text>
- <Text style={tw("w-1/5 text-right")}>
+ <Text style={tw("w-1/6 text-right")}>
{price ? `${price.leadTime} days` : "-"}
</Text>
- <Text style={tw("w-1/5 text-right")}>
+ <Text style={tw("w-1/6 text-right")}>
{netUnitPrice
? formatter.format(totalPrice)
: "-"}