[9.0][IMP] account_banking_mandate: Add valid_mandate field and fix onchange

This commit is contained in:
Carlos Dauden
2017-08-02 11:33:21 +02:00
parent 6e9d926d93
commit 25d2e4d4bd
4 changed files with 39 additions and 30 deletions

View File

@@ -30,7 +30,7 @@ TODO
Usage
=====
To use this module, see menu "Accounting > payment > SEPA direct debit mandates"
To use this module, see menu "Accounting > payment > SEPA direct debit mandates"
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
@@ -56,10 +56,11 @@ 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>
* Carlos Dauden <carlos.dauden@tecnativa.com>
Maintainer
----------

View File

@@ -2,12 +2,13 @@
# © 2014 Compassion CH - Cyril Sester <csester@compassion.ch>
# © 2014 Tecnativa - Pedro M. Baeza
# © 2015-2016 Akretion - Alexis de Lattre <alexis.delattre@akretion.com>
# © 2017 Tecnativa - Carlos Dauden <carlos.dauden@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'Account Banking Mandate',
'summary': 'Banking mandates',
'version': '9.0.1.1.0',
'version': '9.0.1.2.0',
'license': 'AGPL-3',
'author': "Compassion CH, "
"Tecnativa, "

View File

@@ -1,7 +1,8 @@
# -*- 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>)
# 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).
@@ -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 onchanges.items():
if any(f not in vals for f in changed_fields):
@@ -62,33 +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"""
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
self.set_mandate()
@api.onchange('payment_mode_id')
def payment_mode_id_change(self):
super(AccountInvoice, self).payment_mode_id_change()
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()

View File

@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# 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 openerp 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