mirror of
https://github.com/OCA/bank-payment.git
synced 2025-02-02 10:37:31 +02:00
[IMP] account_banking_mandate: Fill payment line with first valid mandate + Add valid_mandate field and fix onchange
(cherry picked from commit 25d2e4d)
This commit is contained in:
@@ -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,11 +56,12 @@ 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>
|
||||
|
||||
Maintainer
|
||||
----------
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2014 Compassion CH - Cyril Sester <csester@compassion.ch>
|
||||
# © 2014 Tecnativa - Pedro M. Baeza
|
||||
# © 2015-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 Tecnativa - Pedro M. Baeza
|
||||
# Copyright 2015-16 Akretion - Alexis de Lattre <alexis.delattre@akretion.com>
|
||||
# Copyright 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': '11.0.1.0.1',
|
||||
'version': '11.0.1.0.0',
|
||||
'version': '10.0.1.1.3',
|
||||
'license': 'AGPL-3',
|
||||
'author': "Compassion CH, "
|
||||
"Tecnativa, "
|
||||
|
||||
@@ -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,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2016 Akretion (http://www.akretion.com/)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
# Copyright Akretion (http://www.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
|
||||
|
||||
@@ -16,14 +18,21 @@ class AccountMoveLine(models.Model):
|
||||
def _prepare_payment_line_vals(self, payment_order):
|
||||
vals = super(AccountMoveLine, self)._prepare_payment_line_vals(
|
||||
payment_order)
|
||||
if payment_order.payment_type == 'inbound' and self.mandate_id:
|
||||
vals['mandate_id'] = self.mandate_id.id
|
||||
vals['partner_bank_id'] = self.mandate_id.partner_bank_id.id
|
||||
partner_bank_id = vals.get('partner_bank_id', False)
|
||||
if partner_bank_id and 'mandate_id' not in vals:
|
||||
mandate = self.env['account.banking.mandate'].search(
|
||||
[('partner_bank_id', '=', partner_bank_id),
|
||||
('state', '=', 'valid')], limit=1)
|
||||
if mandate:
|
||||
vals['mandate_id'] = mandate.id
|
||||
if payment_order.payment_type != 'inbound':
|
||||
return vals
|
||||
mandate = self.mandate_id
|
||||
if not mandate and vals.get('mandate_id', False):
|
||||
mandate = mandate.browse(vals['mandate_id'])
|
||||
if not mandate:
|
||||
partner_bank_id = vals.get('partner_bank_id', False)
|
||||
if partner_bank_id:
|
||||
domain = [('partner_bank_id', '=', partner_bank_id)]
|
||||
else:
|
||||
domain = [('partner_id', '=', self.partner_id.id)]
|
||||
domain.append(('state', '=', 'valid'))
|
||||
mandate = mandate.search(domain, limit=1)
|
||||
vals.update({
|
||||
'mandate_id': mandate.id,
|
||||
'partner_bank_id': mandate.partner_bank_id.id or partner_bank_id,
|
||||
})
|
||||
return vals
|
||||
|
||||
@@ -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='First Valid Mandate')
|
||||
|
||||
@api.multi
|
||||
def _compute_mandate_count(self):
|
||||
@@ -21,3 +26,18 @@ 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 for reducing the duplicated searches on parent/child partners
|
||||
mandates_dic = {}
|
||||
for partner in self:
|
||||
commercial_partner_id = partner.commercial_partner_id.id
|
||||
if commercial_partner_id in mandates_dic:
|
||||
partner.valid_mandate_id = mandates_dic[commercial_partner_id]
|
||||
else:
|
||||
mandates = partner.commercial_partner_id.bank_ids.mapped(
|
||||
'mandate_ids').filtered(lambda x: x.state == 'valid')
|
||||
first_valid_mandate_id = mandates[:1].id
|
||||
partner.valid_mandate_id = first_valid_mandate_id
|
||||
mandates_dic[commercial_partner_id] = first_valid_mandate_id
|
||||
|
||||
Reference in New Issue
Block a user