[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.
This commit is contained in:
Pedro M. Baeza
2018-05-01 17:10:53 +02:00
parent 22b335af28
commit f8939814a4
3 changed files with 60 additions and 38 deletions

View File

@@ -8,7 +8,7 @@
{ {
'name': 'Account Payment Order', 'name': 'Account Payment Order',
'version': '11.0.1.1.1', 'version': '11.0.1.1.2',
'license': 'AGPL-3', 'license': 'AGPL-3',
'author': "ACSONE SA/NV, " 'author': "ACSONE SA/NV, "
"Therp BV, " "Therp BV, "

View File

@@ -11,7 +11,21 @@ class AccountInvoice(models.Model):
_inherit = 'account.invoice' _inherit = 'account.invoice'
payment_order_ok = fields.Boolean( 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 @api.model
def _get_reference_type(self): def _get_reference_type(self):
@@ -30,9 +44,13 @@ class AccountInvoice(models.Model):
return res return res
@api.multi @api.multi
def _prepare_new_payment_order(self): def _prepare_new_payment_order(self, payment_mode=None):
self.ensure_one() 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 # other important fields are set by the inherit of create
# in account_payment_order.py # in account_payment_order.py
return vals return vals
@@ -40,46 +58,50 @@ class AccountInvoice(models.Model):
@api.multi @api.multi
def create_account_payment_line(self): def create_account_payment_line(self):
apoo = self.env['account.payment.order'] apoo = self.env['account.payment.order']
aplo = self.env['account.payment.line']
result_payorder_ids = [] result_payorder_ids = []
action_payment_type = 'debit' action_payment_type = 'debit'
for inv in self: for inv in self:
if inv.state != 'open': if inv.state != 'open':
raise UserError(_( raise UserError(_(
"The invoice %s is not in Open state") % inv.number) "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: if not inv.move_id:
raise UserError(_( raise UserError(_(
"No Journal Entry on invoice %s") % inv.number) "No Journal Entry on invoice %s") % inv.number)
if not inv.payment_order_ok: applicable_lines = inv.move_id.line_ids.filtered(
raise UserError(_( lambda x: (
"The invoice %s has a payment mode '%s' " not x.reconciled and x.payment_mode_id.payment_order_ok and
"which is not selectable in payment orders." % ( x.account_id.internal_type in ('receivable', 'payable') and
inv.number, inv.payment_mode_id.display_name)) not x.payment_line_ids
) )
payorders = apoo.search([ )
('payment_mode_id', '=', inv.payment_mode_id.id), if not applicable_lines:
('state', '=', 'draft')]) raise UserError(_(
if payorders: 'No Payment Line created for invoice %s because '
payorder = payorders[0] '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 new_payorder = False
else: if not payorder:
payorder = apoo.create(inv._prepare_new_payment_order()) payorder = apoo.create(inv._prepare_new_payment_order(
new_payorder = True payment_mode
result_payorder_ids.append(payorder.id) ))
action_payment_type = payorder.payment_type new_payorder = True
count = 0 result_payorder_ids.append(payorder.id)
for line in inv.move_id.line_ids: action_payment_type = payorder.payment_type
if line.account_id == inv.account_id and not line.reconciled: count = 0
paylines = aplo.search([ for line in applicable_lines.filtered(
('move_line_id', '=', line.id), lambda x: x.payment_mode_id == payment_mode
('state', '!=', 'cancel')]) ):
if not paylines: line.create_payment_line_from_move_line(payorder)
line.create_payment_line_from_move_line(payorder) count += 1
count += 1
if count:
if new_payorder: if new_payorder:
inv.message_post(_( inv.message_post(_(
'%d payment lines added to the new draft payment ' '%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 ' '%d payment lines added to the existing draft '
'payment order %s.') 'payment order %s.')
% (count, payorder.name)) % (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( action = self.env['ir.actions.act_window'].for_xml_id(
'account_payment_order', 'account_payment_order',
'account_payment_order_%s_action' % action_payment_type) 'account_payment_order_%s_action' % action_payment_type)

View File

@@ -16,6 +16,11 @@ class AccountMoveLine(models.Model):
bank_payment_line_id = fields.Many2one( bank_payment_line_id = fields.Many2one(
'bank.payment.line', string='Bank Payment Line', 'bank.payment.line', string='Bank Payment Line',
readonly=True) readonly=True)
payment_line_ids = fields.One2many(
comodel_name='account.payment.line',
inverse_name='move_line_id',
string="Payment lines",
)
@api.multi @api.multi
def _prepare_payment_line_vals(self, payment_order): def _prepare_payment_line_vals(self, payment_order):