From 519129a4912d0151944b45d47dff43b2a6d9324f Mon Sep 17 00:00:00 2001 From: Thomas Binsfeld Date: Fri, 22 Feb 2019 13:49:43 +0100 Subject: [PATCH] [REF] Account Banking SEPA Direct Debit: check SEPA ready --- .../models/__init__.py | 1 + .../models/account_payment_line.py | 48 +++++++++++++++++++ .../models/account_payment_order.py | 32 ++----------- 3 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 account_banking_sepa_direct_debit/models/account_payment_line.py diff --git a/account_banking_sepa_direct_debit/models/__init__.py b/account_banking_sepa_direct_debit/models/__init__.py index dea757dab..390ed9216 100644 --- a/account_banking_sepa_direct_debit/models/__init__.py +++ b/account_banking_sepa_direct_debit/models/__init__.py @@ -6,3 +6,4 @@ from . import bank_payment_line from . import account_payment_mode from . import account_payment_method from . import account_payment_order +from . import account_payment_line diff --git a/account_banking_sepa_direct_debit/models/account_payment_line.py b/account_banking_sepa_direct_debit/models/account_payment_line.py new file mode 100644 index 000000000..7b9b887ea --- /dev/null +++ b/account_banking_sepa_direct_debit/models/account_payment_line.py @@ -0,0 +1,48 @@ +# Copyright 2019 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, models, _ +from odoo.exceptions import UserError + + +class AccountPaymentLine(models.Model): + _inherit = 'account.payment.line' + + @api.multi + def draft2open_payment_line_check(self): + res = super(AccountPaymentLine, self).draft2open_payment_line_check() + sepa_dd_lines = self.filtered( + lambda l: l.order_id.payment_method_id.code == 'sepa_direct_debit') + sepa_dd_lines._check_sepa_direct_debit_ready() + return res + + @api.multi + def _check_sepa_direct_debit_ready(self): + """ + This method checks whether the payment line(s) are ready to be used + in the SEPA Direct Debit file generation. + :raise: UserError if a line does not fulfils all requirements + """ + for rec in self: + if not rec.mandate_id: + raise UserError(_( + "Missing SEPA Direct Debit mandate on the line with " + "partner {partner_name} (reference {reference}).").format( + partner_name=rec.partner_id.name, reference=rec.name)) + if rec.mandate_id.state != 'valid': + raise UserError(_( + "The SEPA Direct Debit mandate with reference " + "{mandate_ref} for partner {partner_name} has " + "expired.").format( + mandate_ref=rec.mandate_id.unique_mandate_reference, + partner_name=rec.partner_id.name)) + if rec.mandate_id.type == 'oneoff' and \ + rec.mandate_id.last_debit_date: + raise UserError(_( + "The SEPA Direct Debit mandate with reference " + "{mandate_ref} for partner {partner_name} has type set " + "to 'One-Off' but has a last debit date set to " + "{last_debit_date}. Therefore, it cannot be used.").format( + mandate_ref=rec.mandate_id.unique_mandate_reference, + partner_name=rec.partner_id.name, + last_debit_date=rec.mandate_id.last_debit_date)) 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 b43936aee..a365950fb 100644 --- a/account_banking_sepa_direct_debit/models/account_payment_order.py +++ b/account_banking_sepa_direct_debit/models/account_payment_order.py @@ -69,44 +69,22 @@ class AccountPaymentOrder(models.Model): transactions_count_a += 1 priority = line.priority categ_purpose = line.category_purpose - # The field line.date is the requested payment date - # taking into account the 'date_prefered' setting - # cf account_banking_payment_export/models/account_payment.py - # in the inherit of action_open() - if not line.mandate_id: - raise UserError( - _("Missing SEPA Direct Debit mandate on the " - "bank payment line with partner '%s' " - "(reference '%s').") - % (line.partner_id.name, line.name)) scheme = line.mandate_id.scheme - if line.mandate_id.state != 'valid': - raise Warning( - _("The SEPA Direct Debit mandate with reference '%s' " - "for partner '%s' has expired.") - % (line.mandate_id.unique_mandate_reference, - line.mandate_id.partner_id.name)) if line.mandate_id.type == 'oneoff': seq_type = 'OOFF' - if line.mandate_id.last_debit_date: - raise Warning( - _("The mandate with reference '%s' for partner " - "'%s' has type set to 'One-Off' and it has a " - "last debit date set to '%s', so we can't use " - "it.") - % (line.mandate_id.unique_mandate_reference, - line.mandate_id.partner_id.name, - line.mandate_id.last_debit_date)) elif line.mandate_id.type == 'recurrent': seq_type_map = { 'recurring': 'RCUR', 'first': 'FRST', 'final': 'FNAL', } - seq_type_label = \ - line.mandate_id.recurrent_sequence_type + seq_type_label = line.mandate_id.recurrent_sequence_type assert seq_type_label is not False seq_type = seq_type_map[seq_type_label] + # The field line.date is the requested payment date + # taking into account the 'date_preferred' setting + # cf account_banking_payment_export/models/account_payment.py + # in the inherit of action_open() key = (line.date, priority, categ_purpose, seq_type, scheme) if key in lines_per_group: lines_per_group[key].append(line)