Skip to content

Commit

Permalink
[IMP] l10n_it_asset_management: Monthly depreciation
Browse files Browse the repository at this point in the history
Depreciation mode lines represent years after asset purchase
  • Loading branch information
SirAionTech committed Mar 25, 2024
1 parent d3b41a5 commit aa32cec
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 50 deletions.
2 changes: 1 addition & 1 deletion l10n_it_asset_management/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{
"name": "ITA - Gestione Cespiti",
"version": "16.0.1.0.0",
"version": "16.0.1.1.0",
"category": "Localization/Italy",
"summary": "Gestione Cespiti",
"author": "Openforce, Odoo Community Association (OCA)",
Expand Down
4 changes: 2 additions & 2 deletions l10n_it_asset_management/data/asset_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
</record>
<record id="ad_mode_materiale_line" model="asset.depreciation.mode.line">
<field name="mode_id" ref="ad_mode_materiale" />
<field name="from_nr">1</field>
<field name="to_nr">1</field>
<field name="from_year_nr">1</field>
<field name="to_year_nr">1</field>
<field name="coefficient">0.5</field>
</record>

Expand Down
32 changes: 32 additions & 0 deletions l10n_it_asset_management/migrations/16.0.1.1.0/pre-migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from openupgradelib import openupgrade

MODEL_TO_RENAMED_FIELDS = {
"asset.depreciation.mode.line": [
("from_nr", "from_year_nr"),
("to_nr", "to_year_nr"),
]
}


def _rename_fields(env):
openupgrade.rename_fields(
env,
[
(
model_name,
model_name.replace(".", "_"),
field_spec[0],
field_spec[1],
)
for model_name, field_specs in MODEL_TO_RENAMED_FIELDS.items()
for field_spec in field_specs
],
)


@openupgrade.migrate()
def migrate(env, version):
_rename_fields(env)
22 changes: 22 additions & 0 deletions l10n_it_asset_management/models/account_fiscal_year.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ def get_fiscal_year_by_date_domain(self, date, company=None):
if company:
domain.append(("company_id", "in", company.ids))
return domain

@api.model
def _get_passed_years(self, start_date, end_date):
"""Find all fiscal years between `start_date` and `end_date`."""
if start_date and end_date:
overlapping_fiscal_year_domain = self.new(
{
"date_from": start_date,
"date_to": end_date,
}
)._get_overlapping_domain()
# Exclude current record's NewId
# because it is not supported in domains
overlapping_fiscal_year_domain = [
term if term[0] != "id" else ("id", "!=", 0)
for term in overlapping_fiscal_year_domain
]
overlapping_fiscal_years = self.search(overlapping_fiscal_year_domain)
passed_years = len(overlapping_fiscal_years)
else:
passed_years = None

Check warning on line 58 in l10n_it_asset_management/models/account_fiscal_year.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_management/models/account_fiscal_year.py#L58

Added line #L58 was not covered by tests
return passed_years
33 changes: 16 additions & 17 deletions l10n_it_asset_management/models/asset_depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,26 +289,30 @@ def check_before_generate_depreciation_lines(self, dep_date):
)
)

def generate_depreciation_lines(self, dep_date):
def generate_depreciation_lines(self, dep_date, period=None):
# Set new date within context if necessary
self.check_before_generate_depreciation_lines(dep_date)

new_lines = self.env["asset.depreciation.line"]
for dep in self:
new_line = dep.generate_depreciation_lines_single(dep_date)
new_line = dep.generate_depreciation_lines_single(dep_date, period=period)
if new_line:
new_lines |= new_line

return new_lines

def generate_depreciation_lines_single(self, dep_date):
def generate_depreciation_lines_single(self, dep_date, period=None):
self.ensure_one()
res = self.env["asset.depreciation.line"]
if self.last_depreciation_date and self.last_depreciation_date > dep_date:
return res

Check warning on line 308 in l10n_it_asset_management/models/asset_depreciation.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_management/models/asset_depreciation.py#L308

Added line #L308 was not covered by tests
dep_nr = self.get_max_depreciation_nr() + 1
dep = self.with_context(dep_nr=dep_nr, used_asset=self.asset_id.used)
dep_amount = dep.get_depreciation_amount(dep_date)
passed_fiscal_years = self.env["account.fiscal.year"]._get_passed_years(
self.asset_id.purchase_date, dep_date
)
dep = self.with_context(
passed_fiscal_years=passed_fiscal_years, used_asset=self.asset_id.used
)
dep_amount = dep.get_depreciation_amount(dep_date, period=period)
if not dep_amount:
return res
dep = dep.with_context(dep_amount=dep_amount)
Expand Down Expand Up @@ -393,15 +397,15 @@ def get_depreciable_amount(self, dep_date=None):
depreciable_amount = 0
return depreciable_amount

def get_depreciation_amount(self, dep_date):
def get_depreciation_amount(self, dep_date, period=None):
self.ensure_one()
zero_dep_date = self.zero_depreciation_until
if zero_dep_date and dep_date <= zero_dep_date:
return 0

Check warning on line 404 in l10n_it_asset_management/models/asset_depreciation.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_management/models/asset_depreciation.py#L404

Added line #L404 was not covered by tests

# Get depreciable amount, multiplier and digits
amount = self.get_depreciable_amount(dep_date)
multiplier = self.get_depreciation_amount_multiplier(dep_date)
multiplier = self.get_depreciation_amount_multiplier(dep_date, period=period)
digits = self.env["decimal.precision"].precision_get("Account")
dep_amount = round(amount * multiplier, digits)

Expand All @@ -411,12 +415,15 @@ def get_depreciation_amount(self, dep_date):

return dep_amount

def get_depreciation_amount_multiplier(self, dep_date):
def get_depreciation_amount_multiplier(self, dep_date, period=None):
self.ensure_one()

# Base multiplier
multiplier = self.percentage / 100

if period == "month":
multiplier /= 12

# Update multiplier from depreciation mode data
multiplier *= self.mode_id.get_depreciation_amount_multiplier()

Expand Down Expand Up @@ -503,14 +510,6 @@ def get_dismiss_account_move_vals(self):
"move_type": "entry",
}

def get_max_depreciation_nr(self):
self.ensure_one()
num_lines = self.line_ids.filtered("requires_depreciation_nr")
nums = num_lines.mapped("depreciation_nr")
if not nums:
nums = [0]
return max(nums)

def get_pro_rata_temporis_dates(self, date):
"""
Gets useful dates for pro rata temporis computations, according to
Expand Down
22 changes: 16 additions & 6 deletions l10n_it_asset_management/models/asset_depreciation_mode_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class AssetDepreciationModeLine(models.Model):
_name = "asset.depreciation.mode.line"
_description = "Asset Depreciation Mode Line"
_order = "from_nr asc, to_nr asc"
_order = "from_year_nr asc, to_year_nr asc"

application = fields.Selection(
[("coefficient", "Coefficient"), ("percentage", "Percentage")],
Expand All @@ -24,8 +24,12 @@ class AssetDepreciationModeLine(models.Model):
"res.company", readonly=True, related="mode_id.company_id", string="Company"
)

from_nr = fields.Integer(
from_year_nr = fields.Integer(
required=True,
string="From Year",
help="Minimum number of fiscal years passed "
"from asset purchase date "
"to apply this line.",
)

mode_id = fields.Many2one(
Expand All @@ -38,7 +42,12 @@ class AssetDepreciationModeLine(models.Model):

percentage = fields.Float()

to_nr = fields.Integer()
to_year_nr = fields.Integer(
string="To Year",
help="Maximum number of fiscal years passed "
"from asset purchase date "
"to apply this line.",
)

@api.onchange("application")
def onchange_application(self):
Expand All @@ -53,13 +62,14 @@ def onchange_application(self):

def get_depreciation_amount_multiplier(self):
multiplier = 1
nr = self._context.get("dep_nr")
if nr is None:
passed_fiscal_years = self._context.get("passed_fiscal_years")
if passed_fiscal_years is None:
# Cannot compare to any line
return multiplier

Check warning on line 68 in l10n_it_asset_management/models/asset_depreciation_mode_line.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_management/models/asset_depreciation_mode_line.py#L68

Added line #L68 was not covered by tests

lines = self.filtered(
lambda line: line.from_nr <= nr and (not line.to_nr or line.to_nr >= nr)
lambda line: line.from_year_nr <= passed_fiscal_years
and (not line.to_year_nr or line.to_year_nr >= passed_fiscal_years)
)
if not lines:
return multiplier
Expand Down
Loading

0 comments on commit aa32cec

Please sign in to comment.