diff --git a/account_banking_ach_discount/__manifest__.py b/account_banking_ach_discount/__manifest__.py index 511f07a0..4de311b3 100644 --- a/account_banking_ach_discount/__manifest__.py +++ b/account_banking_ach_discount/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Discount on ACH batch payments", - "version": "14.0.1.0.0", + "version": "16.0.1.0.0", "license": "AGPL-3", "author": "Open Source Integrators, Odoo Community Association (OCA)", "category": "Accounting", diff --git a/account_banking_ach_discount/models/__init__.py b/account_banking_ach_discount/models/__init__.py index e4adbac1..b8e1ebba 100644 --- a/account_banking_ach_discount/models/__init__.py +++ b/account_banking_ach_discount/models/__init__.py @@ -2,6 +2,5 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import account_payment from . import account_move_line -from . import bank_payment_line -from . import account_payment_order from . import account_move +from . import account_payment_line diff --git a/account_banking_ach_discount/models/account_move.py b/account_banking_ach_discount/models/account_move.py index bdd4cecb..c13504c2 100644 --- a/account_banking_ach_discount/models/account_move.py +++ b/account_banking_ach_discount/models/account_move.py @@ -6,99 +6,43 @@ class AccountMove(models.Model): _inherit = "account.move" - def _get_reconciled_info_JSON_values(self): - res = super(AccountMove, self)._get_reconciled_info_JSON_values() - inv_number = self.ref - if res: - flag = False - for item in res: + def _compute_payments_widget_reconciled_info(self): + res = super(AccountMove, self)._compute_payments_widget_reconciled_info() + for move in self: + if move.invoice_payments_widget: + inv_number = move.name + flag = False payment_lines = set() - for line in self.line_ids: + for line in move.line_ids: payment_lines.update( line.mapped("matched_credit_ids.credit_move_id.id") ) payment_lines.update( line.mapped("matched_debit_ids.debit_move_id.id") ) - payment_move_line_ids = ( - self.env["account.move.line"] - .browse(list(payment_lines)) - .sorted() - ) + payment_move_line_ids = ( + self.env["account.move.line"].browse(list(payment_lines)).sorted() + ) for mvl in payment_move_line_ids: - # get bank payment line - if mvl.move_id and mvl.id == item["payment_id"]: - for pay_li in mvl.bank_payment_line_id.payment_line_ids: - # Get related payment line ref - if pay_li.communication == inv_number: - item["amount"] = pay_li.amount_currency - # for non-ach payment - # Deduct the discount only for the related payment. - # Discount is applied on the last payment (i.e. fully reconciled). - if ( - not mvl.bank_payment_line_id - and mvl.move_id.id == self.id - and item["account_payment_id"] == mvl.payment_id.id - ): - if mvl.full_reconcile_id and not flag: - item["amount"] = item["amount"] - self.discount_taken - flag = True - + for item in move.invoice_payments_widget["content"]: + # get payment line + if mvl.payment_id.id == item["account_payment_id"]: + for pay_li in mvl.payment_id.line_ids.filtered( + lambda line: not line.reconciled + ): + # Get related payment line ref + if inv_number in pay_li.name: + item["amount"] = abs(pay_li.amount_currency) + # for non-ach payment + # Deduct the discount only for the related payment. + # Discount is applied on the last payment (i.e. fully reconciled). + if ( + mvl.payment_id + and mvl.move_id.id == self.id + and item["account_payment_id"] == mvl.payment_id.id + ): + if mvl.full_reconcile_id and not flag: + item["amount"] = item["amount"] - move.discount_taken + flag = True return res - - def _prepare_discount_move_line(self, vals): - valid = False - for invoice in self: - if ( - invoice.invoice_payment_term_id - and invoice.invoice_payment_term_id.is_discount - and invoice.invoice_payment_term_id.line_ids - ): - discount_information = ( - invoice.invoice_payment_term_id._check_payment_term_discount( - invoice, invoice.date_invoice - ) - ) - discount_amt = discount_information[0] - discount_account_id = discount_information[1] - if discount_amt > 0.0: - vals.update( - { - "account_id": discount_account_id, - "move_id": invoice.id, - "bank_payment_line_id": False, - "name": "Early Pay Discount", - } - ) - if invoice.type == "out_invoice": - vals.update({"credit": 0.0, "debit": discount_amt}) - valid = True - elif invoice.type == "in_invoice": - vals.update({"credit": discount_amt, "debit": 0.0}) - valid = True - if valid: - return vals - else: - return {} - - def _prepare_writeoff_move_line(self, payment_line, vals): - for invoice in self: - note = "" - if payment_line.reason_code: - note = payment_line.reason_code.display_name + ": " - if payment_line.note: - note += payment_line.note - vals.update( - { - "account_id": payment_line.writeoff_account_id.id, - "bank_payment_line_id": False, - "name": note, - "move_id": invoice.id, - } - ) - if invoice.move_type == "out_invoice": - vals.update({"credit": 0.0, "debit": payment_line.payment_difference}) - elif invoice.move_type == "in_invoice": - vals.update({"credit": payment_line.payment_difference, "debit": 0.0}) - return vals diff --git a/account_banking_ach_discount/models/account_payment.py b/account_banking_ach_discount/models/account_payment.py index dead2639..4a47850b 100644 --- a/account_banking_ach_discount/models/account_payment.py +++ b/account_banking_ach_discount/models/account_payment.py @@ -72,9 +72,9 @@ class AccountPaymentLine(models.Model): domain=[("deprecated", "!=", True)], copy=False, ) - reason_code = fields.Many2one("payment.adjustment.reason", string="Reason Code") - note = fields.Text("Note") - payment_difference = fields.Float(string="Payment Difference") + reason_code = fields.Many2one("payment.adjustment.reason") + note = fields.Text() + payment_difference = fields.Float() move_id = fields.Many2one( "account.move", related="move_line_id.move_id", store=True ) diff --git a/account_banking_ach_discount/models/account_payment_line.py b/account_banking_ach_discount/models/account_payment_line.py new file mode 100644 index 00000000..dc7eb29f --- /dev/null +++ b/account_banking_ach_discount/models/account_payment_line.py @@ -0,0 +1,26 @@ +# Copyright (C) 2019 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import models + + +class AccountPaymentOrder(models.Model): + _inherit = "account.payment.line" + + def _prepare_account_payment_vals(self): + values = super()._prepare_account_payment_vals() + note = "" + if self.reason_code: + note = self.reason_code.display_name + ": " + if self.note: + note += self.note + payment_difference = self.payment_difference + if self.payment_type == "outbound": + payment_difference *= -1 + write_off_line_vals = { + "account_id": self.writeoff_account_id.id, + "name": note, + "amount_currency": payment_difference, + "balance": payment_difference, + } + values["write_off_line_vals"] = [write_off_line_vals] + return values diff --git a/account_banking_ach_discount/models/account_payment_order.py b/account_banking_ach_discount/models/account_payment_order.py deleted file mode 100644 index b9edf1af..00000000 --- a/account_banking_ach_discount/models/account_payment_order.py +++ /dev/null @@ -1,102 +0,0 @@ -# Copyright (C) 2019 Open Source Integrators -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import models - - -class AccountPaymentOrder(models.Model): - _inherit = "account.payment.order" - - def _prepare_move(self, bank_lines=None): - values = super()._prepare_move(bank_lines) - bank_payment_line_pool = self.env["bank.payment.line"] - line_ids = [] - for vals in values.get("line_ids"): - # get the debit line for adjusting A/P entries - if "bank_payment_line_id" in vals[2] and vals[2]["bank_payment_line_id"]: - bank_payment_id = vals[2].get("bank_payment_line_id") - bank_payment = bank_payment_line_pool.browse(bank_payment_id) - for line in bank_payment.payment_line_ids: - - temp_vals = vals[2].copy() - amount = line.amount_currency - discount = line.discount_amount - payment_difference = line.payment_difference - 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", - ) - - temp_vals["move_id"] = line.move_id.id - if use_debit: - temp_vals["debit"] = amount + discount - else: - temp_vals["credit"] = amount + discount - - line_ids.append((0, 0, temp_vals)) - - if discount > 0: - if payment_difference: - pay_term = line.move_id.invoice_payment_term_id - discount_information = ( - pay_term._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: - # 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: - temp_vals["debit"] = amount + discount + round(writeoff, 2) - else: - temp_vals["credit"] = amount + discount + round(writeoff, 2) - writeoff_vals = line.move_id._prepare_writeoff_move_line( - line, temp_vals.copy() - ) - writeoff_vals["bank_payment_line_id"] = False - if writeoff_vals: - line_ids.append((0, 0, writeoff_vals)) - # payment order line - else: - line_ids.append(vals) - values["line_ids"] = line_ids - return values diff --git a/account_banking_ach_discount/models/bank_payment_line.py b/account_banking_ach_discount/models/bank_payment_line.py deleted file mode 100644 index 9ee03c19..00000000 --- a/account_banking_ach_discount/models/bank_payment_line.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright (C) 2019 Open Source Integrators -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). - -from odoo import api, fields, models - - -class BankPaymentLine(models.Model): - _inherit = "bank.payment.line" - - discount_amount = fields.Monetary( - compute="_compute_discount_amount", currency_field="currency_id" - ) - total_amount = fields.Monetary( - compute="_compute_total_amount", currency_field="currency_id" - ) - - @api.depends("amount_currency", "discount_amount") - def _compute_total_amount(self): - for line in self: - line.total_amount = line.amount_currency + line.discount_amount - - @api.depends("payment_line_ids", "payment_line_ids.discount_amount") - def _compute_discount_amount(self): - for bline in self: - discount_amount = sum(bline.mapped("payment_line_ids.discount_amount")) - bline.discount_amount = discount_amount diff --git a/account_banking_ach_discount/readme/CONTRIBUTORS.rst b/account_banking_ach_discount/readme/CONTRIBUTORS.rst index 4a3b34a1..958bc325 100644 --- a/account_banking_ach_discount/readme/CONTRIBUTORS.rst +++ b/account_banking_ach_discount/readme/CONTRIBUTORS.rst @@ -2,3 +2,6 @@ * Bhavesh Odedra * Maxime Chambreuil + +* ForgeFlow + * Jasmin Solanki diff --git a/account_banking_ach_discount/views/account_payment_view.xml b/account_banking_ach_discount/views/account_payment_view.xml index 7f10a3dc..922a1468 100644 --- a/account_banking_ach_discount/views/account_payment_view.xml +++ b/account_banking_ach_discount/views/account_payment_view.xml @@ -35,29 +35,4 @@ - - - banking.bank.payment.line.form - bank.payment.line - - - - - - - - - - - banking.bank.payment.line.tree - bank.payment.line - - - - - - - - - diff --git a/account_banking_ach_discount/wizard/account_payment_register.py b/account_banking_ach_discount/wizard/account_payment_register.py index 45e8cf17..370f1729 100644 --- a/account_banking_ach_discount/wizard/account_payment_register.py +++ b/account_banking_ach_discount/wizard/account_payment_register.py @@ -7,7 +7,7 @@ class AccountPaymentRegister(models.TransientModel): _inherit = "account.payment.register" def make_payments(self): - if self.payment_method_id and self.payment_method_id.code in ( + if self.payment_method_code and self.payment_method_code in ( "ACH-In", "ACH-Out", ): @@ -15,7 +15,11 @@ def make_payments(self): payment_mode = self.env["account.payment.mode"].search( [ ("payment_type", "=", self.payment_type), - ("payment_method_id", "=", self.payment_method_id.id), + ( + "payment_method_id", + "=", + self.payment_method_line_id.payment_method_id.id, + ), ("payment_order_ok", "=", True), ], limit=1,