From 9785a065f5f2b86a8c0a04907cdb6e8fc2c1481e 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 | 89 +++++++++++-------- .../models/account_move_line.py | 5 ++ 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/account_payment_order/__manifest__.py b/account_payment_order/__manifest__.py index f3410cf48..8faa7cc0b 100644 --- a/account_payment_order/__manifest__.py +++ b/account_payment_order/__manifest__.py @@ -9,7 +9,7 @@ { 'name': 'Account Payment Order', - 'version': '10.0.1.3.3', + 'version': '10.0.1.3.4', '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 847df3604..1513c5ef5 100644 --- a/account_payment_order/models/account_invoice.py +++ b/account_payment_order/models/account_invoice.py @@ -12,7 +12,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): @@ -31,9 +45,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 @@ -41,44 +59,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: + 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 + ) + ) + if not applicable_lines: raise UserError(_( - "The invoice %s has a payment mode '%s' " - "which is not selectable in payment orders.")) - payorders = apoo.search([ - ('payment_mode_id', '=', inv.payment_mode_id.id), - ('state', '=', 'draft')]) - if payorders: - payorder = payorders[0] + '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 ' @@ -89,11 +113,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 6f8748ed7..2e88af00d 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -15,6 +15,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):