-
-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds new asset type : unit of activity
- Loading branch information
matthieu.saison
committed
Jan 14, 2025
1 parent
5c53b13
commit a43483c
Showing
10 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
account_asset_management/wizard/account_asset_unit_of_activity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from odoo import _, api, exceptions, fields, models | ||
|
||
|
||
class AccountAssetUnitOfActivity(models.TransientModel): | ||
_name = "account.asset.unit.of.activity" | ||
_description = "Compute Assets unit of activity" | ||
|
||
auto_create_asset_move = fields.Boolean( | ||
String="Create move automatically", | ||
help="If the other asset line has all an move created, the move associated to this asset line will be created automatically", | ||
) | ||
quantity = fields.Integer(default=1) | ||
date = fields.Date(default=fields.date.today()) | ||
|
||
@api.constrains("date") | ||
def _check_date(self): | ||
asset_id = self.env.context.get("active_id") | ||
asset = self.env["account.asset"].browse(asset_id) | ||
if asset.method == "unit-activity": | ||
if self.date < asset.date_start: | ||
raise exceptions.ValidationError( | ||
"Date can't be before assert start date" | ||
) | ||
|
||
def add_usage(self): | ||
asset_id = self.env.context.get("active_id") | ||
asset = self.env["account.asset"].browse(asset_id) | ||
if self.quantity > asset.remaining_usage: | ||
raise exceptions.UserError( | ||
_("The number of remaining asset usage\n" "is exceed") | ||
) | ||
if asset.remaining_usage == 0: | ||
raise exceptions.UserError( | ||
_( | ||
"The asset is totaly depreciated.\n" | ||
"You can't add depreciation line" | ||
) | ||
) | ||
if self.quantity < 0: | ||
raise exceptions.UserError(_("The quantity can't be negative")) | ||
vals = self._compute_values(asset_id) | ||
vals |= { | ||
"line_date": self.date, | ||
"type": "depreciate", | ||
"asset_id": asset_id, | ||
"init_entry": False, | ||
"quantity": self.quantity, | ||
} | ||
|
||
if self.auto_create_asset_move: | ||
previous_depreciation_lines = asset.depreciation_line_ids.filtered( | ||
lambda line: line.type == "depreciate" | ||
and line.line_date <= vals.get("line_date") | ||
) | ||
if all([line.move_id for line in previous_depreciation_lines]): | ||
aal = self.env["account.asset.line"].create(vals) | ||
aal.create_move() | ||
else: | ||
raise exceptions.UserError( | ||
_( | ||
"Some asset lines have no associated accounting entries. \n" | ||
"Validate them before create a new one" | ||
) | ||
) | ||
else: | ||
self.env["account.asset.line"].create(vals) | ||
|
||
def _compute_values(self, asset_id): | ||
asset = self.env["account.asset"].browse(asset_id) | ||
company = asset.company_id | ||
currency = company.currency_id | ||
step = (asset.purchase_value - asset.salvage_value) / asset.total_number_of_use | ||
dlines = asset.depreciation_line_ids.filtered(lambda r: r.type == "depreciate") | ||
amount = currency.round(self.quantity * step) | ||
if self.quantity == asset.remaining_usage: | ||
if dlines: | ||
amount = dlines[-1].remaining_value | ||
else: | ||
amount = asset.purchase_value | ||
return { | ||
"amount": amount, | ||
"previous_id": dlines[-1].id if dlines else None, | ||
} |
34 changes: 34 additions & 0 deletions
34
account_asset_management/wizard/account_asset_unit_of_activity.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<odoo> | ||
<record id="account_asset_unit_of_activity_view_form" model="ir.ui.view"> | ||
<field name="name">account.asset.unit.of.activity</field> | ||
<field name="model">account.asset.unit.of.activity</field> | ||
<field name="arch" type="xml"> | ||
<form string="Compute asset unit of activity"> | ||
|
||
<group> | ||
<field name="quantity" /> | ||
<field name="date" /> | ||
<field name="auto_create_asset_move" /> | ||
</group> | ||
<footer> | ||
<button | ||
string="Add a usage" | ||
name="add_usage" | ||
type="object" | ||
class="oe_highlight" | ||
/> | ||
<button string="Cancel" class="oe_link" special="cancel" /> | ||
</footer> | ||
</form> | ||
</field> | ||
</record> | ||
|
||
<record id="account_asset_unit_of_activity_action" model="ir.actions.act_window"> | ||
<field name="name">Unit of Activity</field> | ||
<field name="res_model">account.asset.unit.of.activity</field> | ||
<field name="view_mode">tree,form</field> | ||
<field name="view_id" ref="account_asset_unit_of_activity_view_form" /> | ||
<field name="target">new</field> | ||
</record> | ||
</odoo> |