From f8939814a4e1ec17d7a749af00db2e8fcbd2d91b Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Tue, 1 May 2018 17:10:53 +0200 Subject: [PATCH] [FIX] account_payment_order: Take payment mode from move (#466) When adding to a payment order from invoices, the payment mode considered is the one in the invoice, but once you have confirmed it, you can't change it, so if you change the payment mode in the journal items, it doesn't make the difference. With this change, this is taken into account for adding to the payment order(s) properly. --- account_payment_order/__manifest__.py | 2 +- .../models/account_invoice.py | 91 +++++++++++-------- .../models/account_move_line.py | 5 + 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/account_payment_order/__manifest__.py b/account_payment_order/__manifest__.py index 9b4775b92..c72235076 100644 --- a/account_payment_order/__manifest__.py +++ b/account_payment_order/__manifest__.py @@ -8,7 +8,7 @@ { 'name': 'Account Payment Order', - 'version': '11.0.1.1.1', + 'version': '11.0.1.1.2', 'license': 'AGPL-3', 'author': "ACSONE SA/NV, " "Therp BV, " diff --git a/account_payment_order/models/account_invoice.py b/account_payment_order/models/account_invoice.py index 3857ddf93..2cda67b8d 100644 --- a/account_payment_order/models/account_invoice.py +++ b/account_payment_order/models/account_invoice.py @@ -11,7 +11,21 @@ class AccountInvoice(models.Model): _inherit = 'account.invoice' payment_order_ok = fields.Boolean( - related='payment_mode_id.payment_order_ok', readonly=True) + compute="_compute_payment_order_ok", + ) + + @api.depends('payment_mode_id', 'move_id', 'move_id.line_ids', + 'move_id.line_ids.payment_mode_id') + def _compute_payment_order_ok(self): + for invoice in self: + payment_mode = ( + invoice.move_id.line_ids.filtered( + lambda x: not x.reconciled + ).mapped('payment_mode_id')[:1] + ) + if not payment_mode: + payment_mode = invoice.payment_mode_id + invoice.payment_order_ok = payment_mode.payment_order_ok @api.model def _get_reference_type(self): @@ -30,9 +44,13 @@ class AccountInvoice(models.Model): return res @api.multi - def _prepare_new_payment_order(self): + def _prepare_new_payment_order(self, payment_mode=None): self.ensure_one() - vals = {'payment_mode_id': self.payment_mode_id.id} + if payment_mode is None: + payment_mode = self.env['account.payment.mode'] + vals = { + 'payment_mode_id': payment_mode.id or self.payment_mode_id.id, + } # other important fields are set by the inherit of create # in account_payment_order.py return vals @@ -40,46 +58,50 @@ class AccountInvoice(models.Model): @api.multi def create_account_payment_line(self): apoo = self.env['account.payment.order'] - aplo = self.env['account.payment.line'] result_payorder_ids = [] action_payment_type = 'debit' for inv in self: if inv.state != 'open': raise UserError(_( "The invoice %s is not in Open state") % inv.number) - if not inv.payment_mode_id: - raise UserError(_( - "No Payment Mode on invoice %s") % inv.number) if not inv.move_id: raise UserError(_( "No Journal Entry on invoice %s") % inv.number) - if not inv.payment_order_ok: - raise UserError(_( - "The invoice %s has a payment mode '%s' " - "which is not selectable in payment orders." % ( - inv.number, inv.payment_mode_id.display_name)) + applicable_lines = inv.move_id.line_ids.filtered( + lambda x: ( + not x.reconciled and x.payment_mode_id.payment_order_ok and + x.account_id.internal_type in ('receivable', 'payable') and + not x.payment_line_ids ) - payorders = apoo.search([ - ('payment_mode_id', '=', inv.payment_mode_id.id), - ('state', '=', 'draft')]) - if payorders: - payorder = payorders[0] + ) + if not applicable_lines: + raise UserError(_( + 'No Payment Line created for invoice %s because ' + 'it already exists or because this invoice is ' + 'already paid.') % inv.number) + payment_modes = applicable_lines.mapped('payment_mode_id') + if not payment_modes: + raise UserError(_( + "No Payment Mode on invoice %s") % inv.number) + for payment_mode in payment_modes: + payorder = apoo.search([ + ('payment_mode_id', '=', payment_mode.id), + ('state', '=', 'draft') + ], limit=1) new_payorder = False - else: - payorder = apoo.create(inv._prepare_new_payment_order()) - new_payorder = True - result_payorder_ids.append(payorder.id) - action_payment_type = payorder.payment_type - count = 0 - for line in inv.move_id.line_ids: - if line.account_id == inv.account_id and not line.reconciled: - paylines = aplo.search([ - ('move_line_id', '=', line.id), - ('state', '!=', 'cancel')]) - if not paylines: - line.create_payment_line_from_move_line(payorder) - count += 1 - if count: + if not payorder: + payorder = apoo.create(inv._prepare_new_payment_order( + payment_mode + )) + new_payorder = True + result_payorder_ids.append(payorder.id) + action_payment_type = payorder.payment_type + count = 0 + for line in applicable_lines.filtered( + lambda x: x.payment_mode_id == payment_mode + ): + line.create_payment_line_from_move_line(payorder) + count += 1 if new_payorder: inv.message_post(_( '%d payment lines added to the new draft payment ' @@ -90,11 +112,6 @@ class AccountInvoice(models.Model): '%d payment lines added to the existing draft ' 'payment order %s.') % (count, payorder.name)) - else: - raise UserError(_( - 'No Payment Line created for invoice %s because ' - 'it already exists or because this invoice is ' - 'already paid.') % inv.number) action = self.env['ir.actions.act_window'].for_xml_id( 'account_payment_order', 'account_payment_order_%s_action' % action_payment_type) diff --git a/account_payment_order/models/account_move_line.py b/account_payment_order/models/account_move_line.py index 21e9b833d..8211c6a74 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -16,6 +16,11 @@ class AccountMoveLine(models.Model): bank_payment_line_id = fields.Many2one( 'bank.payment.line', string='Bank Payment Line', readonly=True) + payment_line_ids = fields.One2many( + comodel_name='account.payment.line', + inverse_name='move_line_id', + string="Payment lines", + ) @api.multi def _prepare_payment_line_vals(self, payment_order):