feat(invoices): per-line percentage discount and separate fakturamarkning (#2084)

* feat(invoices): per-line percentage discount and separate fakturamarkning

User request: rabatt i procent per artikelrad, and a marking field
separate from Er referens.

- invoice_items.discount_percent (0-100, default 0): line_total and
  vat_amount are stored NET of the discount. Shared exact-ore math in
  lib/invoices/line-amounts.ts (gross, discount, net) used by the web
  builder, staged-operation commit, editor preview, PDF, and Peppol.
  Undiscounted lines keep the legacy unrounded qty*price byte-identical.
- ROT/RUT deduction computes on the discounted net line total.
- invoices.invoice_marking: printed on the PDF next to the references
  and mapped to Peppol BT-10 BuyerReference (marking wins over
  your_reference; either satisfies the BT-10 requirement).
- Peppol renders the discount as a BG-27 line AllowanceCharge
  (reason code 95, MultiplierFactorNumeric, Amount, BaseAmount).
- Editor: "Lagg till rabatt" in the row menu (same reveal pattern as
  ROT/RUT), Markning row next to Er referens, forval chip, review
  dialog shows discounts and marking.
- Plumbed through v1 REST projections, MCP create/get/update invoice
  tools, pending-operations update path, and copy-invoice (discount
  copied; marking deliberately not, it is recipient-specific).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JJAt9yM7tgZ69f1XnNnq52

* fix(invoices): carry discount_percent through every deduction, credit, convert and preview path

Skeptic + CI findings on the discount/marking feature, one pass:

- generateRotRutLines and propose-send-lines now pass discount_percent
  into computeDeduction: the send/credit/cash verifikat booked 1513 on
  the GROSS line while deduction_total, the PDF and the Skatteverket
  claim carried the net, stranding the difference on 1513 and pushing
  1510 negative once the customer paid. Test pins 1513=3000/1510=7000
  for a 20%-discounted 10 000 kr ROT line.
- preview-pdf route accepts discount_percent (net totals + net-based
  deduction) and invoice_marking; the editor now sends the marking, so
  the preview equals the invoice it becomes.
- Credit notes carry discount_percent (buildCreditNoteItem, v1 credit
  route select+insert, MCP credit executor) and invoice_marking, so the
  kreditfaktura face arithmetic multiplies out and shows the Rabatt
  column (ML 17 kap 24 §).
- Proforma->invoice convert copies discount_percent + invoice_marking:
  the converted invoice previously failed Peppol LINE_TOTAL_MISMATCH
  and lost the rebate on the next builder pass.
- Editor hides the discount menu in self-billed mode (the self-billed
  wire shape has no discount; previewed net would book gross).
- MCP staging and commitCreateInvoice reject a non-number
  discount_percent (a string coerced past the range check but was
  ignored by the totals math and still stored).
- Regenerated skills/accounted-api (apiskill:check CI failure).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JJAt9yM7tgZ69f1XnNnq52

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-08-31 15:34:06 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 84c8e1ce59
commit f216a60bf8
35 changed files with 826 additions and 51 deletions
+26 -6
View File
@@ -7,6 +7,7 @@ import { Separator } from '@/components/ui/separator'
import { formatCurrency, formatDate } from '@/lib/utils'
import { getDisplayTotal } from '@/lib/invoices/rounding'
import { isTextLikeLine } from '@/lib/invoices/display'
import { computeLineNet } from '@/lib/invoices/line-amounts'
import { itemHasAccrual } from '@/lib/bookkeeping/accruals/account-suggestions'
import type { Customer, Currency } from '@/types'
@@ -15,6 +16,8 @@ interface ReviewItem {
quantity: number
unit: string
unit_price: number
/** Line discount 0-100; amounts render net of it. */
discount_percent?: number | null
vat_rate?: number
/** 'text' rows are free-text/blank lines: description only, no amounts. */
line_type?: 'product' | 'text'
@@ -38,6 +41,7 @@ interface InvoiceReviewContentProps {
total: number
yourReference?: string
ourReference?: string
invoiceMarking?: string
notes?: string
/** The invoice number that will be assigned on confirm. Null when unknown
* (e.g. delivery notes use a different sequence) or unfetched. */
@@ -63,6 +67,7 @@ export function InvoiceReviewContent({
total,
yourReference,
ourReference,
invoiceMarking,
notes,
numberPreview,
oreRounding,
@@ -84,7 +89,7 @@ export function InvoiceReviewContent({
for (const item of items) {
if (isTextLikeLine(item)) continue
const rate = item.vat_rate ?? 0
const lineTotal = item.quantity * item.unit_price
const lineTotal = computeLineNet(item.quantity, item.unit_price, item.discount_percent)
const lineVat = Math.round(lineTotal * rate / 100 * 100) / 100
vatByRate.set(rate, (vatByRate.get(rate) || 0) + lineVat)
}
@@ -182,12 +187,19 @@ export function InvoiceReviewContent({
</td>
<td className="py-2 text-right">{item.quantity}</td>
<td className="py-2 text-center">{item.unit}</td>
<td className="py-2 text-right">{formatCurrency(item.unit_price, currency)}</td>
<td className="py-2 text-right">
{formatCurrency(item.unit_price, currency)}
{(item.discount_percent ?? 0) > 0 && (
<span className="ml-1 text-xs text-muted-foreground">
&minus;{item.discount_percent}%
</span>
)}
</td>
{showVatColumn && (
<td className="py-2 text-right">{item.vat_rate ?? 0}%</td>
)}
<td className="py-2 text-right">
{formatCurrency(item.quantity * item.unit_price, currency)}
{formatCurrency(computeLineNet(item.quantity, item.unit_price, item.discount_percent), currency)}
</td>
</tr>
)
@@ -214,11 +226,14 @@ export function InvoiceReviewContent({
</p>
)}
<div className="flex items-center justify-between text-muted-foreground">
<span>{item.quantity} {item.unit} × {formatCurrency(item.unit_price, currency)}</span>
<span>
{item.quantity} {item.unit} × {formatCurrency(item.unit_price, currency)}
{(item.discount_percent ?? 0) > 0 && <> &minus;{item.discount_percent}%</>}
</span>
{showVatColumn && <span className="text-xs">{t('mobile_vat_suffix', { rate: item.vat_rate ?? 0 })}</span>}
</div>
<p className="text-right font-medium">
{formatCurrency(item.quantity * item.unit_price, currency)}
{formatCurrency(computeLineNet(item.quantity, item.unit_price, item.discount_percent), currency)}
</p>
</div>
)
@@ -260,7 +275,7 @@ export function InvoiceReviewContent({
</div>
{/* References/notes */}
{(yourReference || ourReference || notes) && (
{(yourReference || ourReference || invoiceMarking || notes) && (
<div className="border-t pt-3 space-y-2 text-sm text-muted-foreground">
{yourReference && (
<p>
@@ -272,6 +287,11 @@ export function InvoiceReviewContent({
<span>{t('our_reference')}</span> {ourReference}
</p>
)}
{invoiceMarking && (
<p>
<span>{t('invoice_marking')}</span> {invoiceMarking}
</p>
)}
{notes && <p>{t('notes_prefix', { notes })}</p>}
</div>
)}