From 8719c1a6b8543a62277bfb0892d6411eb5367873 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 12 May 2015 12:28:10 +0530 Subject: [PATCH] Fix taxes and totals in party currency --- erpnext/patches.txt | 1 + .../fix_taxes_and_totals_in_party_currency.py | 63 +++++++++++++++++++ .../taxes_and_totals_in_party_currency.py | 19 +++--- 3 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 erpnext/patches/v5_0/fix_taxes_and_totals_in_party_currency.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 33a3f31304..5dff001622 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -153,3 +153,4 @@ execute:frappe.delete_doc("Page", "stock-ledger") execute:frappe.delete_doc("Page","stock-level") erpnext.patches.v5_0.reclculate_planned_operating_cost_in_production_order erpnext.patches.v5_0.repost_requested_qty +erpnext.patches.v5_0.fix_taxes_and_totals_in_party_currency \ No newline at end of file diff --git a/erpnext/patches/v5_0/fix_taxes_and_totals_in_party_currency.py b/erpnext/patches/v5_0/fix_taxes_and_totals_in_party_currency.py new file mode 100644 index 0000000000..38ee2ffeb8 --- /dev/null +++ b/erpnext/patches/v5_0/fix_taxes_and_totals_in_party_currency.py @@ -0,0 +1,63 @@ + +# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# License: GNU General Public License v3. See license.txt + +import frappe +from frappe.model.meta import get_field_precision + +def execute(): + selling_doctypes = ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"] + buying_doctypes = ["Supplier Quotation", "Purchase Order", "Purchase Receipt", "Purchase Invoice"] + + for dt in selling_doctypes: + update_values(dt, "Sales Taxes and Charges") + + for dt in buying_doctypes: + update_values(dt, "Purchase Taxes and Charges") + +def update_values(dt, tax_table): + rate_field_precision = get_field_precision(frappe.get_meta(dt + " Item").get_field("rate")) + tax_amount_precision = get_field_precision(frappe.get_meta(tax_table).get_field("tax_amount")) + + # update net_total, discount_on + frappe.db.sql(""" + UPDATE + `tab{0}` + SET + total_taxes_and_charges = round(base_total_taxes_and_charges / conversion_rate, {1}) + WHERE + docstatus < 2 + and ifnull(base_total_taxes_and_charges, 0) != 0 + and ifnull(total_taxes_and_charges, 0) = 0 + """.format(dt, tax_amount_precision)) + + # update net_amount + frappe.db.sql(""" + UPDATE + `tab{0}` par, `tab{1}` item + SET + item.net_amount = round(item.base_net_amount / par.conversion_rate, {2}), + item.net_rate = round(item.base_net_rate / par.conversion_rate, {2}) + WHERE + par.name = item.parent + and par.docstatus < 2 + and ((ifnull(item.base_net_amount, 0) != 0 and ifnull(item.net_amount, 0) = 0) + or (ifnull(item.base_net_rate, 0) != 0 and ifnull(item.net_rate, 0) = 0)) + """.format(dt, dt + " Item", rate_field_precision)) + + # update tax in party currency + frappe.db.sql(""" + UPDATE + `tab{0}` par, `tab{1}` tax + SET + tax.tax_amount = round(tax.base_tax_amount / par.conversion_rate, {2}), + tax.total = round(tax.base_total / conversion_rate, {2}), + tax.tax_amount_after_discount_amount = round(tax.base_tax_amount_after_discount_amount / conversion_rate, {2}) + WHERE + par.name = tax.parent + and par.docstatus < 2 + and ((ifnull(tax.base_tax_amount, 0) != 0 and ifnull(tax.tax_amount, 0) = 0) + or (ifnull(tax.base_total, 0) != 0 and ifnull(tax.total, 0) = 0) + or (ifnull(tax.base_tax_amount_after_discount_amount, 0) != 0 and + ifnull(tax.tax_amount_after_discount_amount, 0) = 0)) + """.format(dt, tax_table, tax_amount_precision)) \ No newline at end of file diff --git a/erpnext/patches/v5_0/taxes_and_totals_in_party_currency.py b/erpnext/patches/v5_0/taxes_and_totals_in_party_currency.py index c595f4e0bf..f4ed66bcf6 100644 --- a/erpnext/patches/v5_0/taxes_and_totals_in_party_currency.py +++ b/erpnext/patches/v5_0/taxes_and_totals_in_party_currency.py @@ -46,17 +46,16 @@ def update_values(dt, tax_table): WHERE docstatus < 2 """.format(dt, net_total_precision)) - - + # update net_amount frappe.db.sql(""" UPDATE `tab{0}` par, `tab{1}` item SET - item.base_net_amount = item.base_amount, - item.base_net_rate = item.base_rate, - item.net_amount = round(item.base_net_amount / par.conversion_rate, {2}), - item.net_rate = round(item.base_net_rate / par.conversion_rate, {2}), + item.base_net_amount = round(item.base_amount, {2}), + item.base_net_rate = round(item.base_rate, {2}), + item.net_amount = round(item.base_amount / par.conversion_rate, {2}), + item.net_rate = round(item.base_rate / par.conversion_rate, {2}), item.base_amount = round(item.amount * par.conversion_rate, {2}), item.base_rate = round(item.rate * par.conversion_rate, {2}) WHERE @@ -69,12 +68,12 @@ def update_values(dt, tax_table): UPDATE `tab{0}` par, `tab{1}` tax SET - tax.base_tax_amount = tax.tax_amount, - tax.tax_amount = round(tax.base_tax_amount / par.conversion_rate, {2}), + tax.base_tax_amount = round(tax.tax_amount, {2}), + tax.tax_amount = round(tax.tax_amount / par.conversion_rate, {2}), tax.base_total = round(tax.total, {2}), - tax.total = round(tax.base_total / conversion_rate, {2}), + tax.total = round(tax.total / conversion_rate, {2}), tax.base_tax_amount_after_discount_amount = round(tax.tax_amount_after_discount_amount, {2}), - tax.tax_amount_after_discount_amount = round(tax.base_tax_amount_after_discount_amount / conversion_rate, {2}) + tax.tax_amount_after_discount_amount = round(tax.tax_amount_after_discount_amount / conversion_rate, {2}) WHERE par.name = tax.parent and par.docstatus < 2