From b4c184c26f52216545d148a5f6d68383f014a54d Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Mon, 19 Oct 2026 20:51:33 +0000 Subject: [PATCH] fix(erpsetup): VAT after discount on documents; SEPA days on offers (#80) Co-Authored-By: Claude Opus 5.5 --- internal/erpsetup/branding.go | 13 +++++++++---- internal/erpsetup/erpsetup_test.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/erpsetup/branding.go b/internal/erpsetup/branding.go index caedb83..5c72e44 100644 --- a/internal/erpsetup/branding.go +++ b/internal/erpsetup/branding.go @@ -261,11 +261,15 @@ var documentHTML = brand.Replace(`{%- set is_inv = doc.doctype == "Sales Invoice +{%- set net_discount = (doc.total or 0) - (doc.net_total or 0) -%} +{%- if net_discount > 0.005 %} + + +{%- endif %} -{%- if doc.discount_amount %}{% endif %} -{%- for t in doc.taxes %}{% if t.tax_amount %} - +{%- for t in doc.taxes %}{% if t.tax_amount_after_discount_amount %} + {%- endif %}{% endfor %}
Zwischensumme{{ doc.get_formatted("total") }}
Rabatt− {{ frappe.utils.fmt_money(net_discount, currency=doc.currency) }}
Summe netto{{ doc.get_formatted("net_total") }}
Rabatt− {{ doc.get_formatted("discount_amount") }}
{{ t.description }}{% if t.rate and "%" not in t.description %} ({{ ("%g"|format(t.rate)) | replace(".", ",") }} %){% endif %}{{ t.get_formatted("tax_amount", doc) }}
{{ t.description }}{% if t.rate and "%" not in t.description %} ({{ ("%g"|format(t.rate)) | replace(".", ",") }} %){% endif %}{{ t.get_formatted("tax_amount_after_discount_amount", doc) }}
Gesamtbetrag{{ doc.get_formatted("rounded_total" if doc.rounded_total else "grand_total") }}
@@ -288,7 +292,8 @@ Bitte sorgen Sie für ausreichende Kontodeckung.

Bitte überweisen Sie den Rechnungsbetrag bis zum {{ frappe.utils.formatdate(doc.due_date) }} unter Angabe der Rechnungsnummer {{ doc.name }}.

{%- endif %} {%- elif not is_inv and sepa %} -

Zahlungsart: SEPA-Lastschrift {{ "gemäß erteiltem SEPA-Lastschriftmandat" if has_mandate else "nach Erteilung eines gültigen SEPA-Lastschriftmandats" }}.

+{%- set term = frappe.db.get_value("Payment Term", doc.payment_schedule[0].payment_term, ["credit_days", "due_date_based_on"], as_dict=1) if doc.payment_schedule[0].payment_term else None -%} +

Zahlungsart: SEPA-Lastschrift{% if term and term.credit_days and term.due_date_based_on == "Day(s) after invoice date" %}, Einzug {{ term.credit_days }} Tage nach Rechnungsdatum{% endif %}, {{ "gemäß erteiltem SEPA-Lastschriftmandat" if has_mandate else "nach Erteilung eines gültigen SEPA-Lastschriftmandats" }}.

{%- endif %} {%- if doc.get("terms") %}
{{ doc.terms }}
{% endif %}

Vielen Dank für Ihr Vertrauen.

diff --git a/internal/erpsetup/erpsetup_test.go b/internal/erpsetup/erpsetup_test.go index 2fa3124..742f78a 100644 --- a/internal/erpsetup/erpsetup_test.go +++ b/internal/erpsetup/erpsetup_test.go @@ -517,3 +517,17 @@ func TestLetterHeadSourceIsRestoredAndTbodyIgnored(t *testing.T) { t.Fatalf("source not restored: %v", lh["source"]) } } + +// With a discount (above all one on the grand total) ERPNext lowers net_total +// but keeps tax_amount at the pre-discount value; the printed VAT must be the +// amount after the discount, and the lines must add up to the total. +func TestTotalsPrintTheTaxAfterDiscount(t *testing.T) { + if strings.Contains(documentHTML, `get_formatted("tax_amount", doc)`) || strings.Contains(documentHTML, `get_formatted("discount_amount")`) { + t.Fatal("totals print the pre-discount tax or the gross discount") + } + for _, want := range []string{`tax_amount_after_discount_amount`, `Zwischensumme`, `doc.total`, `Einzug {{ term.credit_days }} Tage`} { + if !strings.Contains(documentHTML, want) { + t.Errorf("document template lacks %q", want) + } + } +}