From 11219083525a830563bbe83861ffaafbc41b36f2 Mon Sep 17 00:00:00 2001 From: Ali <010109ali@gmail.com> Date: Wed, 1 Apr 2026 13:50:19 +0400 Subject: [PATCH] fix: send correct VAT amount to e-taxes instead of total with VAT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit vat_18_percent_with_amount stores the total INCLUDING VAT (e.g. 118), but the e-taxes API vat18 field expects just the tax amount (e.g. 18). Also calculates net cost/pricePerUnit for VAT-inclusive pricing (ƏDV daxil 18%). Co-Authored-By: Claude Opus 4.6 (1M context) --- invoice_az/send_sales_api.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/invoice_az/send_sales_api.py b/invoice_az/send_sales_api.py index 52bbede..631542e 100644 --- a/invoice_az/send_sales_api.py +++ b/invoice_az/send_sales_api.py @@ -486,8 +486,23 @@ def build_invoice_payload(doc, serial_number): for item in doc.items: item_master = frappe.get_doc("Item", item.item_code) + # Get the pure VAT tax amount (not total with VAT) + vat_amount = item.vat_amount if hasattr(item, 'vat_amount') and item.vat_amount else 0 + + # Calculate net amount (without VAT) for cost field + # vat_18_percent_with_amount = total WITH VAT (e.g. 118 for net 100) + # For "ƏDV daxil 18%": item.amount already includes VAT, need to subtract + # For "ƏDV 18%": item.amount is net, formula still works (118 - 18 = 100) + if hasattr(item, 'vat_18_percent_with_amount') and item.vat_18_percent_with_amount: + net_amount = item.vat_18_percent_with_amount - vat_amount + else: + net_amount = item.amount + + price_per_unit = net_amount / item.qty if item.qty else item.rate + # Map VAT fields to E-Taxes format - vat18 = item.vat_18_percent_with_amount if hasattr(item, 'vat_18_percent_with_amount') and item.vat_18_percent_with_amount else None + # vat18 must be the VAT tax amount only (e.g. 18), NOT total with VAT (e.g. 118) + vat18 = vat_amount if vat_amount > 0 else None vat0 = item.vat_0_percent_with_amount if hasattr(item, 'vat_0_percent_with_amount') and item.vat_0_percent_with_amount else None vat_free = item.vat_free_amount if hasattr(item, 'vat_free_amount') and item.vat_free_amount else None exempt = item.amount_without_vat if hasattr(item, 'amount_without_vat') and item.amount_without_vat else None @@ -499,8 +514,8 @@ def build_invoice_payload(doc, serial_number): "unit": item.uom, # Use UOM name as text "unitCode": None, # Not required per user confirmation "quantity": item.qty, - "pricePerUnit": item.rate, - "cost": item.amount, + "pricePerUnit": price_per_unit, + "cost": net_amount, "exciseRate": None, "excise": None, "vat18": vat18,