Skip to content

Commit

Permalink
adds new asset type : unit of activity
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu.saison committed Jan 14, 2025
1 parent 5c53b13 commit a43483c
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 13 deletions.
1 change: 1 addition & 0 deletions account_asset_management/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"data/cron.xml",
"wizard/wiz_account_asset_report.xml",
"wizard/wiz_asset_move_reverse.xml",
"wizard/account_asset_unit_of_activity.xml",
],
}
43 changes: 39 additions & 4 deletions account_asset_management/models/account_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models
from odoo import _, api, exceptions, fields, models
from odoo.exceptions import UserError
from odoo.osv import expression

Expand Down Expand Up @@ -293,6 +293,26 @@ class AccountAsset(models.Model):
carried forward to the first depreciation line of the current open
period.""",
)
total_number_of_use = fields.Integer()
remaining_usage = fields.Integer(compute="_compute_remaining_usage")

@api.constrains("total_number_of_use", "method")
def _check_number_of_use(self):
for rec in self:
if rec.method == "unit-activity":
if rec.total_number_of_use == 0:
raise exceptions.ValidationError("Number of usage can't be null")

Check warning on line 304 in account_asset_management/models/account_asset.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/models/account_asset.py#L304

Added line #L304 was not covered by tests
elif rec.total_number_of_use < 0:
raise exceptions.ValidationError("Number of usage can't be negativ")

Check warning on line 306 in account_asset_management/models/account_asset.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/models/account_asset.py#L306

Added line #L306 was not covered by tests

@api.depends("total_number_of_use")
def _compute_remaining_usage(self):
for asset in self:
asset.remaining_usage = asset.total_number_of_use - sum(
asset.depreciation_line_ids.filtered(
lambda a: a.type == "depreciate"
).mapped("quantity")
)

@api.model
def _default_company_id(self):
Expand Down Expand Up @@ -532,8 +552,11 @@ def validate(self):
asset.state = "close"
else:
asset.state = "open"
if not asset.depreciation_line_ids.filtered(
lambda l: l.type != "create"
if (
not asset.depreciation_line_ids.filtered(
lambda l: l.type != "create"
)
and asset.method != "unit-activity"
):
asset.compute_depreciation_board()
return True
Expand Down Expand Up @@ -578,6 +601,19 @@ def open_entries(self):
"domain": [("id", "in", self.account_move_line_ids.mapped("move_id").ids)],
}

def add_unit_of_activity(self):
self.ensure_one()
ctx = dict(self.env.context, active_ids=self.ids, active_id=self.id)

Check warning on line 606 in account_asset_management/models/account_asset.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/models/account_asset.py#L605-L606

Added lines #L605 - L606 were not covered by tests

return {

Check warning on line 608 in account_asset_management/models/account_asset.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/models/account_asset.py#L608

Added line #L608 was not covered by tests
"name": _("Add Asset Unit of activity"),
"view_mode": "form",
"res_model": "account.asset.unit.of.activity",
"target": "new",
"type": "ir.actions.act_window",
"context": ctx,
}

def _group_lines(self, table):
"""group lines prior to depreciation start period."""

Expand Down Expand Up @@ -765,7 +801,6 @@ def compute_depreciation_board(self):
else: # no posted lines
table_i_start = 0
line_i_start = 0

asset._compute_depreciation_line(
depreciated_value_posted,
table_i_start,
Expand Down
1 change: 1 addition & 0 deletions account_asset_management/models/account_asset_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class AccountAssetLine(models.Model):
company_currency_id = fields.Many2one(
related="asset_id.company_id.currency_id", store=True, string="Company Currency"
)
quantity = fields.Integer(readonly=True)

@api.depends("amount", "previous_id", "type")
def _compute_values(self):
Expand Down
1 change: 1 addition & 0 deletions account_asset_management/models/account_asset_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def _selection_method(self):
("degressive", _("Degressive")),
("degr-linear", _("Degressive-Linear")),
("degr-limit", _("Degressive up to Salvage Value")),
("unit-activity", _("Unit of Activity")),
]

@api.model
Expand Down
1 change: 1 addition & 0 deletions account_asset_management/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ access_account_asset_remove_user,account.asset.remove,model_account_asset_remove
access_account_asset_compute_user,account.asset.compute,model_account_asset_compute,account.group_account_user,1,1,1,1
access_wiz_account_asset_report,wiz.account.asset.report,model_wiz_account_asset_report,account.group_account_readonly,1,1,1,0
access_wiz_asset_move_reverse_user,wiz.asset.move.reverse,model_wiz_asset_move_reverse,account.group_account_user,1,1,1,1
access_account_asset_unit_user,account.asset.unit.of.activity,model_account_asset_unit_of_activity,account.group_account_user,1,1,1,1
49 changes: 43 additions & 6 deletions account_asset_management/views/account_asset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@
/>
</group>
<group>
<group string="Depreciation Dates">
<group
string="Depreciation Dates"
attrs="{'invisible': [('method', '=', 'unit-activity')]}"
>
<field name="method_time" required="1" />
<field
name="method_number"
Expand All @@ -127,18 +130,30 @@
name="use_leap_years"
attrs="{'invisible': [('days_calc', '=', True)]}"
/>

</group>
<group string="Depreciation Method">
<field name="method" required="1" />
<field
name="method_progress_factor"
attrs="{'invisible': [('method', 'in', ['linear', 'linear-limit'])], 'required': [('method', 'in', ['degressive', 'degr-linear', 'degr-limit'])]}"
attrs="{'invisible': [('method', 'in', ['linear', 'linear-limit', 'unit-activity'])], 'required': [('method', 'in', ['degressive', 'degr-linear', 'degr-limit'])]}"
/>
<field
name="prorata"
attrs="{'readonly': [('method_time', '!=', 'year')]}"
attrs="{'readonly': [('method_time', '!=', 'year')], 'invisible': [('method', 'in', ['unit-activity'])]}"
/>
<field name="carry_forward_missed_depreciations" />
<field
name="total_number_of_use"
attrs="{'invisible': [('method', 'not in', ['unit-activity'])],
'readonly':['|', ('move_line_check', '=', True), ('state', '!=', 'draft')]
}"
/>
<field
name="remaining_usage"
readonly="1"
attrs="{'invisible': [('method', 'not in', ['unit-activity'])]}"
/>
</group>
</group>
</page>
Expand All @@ -149,7 +164,16 @@
name="compute_depreciation_board"
string="Compute"
icon="fa-gears"
attrs="{'invisible': [('state', 'in', ['close', 'removed'])]}"
attrs="{'invisible': ['|',('state', 'in', ['close', 'removed']), ('method', '=', 'unit-activity')]}"
/>
<button
name="add_unit_of_activity"
string="Add Unit of Activity"
help="Add a line of usage for this Asset"
icon="fa-plus"
class="oe_highlight"
type="object"
attrs="{'invisible':['|', ('method', '!=', 'unit-activity'),('remaining_usage', '=', 0)]}"
/>
</div>
<field
Expand All @@ -162,8 +186,21 @@
create="false"
>
<field name="type" />
<field name="line_date" />
<field name="line_days" sum="Total Days" />
<field
name="line_date"
attrs="{'invisible': [('move_check', '!=', True)]}"
/>
<field
name="line_days"
sum="Total Days"
attrs="{'column_invisible': [('parent.method', '=', 'unit-activity')]}"
/>
<field
name="quantity"
sum="Total Days"
attrs="{'column_invisible': [('parent.method', '!=', 'unit-activity')]}"
/>

<field name="depreciated_value" readonly="1" />
<field name="amount" />
<field name="remaining_value" readonly="1" />
Expand Down
9 changes: 6 additions & 3 deletions account_asset_management/views/account_asset_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
<field name="account_residual_value_id" />
<field name="allow_reversal" />
</group>
<group string="Depreciation Dates">
<group
string="Depreciation Dates"
attrs="{'invisible': [('method', '=', 'unit-activity')]}"
>
<field name="method_time" />
<field
name="method_number"
Expand All @@ -56,11 +59,11 @@
<field name="method" />
<field
name="method_progress_factor"
attrs="{'invisible': [('method', 'in', ['linear', 'linear-limit'])], 'required': [('method', 'in', ['degressive', 'degr-linear', 'degr-limit'])]}"
attrs="{'invisible': [('method', 'in', ['linear', 'linear-limit', 'unit-activity'])], 'required': [('method', 'in', ['degressive', 'degr-linear', 'degr-limit'])]}"
/>
<field
name="prorata"
attrs="{'readonly':[('method_time','!=','year')]}"
attrs="{'readonly':[('method_time','!=','year')], 'invisible': [('method', 'in', ['unit-activity'])]}"
/>
<field name="open_asset" />
</group>
Expand Down
1 change: 1 addition & 0 deletions account_asset_management/wizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import account_asset_remove
from . import wiz_account_asset_report
from . import wiz_asset_move_reverse
from . import account_asset_unit_of_activity
83 changes: 83 additions & 0 deletions account_asset_management/wizard/account_asset_unit_of_activity.py
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)

Check warning on line 18 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L17-L18

Added lines #L17 - L18 were not covered by tests
if asset.method == "unit-activity":
if self.date < asset.date_start:
raise exceptions.ValidationError(

Check warning on line 21 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L21

Added line #L21 was not covered by tests
"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)

Check warning on line 27 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L26-L27

Added lines #L26 - L27 were not covered by tests
if self.quantity > asset.remaining_usage:
raise exceptions.UserError(

Check warning on line 29 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L29

Added line #L29 was not covered by tests
_("The number of remaining asset usage\n" "is exceed")
)
if asset.remaining_usage == 0:
raise exceptions.UserError(

Check warning on line 33 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L33

Added line #L33 was not covered by tests
_(
"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 |= {

Check warning on line 42 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L40-L42

Added lines #L40 - L42 were not covered by tests
"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()

Check warning on line 57 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L56-L57

Added lines #L56 - L57 were not covered by tests
else:
raise exceptions.UserError(

Check warning on line 59 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L59

Added line #L59 was not covered by tests
_(
"Some asset lines have no associated accounting entries. \n"
"Validate them before create a new one"
)
)
else:
self.env["account.asset.line"].create(vals)

Check warning on line 66 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L66

Added line #L66 was not covered by tests

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

Check warning on line 72 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L69-L72

Added lines #L69 - L72 were not covered by tests
dlines = asset.depreciation_line_ids.filtered(lambda r: r.type == "depreciate")
amount = currency.round(self.quantity * step)

Check warning on line 74 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L74

Added line #L74 was not covered by tests
if self.quantity == asset.remaining_usage:
if dlines:
amount = dlines[-1].remaining_value

Check warning on line 77 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L77

Added line #L77 was not covered by tests
else:
amount = asset.purchase_value
return {

Check warning on line 80 in account_asset_management/wizard/account_asset_unit_of_activity.py

View check run for this annotation

Codecov / codecov/patch

account_asset_management/wizard/account_asset_unit_of_activity.py#L79-L80

Added lines #L79 - L80 were not covered by tests
"amount": amount,
"previous_id": dlines[-1].id if dlines else None,
}
34 changes: 34 additions & 0 deletions account_asset_management/wizard/account_asset_unit_of_activity.xml
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>

0 comments on commit a43483c

Please sign in to comment.