Skip to content

Commit

Permalink
[ENH] account_asset_transfer: add feature expand asset line
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-tubtim committed Jan 27, 2022
1 parent 9671f1d commit 2494e82
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions account_asset_transfer/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* `Ecosoft <http://ecosoft.co.th>`__:

* Kitti U. <kittiu@ecosoft.co.th>
* Pimolnat Suntian <pimolnats@ecosoft.co.th>
2 changes: 2 additions & 0 deletions account_asset_transfer/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ Given asset under construction has been created, i.e., by vendor bill.
- 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)

**Note:** You can click "Expand Asset" button for expand asset line that selects the asset profile set to be "Create an asset by product item"
11 changes: 8 additions & 3 deletions account_asset_transfer/tests/test_account_asset_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def setUpClass(cls):
].id,
"journal_id": cls.company_data["default_journal_purchase"].id,
"name": "Room - 5 Years",
"asset_product_item": True,
"method_time": "year",
"method_number": 5,
"method_period": "year",
Expand Down Expand Up @@ -98,15 +99,19 @@ def test_01_asset_transfer_uac_to_asset(self):
with transfer_form.to_asset_ids.new() as to_asset:
to_asset.asset_name = "Asset 1"
to_asset.asset_profile_id = self.profile_asset
to_asset.asset_value = 2000
to_asset.price_unit = 2000
to_asset.quantity = 1
with transfer_form.to_asset_ids.new() as to_asset:
to_asset.asset_name = "Asset 2"
to_asset.asset_profile_id = self.profile_asset
to_asset.asset_value = 20000
to_asset.price_unit = 2000
to_asset.quantity = 10
transfer_form.save()
transfer_wiz.expand_to_asset_ids()
res = transfer_wiz.transfer()
transfer_move = self.env["account.move"].browse(res["domain"][0][2])
assets = transfer_move.invoice_line_ids.mapped("asset_id")
# 2 new assets created, and value equal to original assets
# 11 new assets created, and value equal to original assets
new_assets = assets.filtered(lambda l: l.state == "draft")
self.assertEqual(len(new_assets), 11)
self.assertEqual(sum(new_assets.mapped("purchase_value")), 22000)
41 changes: 40 additions & 1 deletion account_asset_transfer/wizard/account_asset_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ def _get_transfer_data(self):
]
return move_lines

def expand_to_asset_ids(self):
self.ensure_one()
lines = self.to_asset_ids.filtered(lambda l: l.asset_profile_id and l.quantity)
for line in lines:
line._expand_asset_line()
action = self.env.ref("account_asset_transfer.action_account_asset_transfer")
result = action.sudo().read()[0]
result.update({"res_id": self.id})
return result


class AccountAssetTransferLine(models.TransientModel):
_name = "account.asset.transfer.line"
Expand All @@ -197,10 +207,22 @@ class AccountAssetTransferLine(models.TransientModel):
required=True,
)
asset_name = fields.Char(required=True)
quantity = fields.Float(
string="Quantity",
required=True,
default=0.0,
)
price_unit = fields.Float(
string="Unit Price",
required=True,
default=0.0,
)
asset_value = fields.Float(
string="Asset Value",
required=True,
compute="_compute_asset_value",
default=0.0,
store=True,
required=True,
)
partner_id = fields.Many2one(
comodel_name="res.partner",
Expand All @@ -214,3 +236,20 @@ class AccountAssetTransferLine(models.TransientModel):
comodel_name="account.analytic.tag",
string="Analytic tags",
)

@api.depends("quantity", "price_unit")
def _compute_asset_value(self):
for rec in self:
rec.asset_value = rec.quantity * rec.price_unit

def _expand_asset_line(self):
self.ensure_one()
if self.asset_profile_id and self.quantity > 1.0:
profile = self.asset_profile_id
if profile.asset_product_item:
line = self
qty = self.quantity
name = self.asset_name
self.update({"quantity": 1, "asset_name": "{} {}".format(name, 1)})
for i in range(1, int(qty)):
line.copy({"asset_name": "{} {}".format(name, i + 1)})
17 changes: 17 additions & 0 deletions account_asset_transfer/wizard/account_asset_transfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
</group>
</group>
<notebook>
<div class="oe_right" name="buttons">
<button
name="expand_to_asset_ids"
type="object"
string="Expand Asset"
icon="fa-bars"
/>
</div>
<page name="to_asset" string="To New Asset">
<field
name="to_asset_ids"
Expand All @@ -35,6 +43,8 @@
<tree editable="bottom">
<field name="asset_profile_id" />
<field name="asset_name" />
<field name="price_unit" />
<field name="quantity" />
<field name="asset_value" />
<field name="partner_id" />
<field
Expand Down Expand Up @@ -69,6 +79,13 @@
</field>
</record>

<record id="action_account_asset_transfer" model="ir.actions.act_window">
<field name="name">Transfer Asset</field>
<field name="res_model">account.asset.transfer</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>

<record id="action_asset_transfer_from_list" model="ir.actions.server">
<field name="name">Transfer Asset</field>
<field name="groups_id" eval="[(4, ref('account.group_account_manager'))]" />
Expand Down

0 comments on commit 2494e82

Please sign in to comment.