Skip to content

Commit

Permalink
[FIX] domain field ou and test script
Browse files Browse the repository at this point in the history
  • Loading branch information
Saran440 authored and hbrunn committed Mar 14, 2024
1 parent 87fec2a commit ce7f243
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 44 deletions.
1 change: 0 additions & 1 deletion account_operating_unit/models/account_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
Expand Down
3 changes: 1 addition & 2 deletions account_operating_unit/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]},
Expand Down
8 changes: 6 additions & 2 deletions account_operating_unit/models/account_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
# © 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):
_inherit = "account.payment"

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:
Expand Down
1 change: 0 additions & 1 deletion account_operating_unit/report/account_invoice_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class AccountInvoiceReport(models.Model):

_inherit = "account.invoice.report"

operating_unit_id = fields.Many2one(
Expand Down
3 changes: 3 additions & 0 deletions account_operating_unit/tests/test_account_operating_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
109 changes: 71 additions & 38 deletions account_operating_unit/tests/test_cross_ou_journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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()
13 changes: 13 additions & 0 deletions account_operating_unit/tests/test_invoice_operating_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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([])
3 changes: 3 additions & 0 deletions account_operating_unit/tests/test_operating_unit_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
3 changes: 3 additions & 0 deletions account_operating_unit/tests/test_payment_operating_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit ce7f243

Please sign in to comment.