From 32e73536f6289b60c7da5e57ad661897c74a4be9 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 24 Dec 2020 00:41:36 +0100 Subject: [PATCH] [MIG] account_banking_mandate: Migration to 14.0 Replace constrains on company by check_company=True on fields --- account_banking_mandate/README.rst | 2 +- account_banking_mandate/__manifest__.py | 5 +- .../13.0.1.0.0/noupdate_changes.xml | 8 -- .../migrations/13.0.1.0.0/post-migration.py | 19 --- .../models/account_banking_mandate.py | 121 ++---------------- .../models/account_move.py | 39 ++---- .../models/account_move_line.py | 21 +-- .../models/account_payment_line.py | 3 +- .../models/account_payment_method.py | 2 +- .../models/bank_payment_line.py | 5 +- account_banking_mandate/models/res_partner.py | 6 +- account_banking_mandate/readme/USAGE.rst | 2 +- .../tests/test_invoice_mandate.py | 24 ++-- account_banking_mandate/tests/test_mandate.py | 8 +- .../views/account_banking_mandate_view.xml | 72 +++++++---- .../views/account_move_view.xml | 2 +- 16 files changed, 104 insertions(+), 235 deletions(-) delete mode 100644 account_banking_mandate/migrations/13.0.1.0.0/noupdate_changes.xml delete mode 100644 account_banking_mandate/migrations/13.0.1.0.0/post-migration.py diff --git a/account_banking_mandate/README.rst b/account_banking_mandate/README.rst index b6cdf5fc1..d21bc069c 100644 --- a/account_banking_mandate/README.rst +++ b/account_banking_mandate/README.rst @@ -50,7 +50,7 @@ This module is part of the OCA/bank-payment suite. Usage ===== -To use this module, see menu "Invoicing/Accounting > Payments > Debit Orders" +To use this module, see menu "Invoicing/Accounting > Customers > Debit Orders" Bug Tracker =========== diff --git a/account_banking_mandate/__manifest__.py b/account_banking_mandate/__manifest__.py index 46d3c706e..03f23a5ed 100644 --- a/account_banking_mandate/__manifest__.py +++ b/account_banking_mandate/__manifest__.py @@ -1,13 +1,13 @@ # Copyright 2014 Compassion CH - Cyril Sester # Copyright 2014 Tecnativa - Pedro M. Baeza -# Copyright 2015-16 Akretion - Alexis de Lattre +# Copyright 2015-2020 Akretion - Alexis de Lattre # Copyright 2017 Tecnativa - Carlos Dauden # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). { "name": "Account Banking Mandate", "summary": "Banking mandates", - "version": "13.0.1.1.2", + "version": "14.0.1.0.0", "license": "AGPL-3", "author": "Compassion CH, " "Tecnativa, " @@ -24,7 +24,6 @@ "views/res_partner_bank_view.xml", "views/res_partner.xml", "views/bank_payment_line_view.xml", - "views/account_move_line.xml", "data/mandate_reference_sequence.xml", "security/mandate_security.xml", "security/ir.model.access.csv", diff --git a/account_banking_mandate/migrations/13.0.1.0.0/noupdate_changes.xml b/account_banking_mandate/migrations/13.0.1.0.0/noupdate_changes.xml deleted file mode 100644 index 07c15c830..000000000 --- a/account_banking_mandate/migrations/13.0.1.0.0/noupdate_changes.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - ['|',('company_id','=',False),('company_id','in',company_ids)] - - diff --git a/account_banking_mandate/migrations/13.0.1.0.0/post-migration.py b/account_banking_mandate/migrations/13.0.1.0.0/post-migration.py deleted file mode 100644 index cb0f09e5c..000000000 --- a/account_banking_mandate/migrations/13.0.1.0.0/post-migration.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2020 Tecnativa - Pedro M. Baeza -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from openupgradelib import openupgrade # pylint: disable=W7936 - - -@openupgrade.migrate() -def migrate(env, version): - openupgrade.load_data( - env.cr, "account_banking_mandate", "migrations/13.0.1.0.0/noupdate_changes.xml" - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE account_move am - SET mandate_id = ai.mandate_id - FROM account_invoice ai - WHERE ai.id = am.old_invoice_id""", - ) diff --git a/account_banking_mandate/models/account_banking_mandate.py b/account_banking_mandate/models/account_banking_mandate.py index 0dcab8b1a..c67782408 100644 --- a/account_banking_mandate/models/account_banking_mandate.py +++ b/account_banking_mandate/models/account_banking_mandate.py @@ -1,6 +1,6 @@ # Copyright 2014 Compassion CH - Cyril Sester # Copyright 2014 Tecnativa - Pedro M. Baeza -# Copyright 2015-16 Akretion - Alexis de Lattre +# Copyright 2015-2020 Akretion - Alexis de Lattre # Copyright 2020 Tecnativa - Carlos Dauden # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). @@ -16,8 +16,9 @@ class AccountBankingMandate(models.Model): _name = "account.banking.mandate" _description = "A generic banking mandate" - _inherit = ["mail.thread"] + _inherit = ["mail.thread", "mail.activity.mixin"] _order = "signature_date desc" + _check_company_auto = True def _get_default_partner_bank_id_domain(self): if "default_partner_id" in self.env.context: @@ -30,20 +31,21 @@ class AccountBankingMandate(models.Model): default="basic", required=True, string="Mandate Format", - track_visibility="onchange", + tracking=20, ) type = fields.Selection( [("generic", "Generic Mandate")], string="Type of Mandate", - track_visibility="onchange", + tracking=30, ) partner_bank_id = fields.Many2one( comodel_name="res.partner.bank", string="Bank Account", - track_visibility="onchange", + tracking=40, domain=lambda self: self._get_default_partner_bank_id_domain(), ondelete="restrict", index=True, + check_company=True, ) partner_id = fields.Many2one( comodel_name="res.partner", @@ -59,10 +61,10 @@ class AccountBankingMandate(models.Model): default=lambda self: self.env.company, ) unique_mandate_reference = fields.Char( - string="Unique Mandate Reference", track_visibility="onchange", copy=False + string="Unique Mandate Reference", tracking=10, copy=False ) signature_date = fields.Date( - string="Date of Signature of the Mandate", track_visibility="onchange" + string="Date of Signature of the Mandate", tracking=50, ) scan = fields.Binary(string="Scan of the Mandate") last_debit_date = fields.Date(string="Date of the Last Debit", readonly=True) @@ -75,7 +77,7 @@ class AccountBankingMandate(models.Model): ], string="Status", default="draft", - track_visibility="onchange", + tracking=60, help="Only valid mandates can be used in a payment line. A cancelled " "mandate is a mandate that has been cancelled by the customer.", ) @@ -131,10 +133,11 @@ class AccountBankingMandate(models.Model): @api.constrains("signature_date", "last_debit_date") def _check_dates(self): + today = fields.Date.context_today(self) for mandate in self: if ( mandate.signature_date - and mandate.signature_date > fields.Date.context_today(mandate) + and mandate.signature_date > today ): raise ValidationError( _("The date of signature of mandate '%s' " "is in the future!") @@ -153,101 +156,6 @@ class AccountBankingMandate(models.Model): % mandate.unique_mandate_reference ) - @api.constrains("company_id", "payment_line_ids", "partner_bank_id") - def _company_constrains(self): - for mandate in self: - if ( - mandate.partner_bank_id.company_id - and mandate.partner_bank_id.company_id != mandate.company_id - ): - raise ValidationError( - _( - "The company of the mandate %s differs from the " - "company of partner %s." - ) - % (mandate.display_name, mandate.partner_id.name) - ) - - if ( - self.env["account.payment.line"] - .sudo() - .search( - [ - ("mandate_id", "=", mandate.id), - ("company_id", "!=", mandate.company_id.id), - ], - limit=1, - ) - ): - raise ValidationError( - _( - "You cannot change the company of mandate %s, " - "as there exists payment lines referencing it that " - "belong to another company." - ) - % (mandate.display_name,) - ) - - if ( - self.env["account.move"] - .sudo() - .search( - [ - ("mandate_id", "=", mandate.id), - ("company_id", "!=", mandate.company_id.id), - ], - limit=1, - ) - ): - raise ValidationError( - _( - "You cannot change the company of mandate %s, " - "as there exists invoices referencing it that belong to " - "another company." - ) - % (mandate.display_name,) - ) - - if ( - self.env["account.move.line"] - .sudo() - .search( - [ - ("mandate_id", "=", mandate.id), - ("company_id", "!=", mandate.company_id.id), - ], - limit=1, - ) - ): - raise ValidationError( - _( - "You cannot change the company of mandate %s, " - "as there exists journal items referencing it that " - "belong to another company." - ) - % (mandate.display_name,) - ) - - if ( - self.env["bank.payment.line"] - .sudo() - .search( - [ - ("mandate_id", "=", mandate.id), - ("company_id", "!=", mandate.company_id.id), - ], - limit=1, - ) - ): - raise ValidationError( - _( - "You cannot change the company of mandate %s, " - "as there exists bank payment lines referencing it that " - "belong to another company." - ) - % (mandate.display_name,) - ) - @api.constrains("state", "partner_bank_id", "signature_date") def _check_valid_state(self): for mandate in self: @@ -276,7 +184,7 @@ class AccountBankingMandate(models.Model): vals["unique_mandate_reference"] = ( self.env["ir.sequence"].next_by_code("account.banking.mandate") or "New" ) - return super(AccountBankingMandate, self).create(vals) + return super().create(vals) @api.onchange("partner_bank_id") def mandate_partner_bank_change(self): @@ -288,14 +196,12 @@ class AccountBankingMandate(models.Model): if mandate.state != "draft": raise UserError(_("Mandate should be in draft state.")) self.write({"state": "valid"}) - return True def cancel(self): for mandate in self: if mandate.state not in ("draft", "valid"): raise UserError(_("Mandate should be in draft or valid state.")) self.write({"state": "cancel"}) - return True def back2draft(self): """Allows to set the mandate back to the draft state. @@ -305,4 +211,3 @@ class AccountBankingMandate(models.Model): if mandate.state != "cancel": raise UserError(_("Mandate should be in cancel state.")) self.write({"state": "draft"}) - return True diff --git a/account_banking_mandate/models/account_move.py b/account_banking_mandate/models/account_move.py index 8c07f0795..ba58ef9a8 100644 --- a/account_banking_mandate/models/account_move.py +++ b/account_banking_mandate/models/account_move.py @@ -2,8 +2,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import _, api, fields, models -from odoo.exceptions import ValidationError +from odoo import api, fields, models class AccountMove(models.Model): @@ -14,16 +13,17 @@ class AccountMove(models.Model): string="Direct Debit Mandate", ondelete="restrict", readonly=True, + check_company=True, states={"draft": [("readonly", False)]}, ) mandate_required = fields.Boolean( related="payment_mode_id.payment_method_id.mandate_required", readonly=True ) - def post(self): + def _post(self, soft=True): for record in self: record.line_ids.write({"mandate_id": record.mandate_id}) - return super(AccountMove, self).post() + return super()._post(soft=soft) @api.model def create(self, vals): @@ -35,15 +35,15 @@ class AccountMove(models.Model): } for onchange_method, changed_fields in list(onchanges.items()): if any(f not in vals for f in changed_fields): - invoice = self.new(vals) - invoice = invoice.with_context(force_company=invoice.company_id.id,) - getattr(invoice, onchange_method)() + move = self.new(vals) + move = move.with_company(move.company_id.id) + getattr(move, onchange_method)() for field in changed_fields: - if field not in vals and invoice[field]: - vals[field] = invoice._fields[field].convert_to_write( - invoice[field], invoice + if field not in vals and move[field]: + vals[field] = move._fields[field].convert_to_write( + move[field], move ) - return super(AccountMove, self).create(vals) + return super().create(vals) def set_mandate(self): if self.payment_mode_id.payment_method_id.mandate_required: @@ -54,25 +54,10 @@ class AccountMove(models.Model): @api.onchange("partner_id", "company_id") def _onchange_partner_id(self): """Select by default the first valid mandate of the partner""" - res = super(AccountMove, self)._onchange_partner_id() + res = super()._onchange_partner_id() self.set_mandate() return res @api.onchange("payment_mode_id") def _onchange_payment_mode_id(self): self.set_mandate() - - @api.constrains("mandate_id", "company_id") - def _check_company_constrains(self): - for inv in self: - if ( - inv.mandate_id.company_id - and inv.mandate_id.company_id != inv.company_id - ): - raise ValidationError( - _( - "The invoice %s has a different company than " - "that of the linked mandate %s)." - ) - % (inv.name, inv.mandate_id.display_name) - ) diff --git a/account_banking_mandate/models/account_move_line.py b/account_banking_mandate/models/account_move_line.py index ba5cd6165..4cbd314ac 100644 --- a/account_banking_mandate/models/account_move_line.py +++ b/account_banking_mandate/models/account_move_line.py @@ -3,19 +3,19 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import _, api, fields, models -from odoo.exceptions import ValidationError +from odoo import fields, models class AccountMoveLine(models.Model): _inherit = "account.move.line" mandate_id = fields.Many2one( - "account.banking.mandate", string="Direct Debit Mandate", ondelete="restrict" + "account.banking.mandate", string="Direct Debit Mandate", ondelete="restrict", + check_company=True, ) def _prepare_payment_line_vals(self, payment_order): - vals = super(AccountMoveLine, self)._prepare_payment_line_vals(payment_order) + vals = super()._prepare_payment_line_vals(payment_order) if payment_order.payment_type != "inbound": return vals mandate = self.mandate_id @@ -36,16 +36,3 @@ class AccountMoveLine(models.Model): } ) return vals - - @api.constrains("mandate_id", "company_id") - def _check_company_constrains(self): - for ml in self: - mandate = ml.mandate_id - if mandate.company_id and mandate.company_id != ml.company_id: - raise ValidationError( - _( - "The item %s of journal %s has a different company than " - "that of the linked mandate %s)." - ) - % (ml.name, ml.move_id.name, ml.mandate_id.display_name) - ) diff --git a/account_banking_mandate/models/account_payment_line.py b/account_banking_mandate/models/account_payment_line.py index cf515c728..7acd965c8 100644 --- a/account_banking_mandate/models/account_payment_line.py +++ b/account_banking_mandate/models/account_payment_line.py @@ -14,6 +14,7 @@ class AccountPaymentLine(models.Model): comodel_name="account.banking.mandate", string="Direct Debit Mandate", domain=[("state", "=", "valid")], + check_company=True, ) mandate_required = fields.Boolean( related="order_id.payment_method_id.mandate_required", readonly=True @@ -57,7 +58,7 @@ class AccountPaymentLine(models.Model): ) def draft2open_payment_line_check(self): - res = super(AccountPaymentLine, self).draft2open_payment_line_check() + res = super().draft2open_payment_line_check() if self.mandate_required and not self.mandate_id: raise UserError(_("Missing Mandate on payment line %s") % self.name) return res diff --git a/account_banking_mandate/models/account_payment_method.py b/account_banking_mandate/models/account_payment_method.py index 1c71ab489..26cad8a8d 100644 --- a/account_banking_mandate/models/account_payment_method.py +++ b/account_banking_mandate/models/account_payment_method.py @@ -1,4 +1,4 @@ -# Copyright 2016 Akretion (Alexis de Lattre ) +# Copyright 2016-2020 Akretion (Alexis de Lattre ) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from odoo import fields, models diff --git a/account_banking_mandate/models/bank_payment_line.py b/account_banking_mandate/models/bank_payment_line.py index db736bc82..5eb6c80ba 100644 --- a/account_banking_mandate/models/bank_payment_line.py +++ b/account_banking_mandate/models/bank_payment_line.py @@ -13,12 +13,11 @@ class BankPaymentLine(models.Model): comodel_name="account.banking.mandate", string="Direct Debit Mandate", related="payment_line_ids.mandate_id", + check_company=True, ) @api.model def same_fields_payment_line_and_bank_payment_line(self): - res = super( - BankPaymentLine, self - ).same_fields_payment_line_and_bank_payment_line() + res = super().same_fields_payment_line_and_bank_payment_line() res.append("mandate_id") return res diff --git a/account_banking_mandate/models/res_partner.py b/account_banking_mandate/models/res_partner.py index 2dde0ca9b..4673831cc 100644 --- a/account_banking_mandate/models/res_partner.py +++ b/account_banking_mandate/models/res_partner.py @@ -30,13 +30,11 @@ class ResPartner(models.Model): def _compute_valid_mandate_id(self): # Dict for reducing the duplicated searches on parent/child partners - company_id = self.env.context.get("force_company", False) + company_id = self.env.company.id if company_id: company = self.env["res.company"].browse(company_id) else: - company = self.env["res.company"]._company_default_get( - "account.banking.mandate" - ) + company = self.env.company mandates_dic = {} for partner in self: diff --git a/account_banking_mandate/readme/USAGE.rst b/account_banking_mandate/readme/USAGE.rst index 5fca9a13a..9174b2f89 100644 --- a/account_banking_mandate/readme/USAGE.rst +++ b/account_banking_mandate/readme/USAGE.rst @@ -1 +1 @@ -To use this module, see menu "Invoicing/Accounting > Payments > Debit Orders" +To use this module, see menu "Invoicing/Accounting > Customers > Debit Orders" diff --git a/account_banking_mandate/tests/test_invoice_mandate.py b/account_banking_mandate/tests/test_invoice_mandate.py index 21071463c..0694c663b 100644 --- a/account_banking_mandate/tests/test_invoice_mandate.py +++ b/account_banking_mandate/tests/test_invoice_mandate.py @@ -2,7 +2,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from odoo import fields -from odoo.exceptions import ValidationError +from odoo.exceptions import UserError from odoo.tests.common import TransactionCase @@ -61,7 +61,7 @@ class TestInvoiceMandate(TransactionCase): lambda s: s.account_id == self.invoice_account ) if payable_move_lines: - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): payable_move_lines[0].mandate_id = mandate_2 def test_post_invoice_and_refund_02(self): @@ -107,7 +107,7 @@ class TestInvoiceMandate(TransactionCase): invoice = self.env["account.move"].new( { "partner_id": self.partner.id, - "type": "out_invoice", + "move_type": "out_invoice", "company_id": self.company.id, } ) @@ -120,7 +120,7 @@ class TestInvoiceMandate(TransactionCase): invoice = self.env["account.move"].new( { "partner_id": self.partner.id, - "type": "out_invoice", + "move_type": "out_invoice", "company_id": self.company.id, } ) @@ -171,12 +171,12 @@ class TestInvoiceMandate(TransactionCase): invoice = self.env["account.move"].create( { "partner_id": self.partner.id, - "type": "out_invoice", + "move_type": "out_invoice", "company_id": self.company.id, } ) - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): invoice.mandate_id = mandate_2 def _create_res_partner(self, name): @@ -228,8 +228,10 @@ class TestInvoiceMandate(TransactionCase): } ) bank_journal = self.env["account.journal"].search( - [("type", "=", "bank")], limit=1 - ) + [ + ("type", "=", "bank"), + ('company_id', '=', self.company.id), + ], limit=1) self.mode_inbound_acme.variable_journal_ids = bank_journal self.mode_inbound_acme.payment_method_id.mandate_required = True self.mode_inbound_acme.payment_order_ok = True @@ -279,10 +281,12 @@ class TestInvoiceMandate(TransactionCase): self.invoice = self.env["account.move"].create( { "partner_id": self.partner.id, - "type": "out_invoice", + "move_type": "out_invoice", "company_id": self.company.id, "journal_id": self.env["account.journal"] - .search([("type", "=", "sale")], limit=1) + .search([ + ("type", "=", "sale"), + ('company_id', '=', self.company.id)], limit=1) .id, "invoice_line_ids": invoice_vals, } diff --git a/account_banking_mandate/tests/test_mandate.py b/account_banking_mandate/tests/test_mandate.py index beb037c6e..b6d6ca796 100644 --- a/account_banking_mandate/tests/test_mandate.py +++ b/account_banking_mandate/tests/test_mandate.py @@ -78,7 +78,7 @@ class TestMandate(TransactionCase): bank_account_2 = self.env.ref("account_payment_mode.res_partner_2_iban") mandate.partner_bank_id = bank_account_2 mandate.mandate_partner_bank_change() - self.assertEquals(mandate.partner_id, bank_account_2.partner_id) + self.assertEqual(mandate.partner_id, bank_account_2.partner_id) def test_constrains_01(self): bank_account = self.env.ref("account_payment_mode.res_partner_12_iban") @@ -106,7 +106,7 @@ class TestMandate(TransactionCase): } ) - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): mandate.company_id = self.company_2 def test_constrains_03(self): @@ -125,7 +125,7 @@ class TestMandate(TransactionCase): "partner_id": self.company_2.partner_id.id, } ) - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): mandate.partner_bank_id = bank_account_2 def test_constrains_04(self): @@ -139,7 +139,7 @@ class TestMandate(TransactionCase): "partner_id": self.company_2.partner_id.id, } ) - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): bank_account.mandate_ids += mandate def test_mandate_reference_01(self): diff --git a/account_banking_mandate/views/account_banking_mandate_view.xml b/account_banking_mandate/views/account_banking_mandate_view.xml index 567fcc5a3..85d69305f 100644 --- a/account_banking_mandate/views/account_banking_mandate_view.xml +++ b/account_banking_mandate/views/account_banking_mandate_view.xml @@ -1,6 +1,6 @@ @@ -15,26 +15,29 @@ type="object" string="Validate" states="draft" - class="oe_highlight" + class="btn-primary" invisible="context.get('mandate_bank_partner_view')" + groups="account_payment_order.group_account_payment" />