Skip to content

Commit

Permalink
[MIG] account_operating_unit: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BT-aleonard committed Jul 13, 2023
1 parent 94c70db commit 2a84a5d
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 154 deletions.
2 changes: 1 addition & 1 deletion account_operating_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Accounting with Operating Units",
"summary": "Introduces Operating Unit (OU) in invoices and "
"Accounting Entries with clearing account",
"version": "15.0.1.1.0",
"version": "16.0.1.0.0",
"author": "ForgeFlow, "
"Serpent Consulting Services Pvt. Ltd.,"
"WilldooIT Pty Ltd,"
Expand Down
12 changes: 7 additions & 5 deletions account_operating_unit/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# © 2019 Serpent Consulting Services Pvt. Ltd.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from contextlib import contextmanager

from odoo import _, api, fields, models
from odoo.exceptions import UserError

Expand Down Expand Up @@ -88,11 +90,9 @@ def _default_operating_unit_id(self):

@api.onchange("invoice_line_ids")
def _onchange_invoice_line_ids(self):
res = super()._onchange_invoice_line_ids()
if self.operating_unit_id:
for line in self.line_ids:
line.operating_unit_id = self.operating_unit_id

Check warning on line 95 in account_operating_unit/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_operating_unit/models/account_move.py#L95

Added line #L95 was not covered by tests
return res

@api.onchange("operating_unit_id")
def _onchange_operating_unit(self):
Expand Down Expand Up @@ -190,10 +190,12 @@ def _post(self, soft=True):

return super()._post(soft)

def _check_balanced(self):
@contextmanager
def _check_balanced(self, container):
if self.env.context.get("wip"):
return True
return super()._check_balanced()
yield
else:
yield super()._check_balanced(container)

@api.constrains("line_ids")
def _check_ou(self):
Expand Down
1 change: 1 addition & 0 deletions account_operating_unit/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
* Saran Lim. <[email protected]>
* Pimolnat Suntian <[email protected]>
* Hieu, Vo Minh Bao <[email protected]>
* Alejandro Leonard <[email protected]>
142 changes: 73 additions & 69 deletions account_operating_unit/tests/test_account_operating_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,147 +9,148 @@

@tagged("post_install", "-at_install")
class TestAccountOperatingUnit(AccountTestInvoicingCommon):
def setUp(self):
super().setUp()
self.res_users_model = self.env["res.users"]
self.aml_model = self.env["account.move.line"]
self.move_model = self.env["account.move"]
self.account_model = self.env["account.account"]
self.journal_model = self.env["account.journal"]
self.product_model = self.env["product.product"]
self.payment_model = self.env["account.payment"]
self.register_payments_model = self.env["account.payment.register"]
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.res_users_model = cls.env["res.users"]
cls.aml_model = cls.env["account.move.line"]
cls.move_model = cls.env["account.move"]
cls.account_model = cls.env["account.account"]
cls.journal_model = cls.env["account.journal"]
cls.product_model = cls.env["product.product"]
cls.payment_model = cls.env["account.payment"]
cls.register_payments_model = cls.env["account.payment.register"]

# company
self.company = self.env.user.company_id
self.grp_acc_manager = self.env.ref("account.group_account_manager")
cls.company = cls.env.user.company_id
cls.grp_acc_manager = cls.env.ref("account.group_account_manager")
# Main Operating Unit
self.ou1 = self.env.ref("operating_unit.main_operating_unit")
cls.ou1 = cls.env.ref("operating_unit.main_operating_unit")
# B2B Operating Unit
self.b2b = self.env.ref("operating_unit.b2b_operating_unit")
cls.b2b = cls.env.ref("operating_unit.b2b_operating_unit")
# B2C Operating Unit
self.b2c = self.env.ref("operating_unit.b2c_operating_unit")
cls.b2c = cls.env.ref("operating_unit.b2c_operating_unit")
# Assign user to main company to allow to write OU
self.env.user.write(
cls.env.user.write(
{
"company_ids": [(4, self.env.ref("base.main_company").id)],
"company_ids": [(4, cls.env.ref("base.main_company").id)],
"operating_unit_ids": [
(4, self.b2b.id),
(4, self.b2c.id),
(4, cls.b2b.id),
(4, cls.b2c.id),
],
}
)
# Assign company to OU
(self.ou1 + self.b2b + self.b2c).write({"company_id": self.company.id})
(cls.ou1 + cls.b2b + cls.b2c).write({"company_id": cls.company.id})
# Partner
self.partner1 = self.env.ref("base.res_partner_1")
cls.partner1 = cls.env.ref("base.res_partner_1")
# Products
self.product1 = self.env.ref("product.product_product_7")
self.product2 = self.env.ref("product.product_product_9")
self.product3 = self.env.ref("product.product_product_11")
cls.product1 = cls.env.ref("product.product_product_7")
cls.product2 = cls.env.ref("product.product_product_9")
cls.product3 = cls.env.ref("product.product_product_11")

# Payment methods
self.payment_method_manual_in = self.env.ref(
cls.payment_method_manual_in = cls.env.ref(
"account.account_payment_method_manual_in"
)

# Create user1
self.user_id = self.res_users_model.with_context(no_reset_password=True).create(
cls.user_id = cls.res_users_model.with_context(no_reset_password=True).create(
{
"name": "Test Account User",
"login": "user_1",
"password": "demo",
"email": "[email protected]",
"company_id": self.company.id,
"company_ids": [(4, self.company.id)],
"operating_unit_ids": [(4, self.b2b.id), (4, self.b2c.id)],
"groups_id": [(6, 0, [self.grp_acc_manager.id])],
"company_id": cls.company.id,
"company_ids": [(4, cls.company.id)],
"operating_unit_ids": [(4, cls.b2b.id), (4, cls.b2c.id)],
"groups_id": [(6, 0, [cls.grp_acc_manager.id])],
}
)
# Create cash - test account
user_type = self.env.ref("account.data_account_type_current_assets")
self.current_asset_account_id = self.account_model.create(
# user_type = cls.env.ref("account.data_account_type_current_assets")
cls.current_asset_account_id = cls.account_model.create(
{
"name": "Current asset - Test",
"code": "test_current_asset",
"user_type_id": user_type.id,
"company_id": self.company.id,
"code": "TEST",
"account_type": "asset_current",
"company_id": cls.company.id,
}
)
# Create Inter-OU Clearing - test account
user_type = self.env.ref("account.data_account_type_equity")
self.inter_ou_account_id = self.account_model.create(
# user_type = cls.env.ref("account.data_account_type_equity")
cls.inter_ou_account_id = cls.account_model.create(
{
"name": "Inter-OU Clearing",
"code": "test_inter_ou",
"user_type_id": user_type.id,
"company_id": self.company.id,
"code": "inter",
# "user_type_id": user_type.id,
"account_type": "equity",
"company_id": cls.company.id,
}
)
# Assign the Inter-OU Clearing account to the company
self.company.inter_ou_clearing_account_id = self.inter_ou_account_id.id
self.company.ou_is_self_balanced = True
cls.company.inter_ou_clearing_account_id = cls.inter_ou_account_id.id
cls.company.ou_is_self_balanced = True

# Create user2
self.user2_id = self.res_users_model.with_context(
no_reset_password=True
).create(
cls.user2_id = cls.res_users_model.with_context(no_reset_password=True).create(
{
"name": "Test Account User",
"login": "user_2",
"password": "demo",
"email": "[email protected]",
"company_id": self.company.id,
"company_ids": [(4, self.company.id)],
"operating_unit_ids": [(4, self.b2c.id)],
"groups_id": [(6, 0, [self.grp_acc_manager.id])],
"company_id": cls.company.id,
"company_ids": [(4, cls.company.id)],
"operating_unit_ids": [(4, cls.b2c.id)],
"groups_id": [(6, 0, [cls.grp_acc_manager.id])],
}
)

# Create a cash account 1
user_type = self.env.ref("account.data_account_type_liquidity")
self.cash1_account_id = self.account_model.create(
# user_type = cls.env.ref("account.data_account_type_liquidity")
cls.cash1_account_id = cls.account_model.create(
{
"name": "Cash 1 - Test",
"code": "test_cash_1",
"user_type_id": user_type.id,
"company_id": self.company.id,
"code": "testcash1",
# "user_type_id": user_type.id,
"account_type": "asset_cash",
"company_id": cls.company.id,
}
)

# Create a journal for cash account 1, associated to the main
# operating unit
self.cash_journal_ou1 = self.journal_model.create(
cls.cash_journal_ou1 = cls.journal_model.create(
{
"name": "Cash Journal 1 - Test",
"code": "cash1",
"type": "cash",
"company_id": self.company.id,
"default_account_id": self.cash1_account_id.id,
"operating_unit_id": self.ou1.id,
"company_id": cls.company.id,
"default_account_id": cls.cash1_account_id.id,
"operating_unit_id": cls.ou1.id,
}
)
# Create a cash account 2
user_type = self.env.ref("account.data_account_type_liquidity")
self.cash2_account_id = self.account_model.create(
# user_type = cls.env.ref("account.data_account_type_liquidity")
cls.cash2_account_id = cls.account_model.create(
{
"name": "Cash 2 - Test",
"code": "cash2",
"user_type_id": user_type.id,
"company_id": self.company.id,
"account_type": "liability_payable",
"company_id": cls.company.id,
}
)

# Create a journal for cash account 2, associated to the operating
# unit B2B
self.cash2_journal_b2b = self.journal_model.create(
cls.cash2_journal_b2b = cls.journal_model.create(
{
"name": "Cash Journal 2 - Test",
"code": "test_cash_2",
"code": "testcash2",
"type": "cash",
"company_id": self.company.id,
"default_account_id": self.cash2_account_id.id,
"operating_unit_id": self.b2b.id,
"company_id": cls.company.id,
"default_account_id": cls.cash2_account_id.id,
"operating_unit_id": cls.b2b.id,
}
)

Expand All @@ -161,16 +162,19 @@ def _prepare_invoice(self, operating_unit_id, name="Test Supplier Invoice"):
]
# Prepare invoice lines
lines = []
acc_type = self.env.ref("account.data_account_type_expenses")
# acc_type = self.env.ref("account.data_account_type_expenses")

for product, qty in line_products:
line_values = {
"name": product.name,
"product_id": product.id,
"quantity": qty,
"price_unit": 50,
"account_id": self.env["account.account"]
.search([("user_type_id", "=", acc_type.id)], limit=1)
.search([("account_type", "=", "expense")], limit=1)
.id,
# Adding this line so the taxes are explicitly excluded from the lines
"tax_ids": [],
}
lines.append((0, 0, line_values))
inv_vals = {
Expand Down
31 changes: 9 additions & 22 deletions account_operating_unit/tests/test_cross_ou_journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

@tagged("post_install", "-at_install")
class TestCrossOuJournalEntry(test_ou.TestAccountOperatingUnit):
def setUp(self):
super().setUp()
@classmethod
def setUpClass(cls):
super().setUpClass()

def _check_balance(self, account_id, acc_type="clearing"):
# Check balance for all operating units
Expand Down Expand Up @@ -93,37 +94,23 @@ def test_cross_ou_journal_entry(self):
move_vals.update(
{"journal_id": journal_ids and journal_ids.id, "line_ids": lines}
)
move = self.move_model.with_user(self.user_id.id).create(move_vals)
move = (
self.move_model.with_user(self.user_id.id)
.with_context(check_move_validity=False)
.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 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.code = "bankcode"
f.save()

Check warning on line 116 in account_operating_unit/tests/test_cross_ou_journal_entry.py

View check run for this annotation

Codecov / codecov/patch

account_operating_unit/tests/test_cross_ou_journal_entry.py#L116

Added line #L116 was not covered by tests
Loading

0 comments on commit 2a84a5d

Please sign in to comment.