From d005f2b3028e63bb8f429aee89b9aee8750a943d Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Tue, 26 May 2026 00:26:24 +0530 Subject: [PATCH 1/7] fix: post restructure charges capitalization --- lending/install.py | 8 +++++ .../loan_disbursement/loan_disbursement.py | 3 +- .../doctype/loan_repayment/loan_repayment.py | 7 +++-- .../loan_restructure/loan_restructure.py | 30 +++++++++++++++---- .../loan_restructure/test_loan_restructure.py | 5 +++- lending/patches.txt | 1 + .../add_is_post_restructure_charge_field.py | 18 +++++++++++ 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 lending/patches/v16_0/add_is_post_restructure_charge_field.py diff --git a/lending/install.py b/lending/install.py index 0de3f748c..e5943d569 100644 --- a/lending/install.py +++ b/lending/install.py @@ -30,6 +30,14 @@ "read_only": 1, "print_hide": 1, }, + { + "fieldname": "is_post_restructure_charge", + "label": "Is Post Restructure Charge", + "fieldtype": "Check", + "insert_after": "loan_repayment", + "read_only": 1, + "print_hide": 1, + }, { "fieldname": "value_date", "fieldtype": "Date", diff --git a/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py b/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py index 26071be5f..dcbce89e7 100644 --- a/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py +++ b/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py @@ -864,7 +864,7 @@ def make_gl_entries(self, cancel=0, adv_adj=0, repost=0): def make_sales_invoice_for_charge( - loan, reference_fieldname, reference_doctype, applicant, disbursement_date, company, charges + loan, reference_fieldname, reference_doctype, applicant, disbursement_date, company, charges, is_post_restructure_charge=False ): if not charges: return @@ -906,6 +906,7 @@ def make_sales_invoice_for_charge( if reference_doctype == "Loan Disbursement": si.debit_to = receivable_account si.ignore_default_payment_terms_template = 1 + si.is_post_restructure_charge = is_post_restructure_charge si.save() si.submit() diff --git a/lending/loan_management/doctype/loan_repayment/loan_repayment.py b/lending/loan_management/doctype/loan_repayment/loan_repayment.py index 06d00a0aa..93173ae36 100644 --- a/lending/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/lending/loan_management/doctype/loan_repayment/loan_repayment.py @@ -124,7 +124,7 @@ def validate(self): charges = None if self.get("payable_charges"): - if self.repayment_type == "Charge Payment" or (self.repayment_type in ("Charges Waiver", "Charges Capitalization") and self.loan_restructure): + if self.repayment_type == "Charge Payment" or (self.repayment_type in ("Charges Waiver", "Charges Capitalization", "Principal Capitalization") and self.loan_restructure): charges = [d.get("charge_code") for d in self.get("payable_charges")] else: frappe.throw(_("Payable Charges can only be added if Charge Payment, or for Charges Waiver/Capitalization during Loan Restructure")) @@ -2019,7 +2019,9 @@ def make_gl_entries(self, cancel=0, adv_adj=0): ) return - if self.repayment_type == "Principal Capitalization" and self.loan_restructure: + if self.repayment_type == "Principal Capitalization" and self.loan_restructure and not any( + d.sales_invoice for d in self.get("repayment_details") + ): return if cancel: @@ -2346,6 +2348,7 @@ def get_payment_account(self): "Interest Capitalization": "loan_account", "Penalty Capitalization": "loan_account", "Charges Capitalization": "loan_account", + "Principal Capitalization": "loan_account", } if self.repayment_type in ( diff --git a/lending/loan_management/doctype/loan_restructure/loan_restructure.py b/lending/loan_management/doctype/loan_restructure/loan_restructure.py index c371738f1..2f649daf6 100644 --- a/lending/loan_management/doctype/loan_restructure/loan_restructure.py +++ b/lending/loan_management/doctype/loan_restructure/loan_restructure.py @@ -814,7 +814,7 @@ def make_post_restructure_charge_demands(self): ] if post_restructure_charges: - make_sales_invoice_for_charge( + sales_invoice = make_sales_invoice_for_charge( self.loan, "loan_restructure", self.name, @@ -822,7 +822,17 @@ def make_post_restructure_charge_demands(self): self.restructure_date, self.company, post_restructure_charges, + is_post_restructure_charge=True, ) + if sales_invoice: + create_loan_repayment( + self.loan, + self.restructure_date, + "Principal Capitalization", + sales_invoice.grand_total, + restructure_name=self.name, + charge_code=post_restructure_charges + ) def make_loan_repayment_for_adjustment(self): if self.principal_adjusted: @@ -874,10 +884,20 @@ def create_loan_repayment( repayment.loan_disbursement = loan_disbursement if charge_code and waiver_amount > 0: - repayment.append("payable_charges", { - "charge_code": charge_code, - "amount": waiver_amount - }) + # Check if charge_code is a list or a single value + if isinstance(charge_code, list): + # Handle list of charges (from post_restructure_charges) + for charge in charge_code: + repayment.append("payable_charges", { + "charge_code": charge.get("charge"), + "amount": charge.get("amount") + }) + else: + # Handle single charge code (string from make_waiver_and_capitalization_for_charges) + repayment.append("payable_charges", { + "charge_code": charge_code, + "amount": waiver_amount + }) repayment.save() repayment.submit() diff --git a/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py b/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py index 8ff6e6dc0..44c17890d 100644 --- a/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py +++ b/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py @@ -257,13 +257,16 @@ def test_loan_restructure_charges_waiver_and_capitalization(self): pluck="repayment_type", ) - self.assertEqual(len(repayments), 7) + self.assertEqual(len(repayments), 8) counts = Counter(repayments) self.assertEqual(counts.get("Charges Capitalization", 0), 2) self.assertEqual(counts.get("Charges Waiver", 0), 1) + sales_invoice = frappe.db.get_value("Sales Invoice", {"loan": loan.name, "docstatus": 1, "value_date": "2024-04-11"}, "outstanding_amount") + self.assertEqual(flt(sales_invoice), 0, "Expected the Sales Invoice for post-restructure charges to be fully paid.") + def test_unaccrued_interest_capitalization_gl_entries(self): set_loan_accrual_frequency(loan_accrual_frequency="Daily") diff --git a/lending/patches.txt b/lending/patches.txt index 115456d2d..643599520 100644 --- a/lending/patches.txt +++ b/lending/patches.txt @@ -48,3 +48,4 @@ lending.patches.v1_0.update_value_date_in_loan_refund lending.patches.v1_0.update_value_date_in_pending_doctypes lending.patches.v16_0.add_enable_loan_accounting_field lending.patches.v16_0.update_is_invoice_generated_field +lending.patches.v16_0.add_is_post_restructure_charge_field \ No newline at end of file diff --git a/lending/patches/v16_0/add_is_post_restructure_charge_field.py b/lending/patches/v16_0/add_is_post_restructure_charge_field.py new file mode 100644 index 000000000..bf1ccfe35 --- /dev/null +++ b/lending/patches/v16_0/add_is_post_restructure_charge_field.py @@ -0,0 +1,18 @@ +import frappe +from frappe.custom.doctype.custom_field.custom_field import create_custom_fields + + +def execute(): + if not frappe.db.exists("Custom Field", {"dt": "Sales Invoice", "fieldname": "is_post_restructure_charge"}): + custom_fields = { + "Sales Invoice": [ + { + "fieldname": "is_post_restructure_charge", + "label": "Is Post Restructure Charge", + "fieldtype": "Check", + "insert_after": "loan_repayment", + "read_only": 1, + } + ] + } + create_custom_fields(custom_fields, update=True) From 5d749367672a15b2fe0864fbb413da0ae32a7c0a Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Tue, 26 May 2026 00:28:08 +0530 Subject: [PATCH 2/7] fix: remove comment --- .../doctype/loan_restructure/loan_restructure.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lending/loan_management/doctype/loan_restructure/loan_restructure.py b/lending/loan_management/doctype/loan_restructure/loan_restructure.py index 2f649daf6..b2c448612 100644 --- a/lending/loan_management/doctype/loan_restructure/loan_restructure.py +++ b/lending/loan_management/doctype/loan_restructure/loan_restructure.py @@ -884,16 +884,13 @@ def create_loan_repayment( repayment.loan_disbursement = loan_disbursement if charge_code and waiver_amount > 0: - # Check if charge_code is a list or a single value if isinstance(charge_code, list): - # Handle list of charges (from post_restructure_charges) for charge in charge_code: repayment.append("payable_charges", { "charge_code": charge.get("charge"), "amount": charge.get("amount") }) else: - # Handle single charge code (string from make_waiver_and_capitalization_for_charges) repayment.append("payable_charges", { "charge_code": charge_code, "amount": waiver_amount From 02cace562259872113f2eff01257759a9d523c9b Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Tue, 26 May 2026 01:05:18 +0530 Subject: [PATCH 3/7] test: update testcase --- .../doctype/loan_repayment/loan_repayment.py | 6 +++- .../loan_restructure/test_loan_restructure.py | 32 ++++++++++--------- .../add_is_post_restructure_charge_field.py | 1 + 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lending/loan_management/doctype/loan_repayment/loan_repayment.py b/lending/loan_management/doctype/loan_repayment/loan_repayment.py index 93173ae36..f304c9999 100644 --- a/lending/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/lending/loan_management/doctype/loan_repayment/loan_repayment.py @@ -127,7 +127,11 @@ def validate(self): if self.repayment_type == "Charge Payment" or (self.repayment_type in ("Charges Waiver", "Charges Capitalization", "Principal Capitalization") and self.loan_restructure): charges = [d.get("charge_code") for d in self.get("payable_charges")] else: - frappe.throw(_("Payable Charges can only be added if Charge Payment, or for Charges Waiver/Capitalization during Loan Restructure")) + frappe.throw( + _( + "Payable Charges can only be added for Charge Payment, or for Charges Waiver/Charges Capitalization/Principal Capitalization during Loan Restructure" + ) + ) amounts = calculate_amounts( self.against_loan, diff --git a/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py b/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py index 44c17890d..e6ca027eb 100644 --- a/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py +++ b/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py @@ -195,7 +195,7 @@ def test_loan_restructure_charges_waiver_and_capitalization(self): process_daily_loan_demands(loan=loan.name, posting_date="2024-04-05") - sales_invoice = frappe.get_doc( + frappe.get_doc( { "doctype": "Sales Invoice", "customer": "_Test Customer 1", @@ -212,10 +212,9 @@ def test_loan_restructure_charges_waiver_and_capitalization(self): {"item_code": "Documentation Charge", "qty": 1, "rate": 3000}, ], } - ) - sales_invoice.submit() + ).submit() - sales_invoice = frappe.get_doc( + frappe.get_doc( { "doctype": "Sales Invoice", "customer": "_Test Customer 1", @@ -227,8 +226,7 @@ def test_loan_restructure_charges_waiver_and_capitalization(self): "set_posting_time": 1, "items": [{"item_code": "Processing Fee", "qty": 1, "rate": 2000}], } - ) - sales_invoice.submit() + ).submit() loan_restructure = create_loan_restructure( loan=loan.name, @@ -244,13 +242,6 @@ def test_loan_restructure_charges_waiver_and_capitalization(self): loan_restructure.status = "Approved" loan_restructure.save() - invoices = frappe.db.get_all( - "Sales Invoice", - filters={"loan": loan.name, "docstatus": 1, "value_date": "2024-04-11"}, - pluck="name", - ) - self.assertEqual(len(invoices), 1, "Expected 1 Sales Invoice to be created for post-restructure charges.") - repayments = frappe.db.get_all( "Loan Repayment", filters={"loan_restructure": loan_restructure.name, "docstatus": 1}, @@ -264,8 +255,19 @@ def test_loan_restructure_charges_waiver_and_capitalization(self): self.assertEqual(counts.get("Charges Capitalization", 0), 2) self.assertEqual(counts.get("Charges Waiver", 0), 1) - sales_invoice = frappe.db.get_value("Sales Invoice", {"loan": loan.name, "docstatus": 1, "value_date": "2024-04-11"}, "outstanding_amount") - self.assertEqual(flt(sales_invoice), 0, "Expected the Sales Invoice for post-restructure charges to be fully paid.") + sales_invoice = frappe.db.get_value( + "Sales Invoice", + { + "loan": loan.name, + "docstatus": 1, + "value_date": "2024-04-11", + "is_post_restructure_charge": 1, + }, + ["outstanding_amount", "status"], + as_dict=True, + ) + self.assertEqual(flt(sales_invoice.outstanding_amount), 0, "Expected the Sales Invoice for post-restructure charges to be fully paid.") + self.assertEqual(sales_invoice.status, "Paid", "Expected the Sales Invoice for post-restructure charges to have status 'Paid'.") def test_unaccrued_interest_capitalization_gl_entries(self): set_loan_accrual_frequency(loan_accrual_frequency="Daily") diff --git a/lending/patches/v16_0/add_is_post_restructure_charge_field.py b/lending/patches/v16_0/add_is_post_restructure_charge_field.py index bf1ccfe35..285397cd5 100644 --- a/lending/patches/v16_0/add_is_post_restructure_charge_field.py +++ b/lending/patches/v16_0/add_is_post_restructure_charge_field.py @@ -12,6 +12,7 @@ def execute(): "fieldtype": "Check", "insert_after": "loan_repayment", "read_only": 1, + "print_hide": 1, } ] } From 6713d25c77a3deb73565ec60151a96996456cf5b Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Tue, 26 May 2026 16:55:25 +0530 Subject: [PATCH 4/7] fix: add Principal Capitalization in allocate_amounts --- .../loan_management/doctype/loan_repayment/loan_repayment.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lending/loan_management/doctype/loan_repayment/loan_repayment.py b/lending/loan_management/doctype/loan_repayment/loan_repayment.py index f304c9999..b0d4db9db 100644 --- a/lending/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/lending/loan_management/doctype/loan_repayment/loan_repayment.py @@ -1537,7 +1537,10 @@ def allocate_amounts(self, amounts, on_submit=False): amounts = self.update_amounts_for_write_off_recovery(loan_status, amounts) amount_paid = self.amount_paid - if self.repayment_type == "Charge Payment" or (self.repayment_type in ("Charges Waiver", "Charges Capitalization") and self.loan_restructure): + if self.repayment_type == "Charge Payment" or ( + self.repayment_type in ("Charges Waiver", "Charges Capitalization", "Principal Capitalization") + and self.loan_restructure + ): amount_paid = self.allocate_charges(amount_paid, amounts.get("unpaid_demands")) else: amount_paid = self.allocate_amount_against_demands(loan_status, amounts, amount_paid) From 53baa99429b831589a9286224802be0e28e59577 Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Tue, 26 May 2026 22:04:10 +0530 Subject: [PATCH 5/7] fix: add Principal Capitalization in allocate_amounts --- .../doctype/loan_repayment/loan_repayment.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lending/loan_management/doctype/loan_repayment/loan_repayment.py b/lending/loan_management/doctype/loan_repayment/loan_repayment.py index b0d4db9db..7dbed2c70 100644 --- a/lending/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/lending/loan_management/doctype/loan_repayment/loan_repayment.py @@ -1538,8 +1538,12 @@ def allocate_amounts(self, amounts, on_submit=False): amount_paid = self.amount_paid if self.repayment_type == "Charge Payment" or ( - self.repayment_type in ("Charges Waiver", "Charges Capitalization", "Principal Capitalization") + self.repayment_type in ("Charges Waiver", "Charges Capitalization") and self.loan_restructure + ) or ( + self.repayment_type == "Principal Capitalization" + and self.loan_restructure + and self.get("payable_charges") ): amount_paid = self.allocate_charges(amount_paid, amounts.get("unpaid_demands")) else: From cd584693356097e4bc50c3e41e150b4c95ccc00d Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Wed, 27 May 2026 18:31:14 +0530 Subject: [PATCH 6/7] fix: remove the is_post_restructure_charge field from invoice --- lending/install.py | 8 -------- .../loan_disbursement/loan_disbursement.py | 3 +-- .../loan_restructure/loan_restructure.py | 1 - .../loan_restructure/test_loan_restructure.py | 1 - lending/overrides/sales_invoice.py | 17 ++++++++--------- lending/patches.txt | 3 +-- .../add_is_post_restructure_charge_field.py | 19 ------------------- 7 files changed, 10 insertions(+), 42 deletions(-) delete mode 100644 lending/patches/v16_0/add_is_post_restructure_charge_field.py diff --git a/lending/install.py b/lending/install.py index e5943d569..0de3f748c 100644 --- a/lending/install.py +++ b/lending/install.py @@ -30,14 +30,6 @@ "read_only": 1, "print_hide": 1, }, - { - "fieldname": "is_post_restructure_charge", - "label": "Is Post Restructure Charge", - "fieldtype": "Check", - "insert_after": "loan_repayment", - "read_only": 1, - "print_hide": 1, - }, { "fieldname": "value_date", "fieldtype": "Date", diff --git a/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py b/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py index dcbce89e7..26071be5f 100644 --- a/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py +++ b/lending/loan_management/doctype/loan_disbursement/loan_disbursement.py @@ -864,7 +864,7 @@ def make_gl_entries(self, cancel=0, adv_adj=0, repost=0): def make_sales_invoice_for_charge( - loan, reference_fieldname, reference_doctype, applicant, disbursement_date, company, charges, is_post_restructure_charge=False + loan, reference_fieldname, reference_doctype, applicant, disbursement_date, company, charges ): if not charges: return @@ -906,7 +906,6 @@ def make_sales_invoice_for_charge( if reference_doctype == "Loan Disbursement": si.debit_to = receivable_account si.ignore_default_payment_terms_template = 1 - si.is_post_restructure_charge = is_post_restructure_charge si.save() si.submit() diff --git a/lending/loan_management/doctype/loan_restructure/loan_restructure.py b/lending/loan_management/doctype/loan_restructure/loan_restructure.py index b2c448612..bd0e6a09b 100644 --- a/lending/loan_management/doctype/loan_restructure/loan_restructure.py +++ b/lending/loan_management/doctype/loan_restructure/loan_restructure.py @@ -822,7 +822,6 @@ def make_post_restructure_charge_demands(self): self.restructure_date, self.company, post_restructure_charges, - is_post_restructure_charge=True, ) if sales_invoice: create_loan_repayment( diff --git a/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py b/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py index e6ca027eb..d7d50d08c 100644 --- a/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py +++ b/lending/loan_management/doctype/loan_restructure/test_loan_restructure.py @@ -261,7 +261,6 @@ def test_loan_restructure_charges_waiver_and_capitalization(self): "loan": loan.name, "docstatus": 1, "value_date": "2024-04-11", - "is_post_restructure_charge": 1, }, ["outstanding_amount", "status"], as_dict=True, diff --git a/lending/overrides/sales_invoice.py b/lending/overrides/sales_invoice.py index 527fda647..717e4f6df 100644 --- a/lending/overrides/sales_invoice.py +++ b/lending/overrides/sales_invoice.py @@ -20,7 +20,7 @@ def generate_demand(self, method=None): total_demand_amount = 0 total_items = len(self.get("items") or []) for i, item in enumerate(self.get("items")): - tax_amount = get_tax_amount(self.get("taxes"), item.item_code) + tax_amount = get_tax_amount(self, item.name) demand_amount = item.base_net_amount + flt(tax_amount) total_demand_amount += demand_amount if i == total_items - 1: @@ -48,7 +48,7 @@ def update_waived_amount_in_demand(self, method=None): if self.get("is_return") and not self.get("loan_repayment"): for item in self.get("items"): - tax_amount = get_tax_amount(self.get("taxes"), item.item_code) + tax_amount = get_tax_amount(self, item.name) waived_amount = flt(abs(item.base_net_amount + tax_amount), precision) demand_details = frappe.db.get_value( @@ -190,14 +190,13 @@ def cancel_demand(self, method=None): doc.cancel() -def get_tax_amount(taxes, item_code): +def get_tax_amount(doc, item_row_name): + """Get tax amount for a specific item row from item_wise_tax_details table""" tax_amount = 0 - for tax in taxes: - if tax.item_wise_tax_detail: - item_wise_tax_detail = frappe.parse_json(tax.item_wise_tax_detail) - if item_wise_tax_detail.get(item_code): - tax_amount += flt(item_wise_tax_detail.get(item_code)[1]) - + item_wise_tax_details = doc.get("item_wise_tax_details") or [] + for tax_detail in item_wise_tax_details: + if tax_detail.item_row == item_row_name: + tax_amount += flt(tax_detail.amount) return tax_amount diff --git a/lending/patches.txt b/lending/patches.txt index 643599520..ca04f8dba 100644 --- a/lending/patches.txt +++ b/lending/patches.txt @@ -47,5 +47,4 @@ lending.patches.v1_0.update_value_date_in_loan_repayment lending.patches.v1_0.update_value_date_in_loan_refund lending.patches.v1_0.update_value_date_in_pending_doctypes lending.patches.v16_0.add_enable_loan_accounting_field -lending.patches.v16_0.update_is_invoice_generated_field -lending.patches.v16_0.add_is_post_restructure_charge_field \ No newline at end of file +lending.patches.v16_0.update_is_invoice_generated_field \ No newline at end of file diff --git a/lending/patches/v16_0/add_is_post_restructure_charge_field.py b/lending/patches/v16_0/add_is_post_restructure_charge_field.py deleted file mode 100644 index 285397cd5..000000000 --- a/lending/patches/v16_0/add_is_post_restructure_charge_field.py +++ /dev/null @@ -1,19 +0,0 @@ -import frappe -from frappe.custom.doctype.custom_field.custom_field import create_custom_fields - - -def execute(): - if not frappe.db.exists("Custom Field", {"dt": "Sales Invoice", "fieldname": "is_post_restructure_charge"}): - custom_fields = { - "Sales Invoice": [ - { - "fieldname": "is_post_restructure_charge", - "label": "Is Post Restructure Charge", - "fieldtype": "Check", - "insert_after": "loan_repayment", - "read_only": 1, - "print_hide": 1, - } - ] - } - create_custom_fields(custom_fields, update=True) From a33f16c6bc118ef7e061652725bd1a3656bafe52 Mon Sep 17 00:00:00 2001 From: Nihantra Patel Date: Wed, 27 May 2026 18:46:36 +0530 Subject: [PATCH 7/7] fix: sales invoice changes --- lending/overrides/sales_invoice.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lending/overrides/sales_invoice.py b/lending/overrides/sales_invoice.py index 717e4f6df..527fda647 100644 --- a/lending/overrides/sales_invoice.py +++ b/lending/overrides/sales_invoice.py @@ -20,7 +20,7 @@ def generate_demand(self, method=None): total_demand_amount = 0 total_items = len(self.get("items") or []) for i, item in enumerate(self.get("items")): - tax_amount = get_tax_amount(self, item.name) + tax_amount = get_tax_amount(self.get("taxes"), item.item_code) demand_amount = item.base_net_amount + flt(tax_amount) total_demand_amount += demand_amount if i == total_items - 1: @@ -48,7 +48,7 @@ def update_waived_amount_in_demand(self, method=None): if self.get("is_return") and not self.get("loan_repayment"): for item in self.get("items"): - tax_amount = get_tax_amount(self, item.name) + tax_amount = get_tax_amount(self.get("taxes"), item.item_code) waived_amount = flt(abs(item.base_net_amount + tax_amount), precision) demand_details = frappe.db.get_value( @@ -190,13 +190,14 @@ def cancel_demand(self, method=None): doc.cancel() -def get_tax_amount(doc, item_row_name): - """Get tax amount for a specific item row from item_wise_tax_details table""" +def get_tax_amount(taxes, item_code): tax_amount = 0 - item_wise_tax_details = doc.get("item_wise_tax_details") or [] - for tax_detail in item_wise_tax_details: - if tax_detail.item_row == item_row_name: - tax_amount += flt(tax_detail.amount) + for tax in taxes: + if tax.item_wise_tax_detail: + item_wise_tax_detail = frappe.parse_json(tax.item_wise_tax_detail) + if item_wise_tax_detail.get(item_code): + tax_amount += flt(item_wise_tax_detail.get(item_code)[1]) + return tax_amount