4 Commits
Author SHA1 Message Date
kurogeek ed4062590a feat: show expected withholding tax on Sales Invoice
Custom fields Tax Withholding Category (fetched from Customer),
Withholding Tax and Net Payable After Withholding Tax on Sales Invoice;
computed in the Thai set_tax_withholding override from the category
rate and single threshold, informational only (totals/GL unchanged),
printed under Rounded Total by the standard layout. Payment Entry now
prefers each invoice's category over the customer's.
2026-09-11 10:20:03 +00:00
kurogeek 4767d4bb46 test: withholding tax accounts, fixture re-import, and customer receipts
Integration tests on a throwaway Thai company: account creation and
category linking on Company insert, site-configured account surviving
fixture re-import, Sales Invoice left un-grossed, and Payment Entry
deductions (full, prorated, below threshold, unchecked). Run with
CI=1 bench --site <site> run-tests --app default_thai_company.
2026-09-11 10:09:01 +00:00
kurogeek 6819242521 feat: customer-side withholding tax on Payment Entry
Thai customers withhold tax at payment; ERPNext's customer-side handling
is Indian TCS which adds tax on top of the Sales Invoice. Override
SalesInvoice.set_tax_withholding to no-op for Thai companies, expose
"Apply Tax Withholding Amount" on Customer/Receive Payment Entries, and
on validate compute rate x pre-VAT amount of each allocated reference
(prorated, single threshold honoured) as a deduction to "Withholding Tax
Receivable". Client script fetches the Customer's category when the box
is ticked.
2026-09-11 10:00:24 +00:00
kurogeek 8b3cf9af1e feat: Thai Tax Withholding Category fixtures with per-company accounts
Add 22 Tax Withholding Category fixtures covering Thailand domestic
withholding (Taw.Paw. 4/2528, Sec. 50(2), Sec. 70) with rates and
thresholds. Fixtures ship without accounts; on import each Thai company
gets "Withholding Tax Payable" (Liability) and "Withholding Tax
Receivable" (Asset) created and the payable account linked. Company
on_update does the same for companies created later. Site-configured
accounts survive fixture re-import on migrate.
2026-09-11 09:13:45 +00:00
8 changed files with 1153 additions and 6 deletions
@@ -0,0 +1,50 @@
[
{
"docstatus": 0,
"doctype": "Custom Field",
"dt": "Sales Invoice",
"fetch_from": "customer.tax_withholding_category",
"fetch_if_empty": 1,
"fieldname": "tax_withholding_category",
"fieldtype": "Link",
"insert_after": "taxes_and_charges",
"label": "Tax Withholding Category",
"modified": "2026-09-11 18:00:00.000000",
"name": "Sales Invoice-tax_withholding_category",
"options": "Tax Withholding Category",
"print_hide": 1
},
{
"depends_on": "eval:doc.tax_withholding_category",
"docstatus": 0,
"doctype": "Custom Field",
"dt": "Sales Invoice",
"fieldname": "withholding_tax_amount",
"fieldtype": "Currency",
"insert_after": "rounded_total",
"label": "Withholding Tax",
"modified": "2026-09-11 18:00:00.000000",
"name": "Sales Invoice-withholding_tax_amount",
"no_copy": 1,
"options": "currency",
"print_hide_if_no_value": 1,
"read_only": 1
},
{
"bold": 1,
"depends_on": "eval:doc.tax_withholding_category",
"docstatus": 0,
"doctype": "Custom Field",
"dt": "Sales Invoice",
"fieldname": "amount_after_withholding",
"fieldtype": "Currency",
"insert_after": "withholding_tax_amount",
"label": "Net Payable After Withholding Tax",
"modified": "2026-09-11 18:00:00.000000",
"name": "Sales Invoice-amount_after_withholding",
"no_copy": 1,
"options": "currency",
"print_hide_if_no_value": 1,
"read_only": 1
}
]
@@ -14,5 +14,21 @@
"property_type": "Data", "property_type": "Data",
"row_name": null, "row_name": null,
"value": "Default Standard Sales Invoice" "value": "Default Standard Sales Invoice"
},
{
"default_value": null,
"doc_type": "Payment Entry",
"docstatus": 0,
"doctype": "Property Setter",
"doctype_or_field": "DocField",
"field_name": "apply_tax_withholding_amount",
"is_system_generated": 0,
"modified": "2026-09-11 12:00:00.000000",
"module": null,
"name": "Payment Entry-apply_tax_withholding_amount-depends_on",
"property": "depends_on",
"property_type": "Data",
"row_name": null,
"value": "eval:doc.party_type == 'Supplier' || (doc.party_type == 'Customer' && doc.payment_type == 'Receive')"
} }
] ]
@@ -0,0 +1,574 @@
[
{
"accounts": [],
"category_name": "Commission, brokerage, agency fees - Sec. 40(2), juristic payee (P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 3% - Commission",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 3% - Commission",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 3.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Royalties, goodwill, copyright, other rights - Sec. 40(3), juristic payee (P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 3% - Royalty",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 3% - Royalty",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 3.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Interest - Sec. 40(4)(a), juristic payee excl. banks/finance companies (P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 1% - Interest (Company)",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 1% - Interest (Company)",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 1.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Interest - Sec. 40(4)(a), individual payee, Sec. 50(2) (P.N.D.2)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 15% - Interest (Individual)",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 15% - Interest (Individual)",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1.0,
"tax_withholding_rate": 15.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Dividends, share of profits - Sec. 40(4)(b) (P.N.D.2 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 10% - Dividend",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 10% - Dividend",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1.0,
"tax_withholding_rate": 10.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Rent of property, hire-purchase of movable property - Sec. 40(5) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 5% - Rent",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 5% - Rent",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 5.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Rent of ships under the Merchant Marine Promotion Act - Sec. 40(5) (P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 1% - Ship Rental",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 1% - Ship Rental",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 1.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Law, medicine, engineering, architecture, accounting, fine arts - Sec. 40(6) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 3% - Professional Fee",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 3% - Professional Fee",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 3.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Contract work, contractor supplies essential materials - Sec. 40(7) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 3% - Contract Work",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 3% - Contract Work",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 3.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Hire of work, service fees - Sec. 40(8) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 3% - Service",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 3% - Service",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 3.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Rewards, discounts, sales promotion benefits - Sec. 40(8) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 3% - Sales Promotion",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 3% - Sales Promotion",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 3.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Advertising fees - Sec. 40(8) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 2% - Advertising",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 2% - Advertising",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 2.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Transport of goods or passengers, excl. public transport - Sec. 40(8) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 1% - Transportation",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 1% - Transportation",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 1.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Non-life insurance premiums - Sec. 40(8) (P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 1% - Non-Life Insurance",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 1% - Non-Life Insurance",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 1.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Prizes from contests, competitions, lucky draws - Sec. 40(8) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 5% - Prize",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 5% - Prize",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 5.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Public entertainers resident in Thailand - Sec. 40(8) (P.N.D.3)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 5% - Public Entertainer",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 5% - Public Entertainer",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 5.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Rubber, cassava, jute, rice, maize, sugarcane, coffee beans, oil palm - Sec. 40(8) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 0.75% - Agricultural Produce",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 0.75% - Agricultural Produce",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 0.75,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Aquatic animals bought from non-registered sellers - Sec. 40(8) (P.N.D.3 / P.N.D.53)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 1% - Aquatic Animals",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 1% - Aquatic Animals",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1000.0,
"tax_withholding_rate": 1.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Sale of immovable property, juristic seller - Sec. 69 Ter (remitted at the Land Office)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 1% - Immovable Property (Company)",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 1% - Immovable Property (Company)",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1.0,
"tax_withholding_rate": 1.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Sec. 40(2)-(6) income paid to non-resident individuals - Sec. 50(2) (P.N.D.3)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 15% - Non-Resident Individual",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 15% - Non-Resident Individual",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1.0,
"tax_withholding_rate": 15.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Sec. 40(2)-(6) income excl. dividends to foreign companies not doing business in Thailand - Sec. 70, pre-DTA (P.N.D.54)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 15% - Foreign Company",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 15% - Foreign Company",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1.0,
"tax_withholding_rate": 15.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
},
{
"accounts": [],
"category_name": "Dividends to foreign companies not doing business in Thailand - Sec. 70, pre-DTA (P.N.D.54)",
"consider_party_ledger_amount": 0,
"docstatus": 0,
"doctype": "Tax Withholding Category",
"modified": "2026-09-11 09:00:00.000000",
"name": "WHT 10% - Foreign Company Dividend",
"rates": [
{
"cumulative_threshold": 0.0,
"docstatus": 0,
"doctype": "Tax Withholding Rate",
"from_date": "2024-01-01",
"idx": 1,
"parent": "WHT 10% - Foreign Company Dividend",
"parentfield": "rates",
"parenttype": "Tax Withholding Category",
"single_threshold": 1.0,
"tax_withholding_rate": 10.0,
"to_date": "2099-12-31"
}
],
"round_off_tax_amount": 0,
"tax_on_excess_amount": 0
}
]
+39 -6
View File
@@ -43,7 +43,7 @@ required_apps = ["erpnext"]
# page_js = {"page" : "public/js/file.js"} # page_js = {"page" : "public/js/file.js"}
# include js in doctype views # include js in doctype views
# doctype_js = {"doctype" : "public/js/doctype.js"} doctype_js = {"Payment Entry": "public/js/payment_entry.js"}
# doctype_list_js = {"doctype" : "public/js/doctype_list.js"} # doctype_list_js = {"doctype" : "public/js/doctype_list.js"}
# doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"} # doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"}
# doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"} # doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"}
@@ -90,7 +90,31 @@ fixtures = [
{"doctype": "Print Format", "filters": [["name", "in", ["Default Standard Sales Invoice"]]]}, {"doctype": "Print Format", "filters": [["name", "in", ["Default Standard Sales Invoice"]]]},
{ {
"doctype": "Property Setter", "doctype": "Property Setter",
"filters": [["name", "in", ["Sales Invoice-main-default_print_format"]]], "filters": [
[
"name",
"in",
[
"Sales Invoice-main-default_print_format",
"Payment Entry-apply_tax_withholding_amount-depends_on",
],
]
],
},
{"doctype": "Tax Withholding Category", "filters": [["name", "like", "WHT %"]]},
{
"doctype": "Custom Field",
"filters": [
[
"name",
"in",
[
"Sales Invoice-tax_withholding_category",
"Sales Invoice-withholding_tax_amount",
"Sales Invoice-amount_after_withholding",
],
]
],
}, },
] ]
@@ -144,9 +168,9 @@ fixtures = [
# --------------- # ---------------
# Override standard doctype classes # Override standard doctype classes
# override_doctype_class = { override_doctype_class = {
# "ToDo": "custom_app.overrides.CustomToDo" "Sales Invoice": "default_thai_company.tax_withholding.ThaiSalesInvoice",
# } }
# Document Events # Document Events
# --------------- # ---------------
@@ -156,7 +180,16 @@ doc_events = {
"Letter Head": { "Letter Head": {
"before_import": "default_thai_company.utils.stash_letter_head_source", "before_import": "default_thai_company.utils.stash_letter_head_source",
"before_insert": "default_thai_company.utils.restore_letter_head_source", "before_insert": "default_thai_company.utils.restore_letter_head_source",
} },
"Tax Withholding Category": {
"before_import": "default_thai_company.tax_withholding.prepare_fixture_accounts",
},
"Company": {
"on_update": "default_thai_company.tax_withholding.setup_company",
},
"Payment Entry": {
"validate": "default_thai_company.tax_withholding.set_customer_withholding",
},
} }
# Scheduled Tasks # Scheduled Tasks
@@ -0,0 +1,10 @@
frappe.ui.form.on("Payment Entry", {
apply_tax_withholding_amount(frm) {
if (frm.doc.party_type !== "Customer" || !frm.doc.apply_tax_withholding_amount) return;
// ERPNext's handler looks the category up on Supplier and clears it for a
// Customer; wait for that request to settle, then set the Customer's value.
frappe.db.get_value("Customer", frm.doc.party, "tax_withholding_category").then(({ message }) => {
frappe.after_ajax(() => frm.set_value("tax_withholding_category", message.tax_withholding_category));
});
},
});
+244
View File
@@ -0,0 +1,244 @@
import erpnext
import frappe
from erpnext.accounts.doctype.sales_invoice.sales_invoice import SalesInvoice
from erpnext.accounts.doctype.tax_withholding_category.tax_withholding_category import (
get_tax_withholding_details,
normal_round,
)
from frappe import _
from frappe.utils import cint, flt
# Liability: tax we withhold from suppliers and remit on P.N.D.3/53 (linked on every category).
# Asset: tax customers withhold from us, creditable against corporate income tax.
LIABILITY_ACCOUNT = "Withholding Tax Payable"
ASSET_ACCOUNT = "Withholding Tax Receivable"
# Preferred parent groups by root type; first match wins, else the root account.
PARENT_GROUPS = {
"Liability": ("Duties and Taxes", "Current Liabilities"),
"Asset": ("Tax Assets", "Current Assets"),
}
CATEGORY_FILTERS = {"name": ("like", "WHT %")}
def thai_companies():
return frappe.get_all("Company", filters={"country": "Thailand"}, pluck="name")
def get_or_create_account(company, account_name, root_type):
name = frappe.db.get_value(
"Account", {"company": company, "account_name": account_name, "is_group": 0, "root_type": root_type}
)
if name:
return name
account = frappe.get_doc(
{
"doctype": "Account",
"company": company,
"account_name": account_name,
"parent_account": find_parent_group(company, root_type),
"root_type": root_type,
"account_type": "Tax",
"is_group": 0,
}
).insert(ignore_permissions=True)
return account.name
def find_parent_group(company, root_type):
for account_name in PARENT_GROUPS[root_type]:
parent = frappe.db.get_value(
"Account",
{"company": company, "account_name": account_name, "is_group": 1, "root_type": root_type},
)
if parent:
return parent
return frappe.db.get_value(
"Account",
{"company": company, "root_type": root_type, "is_group": 1, "parent_account": ("is", "not set")},
)
def ensure_company_accounts(company):
"""Create both withholding accounts for `company`; return the liability account name."""
get_or_create_account(company, ASSET_ACCOUNT, "Asset")
return get_or_create_account(company, LIABILITY_ACCOUNT, "Liability")
def link_company(category, company, account):
"""Append `account` for `company` to the category's accounts table if missing."""
if any(row.company == company for row in category.accounts):
return False
category.append("accounts", {"company": company, "account": account})
return True
def prepare_fixture_accounts(doc, method=None):
"""Tax Withholding Category fixtures ship without `accounts` (company-specific).
Fixture import re-inserts the doc on every migrate, so: carry over the rows
already configured on this site, then link every Thai company, creating the
withholding accounts on first use. Mandatory is relaxed for sites without a
Thai company yet (setup wizard not run); `setup_company` links them later.
"""
doc.flags.ignore_mandatory = True
if not doc.accounts and frappe.db.exists(doc.doctype, doc.name):
for row in frappe.get_all(
"Tax Withholding Account",
filters={"parent": doc.name, "parenttype": doc.doctype},
fields=["company", "account"],
order_by="idx",
):
doc.append("accounts", row)
for company in thai_companies():
if not any(row.company == company for row in doc.accounts):
link_company(doc, company, ensure_company_accounts(company))
def setup_company(doc, method=None):
"""Company.on_update: create withholding accounts and link every WHT category."""
if doc.country != "Thailand" or not frappe.db.exists("Account", {"company": doc.name}):
return
account = ensure_company_accounts(doc.name)
for name in frappe.get_all("Tax Withholding Category", filters=CATEGORY_FILTERS, pluck="name"):
category = frappe.get_doc("Tax Withholding Category", name)
if link_company(category, doc.name, account):
category.save(ignore_permissions=True)
def is_thai_company(company):
return frappe.get_cached_value("Company", company, "country") == "Thailand"
def get_withholding_details(category, posting_date, company):
details = get_tax_withholding_details(category, posting_date, company)
if not details:
frappe.throw(
_("Tax Withholding Category {0} has no account for Company {1}").format(category, company)
)
return details
def withholding_on(details, taxable, base_taxable, precision):
"""Withholding for a taxable amount; the single threshold is checked in company currency."""
if details.threshold and flt(base_taxable) < flt(details.threshold):
return 0.0
amount = flt(taxable) * flt(details.rate) / 100
return normal_round(amount) if cint(details.round_off_tax_amount) else flt(amount, precision)
class ThaiSalesInvoice(SalesInvoice):
def set_tax_withholding(self):
"""Thai customers withhold at payment (see `set_customer_withholding`);
ERPNext's customer-side handling is Indian TCS, which adds tax on top of
the invoice. For Thai companies only show the expected withholding and
the net payable; totals and GL are untouched."""
if not is_thai_company(self.company):
return super().set_tax_withholding()
self.withholding_tax_amount = self.amount_after_withholding = 0
if not self.tax_withholding_category:
return
details = get_withholding_details(self.tax_withholding_category, self.posting_date, self.company)
self.withholding_tax_amount = withholding_on(
details, self.net_total, self.base_net_total, self.precision("withholding_tax_amount")
)
if self.withholding_tax_amount:
self.amount_after_withholding = flt(
(self.rounded_total or self.grand_total) - self.withholding_tax_amount,
self.precision("amount_after_withholding"),
)
def set_customer_withholding(doc, method=None):
"""Payment Entry.validate: tax withheld by the customer on a Receive entry
becomes a deduction to the company's withholding receivable account.
Withholding is rate x pre-VAT amount of each allocated reference, prorated
by the allocation. `paid_amount` is the cash actually received, so the
deduction closes the difference against the gross allocation. Runs after
the controller's validate (exchange rates and allocations are final), then
re-derives the two amounts that depend on deductions.
"""
if doc.party_type != "Customer" or doc.payment_type != "Receive" or not doc.apply_tax_withholding_amount:
return
if not doc.tax_withholding_category:
doc.tax_withholding_category = frappe.db.get_value("Customer", doc.party, "tax_withholding_category")
account = frappe.db.get_value(
"Account",
{"company": doc.company, "account_name": ASSET_ACCOUNT, "root_type": "Asset", "is_group": 0},
)
if not account:
frappe.throw(_("Account {0} not found for Company {1}").format(ASSET_ACCOUNT, doc.company))
amount, description = get_customer_withholding(doc)
row = next((d for d in doc.deductions if d.account == account), None)
if not amount:
if row:
doc.remove(row)
else:
if not row:
row = doc.append("deductions", {"account": account})
row.amount = amount
row.description = description
row.cost_center = doc.cost_center or erpnext.get_default_cost_center(doc.company)
doc.set_unallocated_amount()
doc.set_difference_amount()
def get_customer_withholding(doc):
"""Total withheld across allocated references, in company currency.
A Sales Invoice's own category wins over the Payment Entry's; Sales Orders
use the Payment Entry's. Withholding is prorated by allocated / grand total
on the pre-VAT amount, per reference.
"""
total = 0.0
details_by_category = {}
precision = doc.precision("difference_amount")
for ref in doc.references:
if ref.reference_doctype not in ("Sales Invoice", "Sales Order") or not ref.allocated_amount:
continue
fields = ["net_total", "grand_total"]
if ref.reference_doctype == "Sales Invoice":
fields.append("tax_withholding_category")
values = frappe.db.get_value(ref.reference_doctype, ref.reference_name, fields, as_dict=True)
if not values.grand_total:
continue
category = values.get("tax_withholding_category") or doc.tax_withholding_category
if not category:
frappe.throw(
_("Please set Tax Withholding Category on {0} {1} or on this Payment Entry").format(
_(ref.reference_doctype), ref.reference_name
)
)
if category not in details_by_category:
details_by_category[category] = get_withholding_details(category, doc.posting_date, doc.company)
base_taxable = (
flt(ref.allocated_amount)
* flt(values.net_total)
/ flt(values.grand_total)
* flt(doc.source_exchange_rate)
)
total += withholding_on(details_by_category[category], base_taxable, base_taxable, precision)
if len(details_by_category) == 1:
description = next(iter(details_by_category.values())).description
else:
description = _("Withholding tax deducted by customer")
return flt(total, precision), description
@@ -0,0 +1,220 @@
import frappe
from frappe.modules.import_file import import_file_by_path
from frappe.tests.utils import FrappeTestCase
from frappe.utils import nowdate
from default_thai_company.tax_withholding import ASSET_ACCOUNT, LIABILITY_ACCOUNT, thai_companies
COMPANY = "_Test WHT Company"
ABBR = "_TWC"
CUSTOMER = "_Test WHT Customer"
ITEM = "_Test WHT Service"
FIXTURE = frappe.get_app_path("default_thai_company", "fixtures", "tax_withholding_category.json")
CUSTOM_FIELDS = frappe.get_app_path("default_thai_company", "fixtures", "custom_field.json")
class TestTaxWithholding(FrappeTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Fixture categories must exist before the company is created so the
# Company hook has something to link; force=True re-imports.
import_file_by_path(FIXTURE, force=True, data_import=True)
import_file_by_path(CUSTOM_FIELDS, force=True, data_import=True)
frappe.clear_cache(doctype="Sales Invoice")
frappe.get_doc(
{
"doctype": "Company",
"company_name": COMPANY,
"abbr": ABBR,
"country": "Thailand",
"default_currency": "THB",
"chart_of_accounts": "Standard",
}
).insert()
cls.payable = f"{LIABILITY_ACCOUNT} - {ABBR}"
cls.receivable = f"{ASSET_ACCOUNT} - {ABBR}"
cls.vat = frappe.get_doc(
{
"doctype": "Account",
"company": COMPANY,
"account_name": "Output VAT",
"parent_account": f"Duties and Taxes - {ABBR}",
"account_type": "Tax",
}
).insert()
frappe.get_doc(
{
"doctype": "Customer",
"customer_name": CUSTOMER,
"customer_type": "Company",
"customer_group": frappe.db.get_value("Customer Group", {"is_group": 0}),
"territory": frappe.db.get_value("Territory", {"is_group": 0}),
"tax_withholding_category": "WHT 3% - Service",
}
).insert()
frappe.get_doc(
{
"doctype": "Item",
"item_code": ITEM,
"item_group": frappe.db.get_value("Item Group", {"is_group": 0}),
"is_stock_item": 0,
"stock_uom": "Nos",
}
).insert()
def make_invoice(self, rate=10000, category=None):
si = frappe.get_doc(
{
"doctype": "Sales Invoice",
"company": COMPANY,
"customer": CUSTOMER,
"posting_date": nowdate(),
"due_date": nowdate(),
"tax_withholding_category": category,
"items": [{"item_code": ITEM, "qty": 1, "rate": rate}],
"taxes": [
{
"charge_type": "On Net Total",
"account_head": self.vat.name,
"rate": 7,
"description": "VAT 7%",
}
],
}
)
si.set_missing_values()
return si.submit()
def make_receipt(self, invoice, allocated, paid, apply=1):
return frappe.get_doc(
{
"doctype": "Payment Entry",
"company": COMPANY,
"payment_type": "Receive",
"party_type": "Customer",
"party": CUSTOMER,
"posting_date": nowdate(),
"paid_from": f"Debtors - {ABBR}",
"paid_to": f"Cash - {ABBR}",
"paid_amount": paid,
"received_amount": paid,
"apply_tax_withholding_amount": apply,
"references": [
{
"reference_doctype": "Sales Invoice",
"reference_name": invoice.name,
"allocated_amount": allocated,
}
],
}
).insert()
def category_account(self, category, company=COMPANY):
return frappe.db.get_value(
"Tax Withholding Account",
{"parent": category, "parenttype": "Tax Withholding Category", "company": company},
"account",
)
def test_company_creation_adds_accounts_and_links_categories(self):
self.assertIn(COMPANY, thai_companies())
self.assertEqual(
frappe.db.get_value("Account", self.payable, ["root_type", "parent_account"]),
("Liability", f"Duties and Taxes - {ABBR}"),
)
self.assertEqual(
frappe.db.get_value("Account", self.receivable, ["root_type", "parent_account"]),
("Asset", f"Tax Assets - {ABBR}"),
)
categories = frappe.get_all(
"Tax Withholding Category", filters={"name": ("like", "WHT %")}, pluck="name"
)
self.assertEqual(len(categories), 22)
for name in categories:
self.assertEqual(self.category_account(name), self.payable, name)
def test_fixture_reimport_keeps_site_account_and_relinks(self):
alt = frappe.get_doc(
{
"doctype": "Account",
"company": COMPANY,
"account_name": "Alternative WHT Payable",
"parent_account": f"Duties and Taxes - {ABBR}",
"account_type": "Tax",
}
).insert()
rent = frappe.get_doc("Tax Withholding Category", "WHT 5% - Rent")
for row in rent.accounts:
if row.company == COMPANY:
row.account = alt.name
rent.save()
import_file_by_path(FIXTURE, force=True, data_import=True)
self.assertEqual(self.category_account("WHT 5% - Rent"), alt.name)
self.assertEqual(self.category_account("WHT 3% - Service"), self.payable)
rates = frappe.get_doc("Tax Withholding Category", "WHT 5% - Rent").rates
self.assertEqual([(r.tax_withholding_rate, r.single_threshold) for r in rates], [(5.0, 1000.0)])
def test_sales_invoice_shows_withholding_without_grossing_up(self):
si = self.make_invoice()
self.assertEqual((si.net_total, si.grand_total), (10000.0, 10700.0))
self.assertEqual([t.account_head for t in si.taxes], [self.vat.name])
self.assertEqual(si.tax_withholding_category, "WHT 3% - Service") # fetched from Customer
self.assertEqual((si.withholding_tax_amount, si.amount_after_withholding), (300.0, 10400.0))
self.assertEqual(frappe.db.get_value("Sales Invoice", si.name, "outstanding_amount"), 10700.0)
def test_sales_invoice_below_threshold_shows_nothing(self):
si = self.make_invoice(rate=800)
self.assertEqual((si.withholding_tax_amount, si.amount_after_withholding), (0.0, 0.0))
def test_receipt_uses_invoice_category_over_customer_category(self):
si = self.make_invoice(category="WHT 5% - Rent")
self.assertEqual(si.withholding_tax_amount, 500.0)
pe = self.make_receipt(si, allocated=10700, paid=10200)
self.assertEqual([(d.account, d.amount) for d in pe.deductions], [(self.receivable, 500.0)])
self.assertEqual(pe.difference_amount, 0)
def test_receipt_deducts_withholding_and_settles_invoice(self):
si = self.make_invoice()
pe = self.make_receipt(si, allocated=10700, paid=10400)
self.assertEqual(pe.tax_withholding_category, "WHT 3% - Service")
self.assertEqual([(d.account, d.amount) for d in pe.deductions], [(self.receivable, 300.0)])
self.assertEqual(pe.difference_amount, 0)
pe.submit()
gl = {
g.account: (g.debit, g.credit)
for g in frappe.get_all(
"GL Entry", filters={"voucher_no": pe.name}, fields=["account", "debit", "credit"]
)
}
self.assertEqual(gl[f"Cash - {ABBR}"], (10400.0, 0.0))
self.assertEqual(gl[self.receivable], (300.0, 0.0))
self.assertEqual(gl[f"Debtors - {ABBR}"], (0.0, 10700.0))
self.assertEqual(frappe.db.get_value("Sales Invoice", si.name, "outstanding_amount"), 0)
def test_partial_allocation_prorates_withholding(self):
si = self.make_invoice()
pe = self.make_receipt(si, allocated=5350, paid=5200)
self.assertEqual([(d.account, d.amount) for d in pe.deductions], [(self.receivable, 150.0)])
self.assertEqual(pe.difference_amount, 0)
def test_below_single_threshold_has_no_deduction(self):
si = self.make_invoice(rate=800) # net 800 < 1,000 threshold
pe = self.make_receipt(si, allocated=856, paid=856)
self.assertEqual(pe.deductions, [])
self.assertEqual(pe.difference_amount, 0)
def test_unchecked_receipt_is_untouched(self):
si = self.make_invoice()
pe = self.make_receipt(si, allocated=10700, paid=10700, apply=0)
self.assertEqual(pe.deductions, [])
self.assertEqual(pe.difference_amount, 0)