diff --git a/account_asset_compute_batch/__init__.py b/account_asset_compute_batch/__init__.py new file mode 100644 index 000000000..9b4296142 --- /dev/null +++ b/account_asset_compute_batch/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/account_asset_compute_batch/__manifest__.py b/account_asset_compute_batch/__manifest__.py new file mode 100644 index 000000000..094058e77 --- /dev/null +++ b/account_asset_compute_batch/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Assets - Compute Depre. in Batch", + "version": "14.0.1.0.0", + "license": "AGPL-3", + "depends": ["account_asset_management"], + "author": "Ecosoft, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/account-financial-tools", + "category": "Accounting & Finance", + "data": [ + "data/service_cron.xml", + "security/account_asset_security.xml", + "security/ir.model.access.csv", + "wizard/account_asset_compute.xml", + "views/account_asset_compute_batch.xml", + ], +} diff --git a/account_asset_compute_batch/data/service_cron.xml b/account_asset_compute_batch/data/service_cron.xml new file mode 100644 index 000000000..0dffc9de6 --- /dev/null +++ b/account_asset_compute_batch/data/service_cron.xml @@ -0,0 +1,19 @@ + + + + Asset Compute Batch; Post draft batches with auto_compute set to True up to today + 1 + days + -1 + + + + model._autocompute_draft_batches() + code + + diff --git a/account_asset_compute_batch/models/__init__.py b/account_asset_compute_batch/models/__init__.py new file mode 100644 index 000000000..d2fa61daf --- /dev/null +++ b/account_asset_compute_batch/models/__init__.py @@ -0,0 +1,4 @@ +from . import account_asset +from . import account_asset_line +from . import account_move +from . import account_asset_compute_batch diff --git a/account_asset_compute_batch/models/account_asset.py b/account_asset_compute_batch/models/account_asset.py new file mode 100644 index 000000000..081290cb2 --- /dev/null +++ b/account_asset_compute_batch/models/account_asset.py @@ -0,0 +1,16 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class AccountAsset(models.Model): + _inherit = "account.asset" + + def _get_asset_line_domain(self, date_end): + domain = super()._get_asset_line_domain(date_end) + if self.env.context.get("compute_profile_ids"): + domain.append( + ("asset_id.profile_id", "in", self.env.context["compute_profile_ids"]) + ) + return domain diff --git a/account_asset_compute_batch/models/account_asset_compute_batch.py b/account_asset_compute_batch/models/account_asset_compute_batch.py new file mode 100644 index 000000000..ed27749be --- /dev/null +++ b/account_asset_compute_batch/models/account_asset_compute_batch.py @@ -0,0 +1,249 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging +from sys import exc_info +from traceback import format_exception + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + +_logger = logging.getLogger(__name__) + + +class AssetComputeBatch(models.Model): + _name = "account.asset.compute.batch" + _inherit = ["mail.thread", "mail.activity.mixin"] + _description = "Compute Depreciation Batch" + _check_company_auto = True + + name = fields.Char( + string="Name", + required=True, + readonly=True, + states={"draft": [("readonly", False)]}, + ) + description = fields.Char( + string="Description", + required=True, + readonly=True, + states={"draft": [("readonly", False)]}, + ) + date_end = fields.Date( + string="Date", + required=True, + default=fields.Date.today, + readonly=True, + states={"draft": [("readonly", False)]}, + help="All depreciation lines prior to this date will be computed", + ) + note = fields.Text(string="Exception Error") + profile_ids = fields.Many2many( + comodel_name="account.asset.profile", + string="Profiles", + readonly=True, + states={"draft": [("readonly", False)]}, + help="Selected asset to run depreciation. Run all profiles if left blank.", + ) + company_id = fields.Many2one( + comodel_name="res.company", + string="Company", + readonly=True, + default=lambda self: self.env.company, + ) + delay_post = fields.Boolean( + string="Delay Posting", + readonly=True, + states={"draft": [("readonly", False)]}, + help="Dalay account posting of newly created journaly entries, " + "by setting auto_post=True, to be posted by cron job", + ) + auto_compute = fields.Boolean( + string="Auto Compute", + readonly=True, + states={"draft": [("readonly", False)]}, + help="Auto compute draft batches with 'Date' up to today, by cron job", + ) + move_line_ids = fields.One2many( + comodel_name="account.move.line", + inverse_name="compute_batch_id", + readonly=True, + ) + state = fields.Selection( + selection=[ + ("draft", "Draft"), + ("computed", "Computed"), + ("exception", "Exception"), + ], + default="draft", + tracking=True, + index=True, + required=True, + readonly=True, + ) + profile_report = fields.One2many( + comodel_name="account.asset.compute.batch.profile.report", + inverse_name="compute_batch_id", + ) + currency_id = fields.Many2one( + comodel_name="res.currency", + default=lambda self: self.env.company.currency_id, + ) + depre_amount = fields.Monetary( + string="Depreciation Amount", + compute="_compute_depre_amount", + ) + _sql_constraints = [ + ("name_uniq", "UNIQUE(name)", "Batch name must be unique!"), + ] + + @api.depends("state") + def _compute_depre_amount(self): + res = self.env["account.move.line"].read_group( + [("compute_batch_id", "in", self.ids)], + ["compute_batch_id", "debit"], + ["compute_batch_id"], + ) + res = {x["compute_batch_id"][0]: x["debit"] for x in res} + for rec in self: + rec.depre_amount = res.get(rec.id) + + def unlink(self): + if self.filtered(lambda l: l.state != "draft"): + raise ValidationError(_("Only draft batch can be deleted!")) + return super().unlink() + + def action_compute(self): + for batch in self: + assets = self.env["account.asset"].search([("state", "=", "open")]) + created_move_ids, error_log = assets.with_context( + compute_batch_id=batch.id, + compute_profile_ids=batch.profile_ids.ids, + delay_post=batch.delay_post, + )._compute_entries(self.date_end, check_triggers=True) + if error_log: + batch.note = _("Compute Assets errors") + ":\n" + error_log + batch.state = "exception" + else: + batch.state = "computed" + + def open_move_lines(self): + self.ensure_one() + action = { + "name": _("Journal Items"), + "view_type": "tree", + "view_mode": "list,form", + "res_model": "account.move.line", + "type": "ir.actions.act_window", + "context": {"search_default_group_by_account": True}, + "domain": [("id", "in", self.move_line_ids.ids)], + } + return action + + def open_moves(self): + self.ensure_one() + action = { + "name": _("Journal Entries"), + "view_type": "tree", + "view_mode": "list,form", + "res_model": "account.move", + "type": "ir.actions.act_window", + "context": {}, + "domain": [("id", "in", self.move_line_ids.mapped("move_id").ids)], + } + return action + + @api.model + def _autocompute_draft_batches(self): + """This method is called from a cron job. + It is used to auto compute account.asset.compute.batch with auto_compute=True + """ + records = self.search( + [ + ("state", "=", "draft"), + ("date_end", "<=", fields.Date.context_today(self)), + ("auto_compute", "=", True), + ] + ) + for ids in self.env.cr.split_for_in_conditions(records.ids, size=1000): + batches = self.browse(ids) + try: + with self.env.cr.savepoint(): + batches.action_compute() + except Exception: + exc_info()[0] + tb = "".join(format_exception(*exc_info())) + batch_ref = ", ".join(batches.mapped("name")) + error_msg = _("Error while processing batches '%s': \n\n%s") % ( + batch_ref, + tb, + ) + _logger.error("%s, %s", self._name, error_msg) + + +class AccountAssetComputeBatchProfileReport(models.Model): + _name = "account.asset.compute.batch.profile.report" + _description = "Depreciation Amount by Profile" + _auto = False + _order = "profile_id desc" + + compute_batch_id = fields.Many2one( + comodel_name="account.asset.compute.batch", + readonly=True, + ) + profile_id = fields.Many2one( + string="Asset Profile", + comodel_name="account.asset.profile", + readonly=True, + ) + currency_id = fields.Many2one( + comodel_name="res.currency", + readonly=True, + ) + amount = fields.Monetary( + string="Amount", + readonly=True, + ) + + @property + def _table_query(self): + return "%s %s %s %s" % ( + self._select(), + self._from(), + self._where(), + self._group_by(), + ) + + @api.model + def _select(self): + return """ + SELECT + min(ml.id) as id, + compute_batch_id, + p.id as profile_id, + currency_id, + sum(debit) as amount + """ + + @api.model + def _from(self): + return """ + FROM account_move_line ml + JOIN account_asset a on a.id = ml.asset_id + JOIN account_asset_profile p on p.id = a.profile_id + """ + + @api.model + def _where(self): + return """ + WHERE + compute_batch_id IS NOT NULL + """ + + @api.model + def _group_by(self): + return """ + GROUP BY + compute_batch_id, + p.id, + currency_id + """ diff --git a/account_asset_compute_batch/models/account_asset_line.py b/account_asset_compute_batch/models/account_asset_line.py new file mode 100644 index 000000000..53de914a7 --- /dev/null +++ b/account_asset_compute_batch/models/account_asset_line.py @@ -0,0 +1,16 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class AccountAssetLine(models.Model): + _inherit = "account.asset.line" + + def _setup_move_line_data(self, depreciation_date, account, ml_type, move): + move_line_data = super()._setup_move_line_data( + depreciation_date, account, ml_type, move + ) + if self.env.context.get("compute_batch_id"): + move_line_data["compute_batch_id"] = self.env.context["compute_batch_id"] + return move_line_data diff --git a/account_asset_compute_batch/models/account_move.py b/account_asset_compute_batch/models/account_move.py new file mode 100644 index 000000000..ced1219ea --- /dev/null +++ b/account_asset_compute_batch/models/account_move.py @@ -0,0 +1,25 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class AccountMove(models.Model): + _inherit = "account.move" + + def action_post(self): + if self.env.context.get("delay_post"): + self.write({"auto_post": True}) + return False + return super().action_post() + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + compute_batch_id = fields.Many2one( + comodel_name="account.asset.compute.batch", + index=True, + ondelete="set null", + readonly=True, + ) diff --git a/account_asset_compute_batch/readme/CONTRIBUTORS.rst b/account_asset_compute_batch/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..28f80dc65 --- /dev/null +++ b/account_asset_compute_batch/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Ecosoft `_: + + * Kitti U. diff --git a/account_asset_compute_batch/readme/DESCRIPTION.rst b/account_asset_compute_batch/readme/DESCRIPTION.rst new file mode 100644 index 000000000..2609328fe --- /dev/null +++ b/account_asset_compute_batch/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module extend existing "Compute Asset" feature by allowing to create an Compute Asset Batch (record) to track the computation. diff --git a/account_asset_compute_batch/readme/USAGE.rst b/account_asset_compute_batch/readme/USAGE.rst new file mode 100644 index 000000000..a1b73ab7a --- /dev/null +++ b/account_asset_compute_batch/readme/USAGE.rst @@ -0,0 +1,14 @@ +There are 2 ways to create "Compute Asset Batch" + +1. On the Compute Assets wizards, choose "Create Batch" option, + 1.1 Type in batch name and description. + 1.2 Select asset profiles, to limit only some profiles to get computed. + 1.3 Option to "Delay Compute Asset", will only create Batch record for user to execute it later. + +2. Create Compute Asset Batch directly + 2.1 Select date for depreciation + 2.2 Type in batch name and descripton + 2.3 Select asset profiles, to limit only some profiles to get computed. + 2.4 Option to "Auto Compute" if you want to compute this batch by cron job. + 2.4 Option to "Delay Post" if you want to post journal entry by cron job. + 2.5 Click "Compute" button to compute asset. diff --git a/account_asset_compute_batch/security/account_asset_security.xml b/account_asset_compute_batch/security/account_asset_security.xml new file mode 100644 index 000000000..5115213c9 --- /dev/null +++ b/account_asset_compute_batch/security/account_asset_security.xml @@ -0,0 +1,12 @@ + + + + Asset Compute Batch multi-company + + + ['|', ('company_id', '=', False), ('company_id', 'in', company_ids)] + + + diff --git a/account_asset_compute_batch/security/ir.model.access.csv b/account_asset_compute_batch/security/ir.model.access.csv new file mode 100644 index 000000000..f3b696d42 --- /dev/null +++ b/account_asset_compute_batch/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_account_asset_compute_batch_invoice,account.asset.compute.batch,model_account_asset_compute_batch,account.group_account_invoice,1,1,1,1 +access_account_asset_compute_batch_profile_report,access_account_asset_compute_batch_profile_report,model_account_asset_compute_batch_profile_report,base.group_user,1,0,0,0 diff --git a/account_asset_compute_batch/static/description/icon.png b/account_asset_compute_batch/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/account_asset_compute_batch/static/description/icon.png differ diff --git a/account_asset_compute_batch/tests/__init__.py b/account_asset_compute_batch/tests/__init__.py new file mode 100644 index 000000000..a8f856b8c --- /dev/null +++ b/account_asset_compute_batch/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_asset_compute_batch diff --git a/account_asset_compute_batch/tests/test_account_asset_compute_batch.py b/account_asset_compute_batch/tests/test_account_asset_compute_batch.py new file mode 100644 index 000000000..0d48c7eb6 --- /dev/null +++ b/account_asset_compute_batch/tests/test_account_asset_compute_batch.py @@ -0,0 +1,165 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import time + +from freezegun import freeze_time + +from odoo.tests import tagged +from odoo.tests.common import Form + +from odoo.addons.account_asset_management.tests.test_account_asset_management import ( + TestAssetManagement, +) + + +@tagged("post_install", "-at_install") +class TestAssetComputeBatch(TestAssetManagement): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Create 3 assets from 2 profiles + cls.ict0 = cls.asset_model.create( + { + "state": "draft", + "method_time": "year", + "method_number": 3, + "method_period": "year", + "name": "Laptop", + "code": "PI00101", + "purchase_value": 1500.0, + "profile_id": cls.ict3Y.id, + "date_start": time.strftime("2000-01-01"), + } + ) + cls.ict1 = cls.asset_model.create( + { + "state": "draft", + "method_time": "year", + "method_number": 3, + "method_period": "year", + "name": "Monitor", + "code": "PI00102", + "purchase_value": 2100.0, + "profile_id": cls.ict3Y.id, + "date_start": time.strftime("2000-01-01"), + } + ) + # 2nd asset + cls.vehicle0 = cls.asset_model.create( + { + "state": "draft", + "method_time": "year", + "method_number": 5, + "method_period": "year", + "name": "CEO's Car", + "purchase_value": 12000.0, + "salvage_value": 2000.0, + "profile_id": cls.car5y.id, + "date_start": time.strftime("2000-01-01"), + } + ) + + def _create_compute_wizard(self, use_batch=False, delay_compute=False): + with Form(self.env["account.asset.compute"]) as f: + f.batch_name = "Test Batch" + f.description = "Compute asset with 2 profiles" + f.profile_ids.add(self.ict3Y) + f.profile_ids.add(self.car5y) + f.use_batch = use_batch + f.delay_compute = delay_compute + wiz = f.save() + return wiz + + @freeze_time("2000-12-31") + def test_01_asset_compute_batch_normal(self): + # Confirm 3 assets + self.ict0.validate() + self.assertEqual(self.ict0.depreciation_line_ids[1].amount, 500) + self.ict1.validate() + self.assertEqual(self.ict1.depreciation_line_ids[1].amount, 700) + self.vehicle0.validate() + self.assertEqual(self.vehicle0.depreciation_line_ids[1].amount, 2000) + # Compute Asset, no delay + wiz = self._create_compute_wizard(use_batch=True) + res = wiz.asset_compute() + batch = self.env["account.asset.compute.batch"].browse(res["res_id"]) + self.assertEqual(batch.state, "computed") + self.assertEqual(batch.depre_amount, 3200) + # Test summary amount by profile + batch.invalidate_cache() + self.assertEqual( + {x.profile_id: x.amount for x in batch.profile_report}, + {self.ict3Y: 1200, self.car5y: 2000}, + ) + # Test view moves + # 3 account.move + res = batch.open_moves() + self.assertEqual(len(res["domain"][0][2]), 3) + # 6 account.move.line + res = batch.open_move_lines() + self.assertEqual(len(res["domain"][0][2]), 6) + + @freeze_time("2000-12-31") + def test_02_asset_compute_batch_delay_compute(self): + # Confirm 2 assets + self.ict0.validate() + self.assertEqual(self.ict0.depreciation_line_ids[1].amount, 500) + self.vehicle0.validate() + self.assertEqual(self.vehicle0.depreciation_line_ids[1].amount, 2000) + # Compute Asset, with delay + wiz = self._create_compute_wizard(use_batch=True, delay_compute=True) + res = wiz.asset_compute() + batch = self.env["account.asset.compute.batch"].browse(res["res_id"]) + self.assertEqual(batch.state, "draft") + self.assertEqual(batch.depre_amount, 0) + # Batch is still draft, require to click compute + batch.action_compute() + self.assertEqual(batch.state, "computed") + self.assertEqual(batch.depre_amount, 2500) + + @freeze_time("2000-12-31") + def test_03_asset_compute_batch_delay_compute_delay_post(self): + # Confirm 2 assets + self.ict0.validate() + self.assertEqual(self.ict0.depreciation_line_ids[1].amount, 500) + self.vehicle0.validate() + self.assertEqual(self.vehicle0.depreciation_line_ids[1].amount, 2000) + # Compute Asset, with delay + wiz = self._create_compute_wizard(use_batch=True, delay_compute=True) + res = wiz.asset_compute() + batch = self.env["account.asset.compute.batch"].browse(res["res_id"]) + self.assertEqual(batch.state, "draft") + self.assertEqual(batch.depre_amount, 0) + batch.delay_post = True + # Batch is still draft, require to click compute + batch.action_compute() + self.assertEqual(batch.state, "computed") + self.assertEqual(batch.depre_amount, 2500) + # All account.move is flag as auto_post = True, and state in draft + self.assertTrue(all(batch.move_line_ids.mapped("move_id.auto_post"))) + self.assertTrue( + all( + state == "draft" + for state in batch.move_line_ids.mapped("move_id.state") + ) + ) + + @freeze_time("2000-12-31") + def test_04_asset_compute_batch_auto_compute(self): + # Confirm 2 assets + self.ict0.validate() + self.assertEqual(self.ict0.depreciation_line_ids[1].amount, 500) + self.vehicle0.validate() + self.assertEqual(self.vehicle0.depreciation_line_ids[1].amount, 2000) + # Compute Asset, with delay + wiz = self._create_compute_wizard(use_batch=True, delay_compute=True) + res = wiz.asset_compute() + batch = self.env["account.asset.compute.batch"].browse(res["res_id"]) + self.assertEqual(batch.state, "draft") + self.assertEqual(batch.depre_amount, 0) + batch.auto_compute = True + # Batch will be posted by cron job + batch._autocompute_draft_batches() + self.assertEqual(batch.state, "computed") + self.assertEqual(batch.depre_amount, 2500) diff --git a/account_asset_compute_batch/views/account_asset_compute_batch.xml b/account_asset_compute_batch/views/account_asset_compute_batch.xml new file mode 100644 index 000000000..1216a21e9 --- /dev/null +++ b/account_asset_compute_batch/views/account_asset_compute_batch.xml @@ -0,0 +1,147 @@ + + + account.asset.compute.batch.form + account.asset.compute.batch + 10 + +
+
+
+ +
+
+
+

+ +

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+
+
+
+ + + account.asset.compute.batch.tree + account.asset.compute.batch + + + + + + + + + + + + + + search.account.asset.compute.batch.filter + account.asset.compute.batch + + + + + + + + + Compute Asset Batch + account.asset.compute.batch + tree,form + + + + + +
diff --git a/account_asset_compute_batch/wizard/__init__.py b/account_asset_compute_batch/wizard/__init__.py new file mode 100644 index 000000000..07bfe6b38 --- /dev/null +++ b/account_asset_compute_batch/wizard/__init__.py @@ -0,0 +1 @@ +from . import account_asset_compute diff --git a/account_asset_compute_batch/wizard/account_asset_compute.py b/account_asset_compute_batch/wizard/account_asset_compute.py new file mode 100644 index 000000000..32bbf34c6 --- /dev/null +++ b/account_asset_compute_batch/wizard/account_asset_compute.py @@ -0,0 +1,43 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import _, fields, models + + +class AccountAssetCompute(models.TransientModel): + _inherit = "account.asset.compute" + + use_batch = fields.Boolean(string="Create Batch", help="Use batch opton") + batch_name = fields.Char( + string="Batch Name", + help="If batch name is specified, computation will be tracked by a batch", + ) + description = fields.Char( + string="Description", + ) + profile_ids = fields.Many2many( + comodel_name="account.asset.profile", + string="Profiles", + ) + delay_compute = fields.Boolean(string="Delay Compute Asset") + + def asset_compute(self): + if self.use_batch: + vals = { + "date_end": self.date_end, + "name": self.batch_name, + "description": self.description, + "profile_ids": [(4, x.id) for x in self.profile_ids], + } + batch = self.env["account.asset.compute.batch"].create(vals) + if not self.delay_compute: + batch.action_compute() + return { + "name": _("Asset Compute Batch"), + "type": "ir.actions.act_window", + "view_type": "form", + "view_mode": "form", + "res_model": "account.asset.compute.batch", + "res_id": batch.id, + } + return super().asset_compute() diff --git a/account_asset_compute_batch/wizard/account_asset_compute.xml b/account_asset_compute_batch/wizard/account_asset_compute.xml new file mode 100644 index 000000000..c0339052c --- /dev/null +++ b/account_asset_compute_batch/wizard/account_asset_compute.xml @@ -0,0 +1,41 @@ + + + account.asset.compute + account.asset.compute + + + + + + + + + + + + + + + + + + + + + +