From 1474938bdeb2847b8813d6f2945740d15fd7bb9e Mon Sep 17 00:00:00 2001 From: Saran440 Date: Wed, 31 May 2023 11:52:37 +0700 Subject: [PATCH] [ENH] add source/dest asset when transfer asset --- .../models/account_asset.py | 36 +++++++++++++++++++ .../readme/CONTRIBUTORS.rst | 1 + account_asset_transfer/readme/USAGE.rst | 2 +- .../tests/test_account_asset_transfer.py | 12 ++++++- .../views/account_asset.xml | 28 +++++++++++++-- .../wizard/account_asset_transfer.py | 6 ++-- 6 files changed, 79 insertions(+), 6 deletions(-) diff --git a/account_asset_transfer/models/account_asset.py b/account_asset_transfer/models/account_asset.py index eed3f32b4..c15bad2f1 100644 --- a/account_asset_transfer/models/account_asset.py +++ b/account_asset_transfer/models/account_asset.py @@ -13,6 +13,9 @@ class AccountAsset(models.Model): search="_search_can_transfer", help="Allow transfer AUC (running) to Asset", ) + is_transfer = fields.Boolean( + help="flag indicating if the asset has been transferred from/to another asset." + ) def _compute_can_transfer(self): for asset in self: @@ -56,3 +59,36 @@ class AccountAsset(models.Model): "type": "ir.actions.act_window", "context": ctx, } + + def open_assets(self): + self.ensure_one() + moves = self.account_move_line_ids.mapped("move_id") + assets = self.env["account.asset"] + asset_from = self._context.get("asset_from") + asset_to = self._context.get("asset_to") + for move in moves: + # Source Assets, we check from move that create this asset + if ( + asset_from + and self.id + not in move.line_ids.filtered(lambda l: l.credit).mapped("asset_id").ids + ): + assets = move.line_ids.filtered(lambda l: l.credit).mapped("asset_id") + break + # Destination Assets, we check from move that create destination asset + elif ( + asset_to + and self.id + in move.line_ids.filtered(lambda l: l.credit).mapped("asset_id").ids + ): + assets = move.line_ids.filtered(lambda l: l.debit).mapped("asset_id") + break + return { + "name": _("Assets"), + "view_mode": "tree,form", + "res_model": "account.asset", + "view_id": False, + "type": "ir.actions.act_window", + "context": self._context, + "domain": [("id", "in", assets.ids)], + } diff --git a/account_asset_transfer/readme/CONTRIBUTORS.rst b/account_asset_transfer/readme/CONTRIBUTORS.rst index b64335f20..0b1bd3c7d 100644 --- a/account_asset_transfer/readme/CONTRIBUTORS.rst +++ b/account_asset_transfer/readme/CONTRIBUTORS.rst @@ -2,3 +2,4 @@ * Kitti U. * Pimolnat Suntian + * Saran Lim. diff --git a/account_asset_transfer/readme/USAGE.rst b/account_asset_transfer/readme/USAGE.rst index a28c5fe3b..3dd83b8b1 100644 --- a/account_asset_transfer/readme/USAGE.rst +++ b/account_asset_transfer/readme/USAGE.rst @@ -2,7 +2,7 @@ Given asset under construction has been created, i.e., by vendor bill. - Go to asset menu - Filter "Transferrable" assets and look for desired assets to transfer -- Select assets to transfer, and click actoin "Transfer Asset" +- Select assets to transfer, and click action "Transfer Asset" - On asset transfer wizard, on the "To New Asset" tab, choose new profile(s) - Click "Transfer" button - Odoo will create journal entry as well as new asset(s) diff --git a/account_asset_transfer/tests/test_account_asset_transfer.py b/account_asset_transfer/tests/test_account_asset_transfer.py index c7ad7d2d8..633f7b46c 100644 --- a/account_asset_transfer/tests/test_account_asset_transfer.py +++ b/account_asset_transfer/tests/test_account_asset_transfer.py @@ -101,7 +101,9 @@ class TestAccountAssetTransfer(TestAssetManagement): assets.invalidate_cache() # can_transfer = True after validate self.assertTrue(list(set(assets.mapped("can_transfer")))[0]) - + self.assertEqual(list(set(assets.mapped("is_transfer"))), [False]) + # Keep source asset + source_assets = assets # Create Asset Transfer transfer_form = Form( self.env["account.asset.transfer"].with_context(active_ids=assets.ids) @@ -130,3 +132,11 @@ class TestAccountAssetTransfer(TestAssetManagement): # 2 new assets created, and value equal to original assets new_assets = assets.filtered(lambda l: l.state == "draft") self.assertEqual(sum(new_assets.mapped("purchase_value")), 23000) + # All asset transfer will change to is_transfer + self.assertEqual(list(set(assets.mapped("is_transfer"))), [True]) + # Check source asset from new asset + result = new_assets[0].with_context(asset_from=1).open_assets() + self.assertEqual(sorted(result["domain"][0][2]), source_assets.ids) + # Check dest asset from source asset + result = source_assets[0].with_context(asset_to=1).open_assets() + self.assertEqual(result["domain"][0][2], new_assets.ids) diff --git a/account_asset_transfer/views/account_asset.xml b/account_asset_transfer/views/account_asset.xml index eb6c6e106..ba99382de 100644 --- a/account_asset_transfer/views/account_asset.xml +++ b/account_asset_transfer/views/account_asset.xml @@ -1,6 +1,6 @@ - + account.asset.form account.asset