feat(documents): per-item Sleva (% discount) on offers + invoices
Adds an optional per-line-item percentage discount ("Sleva") to offers and
issued invoices, on both the detail-page editor and the PDF.
- DB: discount Decimal(5,2) DEFAULT 0 on quotation_items + invoice_items
(migration 20260613195833_add_item_discount).
- Totals/VAT: new shared lineNet() (qty × price × (1 − discount/100)); offers'
NET total and invoices' subtotal AND VAT now compute on the discounted net
(every getBase/enrichQuotation/stats path). Backward compatible: discount 0
is identical to before.
- Schemas: discount = numberInRange(0,100), default 0; persisted on
create/update (+ offer duplicate).
- Detail editors: editable "Sleva %" column — offers via DocumentItemsEditor's
new opt-in showDiscount prop (issued orders unaffected); invoices in their own
editor. Spinners hidden + 7rem column so "100" is fully visible. Read-only
invoice view shows the column when any line has a discount.
- PDFs (offers + invoices): conditional "Sleva"/"Discount" column rendered only
when an item has a discount; line/total/VAT always use the discounted net.
Tests: +16 (money, totals incl. VAT-on-discounted-net, schema range, PDF
column present/absent). Full suite 665 pass, tsc -b clean, lint 0. Editor
verified live via Playwright. Design spec in docs/superpowers/specs/.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -49,10 +49,31 @@ export interface DocumentItem {
|
||||
quantity: string | number;
|
||||
unit: string;
|
||||
unit_price: string | number;
|
||||
/** Per-item discount as a percentage (0–100). Offers only; 0 elsewhere. */
|
||||
discount: string | number;
|
||||
/** Offers only — undefined (issued orders) counts as included. */
|
||||
is_included_in_total?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discount input styling: centered, and with the native number spinners hidden
|
||||
* so a 3-digit value ("100") isn't clipped under the up/down arrows.
|
||||
*/
|
||||
const discountInputSx = {
|
||||
"& input": { textAlign: "center" },
|
||||
"& input[type=number]": { MozAppearance: "textfield" as const },
|
||||
"& input::-webkit-outer-spin-button, & input::-webkit-inner-spin-button": {
|
||||
WebkitAppearance: "none",
|
||||
margin: 0,
|
||||
},
|
||||
};
|
||||
|
||||
/** NET of one line after its per-item percentage discount. */
|
||||
const discountedLineNet = (item: DocumentItem): number =>
|
||||
(Number(item.quantity) || 0) *
|
||||
(Number(item.unit_price) || 0) *
|
||||
(1 - (Number(item.discount) || 0) / 100);
|
||||
|
||||
let documentItemKeyCounter = 0;
|
||||
|
||||
/** Unique client-side key for a (new or hydrated) document item row. */
|
||||
@@ -66,6 +87,7 @@ export const emptyDocumentItem = (): DocumentItem => ({
|
||||
quantity: 1,
|
||||
unit: "ks",
|
||||
unit_price: 0,
|
||||
discount: 0,
|
||||
is_included_in_total: true,
|
||||
});
|
||||
|
||||
@@ -73,9 +95,7 @@ export const emptyDocumentItem = (): DocumentItem => ({
|
||||
export const documentItemsTotal = (items: DocumentItem[]): number =>
|
||||
items.reduce(
|
||||
(sum, it) =>
|
||||
it.is_included_in_total === false
|
||||
? sum
|
||||
: sum + (Number(it.quantity) || 0) * (Number(it.unit_price) || 0),
|
||||
it.is_included_in_total === false ? sum : sum + discountedLineNet(it),
|
||||
0,
|
||||
);
|
||||
|
||||
@@ -111,6 +131,7 @@ function SortableItemRow({
|
||||
readOnly,
|
||||
canDelete,
|
||||
showIncludedInTotal,
|
||||
showDiscount,
|
||||
itemDescriptionMaxLength,
|
||||
onUpdate,
|
||||
onRemove,
|
||||
@@ -121,6 +142,7 @@ function SortableItemRow({
|
||||
readOnly: boolean;
|
||||
canDelete: boolean;
|
||||
showIncludedInTotal: boolean;
|
||||
showDiscount: boolean;
|
||||
itemDescriptionMaxLength: number;
|
||||
onUpdate: (field: keyof DocumentItem, value: string | boolean) => void;
|
||||
onRemove: () => void;
|
||||
@@ -143,8 +165,7 @@ function SortableItemRow({
|
||||
position: "relative" as const,
|
||||
zIndex: isDragging ? 10 : undefined,
|
||||
};
|
||||
const lineTotal =
|
||||
(Number(item.quantity) || 0) * (Number(item.unit_price) || 0);
|
||||
const lineTotal = discountedLineNet(item);
|
||||
|
||||
if (isMobile) {
|
||||
return (
|
||||
@@ -253,6 +274,17 @@ function SortableItemRow({
|
||||
InputProps={{ readOnly }}
|
||||
sx={{ "& input": { textAlign: "right" } }}
|
||||
/>
|
||||
{showDiscount && (
|
||||
<TextField
|
||||
label="Sleva %"
|
||||
type="number"
|
||||
value={item.discount ?? ""}
|
||||
onChange={(e) => onUpdate("discount", e.target.value)}
|
||||
slotProps={{ htmlInput: { min: "0", max: "100", step: "any" } }}
|
||||
InputProps={{ readOnly }}
|
||||
sx={discountInputSx}
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
<Box
|
||||
sx={{
|
||||
@@ -396,6 +428,18 @@ function SortableItemRow({
|
||||
sx={{ "& input": { textAlign: "right" } }}
|
||||
/>
|
||||
</TableCell>
|
||||
{showDiscount && (
|
||||
<TableCell>
|
||||
<TextField
|
||||
type="number"
|
||||
value={item.discount ?? ""}
|
||||
onChange={(e) => onUpdate("discount", e.target.value)}
|
||||
slotProps={{ htmlInput: { min: "0", max: "100", step: "any" } }}
|
||||
InputProps={{ readOnly }}
|
||||
sx={discountInputSx}
|
||||
/>
|
||||
</TableCell>
|
||||
)}
|
||||
{showIncludedInTotal && (
|
||||
<TableCell align="center">
|
||||
<Checkbox
|
||||
@@ -442,6 +486,8 @@ interface DocumentItemsEditorProps {
|
||||
error?: string;
|
||||
/** Render the offers-only "V ceně" (is_included_in_total) column. */
|
||||
showIncludedInTotal?: boolean;
|
||||
/** Render the per-item "Sleva %" discount column (offers only). */
|
||||
showDiscount?: boolean;
|
||||
/** Schema cap for the detailed description (offers 8000, issued orders 5000). */
|
||||
itemDescriptionMaxLength?: number;
|
||||
/** Optional page-specific control (e.g. item-template Select) next to the add button. */
|
||||
@@ -460,6 +506,7 @@ export default function DocumentItemsEditor({
|
||||
readOnly,
|
||||
error,
|
||||
showIncludedInTotal = false,
|
||||
showDiscount = false,
|
||||
itemDescriptionMaxLength = 5000,
|
||||
templatesSlot,
|
||||
}: DocumentItemsEditorProps) {
|
||||
@@ -563,6 +610,7 @@ export default function DocumentItemsEditor({
|
||||
readOnly={readOnly}
|
||||
canDelete={items.length > 1}
|
||||
showIncludedInTotal={showIncludedInTotal}
|
||||
showDiscount={showDiscount}
|
||||
itemDescriptionMaxLength={itemDescriptionMaxLength}
|
||||
onUpdate={(field, value) => updateItem(index, field, value)}
|
||||
onRemove={() => removeItem(index)}
|
||||
@@ -594,6 +642,11 @@ export default function DocumentItemsEditor({
|
||||
<TableCell sx={{ width: "8rem" }} align="center">
|
||||
Jedn. cena
|
||||
</TableCell>
|
||||
{showDiscount && (
|
||||
<TableCell sx={{ width: "7rem" }} align="center">
|
||||
Sleva %
|
||||
</TableCell>
|
||||
)}
|
||||
{showIncludedInTotal && (
|
||||
<TableCell sx={{ width: "4rem" }} align="center">
|
||||
V ceně
|
||||
@@ -615,6 +668,7 @@ export default function DocumentItemsEditor({
|
||||
readOnly={readOnly}
|
||||
canDelete={items.length > 1}
|
||||
showIncludedInTotal={showIncludedInTotal}
|
||||
showDiscount={showDiscount}
|
||||
itemDescriptionMaxLength={itemDescriptionMaxLength}
|
||||
onUpdate={(field, value) =>
|
||||
updateItem(index, field, value)
|
||||
|
||||
Reference in New Issue
Block a user