refactor: merge taxes from delivery note to Sales Invoice

(cherry picked from commit 550cbbd91c)
This commit is contained in:
ruthra kumar
2024-04-01 16:25:35 +05:30
committed by Mergify
parent 1eb720a7b0
commit 22b16a6b0e
4 changed files with 47 additions and 36 deletions

View File

@@ -3313,6 +3313,37 @@ def check_if_child_table_updated(
return False return False
def merge_taxes(source_taxes, target_doc):
from erpnext.accounts.doctype.pos_invoice_merge_log.pos_invoice_merge_log import (
update_item_wise_tax_detail,
)
existing_taxes = target_doc.get("taxes") or []
idx = 1
for tax in source_taxes:
found = False
for t in existing_taxes:
if t.account_head == tax.account_head and t.cost_center == tax.cost_center:
t.tax_amount = flt(t.tax_amount) + flt(tax.tax_amount_after_discount_amount)
t.base_tax_amount = flt(t.base_tax_amount) + flt(tax.base_tax_amount_after_discount_amount)
update_item_wise_tax_detail(t, tax)
found = True
if not found:
tax.charge_type = "Actual"
tax.idx = idx
idx += 1
tax.included_in_print_rate = 0
tax.dont_recompute_tax = 1
tax.row_id = ""
tax.tax_amount = tax.tax_amount_after_discount_amount
tax.base_tax_amount = tax.base_tax_amount_after_discount_amount
tax.item_wise_tax_detail = tax.item_wise_tax_detail
existing_taxes.append(tax)
target_doc.set("taxes", existing_taxes)
@erpnext.allow_regional @erpnext.allow_regional
def validate_regional(doc): def validate_regional(doc):
pass pass

View File

@@ -819,7 +819,7 @@ erpnext.utils.map_current_doc = function (opts) {
if (opts.source_doctype) { if (opts.source_doctype) {
let data_fields = []; let data_fields = [];
if (opts.source_doctype == "Purchase Receipt") { if (["Purchase Receipt", "Delivery Note"].includes(opts.source_doctype)) {
data_fields.push({ data_fields.push({
fieldname: "merge_taxes", fieldname: "merge_taxes",
fieldtype: "Check", fieldtype: "Check",
@@ -845,7 +845,10 @@ erpnext.utils.map_current_doc = function (opts) {
return; return;
} }
opts.source_name = values; opts.source_name = values;
if (opts.allow_child_item_selection || opts.source_doctype == "Purchase Receipt") { if (
opts.allow_child_item_selection ||
["Purchase Receipt", "Delivery Note"].includes(opts.source_doctype)
) {
// args contains filtered child docnames // args contains filtered child docnames
opts.args = args; opts.args = args;
} }

View File

@@ -10,7 +10,7 @@ from frappe.model.mapper import get_mapped_doc
from frappe.model.utils import get_fetch_values from frappe.model.utils import get_fetch_values
from frappe.utils import cint, flt from frappe.utils import cint, flt
from erpnext.controllers.accounts_controller import get_taxes_and_charges from erpnext.controllers.accounts_controller import get_taxes_and_charges, merge_taxes
from erpnext.controllers.selling_controller import SellingController from erpnext.controllers.selling_controller import SellingController
from erpnext.stock.doctype.batch.batch import set_batch_nos from erpnext.stock.doctype.batch.batch import set_batch_nos
from erpnext.stock.doctype.serial_no.serial_no import get_delivery_note_serial_no from erpnext.stock.doctype.serial_no.serial_no import get_delivery_note_serial_no
@@ -623,7 +623,7 @@ def get_returned_qty_map(delivery_note):
@frappe.whitelist() @frappe.whitelist()
def make_sales_invoice(source_name, target_doc=None): def make_sales_invoice(source_name, target_doc=None, args=None):
doc = frappe.get_doc("Delivery Note", source_name) doc = frappe.get_doc("Delivery Note", source_name)
to_make_invoice_qty_map = {} to_make_invoice_qty_map = {}
@@ -637,6 +637,9 @@ def make_sales_invoice(source_name, target_doc=None):
if len(target.get("items")) == 0: if len(target.get("items")) == 0:
frappe.throw(_("All these items have already been Invoiced/Returned")) frappe.throw(_("All these items have already been Invoiced/Returned"))
if args and args.get("merge_taxes"):
merge_taxes(source.get("taxes") or [], target)
target.run_method("calculate_taxes_and_totals") target.run_method("calculate_taxes_and_totals")
# set company address # set company address
@@ -701,7 +704,11 @@ def make_sales_invoice(source_name, target_doc=None):
if not doc.get("is_return") if not doc.get("is_return")
else get_pending_qty(d) > 0, else get_pending_qty(d) > 0,
}, },
"Sales Taxes and Charges": {"doctype": "Sales Taxes and Charges", "add_if_empty": True}, "Sales Taxes and Charges": {
"doctype": "Sales Taxes and Charges",
"add_if_empty": True,
"ignore": args.get("merge_taxes") if args else 0,
},
"Sales Team": { "Sales Team": {
"doctype": "Sales Team", "doctype": "Sales Team",
"field_map": {"incentives": "incentives"}, "field_map": {"incentives": "incentives"},

View File

@@ -14,6 +14,7 @@ import erpnext
from erpnext.accounts.utils import get_account_currency from erpnext.accounts.utils import get_account_currency
from erpnext.assets.doctype.asset.asset import get_asset_account, is_cwip_accounting_enabled from erpnext.assets.doctype.asset.asset import get_asset_account, is_cwip_accounting_enabled
from erpnext.buying.utils import check_on_hold_or_closed_status from erpnext.buying.utils import check_on_hold_or_closed_status
from erpnext.controllers.accounts_controller import merge_taxes
from erpnext.controllers.buying_controller import BuyingController from erpnext.controllers.buying_controller import BuyingController
from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_transaction from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_transaction
@@ -974,37 +975,6 @@ def get_item_wise_returned_qty(pr_doc):
) )
def merge_taxes(source_taxes, target_doc):
from erpnext.accounts.doctype.pos_invoice_merge_log.pos_invoice_merge_log import (
update_item_wise_tax_detail,
)
existing_taxes = target_doc.get("taxes") or []
idx = 1
for tax in source_taxes:
found = False
for t in existing_taxes:
if t.account_head == tax.account_head and t.cost_center == tax.cost_center:
t.tax_amount = flt(t.tax_amount) + flt(tax.tax_amount_after_discount_amount)
t.base_tax_amount = flt(t.base_tax_amount) + flt(tax.base_tax_amount_after_discount_amount)
update_item_wise_tax_detail(t, tax)
found = True
if not found:
tax.charge_type = "Actual"
tax.idx = idx
idx += 1
tax.included_in_print_rate = 0
tax.dont_recompute_tax = 1
tax.row_id = ""
tax.tax_amount = tax.tax_amount_after_discount_amount
tax.base_tax_amount = tax.base_tax_amount_after_discount_amount
tax.item_wise_tax_detail = tax.item_wise_tax_detail
existing_taxes.append(tax)
target_doc.set("taxes", existing_taxes)
@frappe.whitelist() @frappe.whitelist()
def make_purchase_invoice(source_name, target_doc=None, args=None): def make_purchase_invoice(source_name, target_doc=None, args=None):
from erpnext.accounts.party import get_payment_terms_template from erpnext.accounts.party import get_payment_terms_template