From 37d58d6319fae47eaae85e4725fef7069f7303da Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 9 Apr 2018 11:54:59 +0200 Subject: [PATCH] [10.0][FIX] Split account move generated for SEPA direct debits per sequence_type (#464) The sequence_type of mandates switches from First to Recurring: - BEFORE: when state switched from 'open' to 'generated' - NOW: when state switches from 'generated' to 'uploaded' --- .../__manifest__.py | 2 +- .../models/account_payment_order.py | 66 +++++++++++-------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/account_banking_sepa_direct_debit/__manifest__.py b/account_banking_sepa_direct_debit/__manifest__.py index 9e0020a7e..29d972180 100644 --- a/account_banking_sepa_direct_debit/__manifest__.py +++ b/account_banking_sepa_direct_debit/__manifest__.py @@ -7,7 +7,7 @@ { 'name': 'Account Banking SEPA Direct Debit', 'summary': 'Create SEPA files for Direct Debit', - 'version': '11.0.1.0.1', + 'version': '11.0.1.0.2', 'license': 'AGPL-3', 'author': "Akretion, " "Tecnativa, " diff --git a/account_banking_sepa_direct_debit/models/account_payment_order.py b/account_banking_sepa_direct_debit/models/account_payment_order.py index c759e8323..e8d3b97ad 100644 --- a/account_banking_sepa_direct_debit/models/account_payment_order.py +++ b/account_banking_sepa_direct_debit/models/account_payment_order.py @@ -2,7 +2,7 @@ # © 2016 Akretion (Alexis de Lattre ) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import models, fields, api, _ +from odoo import api, models, _ from odoo.exceptions import UserError from lxml import etree @@ -227,32 +227,44 @@ class AccountPaymentOrder(models.Model): xml_root, gen_args) @api.multi - def finalize_sepa_file_creation(self, xml_root, gen_args): - """Save the SEPA Direct Debit file: mark all payments in the file - as 'sent'. Write 'last debit date' on mandate and set oneoff - mandate to expired. + def generated2uploaded(self): + """Write 'last debit date' on mandates + Set mandates from first to recurring + Set oneoff mandates to expired """ + # I call super() BEFORE updating the sequence_type + # from first to recurring, so that the account move + # is generated BEFORE, which will allow the split + # of the account move per sequence_type + res = super(AccountPaymentOrder, self).generated2uploaded() abmo = self.env['account.banking.mandate'] - to_expire_mandates = abmo.browse([]) - first_mandates = abmo.browse([]) - all_mandates = abmo.browse([]) - for bline in self.bank_line_ids: - if bline.mandate_id in all_mandates: - continue - all_mandates += bline.mandate_id - if bline.mandate_id.type == 'oneoff': - to_expire_mandates += bline.mandate_id - elif bline.mandate_id.type == 'recurrent': - seq_type = bline.mandate_id.recurrent_sequence_type - if seq_type == 'final': + for order in self: + to_expire_mandates = abmo.browse([]) + first_mandates = abmo.browse([]) + all_mandates = abmo.browse([]) + for bline in order.bank_line_ids: + if bline.mandate_id in all_mandates: + continue + all_mandates += bline.mandate_id + if bline.mandate_id.type == 'oneoff': to_expire_mandates += bline.mandate_id - elif seq_type == 'first': - first_mandates += bline.mandate_id - all_mandates.write( - {'last_debit_date': fields.Date.context_today(self)}) - to_expire_mandates.write({'state': 'expired'}) - first_mandates.write({ - 'recurrent_sequence_type': 'recurring', - }) - return super(AccountPaymentOrder, self).finalize_sepa_file_creation( - xml_root, gen_args) + elif bline.mandate_id.type == 'recurrent': + seq_type = bline.mandate_id.recurrent_sequence_type + if seq_type == 'final': + to_expire_mandates += bline.mandate_id + elif seq_type == 'first': + first_mandates += bline.mandate_id + all_mandates.write( + {'last_debit_date': order.date_generated}) + to_expire_mandates.write({'state': 'expired'}) + first_mandates.write({ + 'recurrent_sequence_type': 'recurring', + }) + for first_mandate in first_mandates: + first_mandate.message_post(_( + "Automatically switched from First to " + "Recurring when the debit order " + "%s has been marked as uploaded.") + % (order.id, order.name)) + return res