diff --git a/account_advanced_protection_features/models/account_bank_statement.py b/account_advanced_protection_features/models/account_bank_statement.py index f7b4dbe..e61314e 100644 --- a/account_advanced_protection_features/models/account_bank_statement.py +++ b/account_advanced_protection_features/models/account_bank_statement.py @@ -21,15 +21,18 @@ class AccountBankStatementLine(models.Model): @api.model_create_multi def create(self, vals_list): - for vals in vals_list: - journal_id = vals.get("journal_id") - if journal_id: - journal = self.env["account.journal"].browse(journal_id) - if not journal.allow_bank_statement_line_creation: - raise UserError( - _("Manual creation of bank statement lines is not allowed for the journal %s.") - % journal.display_name - ) + # Condition on action_name is here to check permissions only for the manual creation. + # Import of files and pulling online bank statements should not be concerned + if self.env.context.get("action_name") == "action_bank_statement_tree": + for vals in vals_list: + journal_id = vals.get("journal_id") + if journal_id: + journal = self.env["account.journal"].browse(journal_id) + if not journal.allow_bank_statement_line_creation: + raise UserError( + _("Manual creation of bank statement lines is not allowed for the journal %s.") + % journal.display_name + ) return super().create(vals_list) def unlink(self): diff --git a/account_advanced_protection_features/tests/test_account_bank_statement.py b/account_advanced_protection_features/tests/test_account_bank_statement.py index 090a679..1a3783c 100644 --- a/account_advanced_protection_features/tests/test_account_bank_statement.py +++ b/account_advanced_protection_features/tests/test_account_bank_statement.py @@ -14,23 +14,27 @@ class TestBankStatementLineCreation(TransactionCase): ) cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) - def test_create_blocks_creation_when_not_allowed(self): - """Test that create raises UserError when creation is not allowed.""" + def test_manual_create_blocks_when_not_allowed(self): + """Test that manual creation from UI raises UserError when not allowed.""" self.bank_journal.allow_bank_statement_line_creation = False with self.assertRaises(UserError): - self.env["account.bank.statement.line"].create({ + self.env["account.bank.statement.line"].with_context( + action_name="action_bank_statement_tree" + ).create({ "journal_id": self.bank_journal.id, "amount": 100.0, "payment_ref": "Test", "date": "2024-01-01", }) - def test_create_allows_creation_when_allowed(self): - """Test that create works when creation is allowed.""" + def test_manual_create_works_when_allowed(self): + """Test that manual creation from UI works when allowed.""" self.bank_journal.allow_bank_statement_line_creation = True - line = self.env["account.bank.statement.line"].create({ + line = self.env["account.bank.statement.line"].with_context( + action_name="action_bank_statement_tree" + ).create({ "journal_id": self.bank_journal.id, "amount": 100.0, "payment_ref": "Test", @@ -38,3 +42,17 @@ class TestBankStatementLineCreation(TransactionCase): }) self.assertTrue(line.exists()) + + def test_programmatic_create_works_even_when_not_allowed(self): + """Test that creation without UI context (e.g. OFX import, pulling online bank statements) + works even when manual creation is not allowed.""" + self.bank_journal.allow_bank_statement_line_creation = False + + line = self.env["account.bank.statement.line"].create({ + "journal_id": self.bank_journal.id, + "amount": 100.0, + "payment_ref": "Test Import", + "date": "2024-01-01", + }) + + self.assertTrue(line.exists())