From df43dc32c14998431dec058ba5c070e59163456b Mon Sep 17 00:00:00 2001 From: Sandip Mangukiya Date: Wed, 2 Mar 2022 14:53:08 -0700 Subject: [PATCH] [FIX]account_banking_ach_discount: fix manual discount case --- .../models/account_payment_order.py | 59 +++++++++++++------ .../models/bank_payment_line.py | 12 ---- .../views/account_payment_view.xml | 13 ++-- 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/account_banking_ach_discount/models/account_payment_order.py b/account_banking_ach_discount/models/account_payment_order.py index d90b051c..329e3d64 100644 --- a/account_banking_ach_discount/models/account_payment_order.py +++ b/account_banking_ach_discount/models/account_payment_order.py @@ -21,10 +21,13 @@ def _prepare_move(self, bank_lines=None): amount = line.amount_currency discount = line.discount_amount payment_difference = line.payment_difference - writeoff = ( - payment_difference and payment_difference - discount or 0.0 - ) - invoice_close = line.payment_difference_handling != "open" + writeoff = 0.0 + invoice_close = False + if payment_difference: + writeoff = ( + payment_difference and payment_difference - discount or 0.0 + ) + invoice_close = line.payment_difference_handling != "open" use_debit = line.move_id.move_type in ( "in_invoice", "out_refund", @@ -39,21 +42,41 @@ def _prepare_move(self, bank_lines=None): line_ids.append((0, 0, temp_vals)) if discount > 0: - discount_information = line.move_id.invoice_payment_term_id._check_payment_term_discount( - line.move_id, line.date - ) - discount_vals = temp_vals.copy() - discount_vals["account_id"] = discount_information[1] - discount_vals["name"] = "Early Pay Discount" - if use_debit: - discount_vals["debit"] = 0.0 - discount_vals["credit"] = discount_information[0] + if payment_difference: + discount_information = line.move_id.invoice_payment_term_id._check_payment_term_discount( + line.move_id, line.date + ) + discount_vals = temp_vals.copy() + discount_vals["account_id"] = discount_information[1] + discount_vals["name"] = "Early Pay Discount" + if use_debit: + discount_vals["debit"] = 0.0 + discount_vals["credit"] = discount_information[0] + else: + discount_vals["credit"] = 0.0 + discount_vals["debit"] = discount_information[0] + discount_vals["bank_payment_line_id"] = False + if discount_vals: + line_ids.append((0, 0, discount_vals)) + # Discount Taken Update + line.move_id.discount_taken = discount else: - discount_vals["credit"] = 0.0 - discount_vals["debit"] = discount_information[0] - discount_vals["bank_payment_line_id"] = False - if discount_vals: - line_ids.append((0, 0, discount_vals)) + #Case: If user Manually enters discount amount + discount_vals = temp_vals.copy() + discount_vals["account_id"] = line.writeoff_account_id and line.writeoff_account_id.id or False + discount_vals["name"] = "Early Pay Discount" + if use_debit: + discount_vals["debit"] = 0.0 + discount_vals["credit"] = discount + else: + discount_vals["credit"] = 0.0 + discount_vals["debit"] = discount + discount_vals["bank_payment_line_id"] = False + if discount_vals: + line_ids.append((0, 0, discount_vals)) + # Discount Taken Update + line.move_id.discount_taken = discount + if invoice_close and round(writeoff, 2): if use_debit: diff --git a/account_banking_ach_discount/models/bank_payment_line.py b/account_banking_ach_discount/models/bank_payment_line.py index d1f4a1da..9ee03c19 100644 --- a/account_banking_ach_discount/models/bank_payment_line.py +++ b/account_banking_ach_discount/models/bank_payment_line.py @@ -24,15 +24,3 @@ def _compute_discount_amount(self): for bline in self: discount_amount = sum(bline.mapped("payment_line_ids.discount_amount")) bline.discount_amount = discount_amount - - # def reconcile(self): - # self.ensure_one() - # amlo = self.env["account.move.line"] - # transit_mlines = amlo.search([("bank_payment_line_id", "=", self.id)]) - # for line in transit_mlines: - # ap_mlines = line.move_id.line_ids.filtered( - # lambda x: x.account_id == line.account_id - # ) - # lines_to_rec = line - # lines_to_rec += ap_mlines - # lines_to_rec.reconcile() diff --git a/account_banking_ach_discount/views/account_payment_view.xml b/account_banking_ach_discount/views/account_payment_view.xml index 7c37a412..3d0c41e7 100644 --- a/account_banking_ach_discount/views/account_payment_view.xml +++ b/account_banking_ach_discount/views/account_payment_view.xml @@ -11,6 +11,7 @@ + @@ -25,14 +26,12 @@ /> - - - - - - - + + + + +