diff --git a/account_move_force_removal/__manifest__.py b/account_move_force_removal/__manifest__.py index e22d58473..4838b1c0f 100644 --- a/account_move_force_removal/__manifest__.py +++ b/account_move_force_removal/__manifest__.py @@ -4,10 +4,11 @@ { "name": "Account Move Force Removal", "summary": """Allow force removal account moves""", - "version": "15.0.1.0.0", + "version": "15.0.1.0.1", "category": "Account", "license": "AGPL-3", "website": "https://github.com/OCA/account-financial-tools", "author": "Tecnativa, Odoo Community Association (OCA)", "depends": ["account"], + "data": ["security/account_move_force_removal_security.xml"], } diff --git a/account_move_force_removal/migrations/15.0.1.0.1/post-migration.py b/account_move_force_removal/migrations/15.0.1.0.1/post-migration.py new file mode 100644 index 000000000..a4b9cdffd --- /dev/null +++ b/account_move_force_removal/migrations/15.0.1.0.1/post-migration.py @@ -0,0 +1,4 @@ +def migrate(env, version): + users_billing = env.ref("account.group_account_invoice").users + group = env.ref("account_move_force_removal.group_account_move_force_removal") + group.users = [(4, user.id) for user in users_billing] diff --git a/account_move_force_removal/models/account_move.py b/account_move_force_removal/models/account_move.py index 9a70e7492..5b183141f 100644 --- a/account_move_force_removal/models/account_move.py +++ b/account_move_force_removal/models/account_move.py @@ -8,6 +8,10 @@ class AccountMove(models.Model): _inherit = "account.move" def unlink(self): - cancelled_moves = self.filtered(lambda m: m.state == "cancel") - super(AccountMove, cancelled_moves.with_context(force_delete=True)).unlink() + cancelled_moves = self.env["account.move"] + if self.env.user.has_group( + "account_move_force_removal.group_account_move_force_removal" + ): + cancelled_moves = self.filtered(lambda m: m.state == "cancel") + super(AccountMove, cancelled_moves.with_context(force_delete=True)).unlink() return super(AccountMove, self - cancelled_moves).unlink() diff --git a/account_move_force_removal/security/account_move_force_removal_security.xml b/account_move_force_removal/security/account_move_force_removal_security.xml new file mode 100644 index 000000000..678327c4e --- /dev/null +++ b/account_move_force_removal/security/account_move_force_removal_security.xml @@ -0,0 +1,10 @@ + + + + + Allow to delete invoices/journal entries + + + + diff --git a/account_move_force_removal/tests/test_move.py b/account_move_force_removal/tests/test_move.py index 73b5a8bee..0007243a5 100644 --- a/account_move_force_removal/tests/test_move.py +++ b/account_move_force_removal/tests/test_move.py @@ -2,10 +2,11 @@ # License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html from odoo.exceptions import UserError -from odoo.tests import Form +from odoo.tests import Form, tagged from odoo.tests.common import TransactionCase +@tagged("post_install", "-at_install") class TestMove(TransactionCase): @classmethod def setUpClass(cls): @@ -25,6 +26,7 @@ class TestMove(TransactionCase): {"name": "Test product", "type": "service"} ) cls.company = cls.env.company + cls.company.currency_id.active = True account_type = cls.env.ref("account.data_account_type_other_income") cls.income_account = cls.env["account.account"].search( [ @@ -50,14 +52,36 @@ class TestMove(TransactionCase): invoice = invoice.save() invoice.action_post() cls.invoice = invoice + cls.invoice2 = cls.invoice.copy() + cls.invoice2.action_post() + cls.invoice3 = cls.invoice.copy() + cls.invoice3.action_post() def test_remove_invoice_error(self): - # Delete invoice while name isn't / + # Delete invoice while name isn't / and + # user not in group_account_move_force_removal with self.assertRaises(UserError): self.invoice.unlink() - - def test_ok_invoice_error(self): - # Delete invoice (previously draft + camcel) + # Delete invoice (previously draft + cancel) and + # user not in group_account_move_force_removal self.invoice.button_draft() self.invoice.button_cancel() + with self.assertRaises(UserError): + self.invoice.unlink() + # Delete invoice while name isn't / and + # user in group_account_move_force_removal + self.env.user.groups_id += self.env.ref( + "account_move_force_removal.group_account_move_force_removal" + ) + with self.assertRaises(UserError): + self.invoice3.unlink() + + def test_ok_invoice_error(self): + # Delete invoice (previously draft + cancel) and + # user in group_account_move_force_removal + self.invoice.button_draft() + self.invoice.button_cancel() + self.env.user.groups_id += self.env.ref( + "account_move_force_removal.group_account_move_force_removal" + ) self.invoice.unlink()