diff --git a/account_operating_unit/models/account_journal.py b/account_operating_unit/models/account_journal.py index 6e1e452577..203d5b5b17 100644 --- a/account_operating_unit/models/account_journal.py +++ b/account_operating_unit/models/account_journal.py @@ -11,7 +11,6 @@ class AccountJournal(models.Model): operating_unit_id = fields.Many2one( comodel_name="operating.unit", - domain="[('user_ids', '=', uid)]", help="Operating Unit that will be used in payments, " "when this journal is used.", ) diff --git a/account_operating_unit/models/account_move.py b/account_operating_unit/models/account_move.py index 09c13d0a77..01aecc8e34 100644 --- a/account_operating_unit/models/account_move.py +++ b/account_operating_unit/models/account_move.py @@ -10,7 +10,7 @@ class AccountMoveLine(models.Model): _inherit = "account.move.line" operating_unit_id = fields.Many2one( - comodel_name="operating.unit", domain="[('user_ids', '=', uid)]" + comodel_name="operating.unit", ) @api.model_create_multi @@ -81,7 +81,6 @@ def _default_operating_unit_id(self): operating_unit_id = fields.Many2one( comodel_name="operating.unit", default=_default_operating_unit_id, - domain="[('user_ids', '=', uid)]", help="This operating unit will be defaulted in the move lines.", readonly=True, states={"draft": [("readonly", False)]}, diff --git a/account_operating_unit/models/account_payment.py b/account_operating_unit/models/account_payment.py index d03b4b571b..b132c81f1b 100644 --- a/account_operating_unit/models/account_payment.py +++ b/account_operating_unit/models/account_payment.py @@ -2,7 +2,7 @@ # © 2019 Serpent Consulting Services Pvt. Ltd. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from odoo import fields, models +from odoo import api, fields, models class AccountPayment(models.Model): @@ -10,11 +10,15 @@ class AccountPayment(models.Model): operating_unit_id = fields.Many2one( comodel_name="operating.unit", - domain="[('user_ids', '=', uid)]", compute="_compute_operating_unit_id", store=True, ) + @api.depends("journal_id") + def _compute_operating_unit_id(self): + for payment in self.filtered("journal_id"): + payment.operating_unit_id = payment.journal_id.operating_unit_id + def _prepare_move_line_default_vals(self, write_off_line_vals=None): lines = super()._prepare_move_line_default_vals(write_off_line_vals) for line in lines: diff --git a/account_operating_unit/report/account_invoice_report.py b/account_operating_unit/report/account_invoice_report.py index fd07326dcc..87547bfcf1 100644 --- a/account_operating_unit/report/account_invoice_report.py +++ b/account_operating_unit/report/account_invoice_report.py @@ -6,7 +6,6 @@ class AccountInvoiceReport(models.Model): - _inherit = "account.invoice.report" operating_unit_id = fields.Many2one( diff --git a/account_operating_unit/tests/test_account_operating_unit.py b/account_operating_unit/tests/test_account_operating_unit.py index 36bde8d531..2a9f379081 100644 --- a/account_operating_unit/tests/test_account_operating_unit.py +++ b/account_operating_unit/tests/test_account_operating_unit.py @@ -2,9 +2,12 @@ # © 2019 Serpent Consulting Services Pvt. Ltd. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.tests import tagged + from odoo.addons.account.tests.common import AccountTestInvoicingCommon +@tagged("post_install", "-at_install") class TestAccountOperatingUnit(AccountTestInvoicingCommon): def setUp(self): super().setUp() diff --git a/account_operating_unit/tests/test_cross_ou_journal_entry.py b/account_operating_unit/tests/test_cross_ou_journal_entry.py index 19e64ca179..1107ce1b44 100644 --- a/account_operating_unit/tests/test_cross_ou_journal_entry.py +++ b/account_operating_unit/tests/test_cross_ou_journal_entry.py @@ -2,10 +2,53 @@ # © 2019 Serpent Consulting Services Pvt. Ltd. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.exceptions import UserError +from odoo.tests import tagged +from odoo.tests.common import Form + from . import test_account_operating_unit as test_ou +@tagged("post_install", "-at_install") class TestCrossOuJournalEntry(test_ou.TestAccountOperatingUnit): + def setUp(self): + super().setUp() + + def _check_balance(self, account_id, acc_type="clearing"): + # Check balance for all operating units + domain = [("account_id", "=", account_id)] + balance = self._get_balance(domain) + self.assertEqual(balance, 0.0, "Balance is 0 for all Operating Units.") + # Check balance for operating B2B units + domain = [ + ("account_id", "=", account_id), + ("operating_unit_id", "=", self.b2b.id), + ] + balance = self._get_balance(domain) + if acc_type == "other": + self.assertEqual(balance, -100, "Balance is -100 for Operating Unit B2B.") + else: + self.assertEqual(balance, 100, "Balance is 100 for Operating Unit B2B.") + # Check balance for operating B2C units + domain = [ + ("account_id", "=", account_id), + ("operating_unit_id", "=", self.b2c.id), + ] + balance = self._get_balance(domain) + if acc_type == "other": + self.assertEqual(balance, 100.0, "Balance is 100 for Operating Unit B2C.") + else: + self.assertEqual(balance, -100.0, "Balance is -100 for Operating Unit B2C.") + + def _get_balance(self, domain): + """ + Call read_group method and return the balance of particular account. + """ + aml_rec = self.aml_model.with_user(self.user_id.id).read_group( + domain, ["debit", "credit", "account_id"], ["account_id"] + )[0] + return aml_rec.get("debit", 0) - aml_rec.get("credit", 0) + def test_cross_ou_journal_entry(self): """Test balance of cross OU journal entries. Test that when I create a manual journal entry with multiple @@ -17,14 +60,12 @@ def test_cross_ou_journal_entry(self): self.company.write( {"inter_ou_clearing_account_id": self.inter_ou_account_id.id} ) - self.acc_move_model = self.env["account.move"] - self.journal_model = self.env["account.journal"] # Create Journal Entries journal_ids = self.journal_model.search( [("code", "=", "MISC"), ("company_id", "=", self.company.id)], limit=1 ) # get default values of account move - move_vals = self.acc_move_model.default_get([]) + move_vals = self.move_model.default_get([]) lines = [ ( 0, @@ -52,45 +93,37 @@ def test_cross_ou_journal_entry(self): move_vals.update( {"journal_id": journal_ids and journal_ids.id, "line_ids": lines} ) - move = self.acc_move_model.with_user(self.user_id.id).create(move_vals) + move = self.move_model.with_user(self.user_id.id).create(move_vals) # Post journal entries move.action_post() # Check the balance of the account self._check_balance(self.current_asset_account_id.id, acc_type="other") clearing_account_id = self.company.inter_ou_clearing_account_id.id self._check_balance(clearing_account_id, acc_type="clearing") + # Report journal + report_journal = ( + self.env["report.account.report_journal"] + .sudo() + ._get_report_values( + docids=[journal_ids.id], + data={ + "form": { + "journal_ids": journal_ids.ids, + "company_id": journal_ids.company_id, + "used_context": { + "operating_unit_ids": journal_ids.operating_unit_id.id + }, + } + }, + ) + ) + self.assertTrue(report_journal) - def _check_balance(self, account_id, acc_type="clearing"): - # Check balance for all operating units - domain = [("account_id", "=", account_id)] - balance = self._get_balance(domain) - self.assertEqual(balance, 0.0, "Balance is 0 for all Operating Units.") - # Check balance for operating B2B units - domain = [ - ("account_id", "=", account_id), - ("operating_unit_id", "=", self.b2b.id), - ] - balance = self._get_balance(domain) - if acc_type == "other": - self.assertEqual(balance, -100, "Balance is -100 for Operating Unit B2B.") - else: - self.assertEqual(balance, 100, "Balance is 100 for Operating Unit B2B.") - # Check balance for operating B2C units - domain = [ - ("account_id", "=", account_id), - ("operating_unit_id", "=", self.b2c.id), - ] - balance = self._get_balance(domain) - if acc_type == "other": - self.assertEqual(balance, 100.0, "Balance is 100 for Operating Unit B2C.") - else: - self.assertEqual(balance, -100.0, "Balance is -100 for Operating Unit B2C.") - - def _get_balance(self, domain): - """ - Call read_group method and return the balance of particular account. - """ - aml_rec = self.aml_model.with_user(self.user_id.id).read_group( - domain, ["debit", "credit", "account_id"], ["account_id"] - )[0] - return aml_rec.get("debit", 0) - aml_rec.get("credit", 0) + def test_journal_no_ou(self): + """Test journal can not create if use self-balance but not ou in journal""" + with self.assertRaises(UserError): + with Form(self.journal_model) as f: + f.type = "bank" + f.name = "Test new bank not ou" + f.code = "testcode" + f.save() diff --git a/account_operating_unit/tests/test_invoice_operating_unit.py b/account_operating_unit/tests/test_invoice_operating_unit.py index f20d7a9bf6..dc8d7be33d 100644 --- a/account_operating_unit/tests/test_invoice_operating_unit.py +++ b/account_operating_unit/tests/test_invoice_operating_unit.py @@ -2,9 +2,13 @@ # © 2019 Serpent Consulting Services Pvt. Ltd. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.exceptions import UserError +from odoo.tests import tagged + from . import test_account_operating_unit as test_ou +@tagged("post_install", "-at_install") class TestInvoiceOperatingUnit(test_ou.TestAccountOperatingUnit): def test_create_invoice_validate(self): """Create & Validate the invoice. @@ -30,3 +34,12 @@ def test_create_invoice_validate(self): False, "Journal Entries have different Operating Units.", ) + # Test change ou in move + with self.assertRaises(UserError): + self.invoice.line_ids[0].operating_unit_id = self.b2c.id + # Test change company in move + new_company = self.env["res.company"].create({"name": "New Company"}) + with self.assertRaises(UserError): + self.invoice.line_ids[0].company_id = new_company.id + # Check report invoice + self.env["account.invoice.report"].sudo().search_read([]) diff --git a/account_operating_unit/tests/test_operating_unit_security.py b/account_operating_unit/tests/test_operating_unit_security.py index 09b777b0e9..e523b8c872 100644 --- a/account_operating_unit/tests/test_operating_unit_security.py +++ b/account_operating_unit/tests/test_operating_unit_security.py @@ -2,9 +2,12 @@ # © 2019 Serpent Consulting Services Pvt. Ltd. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.tests import tagged + from . import test_account_operating_unit as test_ou +@tagged("post_install", "-at_install") class TestOuSecurity(test_ou.TestAccountOperatingUnit): def test_security(self): """Test Security of Account Operating Unit""" diff --git a/account_operating_unit/tests/test_payment_operating_unit.py b/account_operating_unit/tests/test_payment_operating_unit.py index c01f69d672..a3e4248f2f 100644 --- a/account_operating_unit/tests/test_payment_operating_unit.py +++ b/account_operating_unit/tests/test_payment_operating_unit.py @@ -4,9 +4,12 @@ import time +from odoo.tests import tagged + from . import test_account_operating_unit as test_ou +@tagged("post_install", "-at_install") class TestInvoiceOperatingUnit(test_ou.TestAccountOperatingUnit): def test_payment_from_invoice(self): """Create and invoice and a subsquent payment, in another OU"""