diff --git a/sales_team_account_journal/__manifest__.py b/sales_team_account_journal/__manifest__.py index 5199506b..150ab2fd 100644 --- a/sales_team_account_journal/__manifest__.py +++ b/sales_team_account_journal/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Sales Team Account Journal", - "version": "1.0.0", + "version": "1.0.1", "author": "Numigi", "maintainer": "Numigi", "website": "https://bit.ly/numigi-com", @@ -11,6 +11,7 @@ "category": "Sale", "depends": [ "account", + "sale", "sales_team", ], "summary": "Add a default sales journal on a sales team for invoices", diff --git a/sales_team_account_journal/models/account_move.py b/sales_team_account_journal/models/account_move.py index d6650315..082c8123 100644 --- a/sales_team_account_journal/models/account_move.py +++ b/sales_team_account_journal/models/account_move.py @@ -7,30 +7,35 @@ class AccountMove(models.Model): _inherit = "account.move" + def _check_team_journal(self, team_id, company_id, currency_id): + return ( + True + if team_id.journal_id + and team_id.journal_id.company_id.id == company_id + and team_id.journal_id.currency_id.id == currency_id + else False + ) + + def _check_sale_move_type(self): + move_type = self._context.get('default_move_type', 'entry') + return True if move_type in self.get_sale_types(include_receipts=False) else False + @api.onchange("team_id") def onchange_team_id(self): - if ( - self.team_id - and self.team_id.journal_id - and ( - not self.team_id.journal_id.currency_id - or self.team_id.journal_id.currency_id == self.currency_id - ) - and self.team_id.journal_id.company_id == self.company_id - ): - self.journal_id = self.team_id.journal_id + if self.team_id: + if self._check_sale_move_type() and self._check_team_journal( + self.team_id, self.company_id.id, self.currency_id.id + ): + self.journal_id = self.team_id.journal_id @api.model def create(self, vals): - rec = super(AccountMove, self).create(vals) - if ( - rec.team_id - and rec.team_id.journal_id - and ( - not rec.team_id.journal_id.currency_id - or rec.team_id.journal_id.currency_id == rec.currency_id - ) - and rec.team_id.journal_id.company_id == rec.company_id - ): - rec.journal_id = rec.team_id.journal_id.id - return rec + if vals.get("team_id", False): + team_id = self.env["crm.team"].browse(vals["team_id"]) + company_id = vals.get("company_id", self.env.company.id) + currency_id = vals.get("currency_id", False) + if self._check_sale_move_type() and self._check_team_journal( + team_id, company_id, currency_id + ): + vals["journal_id"] = team_id.journal_id.id + return super(AccountMove, self).create(vals) diff --git a/sales_team_account_journal/tests/test__account_move_journal.py b/sales_team_account_journal/tests/test__account_move_journal.py index 39c7dd05..33e59120 100644 --- a/sales_team_account_journal/tests/test__account_move_journal.py +++ b/sales_team_account_journal/tests/test__account_move_journal.py @@ -8,28 +8,40 @@ class AccountMoveJournal(SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() + cls.company_id = cls.env.ref("base.main_company") + cls.client_id = cls.env["res.partner"].create({"name": "My Client"}) cls.journal = cls.env["account.journal"].create( { "name": "My Sale Journal", "type": "sale", "code": "MSJ", "currency_id": cls.env.ref("base.USD").id, + "company_id": cls.company_id.id, } ) - cls.team_id = cls.env["crm.team"].create( + cls.team_id = cls.env.ref("sales_team.team_sales_department") + cls.team_id.write( { - "name": "Super Sales Team", + "company_id": cls.company_id.id, "journal_id": cls.journal.id, } ) def test__01_team_journal_exists(self): - move = self.env["account.move"].create( - { - "team_id": self.team_id.id, - "currency_id": self.env.ref("base.USD").id, - } + move = ( + self.env["account.move"] + .with_context(default_move_type="out_invoice") + .create( + { + "ref": "test_invoice_1", + "partner_id": self.client_id.id, + "currency_id": self.env.ref("base.USD").id, + "company_id": self.company_id.id, + "team_id": self.team_id.id, + } + ) ) + self.assertEqual(move.journal_id.name, self.journal.name) def test__02_team_journal_not_exists(self): @@ -37,16 +49,25 @@ def test__02_team_journal_not_exists(self): move = self.env["account.move"].create( { "currency_id": self.env.ref("base.USD").id, + "move_type": "out_invoice", } ) self.assertNotEqual(move.journal_id, self.journal) def test__03_team_journal_exists_but_different_currency(self): - self.journal.currency_id = self.env.ref("base.EUR").id move = self.env["account.move"].create( { - "team_id": self.team_id.id, + "currency_id": self.env.ref("base.EUR").id, + "move_type": "out_invoice", + } + ) + self.assertNotEqual(move.journal_id.name, self.journal.name) + + def test__04_in_invoice_journal_not_changed_by_team(self): + move = self.env["account.move"].create( + { "currency_id": self.env.ref("base.USD").id, + "move_type": "entry", } ) self.assertNotEqual(move.journal_id.name, self.journal.name)