Skip to content

Commit

Permalink
[9.0][IMP] account_banking_mandate: Add valid_mandate field and fix o…
Browse files Browse the repository at this point in the history
…nchange
  • Loading branch information
carlosdauden committed Aug 2, 2017
1 parent 6e9d926 commit 25d2e4d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
7 changes: 4 additions & 3 deletions account_banking_mandate/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
----------
Expand Down
3 changes: 2 additions & 1 deletion account_banking_mandate/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand Down
38 changes: 13 additions & 25 deletions account_banking_mandate/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -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).


Expand Down Expand Up @@ -35,6 +36,7 @@ def create(self, vals):
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):
Expand Down Expand Up @@ -62,33 +64,19 @@ def _prepare_refund(
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()
21 changes: 20 additions & 1 deletion account_banking_mandate/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -21,3 +26,17 @@ def _compute_mandate_count(self):
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

0 comments on commit 25d2e4d

Please sign in to comment.