2019-10-31 07:20:20 +01:00
|
|
|
# Copyright 2016-19 ACSONE SA/NV
|
2021-02-22 18:39:52 +01:00
|
|
|
# Copyright 2019 ForgeFlow S.L.
|
2016-08-25 15:26:49 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
2019-10-31 07:20:20 +01:00
|
|
|
import time
|
2016-08-25 15:26:49 +02:00
|
|
|
from datetime import date
|
2021-02-22 17:48:48 +01:00
|
|
|
|
2017-03-23 07:52:07 +01:00
|
|
|
from dateutil import relativedelta
|
|
|
|
|
2021-02-22 17:48:48 +01:00
|
|
|
from odoo.tests.common import TransactionCase
|
|
|
|
|
2017-03-23 07:52:07 +01:00
|
|
|
from odoo.addons.queue_job.job import Job
|
|
|
|
|
2016-08-25 15:26:49 +02:00
|
|
|
|
|
|
|
class TestAccountAssetBatchCompute(TransactionCase):
|
2023-03-13 11:16:44 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
cls.wiz_obj = cls.env["account.asset.compute"]
|
|
|
|
cls.asset_model = cls.env["account.asset"]
|
|
|
|
cls.asset_profile_model = cls.env["account.asset.profile"]
|
|
|
|
cls.account = cls.env["account.account"].create(
|
2021-02-22 17:48:48 +01:00
|
|
|
{
|
|
|
|
"name": "Test account",
|
|
|
|
"code": "TAC",
|
2023-03-13 11:16:44 +01:00
|
|
|
"account_type": "liability_payable",
|
2021-02-22 17:48:48 +01:00
|
|
|
}
|
|
|
|
)
|
2023-03-13 11:16:44 +01:00
|
|
|
cls.journal = cls.env["account.journal"].create(
|
2021-02-22 18:39:52 +01:00
|
|
|
{"name": "Test Journal", "code": "TJ", "type": "general"}
|
2021-02-22 17:48:48 +01:00
|
|
|
)
|
2023-03-13 11:16:44 +01:00
|
|
|
cls.profile = cls.asset_profile_model.create(
|
2021-02-22 17:48:48 +01:00
|
|
|
{
|
2023-03-13 11:16:44 +01:00
|
|
|
"account_expense_depreciation_id": cls.account.id,
|
|
|
|
"account_asset_id": cls.account.id,
|
|
|
|
"account_depreciation_id": cls.account.id,
|
|
|
|
"journal_id": cls.journal.id,
|
2021-02-22 17:48:48 +01:00
|
|
|
"name": "Test",
|
|
|
|
}
|
|
|
|
)
|
2023-03-13 11:16:44 +01:00
|
|
|
cls.asset01 = cls.asset_model.create(
|
2021-02-22 17:48:48 +01:00
|
|
|
{
|
|
|
|
"name": "test asset",
|
2023-03-13 11:16:44 +01:00
|
|
|
"profile_id": cls.profile.id,
|
2021-02-22 17:48:48 +01:00
|
|
|
"purchase_value": 1000,
|
|
|
|
"salvage_value": 0,
|
|
|
|
"date_start": time.strftime("2003-01-01"),
|
|
|
|
"method_time": "year",
|
|
|
|
"method_number": 1,
|
|
|
|
"method_period": "month",
|
|
|
|
"prorata": False,
|
|
|
|
}
|
|
|
|
)
|
2016-08-25 15:26:49 +02:00
|
|
|
today = date.today()
|
|
|
|
first_day_of_month = date(today.year, today.month, 1)
|
2023-03-13 11:16:44 +01:00
|
|
|
cls.nextmonth = first_day_of_month + relativedelta.relativedelta(months=1)
|
|
|
|
cls.asset01.date_start = first_day_of_month
|
2016-08-25 15:26:49 +02:00
|
|
|
|
2021-02-22 18:39:52 +01:00
|
|
|
def test_no_batch_processing(self):
|
2021-02-22 17:48:48 +01:00
|
|
|
wiz = self.wiz_obj.create(
|
|
|
|
{"batch_processing": False, "date_end": self.nextmonth}
|
|
|
|
)
|
2016-08-25 15:26:49 +02:00
|
|
|
# I check if this asset is draft
|
2021-02-22 17:48:48 +01:00
|
|
|
self.assertEqual(self.asset01.state, "draft")
|
2016-08-25 15:26:49 +02:00
|
|
|
# I confirm this asset
|
|
|
|
self.asset01.validate()
|
|
|
|
# I check if this asset is running
|
2021-02-22 17:48:48 +01:00
|
|
|
self.assertEqual(self.asset01.state, "open")
|
2016-08-25 15:26:49 +02:00
|
|
|
self.asset01.compute_depreciation_board()
|
|
|
|
# I check that there is no depreciation line
|
2021-02-22 17:48:48 +01:00
|
|
|
depreciation_line = self.asset01.depreciation_line_ids.filtered(
|
|
|
|
lambda r: r.type == "depreciate" and r.move_id
|
|
|
|
)
|
2016-08-25 15:26:49 +02:00
|
|
|
self.assertTrue(len(depreciation_line) == 0)
|
|
|
|
wiz.asset_compute()
|
2021-02-22 17:48:48 +01:00
|
|
|
depreciation_line = self.asset01.depreciation_line_ids.filtered(
|
|
|
|
lambda r: r.type == "depreciate" and r.move_id
|
|
|
|
)
|
2016-08-25 15:26:49 +02:00
|
|
|
self.assertTrue(len(depreciation_line) == 1)
|
|
|
|
|
2021-02-22 18:39:52 +01:00
|
|
|
def test_batch_processing(self):
|
2021-02-22 17:48:48 +01:00
|
|
|
wiz = self.wiz_obj.create(
|
|
|
|
{"batch_processing": True, "date_end": self.nextmonth}
|
|
|
|
)
|
2016-08-25 15:26:49 +02:00
|
|
|
# I check if this asset is draft
|
2021-02-22 17:48:48 +01:00
|
|
|
self.assertEqual(self.asset01.state, "draft")
|
2016-08-25 15:26:49 +02:00
|
|
|
# I confirm this asset
|
|
|
|
self.asset01.validate()
|
|
|
|
# I check if this asset is running
|
2021-02-22 17:48:48 +01:00
|
|
|
self.assertEqual(self.asset01.state, "open")
|
2016-08-25 15:26:49 +02:00
|
|
|
self.asset01.compute_depreciation_board()
|
|
|
|
# I check that there is no depreciation line
|
2021-02-22 17:48:48 +01:00
|
|
|
depreciation_line = self.asset01.depreciation_line_ids.filtered(
|
|
|
|
lambda r: r.type == "depreciate" and r.move_id
|
|
|
|
)
|
2016-08-25 15:26:49 +02:00
|
|
|
self.assertTrue(len(depreciation_line) == 0)
|
2019-11-03 22:08:09 +01:00
|
|
|
wiz.with_context(test_queue_job_no_delay=False).asset_compute()
|
2021-02-22 17:48:48 +01:00
|
|
|
depreciation_line = self.asset01.depreciation_line_ids.filtered(
|
|
|
|
lambda r: r.type == "depreciate" and r.move_id
|
|
|
|
)
|
2017-03-23 07:52:07 +01:00
|
|
|
self.assertTrue(len(depreciation_line) == 0)
|
2021-02-22 17:48:48 +01:00
|
|
|
job_name = "Creating jobs to create moves for assets to %s" % (self.nextmonth)
|
|
|
|
jobs = self.env["queue.job"].search(
|
|
|
|
[("name", "=", job_name)], order="date_created desc", limit=1
|
|
|
|
)
|
2021-02-22 18:39:52 +01:00
|
|
|
self.assertEqual(
|
|
|
|
jobs.job_function_id,
|
|
|
|
self.env.ref(
|
|
|
|
"account_asset_batch_compute."
|
|
|
|
"job_function_account_asset_compute_asset_compute"
|
|
|
|
),
|
|
|
|
)
|
2017-03-23 07:52:07 +01:00
|
|
|
self.assertTrue(len(jobs) == 1)
|
|
|
|
job = Job.load(self.env, jobs.uuid)
|
2019-10-31 07:20:20 +01:00
|
|
|
# perform job
|
2017-03-23 07:52:07 +01:00
|
|
|
job.perform()
|
2021-02-22 17:48:48 +01:00
|
|
|
depreciation_line = self.asset01.depreciation_line_ids.filtered(
|
|
|
|
lambda r: r.type == "depreciate" and r.move_id
|
|
|
|
)
|
2017-03-23 07:52:07 +01:00
|
|
|
self.assertTrue(len(depreciation_line) == 0)
|
2021-02-22 17:48:48 +01:00
|
|
|
job_name = "Creating move for asset with id {} to {}".format(
|
2022-01-12 10:04:42 +01:00
|
|
|
self.asset01.id,
|
|
|
|
self.nextmonth,
|
2021-02-22 17:48:48 +01:00
|
|
|
)
|
|
|
|
jobs = self.env["queue.job"].search(
|
|
|
|
[("name", "=", job_name)], order="date_created desc", limit=1
|
|
|
|
)
|
2017-03-23 07:52:07 +01:00
|
|
|
self.assertTrue(len(jobs) == 1)
|
2021-02-22 18:39:52 +01:00
|
|
|
self.assertEqual(
|
|
|
|
jobs.job_function_id,
|
|
|
|
self.env.ref(
|
|
|
|
"account_asset_batch_compute.job_function_account_asset_compute_entries"
|
|
|
|
),
|
|
|
|
)
|
2017-03-23 07:52:07 +01:00
|
|
|
job = Job.load(self.env, jobs.uuid)
|
|
|
|
job.perform()
|
2021-02-22 17:48:48 +01:00
|
|
|
depreciation_line = self.asset01.depreciation_line_ids.filtered(
|
|
|
|
lambda r: r.type == "depreciate" and r.move_id
|
|
|
|
)
|
2022-05-06 10:47:18 +02:00
|
|
|
self.assertEqual(len(depreciation_line), 1)
|