2
0

[IMP] account_asset_batch_compute: black,isort,prettier

This commit is contained in:
Alba Riera 2021-02-22 17:48:48 +01:00 committed by Javier Iniesta
parent cc2733d2c6
commit ec7d5b5906
6 changed files with 136 additions and 123 deletions

View File

@ -2,20 +2,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{ {
'name': 'Account Asset Batch Compute', "name": "Account Asset Batch Compute",
'summary': """ "summary": """
Add the possibility to compute assets in batch""", Add the possibility to compute assets in batch""",
'version': '12.0.1.0.0', "version": "12.0.1.0.0",
'license': 'AGPL-3', "license": "AGPL-3",
'author': 'ACSONE SA/NV,' "author": "ACSONE SA/NV," "Eficent," "Odoo Community Association (OCA)",
'Eficent,' "website": "https://github.com/OCA/account-financial-tools",
'Odoo Community Association (OCA)', "depends": ["account_asset_management", "queue_job",],
'website': 'https://github.com/OCA/account-financial-tools', "data": ["wizards/account_asset_compute_views.xml",],
'depends': [
'account_asset_management',
'queue_job',
],
'data': [
'wizards/account_asset_compute_views.xml',
],
} }

View File

@ -1,37 +1,40 @@
# Copyright 2016 ACSONE SA/NV # Copyright 2016 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models, _
import logging import logging
from odoo import _, api, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
try: try:
from odoo.addons.queue_job.job import job from odoo.addons.queue_job.job import job
except ImportError: except ImportError:
_logger.debug('Can not `import queue_job`.') _logger.debug("Can not `import queue_job`.")
class AccountAsset(models.Model): class AccountAsset(models.Model):
_inherit = 'account.asset' _inherit = "account.asset"
@api.multi @api.multi
@job(default_channel='root.account_asset_batch_compute') @job(default_channel="root.account_asset_batch_compute")
def _compute_entries(self, date_end, check_triggers=False): def _compute_entries(self, date_end, check_triggers=False):
if self.env.context.get( if self.env.context.get(
'asset_batch_processing', False "asset_batch_processing", False
) and not self.env.context.get('test_queue_job_no_delay', False): ) and not self.env.context.get("test_queue_job_no_delay", False):
results = [] results = []
log_error = '' log_error = ""
for record in self: for record in self:
description =\ description = _("Creating move for asset with id %s to %s") % (
_("Creating move for asset with id %s to %s") %\ record.id,
(record.id, date_end) date_end,
record.with_delay( )
description=description)._compute_entries( record.with_delay(description=description)._compute_entries(
date_end, check_triggers=check_triggers) date_end, check_triggers=check_triggers
)
return results, log_error return results, log_error
else: else:
return super(AccountAsset, self)._compute_entries( return super(AccountAsset, self)._compute_entries(
date_end, check_triggers=check_triggers) date_end, check_triggers=check_triggers
)

View File

@ -1,4 +1,3 @@
Add the possibility to compute assets in batch. Add the possibility to compute assets in batch.
This module adds a flag on compute assets wizard in order to execute This module adds a flag on compute assets wizard in order to execute
this process in batch. this process in batch.

View File

@ -2,119 +2,134 @@
# Copyright 2019 Eficent Business and IT Consulting Services, S.L. # Copyright 2019 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import time import time
from odoo.tests.common import TransactionCase
from datetime import date from datetime import date
from dateutil import relativedelta from dateutil import relativedelta
from odoo.tests.common import TransactionCase
from odoo.addons.queue_job.job import Job from odoo.addons.queue_job.job import Job
class TestAccountAssetBatchCompute(TransactionCase): class TestAccountAssetBatchCompute(TransactionCase):
def setUp(self): def setUp(self):
super(TestAccountAssetBatchCompute, self).setUp() super(TestAccountAssetBatchCompute, self).setUp()
self.wiz_obj = self.env['account.asset.compute'] self.wiz_obj = self.env["account.asset.compute"]
self.asset_model = self.env['account.asset'] self.asset_model = self.env["account.asset"]
self.asset_profile_model = self.env['account.asset.profile'] self.asset_profile_model = self.env["account.asset.profile"]
self.account_account_type_model = self.env['account.account.type'] self.account_account_type_model = self.env["account.account.type"]
self.account_type_regular = self.account_account_type_model.create({ self.account_type_regular = self.account_account_type_model.create(
'name': 'Test Regular', {"name": "Test Regular", "type": "other",}
'type': 'other', )
}) self.account = self.env["account.account"].create(
self.account = self.env['account.account'].create({ {
'name': 'Test account', "name": "Test account",
'code': 'TAC', "code": "TAC",
'user_type_id': self.account_type_regular.id, "user_type_id": self.account_type_regular.id,
}) }
self.journal = self.env['account.journal'].create({ )
'name': 'Test Journal', self.journal = self.env["account.journal"].create(
'code': 'TJ', {"name": "Test Journal", "code": "TJ", "type": "general",}
'type': 'general', )
}) self.profile = self.asset_profile_model.create(
self.profile = self.asset_profile_model.create({ {
'account_expense_depreciation_id': self.account.id, "account_expense_depreciation_id": self.account.id,
'account_asset_id': self.account.id, "account_asset_id": self.account.id,
'account_depreciation_id': self.account.id, "account_depreciation_id": self.account.id,
'journal_id': self.journal.id, "journal_id": self.journal.id,
'name': "Test", "name": "Test",
}) }
self.fiscal_year = self.env['account.fiscal.year'].create({ )
'name': 'FY', self.fiscal_year = self.env["account.fiscal.year"].create(
'date_from': time.strftime('2019-01-01'), {
'date_to': time.strftime('2019-12-31'), "name": "FY",
}) "date_from": time.strftime("2019-01-01"),
self.asset01 = self.asset_model.create({ "date_to": time.strftime("2019-12-31"),
'name': 'test asset', }
'profile_id': self.profile.id, )
'purchase_value': 1000, self.asset01 = self.asset_model.create(
'salvage_value': 0, {
'date_start': time.strftime('2003-01-01'), "name": "test asset",
'method_time': 'year', "profile_id": self.profile.id,
'method_number': 1, "purchase_value": 1000,
'method_period': 'month', "salvage_value": 0,
'prorata': False, "date_start": time.strftime("2003-01-01"),
}) "method_time": "year",
"method_number": 1,
"method_period": "month",
"prorata": False,
}
)
today = date.today() today = date.today()
first_day_of_month = date(today.year, today.month, 1) first_day_of_month = date(today.year, today.month, 1)
self.nextmonth = first_day_of_month + relativedelta.relativedelta( self.nextmonth = first_day_of_month + relativedelta.relativedelta(months=1)
months=1)
self.asset01.date_start = first_day_of_month self.asset01.date_start = first_day_of_month
def test_1(self): def test_1(self):
wiz = self.wiz_obj.create({'batch_processing': False, wiz = self.wiz_obj.create(
'date_end': self.nextmonth}) {"batch_processing": False, "date_end": self.nextmonth}
)
# I check if this asset is draft # I check if this asset is draft
self.assertEqual(self.asset01.state, 'draft') self.assertEqual(self.asset01.state, "draft")
# I confirm this asset # I confirm this asset
self.asset01.validate() self.asset01.validate()
# I check if this asset is running # I check if this asset is running
self.assertEqual(self.asset01.state, 'open') self.assertEqual(self.asset01.state, "open")
self.asset01.compute_depreciation_board() self.asset01.compute_depreciation_board()
# I check that there is no depreciation line # I check that there is no depreciation line
depreciation_line = self.asset01.depreciation_line_ids\ depreciation_line = self.asset01.depreciation_line_ids.filtered(
.filtered(lambda r: r.type == 'depreciate' and r.move_id) lambda r: r.type == "depreciate" and r.move_id
)
self.assertTrue(len(depreciation_line) == 0) self.assertTrue(len(depreciation_line) == 0)
wiz.asset_compute() wiz.asset_compute()
depreciation_line = self.asset01.depreciation_line_ids\ depreciation_line = self.asset01.depreciation_line_ids.filtered(
.filtered(lambda r: r.type == 'depreciate' and r.move_id) lambda r: r.type == "depreciate" and r.move_id
)
self.assertTrue(len(depreciation_line) == 1) self.assertTrue(len(depreciation_line) == 1)
def test_2(self): def test_2(self):
wiz = self.wiz_obj.create({'batch_processing': True, wiz = self.wiz_obj.create(
'date_end': self.nextmonth}) {"batch_processing": True, "date_end": self.nextmonth}
)
# I check if this asset is draft # I check if this asset is draft
self.assertEqual(self.asset01.state, 'draft') self.assertEqual(self.asset01.state, "draft")
# I confirm this asset # I confirm this asset
self.asset01.validate() self.asset01.validate()
# I check if this asset is running # I check if this asset is running
self.assertEqual(self.asset01.state, 'open') self.assertEqual(self.asset01.state, "open")
self.asset01.compute_depreciation_board() self.asset01.compute_depreciation_board()
# I check that there is no depreciation line # I check that there is no depreciation line
depreciation_line = self.asset01.depreciation_line_ids\ depreciation_line = self.asset01.depreciation_line_ids.filtered(
.filtered(lambda r: r.type == 'depreciate' and r.move_id) lambda r: r.type == "depreciate" and r.move_id
)
self.assertTrue(len(depreciation_line) == 0) self.assertTrue(len(depreciation_line) == 0)
wiz.with_context(test_queue_job_no_delay=False).asset_compute() wiz.with_context(test_queue_job_no_delay=False).asset_compute()
depreciation_line = self.asset01.depreciation_line_ids \ depreciation_line = self.asset01.depreciation_line_ids.filtered(
.filtered(lambda r: r.type == 'depreciate' and r.move_id) lambda r: r.type == "depreciate" and r.move_id
)
self.assertTrue(len(depreciation_line) == 0) self.assertTrue(len(depreciation_line) == 0)
job_name = "Creating jobs to create moves for assets to %s" % ( job_name = "Creating jobs to create moves for assets to %s" % (self.nextmonth)
self.nextmonth) jobs = self.env["queue.job"].search(
jobs = self.env['queue.job'].search( [("name", "=", job_name)], order="date_created desc", limit=1
[('name', '=', job_name)], order='date_created desc', limit=1) )
self.assertTrue(len(jobs) == 1) self.assertTrue(len(jobs) == 1)
job = Job.load(self.env, jobs.uuid) job = Job.load(self.env, jobs.uuid)
# perform job # perform job
job.perform() job.perform()
depreciation_line = self.asset01.depreciation_line_ids \ depreciation_line = self.asset01.depreciation_line_ids.filtered(
.filtered(lambda r: r.type == 'depreciate' and r.move_id) lambda r: r.type == "depreciate" and r.move_id
)
self.assertTrue(len(depreciation_line) == 0) self.assertTrue(len(depreciation_line) == 0)
job_name = "Creating move for asset with id %s to %s" % ( job_name = "Creating move for asset with id {} to {}".format(
self.asset01.id, self.nextmonth) self.asset01.id, self.nextmonth,
jobs = self.env['queue.job'].search( )
[('name', '=', job_name)], order='date_created desc', limit=1) jobs = self.env["queue.job"].search(
[("name", "=", job_name)], order="date_created desc", limit=1
)
self.assertTrue(len(jobs) == 1) self.assertTrue(len(jobs) == 1)
job = Job.load(self.env, jobs.uuid) job = Job.load(self.env, jobs.uuid)
job.perform() job.perform()
depreciation_line = self.asset01.depreciation_line_ids \ depreciation_line = self.asset01.depreciation_line_ids.filtered(
.filtered(lambda r: r.type == 'depreciate' and r.move_id) lambda r: r.type == "depreciate" and r.move_id
)
self.assertEquals(len(depreciation_line), 1) self.assertEquals(len(depreciation_line), 1)

View File

@ -1,38 +1,39 @@
# Copyright 2016-2017 ACSONE SA/NV # Copyright 2016-2017 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models, _
import logging import logging
from odoo import _, api, fields, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
try: try:
from odoo.addons.queue_job.job import job from odoo.addons.queue_job.job import job
except ImportError: except ImportError:
_logger.debug('Can not `import queue_job`.') _logger.debug("Can not `import queue_job`.")
class AccountAssetCompute(models.TransientModel): class AccountAssetCompute(models.TransientModel):
_inherit = 'account.asset.compute' _inherit = "account.asset.compute"
batch_processing = fields.Boolean() batch_processing = fields.Boolean()
@api.multi @api.multi
@job(default_channel='root.account_asset_batch_compute') @job(default_channel="root.account_asset_batch_compute")
def asset_compute(self): def asset_compute(self):
self.ensure_one() self.ensure_one()
if not self.batch_processing: if not self.batch_processing:
return super(AccountAssetCompute, self).asset_compute() return super(AccountAssetCompute, self).asset_compute()
if not self.env.context.get('job_uuid') and not self.env.context.get( if not self.env.context.get("job_uuid") and not self.env.context.get(
'test_queue_job_no_delay' "test_queue_job_no_delay"
): ):
description = \ description = _("Creating jobs to create moves for assets to %s") % (
_("Creating jobs to create moves for assets to %s") % ( self.date_end,
self.date_end,) )
job = self.with_delay(description=description).asset_compute() job = self.with_delay(description=description).asset_compute()
return u'Job created with uuid %s' % (job.uuid,) return u"Job created with uuid {}".format(job.uuid)
else: else:
return super( return super(
AccountAssetCompute, self.with_context( AccountAssetCompute, self.with_context(asset_batch_processing=True)
asset_batch_processing=True)).asset_compute() ).asset_compute()

View File

@ -1,18 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2016 ACSONE SA/NV <!-- Copyright 2016 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo> <odoo>
<record model="ir.ui.view" id="account_asset_compute_view_form"> <record model="ir.ui.view" id="account_asset_compute_view_form">
<field name="name">account.asset.compute (in account_asset_batch_compute)</field> <field
name="name"
>account.asset.compute (in account_asset_batch_compute)</field>
<field name="model">account.asset.compute</field> <field name="model">account.asset.compute</field>
<field name="inherit_id" ref="account_asset_management.account_asset_compute_view_form"/> <field
name="inherit_id"
ref="account_asset_management.account_asset_compute_view_form"
/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="date_end" position="after"> <field name="date_end" position="after">
<field name="batch_processing" /> <field name="batch_processing" />
</field> </field>
</field> </field>
</record> </record>
</odoo> </odoo>