mirror of
https://github.com/OCA/bank-payment.git
synced 2025-02-02 10:37:31 +02:00
[IMP] account_banking_mandate: Add valid_mandate field and fix onchange
(cherry picked from commit 25d2e4d)
This commit is contained in:
committed by
Enric Tobella
parent
04e876192d
commit
f668e8c4c8
@@ -56,10 +56,10 @@ Contributors
|
||||
------------
|
||||
|
||||
* Alexis de Lattre <alexis.delattre@akretion.com>
|
||||
* Pedro M. Baeza
|
||||
* Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
||||
* Alexandre Fayolle
|
||||
* Stéphane Bidoul <stephane.bidoul@acsone.eu>
|
||||
* Sergio Teruel (Incaser) <sergio@incaser.es>
|
||||
* Sergio Teruel <sergio.teruel@tecnativa.com>
|
||||
* Cédric Pigeon <cedric.pigeon@acsone.eu>
|
||||
* Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2014 Compassion CH - Cyril Sester <csester@compassion.ch>
|
||||
# © 2014 Serv. Tecnol. Avanzados - Pedro M. Baeza
|
||||
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
# Copyright 2014 Compassion CH - Cyril Sester <csester@compassion.ch>
|
||||
# Copyright 2014 Serv. Tecnol. Avanzados - Pedro M. Baeza
|
||||
# Copyright 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||
# Copyright 2017 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
|
||||
from odoo import models, fields, api
|
||||
@@ -35,6 +36,7 @@ class AccountInvoice(models.Model):
|
||||
creation, using same method as upstream."""
|
||||
onchanges = {
|
||||
'_onchange_partner_id': ['mandate_id'],
|
||||
'payment_mode_id_change': ['mandate_id'],
|
||||
}
|
||||
for onchange_method, changed_fields in list(onchanges.items()):
|
||||
if any(f not in vals for f in changed_fields):
|
||||
@@ -43,7 +45,7 @@ class AccountInvoice(models.Model):
|
||||
for field in changed_fields:
|
||||
if field not in vals and invoice[field]:
|
||||
vals[field] = invoice._fields[field].convert_to_write(
|
||||
invoice[field], invoice,
|
||||
invoice[field],
|
||||
)
|
||||
return super(AccountInvoice, self).create(vals)
|
||||
|
||||
@@ -62,34 +64,19 @@ class AccountInvoice(models.Model):
|
||||
vals['mandate_id'] = invoice.mandate_id.id
|
||||
return vals
|
||||
|
||||
def set_mandate(self):
|
||||
if self.payment_mode_id.payment_method_id.mandate_required:
|
||||
self.mandate_id = self.partner_id.valid_mandate_id
|
||||
else:
|
||||
self.mandate_id = False
|
||||
|
||||
@api.onchange('partner_id', 'company_id')
|
||||
def _onchange_partner_id(self):
|
||||
"""Select by default the first valid mandate of the partner"""
|
||||
res = super(AccountInvoice, self)._onchange_partner_id()
|
||||
if (
|
||||
self.type == 'out_invoice' and
|
||||
self.partner_id.customer_payment_mode_id.
|
||||
payment_type == 'inbound' and
|
||||
self.partner_id.customer_payment_mode_id.payment_method_id.
|
||||
mandate_required and
|
||||
self.commercial_partner_id):
|
||||
mandates = self.env['account.banking.mandate'].search([
|
||||
('state', '=', 'valid'),
|
||||
('partner_id', '=', self.commercial_partner_id.id),
|
||||
])
|
||||
if mandates:
|
||||
self.mandate_id = mandates[0]
|
||||
else:
|
||||
self.mandate_id = False
|
||||
return res
|
||||
super(AccountInvoice, self)._onchange_partner_id()
|
||||
self.set_mandate()
|
||||
|
||||
@api.onchange('payment_mode_id')
|
||||
def _onchange_payment_mode_id(self):
|
||||
super(AccountInvoice, self)._onchange_payment_mode_id()
|
||||
if (
|
||||
self.payment_mode_id and
|
||||
self.payment_mode_id.payment_type == 'inbound' and
|
||||
not self.payment_mode_id.payment_method_id.mandate_required):
|
||||
self.mandate_id = False
|
||||
elif not self.payment_mode_id:
|
||||
self.mandate_id = False
|
||||
self.set_mandate()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
# Copyright 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
|
||||
# Copyright 2017 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import models, fields, api
|
||||
|
||||
@@ -11,6 +12,10 @@ class ResPartner(models.Model):
|
||||
mandate_count = fields.Integer(
|
||||
compute='_compute_mandate_count', string="Number of Mandates",
|
||||
readonly=True)
|
||||
valid_mandate_id = fields.Many2one(
|
||||
comodel_name='account.banking.mandate',
|
||||
compute='compute_valid_mandate_id',
|
||||
string='Valid Mandate')
|
||||
|
||||
@api.multi
|
||||
def _compute_mandate_count(self):
|
||||
@@ -21,3 +26,17 @@ class ResPartner(models.Model):
|
||||
for mandate in mandate_data])
|
||||
for partner in self:
|
||||
partner.mandate_count = mapped_data.get(partner.id, 0)
|
||||
|
||||
@api.multi
|
||||
def compute_valid_mandate_id(self):
|
||||
# Dict to reduce impact with "bug" that process all partners related
|
||||
mandates_dic = {}
|
||||
for partner in self:
|
||||
commercial_partner_id = partner.commercial_partner_id
|
||||
if commercial_partner_id in mandates_dic:
|
||||
partner.valid_mandate_id = mandates_dic[commercial_partner_id]
|
||||
else:
|
||||
mandate_id = partner.commercial_partner_id.bank_ids.mapped(
|
||||
'mandate_ids').filtered(lambda x: x.state == 'valid').id
|
||||
partner.valid_mandate_id = mandate_id
|
||||
mandates_dic[commercial_partner_id] = mandate_id
|
||||
|
||||
Reference in New Issue
Block a user