Compare commits
3 Commits
43c93fa093
...
16.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 189b068097 | |||
| e1f1d748ff | |||
| fefd9a6a34 |
@@ -19,11 +19,13 @@ class AccountBankStatement(models.Model):
|
|||||||
class AccountBankStatementLine(models.Model):
|
class AccountBankStatementLine(models.Model):
|
||||||
_inherit = "account.bank.statement.line"
|
_inherit = "account.bank.statement.line"
|
||||||
|
|
||||||
# This function is called when a user opens the form to create a new bank statement line
|
@api.model_create_multi
|
||||||
@api.model
|
def create(self, vals_list):
|
||||||
def default_get(self, fields_list):
|
# Condition on action_name is here to check permissions only for the manual creation.
|
||||||
res = super().default_get(fields_list)
|
# Import of files and pulling online bank statements should not be concerned
|
||||||
journal_id = self._context.get('default_journal_id') or res.get('journal_id')
|
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:
|
if journal_id:
|
||||||
journal = self.env["account.journal"].browse(journal_id)
|
journal = self.env["account.journal"].browse(journal_id)
|
||||||
if not journal.allow_bank_statement_line_creation:
|
if not journal.allow_bank_statement_line_creation:
|
||||||
@@ -31,7 +33,7 @@ class AccountBankStatementLine(models.Model):
|
|||||||
_("Manual creation of bank statement lines is not allowed for the journal %s.")
|
_("Manual creation of bank statement lines is not allowed for the journal %s.")
|
||||||
% journal.display_name
|
% journal.display_name
|
||||||
)
|
)
|
||||||
return res
|
return super().create(vals_list)
|
||||||
|
|
||||||
def unlink(self):
|
def unlink(self):
|
||||||
for line in self:
|
for line in self:
|
||||||
|
|||||||
@@ -14,22 +14,45 @@ class TestBankStatementLineCreation(TransactionCase):
|
|||||||
)
|
)
|
||||||
cls.partner = cls.env["res.partner"].create({"name": "Test Partner"})
|
cls.partner = cls.env["res.partner"].create({"name": "Test Partner"})
|
||||||
|
|
||||||
def test_default_get_blocks_creation_when_not_allowed(self):
|
def test_manual_create_blocks_when_not_allowed(self):
|
||||||
"""Test that default_get raises UserError when creation is not allowed."""
|
"""Test that manual creation from UI raises UserError when not allowed."""
|
||||||
self.bank_journal.allow_bank_statement_line_creation = False
|
self.bank_journal.allow_bank_statement_line_creation = False
|
||||||
|
|
||||||
with self.assertRaises(UserError):
|
with self.assertRaises(UserError):
|
||||||
self.env["account.bank.statement.line"].with_context(
|
self.env["account.bank.statement.line"].with_context(
|
||||||
default_journal_id=self.bank_journal.id
|
action_name="action_bank_statement_tree"
|
||||||
).default_get(["journal_id", "amount", "payment_ref"])
|
).create({
|
||||||
|
"journal_id": self.bank_journal.id,
|
||||||
|
"amount": 100.0,
|
||||||
|
"payment_ref": "Test",
|
||||||
|
"date": "2024-01-01",
|
||||||
|
})
|
||||||
|
|
||||||
def test_default_get_allows_creation_when_allowed(self):
|
def test_manual_create_works_when_allowed(self):
|
||||||
"""Test that default_get works when creation is allowed."""
|
"""Test that manual creation from UI works when allowed."""
|
||||||
self.bank_journal.allow_bank_statement_line_creation = True
|
self.bank_journal.allow_bank_statement_line_creation = True
|
||||||
|
|
||||||
# Should not raise
|
line = self.env["account.bank.statement.line"].with_context(
|
||||||
result = self.env["account.bank.statement.line"].with_context(
|
action_name="action_bank_statement_tree"
|
||||||
default_journal_id=self.bank_journal.id
|
).create({
|
||||||
).default_get(["journal_id", "amount", "payment_ref"])
|
"journal_id": self.bank_journal.id,
|
||||||
|
"amount": 100.0,
|
||||||
|
"payment_ref": "Test",
|
||||||
|
"date": "2024-01-01",
|
||||||
|
})
|
||||||
|
|
||||||
self.assertIsInstance(result, dict)
|
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())
|
||||||
|
|||||||
Reference in New Issue
Block a user