2018-08-05 20:33:56 +02:00
|
|
|
# Copyright 2009-2018 Noviat
|
2019-08-12 11:42:21 +02:00
|
|
|
# Copyright 2019 Tecnativa - Pedro M. Baeza
|
2018-08-05 20:33:56 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
|
|
|
|
import calendar
|
|
|
|
import logging
|
2020-01-29 18:34:31 +01:00
|
|
|
from datetime import date
|
|
|
|
from functools import reduce
|
2018-08-05 20:33:56 +02:00
|
|
|
from sys import exc_info
|
|
|
|
from traceback import format_exception
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
|
|
from odoo import _, api, fields, models
|
2018-08-05 20:33:56 +02:00
|
|
|
from odoo.exceptions import UserError
|
|
|
|
from odoo.osv import expression
|
2020-01-29 18:34:31 +01:00
|
|
|
|
2018-08-05 20:33:56 +02:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2020-03-19 13:17:36 +01:00
|
|
|
READONLY_STATES = {
|
|
|
|
"open": [("readonly", True)],
|
|
|
|
"close": [("readonly", True)],
|
|
|
|
"removed": [("readonly", True)],
|
|
|
|
}
|
|
|
|
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
class DummyFy(object):
|
|
|
|
def __init__(self, *args, **argv):
|
|
|
|
for key, arg in argv.items():
|
|
|
|
setattr(self, key, arg)
|
|
|
|
|
|
|
|
|
|
|
|
class AccountAsset(models.Model):
|
2020-01-29 18:34:31 +01:00
|
|
|
_name = "account.asset"
|
2022-12-24 01:28:55 +01:00
|
|
|
_inherit = ["mail.thread", "mail.activity.mixin", "analytic.mixin"]
|
2020-01-29 18:34:31 +01:00
|
|
|
_description = "Asset"
|
|
|
|
_order = "date_start desc, code, name"
|
2021-02-11 11:48:48 +01:00
|
|
|
_check_company_auto = True
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
account_move_line_ids = fields.One2many(
|
2020-01-29 18:34:31 +01:00
|
|
|
comodel_name="account.move.line",
|
|
|
|
inverse_name="asset_id",
|
|
|
|
string="Entries",
|
|
|
|
readonly=True,
|
|
|
|
copy=False,
|
2021-02-11 11:48:48 +01:00
|
|
|
check_company=True,
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
move_line_check = fields.Boolean(
|
2020-01-29 18:34:31 +01:00
|
|
|
compute="_compute_move_line_check", string="Has accounting entries"
|
|
|
|
)
|
2021-01-14 08:53:25 +01:00
|
|
|
name = fields.Char(
|
|
|
|
string="Asset Name",
|
|
|
|
required=True,
|
|
|
|
states=READONLY_STATES,
|
|
|
|
)
|
|
|
|
code = fields.Char(
|
|
|
|
string="Reference",
|
|
|
|
size=32,
|
|
|
|
states=READONLY_STATES,
|
|
|
|
)
|
2022-11-29 23:30:33 +01:00
|
|
|
purchase_value = fields.Monetary(
|
2020-01-29 18:34:31 +01:00
|
|
|
required=True,
|
2020-03-19 13:17:36 +01:00
|
|
|
states=READONLY_STATES,
|
2018-08-05 20:33:56 +02:00
|
|
|
help="This amount represent the initial value of the asset."
|
2020-01-29 18:34:31 +01:00
|
|
|
"\nThe Depreciation Base is calculated as follows:"
|
|
|
|
"\nPurchase Value - Salvage Value.",
|
|
|
|
)
|
2022-11-29 23:30:33 +01:00
|
|
|
salvage_value = fields.Monetary(
|
2023-01-26 08:25:04 +01:00
|
|
|
compute="_compute_salvage_value",
|
|
|
|
store=True,
|
|
|
|
readonly=False,
|
2020-03-19 13:17:36 +01:00
|
|
|
states=READONLY_STATES,
|
2018-08-05 20:33:56 +02:00
|
|
|
help="The estimated value that an asset will realize upon "
|
2020-01-29 18:34:31 +01:00
|
|
|
"its sale at the end of its useful life.\n"
|
|
|
|
"This value is used to determine the depreciation amounts.",
|
|
|
|
)
|
2022-11-29 23:30:33 +01:00
|
|
|
depreciation_base = fields.Monetary(
|
2020-01-29 18:34:31 +01:00
|
|
|
compute="_compute_depreciation_base",
|
2018-08-05 20:33:56 +02:00
|
|
|
store=True,
|
|
|
|
help="This amount represent the depreciation base "
|
2020-03-19 13:17:36 +01:00
|
|
|
"of the asset (Purchase Value - Salvage Value).",
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2022-11-29 23:30:33 +01:00
|
|
|
value_residual = fields.Monetary(
|
2020-01-29 18:34:31 +01:00
|
|
|
compute="_compute_depreciation",
|
|
|
|
string="Residual Value",
|
|
|
|
store=True,
|
|
|
|
)
|
2022-11-29 23:30:33 +01:00
|
|
|
value_depreciated = fields.Monetary(
|
2020-01-29 18:34:31 +01:00
|
|
|
compute="_compute_depreciation",
|
|
|
|
string="Depreciated Value",
|
|
|
|
store=True,
|
|
|
|
)
|
2022-04-13 12:18:36 +02:00
|
|
|
note = fields.Text()
|
2018-08-05 20:33:56 +02:00
|
|
|
profile_id = fields.Many2one(
|
2020-01-29 18:34:31 +01:00
|
|
|
comodel_name="account.asset.profile",
|
|
|
|
string="Asset Profile",
|
2019-08-26 21:28:27 +02:00
|
|
|
change_default=True,
|
|
|
|
required=True,
|
2020-03-19 13:17:36 +01:00
|
|
|
states=READONLY_STATES,
|
2021-02-11 11:48:48 +01:00
|
|
|
check_company=True,
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2019-08-31 12:14:51 +02:00
|
|
|
group_ids = fields.Many2many(
|
2020-01-29 18:34:31 +01:00
|
|
|
comodel_name="account.asset.group",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_group_ids",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
2019-08-31 12:14:51 +02:00
|
|
|
relation="account_asset_group_rel",
|
|
|
|
column1="asset_id",
|
|
|
|
column2="group_id",
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Asset Groups",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
date_start = fields.Date(
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Asset Start Date",
|
2019-08-26 21:28:27 +02:00
|
|
|
required=True,
|
2020-03-19 13:17:36 +01:00
|
|
|
states=READONLY_STATES,
|
2018-08-05 20:33:56 +02:00
|
|
|
help="You should manually add depreciation lines "
|
2020-01-29 18:34:31 +01:00
|
|
|
"with the depreciations of previous fiscal years "
|
|
|
|
"if the Depreciation Start Date is different from the date "
|
|
|
|
"for which accounting entries need to be generated.",
|
|
|
|
)
|
|
|
|
date_remove = fields.Date(string="Asset Removal Date", readonly=True)
|
2018-08-05 20:33:56 +02:00
|
|
|
state = fields.Selection(
|
|
|
|
selection=[
|
2020-01-29 18:34:31 +01:00
|
|
|
("draft", "Draft"),
|
|
|
|
("open", "Running"),
|
|
|
|
("close", "Close"),
|
|
|
|
("removed", "Removed"),
|
|
|
|
],
|
|
|
|
string="Status",
|
|
|
|
required=True,
|
|
|
|
default="draft",
|
|
|
|
copy=False,
|
2018-08-05 20:33:56 +02:00
|
|
|
help="When an asset is created, the status is 'Draft'.\n"
|
2020-01-29 18:34:31 +01:00
|
|
|
"If the asset is confirmed, the status goes in 'Running' "
|
|
|
|
"and the depreciation lines can be posted "
|
|
|
|
"to the accounting.\n"
|
|
|
|
"If the last depreciation line is posted, "
|
|
|
|
"the asset goes into the 'Close' status.\n"
|
|
|
|
"When the removal entries are generated, "
|
|
|
|
"the asset goes into the 'Removed' status.",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
active = fields.Boolean(default=True)
|
|
|
|
partner_id = fields.Many2one(
|
2021-01-14 08:53:25 +01:00
|
|
|
comodel_name="res.partner",
|
|
|
|
string="Partner",
|
|
|
|
states=READONLY_STATES,
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
method = fields.Selection(
|
2020-01-29 18:34:31 +01:00
|
|
|
selection=lambda self: self.env["account.asset.profile"]._selection_method(),
|
|
|
|
string="Computation Method",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_method",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
|
|
|
states=READONLY_STATES,
|
|
|
|
help="Choose the method to use to compute the depreciation lines.\n"
|
2020-01-29 18:34:31 +01:00
|
|
|
" * Linear: Calculated on basis of: "
|
2020-03-19 13:17:36 +01:00
|
|
|
"Depreciation Base / Number of Depreciations. "
|
|
|
|
"Depreciation Base = Purchase Value - Salvage Value.\n"
|
|
|
|
" * Linear-Limit: Linear up to Salvage Value. "
|
|
|
|
"Depreciation Base = Purchase Value.\n"
|
2020-01-29 18:34:31 +01:00
|
|
|
" * Degressive: Calculated on basis of: "
|
2020-03-19 13:17:36 +01:00
|
|
|
"Residual Value * Degressive Factor.\n"
|
2020-01-29 18:34:31 +01:00
|
|
|
" * Degressive-Linear (only for Time Method = Year): "
|
|
|
|
"Degressive becomes linear when the annual linear "
|
2020-03-19 13:17:36 +01:00
|
|
|
"depreciation exceeds the annual degressive depreciation.\n"
|
|
|
|
" * Degressive-Limit: Degressive up to Salvage Value. "
|
|
|
|
"The Depreciation Base is equal to the asset value.",
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
method_number = fields.Integer(
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Number of Years",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_method_number",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
|
|
|
states=READONLY_STATES,
|
2020-01-29 18:34:31 +01:00
|
|
|
help="The number of years needed to depreciate your asset",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
method_period = fields.Selection(
|
2018-09-28 12:24:01 +02:00
|
|
|
selection=lambda self: self.env[
|
2020-01-29 18:34:31 +01:00
|
|
|
"account.asset.profile"
|
|
|
|
]._selection_method_period(),
|
|
|
|
string="Period Length",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_method_period",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
|
|
|
states=READONLY_STATES,
|
2020-01-29 18:34:31 +01:00
|
|
|
help="Period length for the depreciation accounting entries",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
method_end = fields.Date(
|
2020-03-19 13:17:36 +01:00
|
|
|
string="Ending Date",
|
|
|
|
compute="_compute_method_end",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
|
|
|
states=READONLY_STATES,
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
method_progress_factor = fields.Float(
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Degressive Factor",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_method_progress_factor",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
|
|
|
states=READONLY_STATES,
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
method_time = fields.Selection(
|
2018-09-28 12:24:01 +02:00
|
|
|
selection=lambda self: self.env[
|
2020-01-29 18:34:31 +01:00
|
|
|
"account.asset.profile"
|
|
|
|
]._selection_method_time(),
|
|
|
|
string="Time Method",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_method_time",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
|
|
|
states=READONLY_STATES,
|
2018-08-05 20:33:56 +02:00
|
|
|
help="Choose the method to use to compute the dates and "
|
2020-01-29 18:34:31 +01:00
|
|
|
"number of depreciation lines.\n"
|
|
|
|
" * Number of Years: Specify the number of years "
|
2021-04-19 13:04:19 +02:00
|
|
|
"for the depreciation.\n"
|
|
|
|
" * Number of Depreciations: Fix the number of "
|
|
|
|
"depreciation lines and the time between 2 depreciations.\n",
|
2018-08-05 20:33:56 +02:00
|
|
|
)
|
2019-07-08 13:21:20 +02:00
|
|
|
days_calc = fields.Boolean(
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Calculate by days",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_days_calc",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
2019-07-08 13:21:20 +02:00
|
|
|
help="Use number of days to calculate depreciation amount",
|
|
|
|
)
|
2019-10-22 09:59:57 +02:00
|
|
|
use_leap_years = fields.Boolean(
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_use_leap_years",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
2019-10-22 09:59:57 +02:00
|
|
|
help="If not set, the system will distribute evenly the amount to "
|
2020-01-29 18:34:31 +01:00
|
|
|
"amortize across the years, based on the number of years. "
|
|
|
|
"So the amount per year will be the "
|
|
|
|
"depreciation base / number of years.\n "
|
|
|
|
"If set, the system will consider if the current year "
|
|
|
|
"is a leap year. The amount to depreciate per year will be "
|
|
|
|
"calculated as depreciation base / (depreciation end date - "
|
|
|
|
"start date + 1) * days in the current year.",
|
2019-10-22 09:59:57 +02:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
prorata = fields.Boolean(
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Prorata Temporis",
|
2020-03-19 13:17:36 +01:00
|
|
|
compute="_compute_prorrata",
|
|
|
|
readonly=False,
|
|
|
|
store=True,
|
|
|
|
states=READONLY_STATES,
|
2018-08-05 20:33:56 +02:00
|
|
|
help="Indicates that the first depreciation entry for this asset "
|
2020-03-19 13:17:36 +01:00
|
|
|
"has to be done from the depreciation start date instead of "
|
|
|
|
"the first day of the fiscal year.",
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
depreciation_line_ids = fields.One2many(
|
2020-01-29 18:34:31 +01:00
|
|
|
comodel_name="account.asset.line",
|
|
|
|
inverse_name="asset_id",
|
|
|
|
string="Depreciation Lines",
|
|
|
|
copy=False,
|
2020-03-19 13:17:36 +01:00
|
|
|
states=READONLY_STATES,
|
2021-02-11 11:48:48 +01:00
|
|
|
check_company=True,
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
company_id = fields.Many2one(
|
2020-01-29 18:34:31 +01:00
|
|
|
comodel_name="res.company",
|
|
|
|
string="Company",
|
|
|
|
required=True,
|
|
|
|
readonly=True,
|
|
|
|
default=lambda self: self._default_company_id(),
|
|
|
|
)
|
2022-12-24 01:54:58 +01:00
|
|
|
currency_id = fields.Many2one(
|
2020-01-29 18:34:31 +01:00
|
|
|
comodel_name="res.currency",
|
|
|
|
related="company_id.currency_id",
|
|
|
|
string="Company Currency",
|
|
|
|
store=True,
|
|
|
|
)
|
2021-06-10 14:12:43 +02:00
|
|
|
carry_forward_missed_depreciations = fields.Boolean(
|
|
|
|
string="Accumulate missed depreciations",
|
|
|
|
help="""If create an asset in a fiscal period that is now closed
|
|
|
|
the accumulated amount of depreciations that cannot be posted will be
|
|
|
|
carried forward to the first depreciation line of the current open
|
|
|
|
period.""",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _default_company_id(self):
|
2020-01-29 18:37:08 +01:00
|
|
|
return self.env.company
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-03-19 13:17:36 +01:00
|
|
|
@api.depends("depreciation_line_ids.move_id")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _compute_move_line_check(self):
|
|
|
|
for asset in self:
|
2020-03-19 13:17:36 +01:00
|
|
|
asset.move_line_check = bool(
|
|
|
|
asset.depreciation_line_ids.filtered("move_id")
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2023-01-26 08:25:04 +01:00
|
|
|
def _get_salvage_value_profile(self):
|
|
|
|
self.ensure_one()
|
|
|
|
salvage_value = self.profile_id.salvage_value
|
|
|
|
if self.profile_id.salvage_type == "percent":
|
|
|
|
salvage_value = (salvage_value / 100) * self.purchase_value
|
|
|
|
return salvage_value
|
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_salvage_value(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.salvage_value = asset._get_salvage_value_profile()
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.depends("purchase_value", "salvage_value", "method")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _compute_depreciation_base(self):
|
|
|
|
for asset in self:
|
2020-01-29 18:34:31 +01:00
|
|
|
if asset.method in ["linear-limit", "degr-limit"]:
|
2018-08-05 20:33:56 +02:00
|
|
|
asset.depreciation_base = asset.purchase_value
|
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
asset.depreciation_base = asset.purchase_value - asset.salvage_value
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.depends(
|
|
|
|
"depreciation_base",
|
|
|
|
"depreciation_line_ids.type",
|
|
|
|
"depreciation_line_ids.amount",
|
|
|
|
"depreciation_line_ids.previous_id",
|
|
|
|
"depreciation_line_ids.init_entry",
|
|
|
|
"depreciation_line_ids.move_check",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
def _compute_depreciation(self):
|
|
|
|
for asset in self:
|
2019-08-26 21:28:27 +02:00
|
|
|
lines = asset.depreciation_line_ids.filtered(
|
2020-01-29 18:34:31 +01:00
|
|
|
lambda l: l.type in ("depreciate", "remove")
|
|
|
|
and (l.init_entry or l.move_check)
|
|
|
|
)
|
2022-04-13 10:58:58 +02:00
|
|
|
value_depreciated = sum(line.amount for line in lines)
|
2019-08-26 21:28:27 +02:00
|
|
|
residual = asset.depreciation_base - value_depreciated
|
|
|
|
depreciated = value_depreciated
|
2020-01-29 18:34:31 +01:00
|
|
|
asset.update({"value_residual": residual, "value_depreciated": depreciated})
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-03-19 13:17:36 +01:00
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_group_ids(self):
|
2021-04-30 11:36:21 +02:00
|
|
|
for asset in self:
|
|
|
|
if asset.profile_id:
|
|
|
|
asset.group_ids = asset.profile_id.group_ids
|
2020-03-19 13:17:36 +01:00
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_method(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.method = asset.profile_id.method
|
|
|
|
|
|
|
|
@api.depends("profile_id", "method_end")
|
|
|
|
def _compute_method_number(self):
|
|
|
|
for asset in self:
|
|
|
|
if asset.method_end:
|
|
|
|
asset.method_number = 0
|
|
|
|
else:
|
|
|
|
asset.method_number = asset.profile_id.method_number
|
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_method_period(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.method_period = asset.profile_id.method_period
|
|
|
|
|
|
|
|
@api.depends("method_number")
|
|
|
|
def _compute_method_end(self):
|
|
|
|
for asset in self:
|
|
|
|
if asset.method_number:
|
|
|
|
asset.method_end = False
|
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_method_progress_factor(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.method_progress_factor = asset.profile_id.method_progress_factor
|
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_method_time(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.method_time = asset.profile_id.method_time
|
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_days_calc(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.days_calc = asset.profile_id.days_calc
|
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_use_leap_years(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.use_leap_years = asset.profile_id.use_leap_years
|
|
|
|
|
|
|
|
@api.depends("profile_id", "method_time")
|
|
|
|
def _compute_prorrata(self):
|
|
|
|
for asset in self:
|
|
|
|
if asset.method_time != "year":
|
|
|
|
asset.prorata = True
|
|
|
|
else:
|
|
|
|
asset.prorata = asset.profile_id.prorata
|
|
|
|
|
|
|
|
@api.depends("profile_id")
|
|
|
|
def _compute_account_analytic_id(self):
|
|
|
|
for asset in self:
|
|
|
|
asset.account_analytic_id = asset.profile_id.account_analytic_id
|
|
|
|
|
2021-04-06 15:24:08 +02:00
|
|
|
@api.depends("profile_id")
|
2022-12-24 01:28:55 +01:00
|
|
|
def _compute_analytic_distribution(self):
|
2021-04-06 15:24:08 +02:00
|
|
|
for asset in self:
|
2022-12-24 01:28:55 +01:00
|
|
|
asset.analytic_distribution = asset.profile_id.analytic_distribution
|
2021-04-06 15:24:08 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.constrains("method", "method_time")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _check_method(self):
|
2020-03-19 13:17:36 +01:00
|
|
|
if self.filtered(
|
|
|
|
lambda a: a.method == "degr-linear" and a.method_time != "year"
|
|
|
|
):
|
|
|
|
raise UserError(
|
|
|
|
_("Degressive-Linear is only supported for Time Method = Year.")
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2021-04-19 13:04:19 +02:00
|
|
|
@api.constrains("date_start", "method_end", "method_number", "method_time")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _check_dates(self):
|
2020-03-19 13:17:36 +01:00
|
|
|
if self.filtered(
|
2021-04-19 13:04:19 +02:00
|
|
|
lambda a: a.method_time == "year"
|
|
|
|
and not a.method_number
|
2021-08-18 21:25:08 +02:00
|
|
|
and a.method_end
|
2021-04-19 13:04:19 +02:00
|
|
|
and a.method_end <= a.date_start
|
2020-03-19 13:17:36 +01:00
|
|
|
):
|
|
|
|
raise UserError(_("The Start Date must precede the Ending Date."))
|
|
|
|
|
|
|
|
@api.constrains("profile_id")
|
|
|
|
def _check_profile_change(self):
|
|
|
|
if self.depreciation_line_ids.filtered("move_id"):
|
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"You cannot change the profile of an asset "
|
|
|
|
"with accounting entries."
|
|
|
|
)
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.onchange("purchase_value", "salvage_value", "date_start", "method")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _onchange_purchase_salvage_value(self):
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method in ["linear-limit", "degr-limit"]:
|
2018-08-05 20:33:56 +02:00
|
|
|
self.depreciation_base = self.purchase_value or 0.0
|
|
|
|
else:
|
|
|
|
purchase_value = self.purchase_value or 0.0
|
|
|
|
salvage_value = self.salvage_value or 0.0
|
|
|
|
self.depreciation_base = purchase_value - salvage_value
|
|
|
|
dl_create_line = self.depreciation_line_ids.filtered(
|
2020-01-29 18:34:31 +01:00
|
|
|
lambda r: r.type == "create"
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
if dl_create_line:
|
2020-01-29 18:34:31 +01:00
|
|
|
dl_create_line.update(
|
|
|
|
{"amount": self.depreciation_base, "line_date": self.date_start}
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2022-12-24 01:28:55 +01:00
|
|
|
@api.model_create_multi
|
|
|
|
def create(self, vals_list):
|
|
|
|
asset_ids = super().create(vals_list)
|
|
|
|
for asset_id in asset_ids:
|
|
|
|
asset_id._create_first_asset_line()
|
|
|
|
return asset_ids
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def write(self, vals):
|
2018-10-01 11:57:07 +02:00
|
|
|
res = super().write(vals)
|
2018-08-05 20:33:56 +02:00
|
|
|
for asset in self:
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.env.context.get("asset_validate_from_write"):
|
2018-08-05 20:33:56 +02:00
|
|
|
continue
|
|
|
|
asset._create_first_asset_line()
|
2020-01-29 18:34:31 +01:00
|
|
|
if asset.profile_id.open_asset and self.env.context.get(
|
|
|
|
"create_asset_from_move_line"
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
asset.compute_depreciation_board()
|
|
|
|
# extra context to avoid recursion
|
2018-10-01 11:57:07 +02:00
|
|
|
asset.with_context(asset_validate_from_write=True).validate()
|
|
|
|
return res
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def _create_first_asset_line(self):
|
|
|
|
self.ensure_one()
|
|
|
|
if self.depreciation_base and not self.depreciation_line_ids:
|
2020-01-29 18:34:31 +01:00
|
|
|
asset_line_obj = self.env["account.asset.line"]
|
2018-08-05 20:33:56 +02:00
|
|
|
line_name = self._get_depreciation_entry_name(0)
|
|
|
|
asset_line_vals = {
|
2020-01-29 18:34:31 +01:00
|
|
|
"amount": self.depreciation_base,
|
|
|
|
"asset_id": self.id,
|
|
|
|
"name": line_name,
|
|
|
|
"line_date": self.date_start,
|
|
|
|
"init_entry": True,
|
|
|
|
"type": "create",
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
asset_line = asset_line_obj.create(asset_line_vals)
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.env.context.get("create_asset_from_move_line"):
|
|
|
|
asset_line.move_id = self.env.context["move_id"]
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def unlink(self):
|
|
|
|
for asset in self:
|
2020-01-29 18:34:31 +01:00
|
|
|
if asset.state != "draft":
|
|
|
|
raise UserError(_("You can only delete assets in draft state."))
|
2018-08-05 20:33:56 +02:00
|
|
|
if asset.depreciation_line_ids.filtered(
|
2020-01-29 18:34:31 +01:00
|
|
|
lambda r: r.type == "depreciate" and r.move_check
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
raise UserError(
|
2020-01-29 18:34:31 +01:00
|
|
|
_(
|
|
|
|
"You cannot delete an asset that contains "
|
|
|
|
"posted depreciation lines."
|
|
|
|
)
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
# update accounting entries linked to lines of type 'create'
|
2020-01-29 18:34:31 +01:00
|
|
|
amls = self.with_context(allow_asset_removal=True).mapped(
|
|
|
|
"account_move_line_ids"
|
|
|
|
)
|
|
|
|
amls.write({"asset_id": False})
|
2018-10-01 11:57:07 +02:00
|
|
|
return super().unlink()
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
@api.model
|
2020-01-29 18:34:31 +01:00
|
|
|
def name_search(self, name, args=None, operator="ilike", limit=100):
|
2018-08-05 20:33:56 +02:00
|
|
|
args = args or []
|
|
|
|
domain = []
|
|
|
|
if name:
|
2020-01-29 18:34:31 +01:00
|
|
|
domain = ["|", ("code", "=ilike", name + "%"), ("name", operator, name)]
|
2018-08-05 20:33:56 +02:00
|
|
|
if operator in expression.NEGATIVE_TERM_OPERATORS:
|
2020-01-29 18:34:31 +01:00
|
|
|
domain = ["&", "!"] + domain[1:]
|
2018-08-05 20:33:56 +02:00
|
|
|
assets = self.search(domain + args, limit=limit)
|
|
|
|
return assets.name_get()
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.depends("name", "code")
|
2018-08-05 20:33:56 +02:00
|
|
|
def name_get(self):
|
|
|
|
result = []
|
|
|
|
for asset in self:
|
|
|
|
name = asset.name
|
|
|
|
if asset.code:
|
2020-01-29 18:34:31 +01:00
|
|
|
name = " - ".join([asset.code, name])
|
2018-08-05 20:33:56 +02:00
|
|
|
result.append((asset.id, name))
|
|
|
|
return result
|
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
for asset in self:
|
2022-12-24 01:54:58 +01:00
|
|
|
if asset.currency_id.is_zero(asset.value_residual):
|
2020-01-29 18:34:31 +01:00
|
|
|
asset.state = "close"
|
2018-08-05 20:33:56 +02:00
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
asset.state = "open"
|
2020-04-23 10:40:38 +02:00
|
|
|
if not asset.depreciation_line_ids.filtered(
|
|
|
|
lambda l: l.type != "create"
|
|
|
|
):
|
|
|
|
asset.compute_depreciation_board()
|
2018-08-05 20:33:56 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
self.ensure_one()
|
2018-10-01 11:57:07 +02:00
|
|
|
ctx = dict(self.env.context, active_ids=self.ids, active_id=self.id)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
early_removal = False
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method in ["linear-limit", "degr-limit"]:
|
2018-08-05 20:33:56 +02:00
|
|
|
if self.value_residual != self.salvage_value:
|
|
|
|
early_removal = True
|
|
|
|
elif self.value_residual:
|
|
|
|
early_removal = True
|
|
|
|
if early_removal:
|
2020-01-29 18:34:31 +01:00
|
|
|
ctx.update({"early_removal": True})
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
return {
|
2020-01-29 18:34:31 +01:00
|
|
|
"name": _("Generate Asset Removal entries"),
|
|
|
|
"view_mode": "form",
|
|
|
|
"res_model": "account.asset.remove",
|
|
|
|
"target": "new",
|
|
|
|
"type": "ir.actions.act_window",
|
|
|
|
"context": ctx,
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def set_to_draft(self):
|
2020-01-29 18:34:31 +01:00
|
|
|
return self.write({"state": "draft"})
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def open_entries(self):
|
|
|
|
self.ensure_one()
|
2020-03-18 19:25:11 +01:00
|
|
|
# needed for avoiding errors after grouping in assets
|
|
|
|
context = dict(self.env.context)
|
|
|
|
context.pop("group_by", None)
|
2018-08-05 20:33:56 +02:00
|
|
|
return {
|
2020-01-29 18:34:31 +01:00
|
|
|
"name": _("Journal Entries"),
|
|
|
|
"view_mode": "tree,form",
|
|
|
|
"res_model": "account.move",
|
|
|
|
"view_id": False,
|
|
|
|
"type": "ir.actions.act_window",
|
2020-03-18 19:25:11 +01:00
|
|
|
"context": context,
|
2020-03-18 19:35:44 +01:00
|
|
|
"domain": [("id", "in", self.account_move_line_ids.mapped("move_id").ids)],
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 18:37:08 +01:00
|
|
|
def _group_lines(self, table):
|
|
|
|
"""group lines prior to depreciation start period."""
|
|
|
|
|
2018-10-01 11:57:07 +02:00
|
|
|
def group_lines(x, y):
|
2020-01-29 18:34:31 +01:00
|
|
|
y.update({"amount": x["amount"] + y["amount"]})
|
2018-10-01 11:57:07 +02:00
|
|
|
return y
|
|
|
|
|
2020-01-29 18:37:08 +01:00
|
|
|
depreciation_start_date = self.date_start
|
|
|
|
lines = table[0]["lines"]
|
|
|
|
lines1 = []
|
|
|
|
lines2 = []
|
|
|
|
flag = lines[0]["date"] < depreciation_start_date
|
|
|
|
for line in lines:
|
|
|
|
if flag:
|
|
|
|
lines1.append(line)
|
|
|
|
if line["date"] >= depreciation_start_date:
|
|
|
|
flag = False
|
|
|
|
else:
|
|
|
|
lines2.append(line)
|
|
|
|
if lines1:
|
|
|
|
lines1 = [reduce(group_lines, lines1)]
|
|
|
|
lines1[0]["depreciated_value"] = 0.0
|
|
|
|
table[0]["lines"] = lines1 + lines2
|
|
|
|
|
|
|
|
def _compute_depreciation_line(
|
|
|
|
self,
|
|
|
|
depreciated_value_posted,
|
|
|
|
table_i_start,
|
|
|
|
line_i_start,
|
|
|
|
table,
|
|
|
|
last_line,
|
|
|
|
posted_lines,
|
|
|
|
):
|
2021-03-22 08:11:23 +01:00
|
|
|
company = self.company_id
|
2022-11-29 23:30:33 +01:00
|
|
|
currency = company.currency_id
|
2021-03-22 08:11:23 +01:00
|
|
|
fiscalyear_lock_date = company.fiscalyear_lock_date or fields.Date.to_date(
|
|
|
|
"1901-01-01"
|
|
|
|
)
|
2020-01-29 18:37:08 +01:00
|
|
|
|
|
|
|
seq = len(posted_lines)
|
|
|
|
depr_line = last_line
|
|
|
|
last_date = table[-1]["lines"][-1]["date"]
|
|
|
|
depreciated_value = depreciated_value_posted
|
2021-06-10 14:12:43 +02:00
|
|
|
amount_to_allocate = 0.0
|
2020-01-29 18:37:08 +01:00
|
|
|
for entry in table[table_i_start:]:
|
|
|
|
for line in entry["lines"][line_i_start:]:
|
|
|
|
seq += 1
|
|
|
|
name = self._get_depreciation_entry_name(seq)
|
|
|
|
amount = line["amount"]
|
2021-06-10 14:12:43 +02:00
|
|
|
if self.carry_forward_missed_depreciations:
|
|
|
|
if line["init"]:
|
|
|
|
amount_to_allocate += amount
|
|
|
|
amount = 0
|
|
|
|
else:
|
|
|
|
amount += amount_to_allocate
|
|
|
|
amount_to_allocate = 0.0
|
2020-01-29 18:37:08 +01:00
|
|
|
if line["date"] == last_date:
|
|
|
|
# ensure that the last entry of the table always
|
|
|
|
# depreciates the remaining value
|
|
|
|
amount = self.depreciation_base - depreciated_value
|
|
|
|
if self.method in ["linear-limit", "degr-limit"]:
|
|
|
|
amount -= self.salvage_value
|
2021-06-10 14:12:43 +02:00
|
|
|
if amount or self.carry_forward_missed_depreciations:
|
2020-01-29 18:37:08 +01:00
|
|
|
vals = {
|
|
|
|
"previous_id": depr_line.id,
|
2022-11-29 23:30:33 +01:00
|
|
|
"amount": currency.round(amount),
|
2020-01-29 18:37:08 +01:00
|
|
|
"asset_id": self.id,
|
|
|
|
"name": name,
|
|
|
|
"line_date": line["date"],
|
|
|
|
"line_days": line["days"],
|
2021-03-22 08:11:23 +01:00
|
|
|
"init_entry": fiscalyear_lock_date >= line["date"],
|
2020-01-29 18:37:08 +01:00
|
|
|
}
|
2022-11-29 23:30:33 +01:00
|
|
|
depreciated_value += currency.round(amount)
|
2020-01-29 18:37:08 +01:00
|
|
|
depr_line = self.env["account.asset.line"].create(vals)
|
|
|
|
else:
|
|
|
|
seq -= 1
|
|
|
|
line_i_start = 0
|
|
|
|
|
|
|
|
def compute_depreciation_board(self):
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
line_obj = self.env["account.asset.line"]
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
for asset in self:
|
2022-11-29 23:30:33 +01:00
|
|
|
currency = asset.company_id.currency_id
|
|
|
|
if currency.is_zero(asset.value_residual):
|
2018-08-05 20:33:56 +02:00
|
|
|
continue
|
|
|
|
domain = [
|
2020-01-29 18:34:31 +01:00
|
|
|
("asset_id", "=", asset.id),
|
|
|
|
("type", "=", "depreciate"),
|
|
|
|
"|",
|
|
|
|
("move_check", "=", True),
|
|
|
|
("init_entry", "=", True),
|
|
|
|
]
|
|
|
|
posted_lines = line_obj.search(domain, order="line_date desc")
|
2018-09-28 12:24:01 +02:00
|
|
|
if posted_lines:
|
2018-08-05 20:33:56 +02:00
|
|
|
last_line = posted_lines[0]
|
|
|
|
else:
|
|
|
|
last_line = line_obj
|
|
|
|
domain = [
|
2020-01-29 18:34:31 +01:00
|
|
|
("asset_id", "=", asset.id),
|
|
|
|
("type", "=", "depreciate"),
|
|
|
|
("move_id", "=", False),
|
|
|
|
("init_entry", "=", False),
|
|
|
|
]
|
2018-08-05 20:33:56 +02:00
|
|
|
old_lines = line_obj.search(domain)
|
|
|
|
if old_lines:
|
|
|
|
old_lines.unlink()
|
|
|
|
|
|
|
|
table = asset._compute_depreciation_table()
|
|
|
|
if not table:
|
|
|
|
continue
|
|
|
|
|
2020-01-29 18:37:08 +01:00
|
|
|
asset._group_lines(table)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
# check table with posted entries and
|
|
|
|
# recompute in case of deviation
|
|
|
|
depreciated_value_posted = depreciated_value = 0.0
|
2018-09-28 12:24:01 +02:00
|
|
|
if posted_lines:
|
2022-04-13 10:58:58 +02:00
|
|
|
total_table_lines = sum(len(entry["lines"]) for entry in table)
|
2021-11-09 09:49:42 +01:00
|
|
|
move_check_lines = asset.depreciation_line_ids.filtered("move_check")
|
2019-01-11 15:24:15 +01:00
|
|
|
last_depreciation_date = last_line.line_date
|
2020-01-29 18:34:31 +01:00
|
|
|
last_date_in_table = table[-1]["lines"][-1]["date"]
|
2021-11-09 09:49:42 +01:00
|
|
|
# If the number of lines in the table is the same as the depreciation
|
|
|
|
# lines, we will not show an error even if the dates are the same.
|
|
|
|
if (last_date_in_table < last_depreciation_date) or (
|
|
|
|
last_date_in_table == last_depreciation_date
|
|
|
|
and total_table_lines != len(move_check_lines)
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
raise UserError(
|
2020-01-29 18:34:31 +01:00
|
|
|
_(
|
|
|
|
"The duration of the asset conflicts with the "
|
|
|
|
"posted depreciation table entry dates."
|
|
|
|
)
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:37:08 +01:00
|
|
|
for _table_i, entry in enumerate(table):
|
2020-01-29 18:34:31 +01:00
|
|
|
residual_amount_table = entry["lines"][-1]["remaining_value"]
|
|
|
|
if (
|
|
|
|
entry["date_start"]
|
|
|
|
<= last_depreciation_date
|
|
|
|
<= entry["date_stop"]
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
break
|
2020-01-29 18:37:08 +01:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
if entry["date_stop"] == last_depreciation_date:
|
2020-01-29 18:37:08 +01:00
|
|
|
_table_i += 1
|
|
|
|
_line_i = 0
|
2018-08-05 20:33:56 +02:00
|
|
|
else:
|
2020-01-29 18:37:08 +01:00
|
|
|
entry = table[_table_i]
|
2020-01-29 18:34:31 +01:00
|
|
|
date_min = entry["date_start"]
|
2020-01-29 18:37:08 +01:00
|
|
|
for _line_i, line in enumerate(entry["lines"]):
|
2020-01-29 18:34:31 +01:00
|
|
|
residual_amount_table = line["remaining_value"]
|
|
|
|
if date_min <= last_depreciation_date <= line["date"]:
|
2018-08-05 20:33:56 +02:00
|
|
|
break
|
2020-01-29 18:34:31 +01:00
|
|
|
date_min = line["date"]
|
|
|
|
if line["date"] == last_depreciation_date:
|
2020-01-29 18:37:08 +01:00
|
|
|
_line_i += 1
|
|
|
|
table_i_start = _table_i
|
|
|
|
line_i_start = _line_i
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
# check if residual value corresponds with table
|
|
|
|
# and adjust table when needed
|
2020-01-29 18:34:31 +01:00
|
|
|
depreciated_value_posted = depreciated_value = sum(
|
2022-04-13 10:58:58 +02:00
|
|
|
posted_line.amount for posted_line in posted_lines
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
residual_amount = asset.depreciation_base - depreciated_value
|
2022-11-29 23:30:33 +01:00
|
|
|
amount_diff = currency.round(residual_amount_table - residual_amount)
|
2018-08-05 20:33:56 +02:00
|
|
|
if amount_diff:
|
2021-11-09 09:49:42 +01:00
|
|
|
# We will auto-create a new line because the number of lines in
|
|
|
|
# the tables are the same as the posted depreciations and there
|
|
|
|
# is still a residual value. Only in this case we will need to
|
|
|
|
# add a new line to the table with the amount of the difference.
|
|
|
|
if len(move_check_lines) == total_table_lines:
|
|
|
|
table[table_i_start]["lines"].append(
|
|
|
|
table[table_i_start]["lines"][line_i_start - 1]
|
|
|
|
)
|
|
|
|
line = table[table_i_start]["lines"][line_i_start]
|
|
|
|
line["days"] = 0
|
|
|
|
line["amount"] = amount_diff
|
2018-08-05 20:33:56 +02:00
|
|
|
# compensate in first depreciation entry
|
|
|
|
# after last posting
|
2020-01-29 18:34:31 +01:00
|
|
|
line = table[table_i_start]["lines"][line_i_start]
|
|
|
|
line["amount"] -= amount_diff
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
else: # no posted lines
|
|
|
|
table_i_start = 0
|
|
|
|
line_i_start = 0
|
|
|
|
|
2020-01-29 18:37:08 +01:00
|
|
|
asset._compute_depreciation_line(
|
|
|
|
depreciated_value_posted,
|
|
|
|
table_i_start,
|
|
|
|
line_i_start,
|
|
|
|
table,
|
|
|
|
last_line,
|
|
|
|
posted_lines,
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
return True
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
def _get_fy_duration(self, fy, option="days"):
|
2019-08-10 14:40:49 +02:00
|
|
|
"""Returns fiscal year duration.
|
|
|
|
|
2018-08-05 20:33:56 +02:00
|
|
|
@param option:
|
|
|
|
- days: duration in days
|
|
|
|
- months: duration in months,
|
|
|
|
a started month is counted as a full month
|
|
|
|
- years: duration in calendar years, considering also leap years
|
|
|
|
"""
|
2019-01-11 15:24:15 +01:00
|
|
|
fy_date_start = fy.date_from
|
|
|
|
fy_date_stop = fy.date_to
|
2018-08-05 20:33:56 +02:00
|
|
|
days = (fy_date_stop - fy_date_start).days + 1
|
2020-01-29 18:34:31 +01:00
|
|
|
months = (
|
|
|
|
(fy_date_stop.year - fy_date_start.year) * 12
|
|
|
|
+ (fy_date_stop.month - fy_date_start.month)
|
|
|
|
+ 1
|
|
|
|
)
|
|
|
|
if option == "days":
|
2018-08-05 20:33:56 +02:00
|
|
|
return days
|
2020-01-29 18:34:31 +01:00
|
|
|
elif option == "months":
|
2018-08-05 20:33:56 +02:00
|
|
|
return months
|
2020-01-29 18:34:31 +01:00
|
|
|
elif option == "years":
|
2018-08-05 20:33:56 +02:00
|
|
|
year = fy_date_start.year
|
|
|
|
cnt = fy_date_stop.year - fy_date_start.year + 1
|
|
|
|
for i in range(cnt):
|
|
|
|
cy_days = calendar.isleap(year) and 366 or 365
|
|
|
|
if i == 0: # first year
|
|
|
|
if fy_date_stop.year == year:
|
|
|
|
duration = (fy_date_stop - fy_date_start).days + 1
|
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
duration = (date(year, 12, 31) - fy_date_start).days + 1
|
2018-08-05 20:33:56 +02:00
|
|
|
factor = float(duration) / cy_days
|
|
|
|
elif i == cnt - 1: # last year
|
2020-01-29 18:34:31 +01:00
|
|
|
duration = (fy_date_stop - date(year, 1, 1)).days + 1
|
2018-08-05 20:33:56 +02:00
|
|
|
factor += float(duration) / cy_days
|
|
|
|
else:
|
|
|
|
factor += 1.0
|
|
|
|
year += 1
|
|
|
|
return factor
|
|
|
|
|
|
|
|
def _get_fy_duration_factor(self, entry, firstyear):
|
|
|
|
"""
|
|
|
|
localization: override this method to change the logic used to
|
|
|
|
calculate the impact of extended/shortened fiscal years
|
|
|
|
"""
|
|
|
|
duration_factor = 1.0
|
2020-01-29 18:34:31 +01:00
|
|
|
fy = entry["fy"]
|
2018-08-05 20:33:56 +02:00
|
|
|
if self.prorata:
|
|
|
|
if firstyear:
|
2019-01-11 15:24:15 +01:00
|
|
|
depreciation_date_start = self.date_start
|
2020-01-29 18:34:31 +01:00
|
|
|
fy_date_stop = entry["date_stop"]
|
|
|
|
first_fy_asset_days = (fy_date_stop - depreciation_date_start).days + 1
|
|
|
|
first_fy_duration = self._get_fy_duration(fy, option="days")
|
|
|
|
first_fy_year_factor = self._get_fy_duration(fy, option="years")
|
|
|
|
duration_factor = (
|
|
|
|
float(first_fy_asset_days)
|
|
|
|
/ first_fy_duration
|
2019-08-10 14:40:49 +02:00
|
|
|
* first_fy_year_factor
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2019-08-10 14:40:49 +02:00
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
duration_factor = self._get_fy_duration(fy, option="years")
|
2019-08-10 14:40:49 +02:00
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
fy_months = self._get_fy_duration(fy, option="months")
|
2018-08-05 20:33:56 +02:00
|
|
|
duration_factor = float(fy_months) / 12
|
|
|
|
return duration_factor
|
|
|
|
|
|
|
|
def _get_depreciation_start_date(self, fy):
|
|
|
|
"""
|
|
|
|
In case of 'Linear': the first month is counted as a full month
|
|
|
|
if the fiscal year starts in the middle of a month.
|
|
|
|
"""
|
|
|
|
if self.prorata:
|
2019-01-11 15:24:15 +01:00
|
|
|
depreciation_start_date = self.date_start
|
2018-08-05 20:33:56 +02:00
|
|
|
else:
|
2019-01-11 15:24:15 +01:00
|
|
|
depreciation_start_date = fy.date_from
|
2018-08-05 20:33:56 +02:00
|
|
|
return depreciation_start_date
|
|
|
|
|
|
|
|
def _get_depreciation_stop_date(self, depreciation_start_date):
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_time == "year" and not self.method_end:
|
|
|
|
depreciation_stop_date = depreciation_start_date + relativedelta(
|
|
|
|
years=self.method_number, days=-1
|
|
|
|
)
|
|
|
|
elif self.method_time == "number":
|
|
|
|
if self.method_period == "month":
|
|
|
|
depreciation_stop_date = depreciation_start_date + relativedelta(
|
|
|
|
months=self.method_number, days=-1
|
|
|
|
)
|
|
|
|
elif self.method_period == "quarter":
|
|
|
|
m = [x for x in [3, 6, 9, 12] if x >= depreciation_start_date.month][0]
|
|
|
|
first_line_date = depreciation_start_date + relativedelta(
|
|
|
|
month=m, day=31
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
months = self.method_number * 3
|
2020-01-29 18:34:31 +01:00
|
|
|
depreciation_stop_date = first_line_date + relativedelta(
|
|
|
|
months=months - 1, days=-1
|
|
|
|
)
|
|
|
|
elif self.method_period == "year":
|
|
|
|
depreciation_stop_date = depreciation_start_date + relativedelta(
|
|
|
|
years=self.method_number, days=-1
|
|
|
|
)
|
|
|
|
elif self.method_time == "year" and self.method_end:
|
2019-01-11 15:24:15 +01:00
|
|
|
depreciation_stop_date = self.method_end
|
2018-08-05 20:33:56 +02:00
|
|
|
return depreciation_stop_date
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
def _get_first_period_amount(
|
|
|
|
self, table, entry, depreciation_start_date, line_dates
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
"""
|
|
|
|
Return prorata amount for Time Method 'Year' in case of
|
|
|
|
'Prorata Temporis'
|
|
|
|
"""
|
2020-01-29 18:34:31 +01:00
|
|
|
amount = entry.get("period_amount")
|
|
|
|
if self.prorata and self.method_time == "year":
|
|
|
|
dates = [x for x in line_dates if x <= entry["date_stop"]]
|
2018-08-05 20:33:56 +02:00
|
|
|
full_periods = len(dates) - 1
|
2020-01-29 18:34:31 +01:00
|
|
|
amount = entry["fy_amount"] - amount * full_periods
|
2018-08-05 20:33:56 +02:00
|
|
|
return amount
|
|
|
|
|
2019-10-22 09:59:57 +02:00
|
|
|
def _get_amount_linear(
|
2020-01-29 18:34:31 +01:00
|
|
|
self, depreciation_start_date, depreciation_stop_date, entry
|
|
|
|
):
|
2019-10-22 09:59:57 +02:00
|
|
|
"""
|
|
|
|
Override this method if you want to compute differently the
|
|
|
|
yearly amount.
|
|
|
|
"""
|
2019-10-23 13:05:26 +02:00
|
|
|
if not self.use_leap_years and self.method_number:
|
2019-10-22 09:59:57 +02:00
|
|
|
return self.depreciation_base / self.method_number
|
2020-01-29 18:34:31 +01:00
|
|
|
year = entry["date_stop"].year
|
2019-10-22 09:59:57 +02:00
|
|
|
cy_days = calendar.isleap(year) and 366 or 365
|
|
|
|
days = (depreciation_stop_date - depreciation_start_date).days + 1
|
|
|
|
return (self.depreciation_base / days) * cy_days
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
def _compute_year_amount(
|
|
|
|
self, residual_amount, depreciation_start_date, depreciation_stop_date, entry
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
"""
|
|
|
|
Localization: override this method to change the degressive-linear
|
|
|
|
calculation logic according to local legislation.
|
|
|
|
"""
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_time != "year":
|
2018-08-05 20:33:56 +02:00
|
|
|
raise UserError(
|
2020-01-29 18:34:31 +01:00
|
|
|
_(
|
|
|
|
"The '_compute_year_amount' method is only intended for "
|
2020-03-19 13:17:36 +01:00
|
|
|
"Time Method 'Number of Years'."
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
|
|
|
)
|
2019-10-22 09:59:57 +02:00
|
|
|
year_amount_linear = self._get_amount_linear(
|
2020-01-29 18:34:31 +01:00
|
|
|
depreciation_start_date, depreciation_stop_date, entry
|
|
|
|
)
|
|
|
|
if self.method == "linear":
|
2018-08-05 20:33:56 +02:00
|
|
|
return year_amount_linear
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method == "linear-limit":
|
2018-08-05 20:33:56 +02:00
|
|
|
if (residual_amount - year_amount_linear) < self.salvage_value:
|
|
|
|
return residual_amount - self.salvage_value
|
|
|
|
else:
|
|
|
|
return year_amount_linear
|
2020-01-29 18:34:31 +01:00
|
|
|
year_amount_degressive = residual_amount * self.method_progress_factor
|
|
|
|
if self.method == "degressive":
|
2018-08-05 20:33:56 +02:00
|
|
|
return year_amount_degressive
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method == "degr-linear":
|
2018-08-05 20:33:56 +02:00
|
|
|
if year_amount_linear > year_amount_degressive:
|
|
|
|
return min(year_amount_linear, residual_amount)
|
|
|
|
else:
|
|
|
|
return min(year_amount_degressive, residual_amount)
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method == "degr-limit":
|
2018-08-05 20:33:56 +02:00
|
|
|
if (residual_amount - year_amount_degressive) < self.salvage_value:
|
|
|
|
return residual_amount - self.salvage_value
|
|
|
|
else:
|
|
|
|
return year_amount_degressive
|
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
raise UserError(_("Illegal value %s in asset.method.") % self.method)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def _compute_line_dates(self, table, start_date, stop_date):
|
|
|
|
"""
|
|
|
|
The posting dates of the accounting entries depend on the
|
|
|
|
chosen 'Period Length' as follows:
|
|
|
|
- month: last day of the month
|
|
|
|
- quarter: last of the quarter
|
|
|
|
- year: last day of the fiscal year
|
|
|
|
|
|
|
|
Override this method if another posting date logic is required.
|
|
|
|
"""
|
|
|
|
line_dates = []
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_period == "month":
|
2018-08-05 20:33:56 +02:00
|
|
|
line_date = start_date + relativedelta(day=31)
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_period == "quarter":
|
2018-08-05 20:33:56 +02:00
|
|
|
m = [x for x in [3, 6, 9, 12] if x >= start_date.month][0]
|
|
|
|
line_date = start_date + relativedelta(month=m, day=31)
|
2020-01-29 18:34:31 +01:00
|
|
|
elif self.method_period == "year":
|
|
|
|
line_date = table[0]["date_stop"]
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
i = 1
|
|
|
|
while line_date < stop_date:
|
|
|
|
line_dates.append(line_date)
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_period == "month":
|
2018-08-05 20:33:56 +02:00
|
|
|
line_date = line_date + relativedelta(months=1, day=31)
|
2020-01-29 18:34:31 +01:00
|
|
|
elif self.method_period == "quarter":
|
2018-08-05 20:33:56 +02:00
|
|
|
line_date = line_date + relativedelta(months=3, day=31)
|
2020-01-29 18:34:31 +01:00
|
|
|
elif self.method_period == "year":
|
|
|
|
line_date = table[i]["date_stop"]
|
2018-08-05 20:33:56 +02:00
|
|
|
i += 1
|
|
|
|
|
|
|
|
# last entry
|
2020-01-29 18:34:31 +01:00
|
|
|
if not (self.method_time == "number" and len(line_dates) == self.method_number):
|
2019-07-08 13:21:20 +02:00
|
|
|
if self.days_calc:
|
|
|
|
line_dates.append(stop_date)
|
|
|
|
else:
|
|
|
|
line_dates.append(line_date)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
return line_dates
|
|
|
|
|
2019-07-08 13:21:20 +02:00
|
|
|
def _compute_depreciation_amount_per_fiscal_year(
|
2020-01-29 18:34:31 +01:00
|
|
|
self, table, line_dates, depreciation_start_date, depreciation_stop_date
|
|
|
|
):
|
2022-11-29 23:30:33 +01:00
|
|
|
self.ensure_one()
|
|
|
|
currency = self.company_id.currency_id
|
2019-08-12 11:42:21 +02:00
|
|
|
fy_residual_amount = self.depreciation_base
|
|
|
|
i_max = len(table) - 1
|
|
|
|
asset_sign = self.depreciation_base >= 0 and 1 or -1
|
2019-07-08 13:21:20 +02:00
|
|
|
day_amount = 0.0
|
|
|
|
if self.days_calc:
|
|
|
|
days = (depreciation_stop_date - depreciation_start_date).days + 1
|
|
|
|
day_amount = self.depreciation_base / days
|
|
|
|
|
2019-08-12 11:42:21 +02:00
|
|
|
for i, entry in enumerate(table):
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_time == "year":
|
2019-10-22 09:59:57 +02:00
|
|
|
year_amount = self._compute_year_amount(
|
2020-01-29 18:34:31 +01:00
|
|
|
fy_residual_amount,
|
|
|
|
depreciation_start_date,
|
|
|
|
depreciation_stop_date,
|
|
|
|
entry,
|
|
|
|
)
|
|
|
|
if self.method_period == "year":
|
2019-08-12 11:42:21 +02:00
|
|
|
period_amount = year_amount
|
2020-01-29 18:34:31 +01:00
|
|
|
elif self.method_period == "quarter":
|
2019-08-12 11:42:21 +02:00
|
|
|
period_amount = year_amount / 4
|
2020-01-29 18:34:31 +01:00
|
|
|
elif self.method_period == "month":
|
2019-08-12 11:42:21 +02:00
|
|
|
period_amount = year_amount / 12
|
|
|
|
if i == i_max:
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method in ["linear-limit", "degr-limit"]:
|
2019-08-12 11:42:21 +02:00
|
|
|
fy_amount = fy_residual_amount - self.salvage_value
|
|
|
|
else:
|
|
|
|
fy_amount = fy_residual_amount
|
|
|
|
else:
|
|
|
|
firstyear = i == 0 and True or False
|
2020-01-29 18:34:31 +01:00
|
|
|
fy_factor = self._get_fy_duration_factor(entry, firstyear)
|
2019-08-12 11:42:21 +02:00
|
|
|
fy_amount = year_amount * fy_factor
|
2022-11-29 23:30:33 +01:00
|
|
|
if (
|
|
|
|
currency.compare_amounts(
|
|
|
|
asset_sign * (fy_amount - fy_residual_amount), 0
|
|
|
|
)
|
|
|
|
> 0
|
|
|
|
):
|
2019-08-12 11:42:21 +02:00
|
|
|
fy_amount = fy_residual_amount
|
2022-11-29 23:30:33 +01:00
|
|
|
period_amount = currency.round(period_amount)
|
|
|
|
fy_amount = currency.round(fy_amount)
|
2019-08-12 11:42:21 +02:00
|
|
|
else:
|
|
|
|
fy_amount = False
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_time == "number":
|
2019-08-12 11:42:21 +02:00
|
|
|
number = self.method_number
|
|
|
|
else:
|
|
|
|
number = len(line_dates)
|
2022-11-29 23:30:33 +01:00
|
|
|
period_amount = currency.round(self.depreciation_base / number)
|
2020-01-29 18:34:31 +01:00
|
|
|
entry.update(
|
|
|
|
{
|
|
|
|
"period_amount": period_amount,
|
|
|
|
"fy_amount": fy_amount,
|
|
|
|
"day_amount": day_amount,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if self.method_time == "year":
|
2019-08-12 11:42:21 +02:00
|
|
|
fy_residual_amount -= fy_amount
|
2022-11-29 23:30:33 +01:00
|
|
|
if currency.is_zero(fy_residual_amount):
|
2019-08-12 11:42:21 +02:00
|
|
|
break
|
|
|
|
i_max = i
|
2020-01-29 18:34:31 +01:00
|
|
|
table = table[: i_max + 1]
|
2019-08-12 11:42:21 +02:00
|
|
|
return table
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
def _compute_depreciation_table_lines(
|
|
|
|
self, table, depreciation_start_date, depreciation_stop_date, line_dates
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2022-11-29 23:30:33 +01:00
|
|
|
self.ensure_one()
|
|
|
|
currency = self.company_id.currency_id
|
2018-10-01 11:57:07 +02:00
|
|
|
asset_sign = 1 if self.depreciation_base >= 0 else -1
|
2018-08-05 20:33:56 +02:00
|
|
|
i_max = len(table) - 1
|
|
|
|
remaining_value = self.depreciation_base
|
|
|
|
depreciated_value = 0.0
|
2021-03-22 08:11:23 +01:00
|
|
|
company = self.company_id
|
|
|
|
fiscalyear_lock_date = company.fiscalyear_lock_date or fields.Date.to_date(
|
|
|
|
"1901-01-01"
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
for i, entry in enumerate(table):
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
fy_amount_check = 0.0
|
2020-01-29 18:34:31 +01:00
|
|
|
fy_amount = entry["fy_amount"]
|
2018-08-05 20:33:56 +02:00
|
|
|
li_max = len(line_dates) - 1
|
2020-01-29 18:34:31 +01:00
|
|
|
prev_date = max(entry["date_start"], depreciation_start_date)
|
2018-08-05 20:33:56 +02:00
|
|
|
for li, line_date in enumerate(line_dates):
|
2019-07-08 13:21:20 +02:00
|
|
|
line_days = (line_date - prev_date).days + 1
|
2022-11-29 23:30:33 +01:00
|
|
|
if currency.is_zero(remaining_value):
|
2018-08-05 20:33:56 +02:00
|
|
|
break
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
if line_date > min(entry["date_stop"], depreciation_stop_date) and not (
|
|
|
|
i == i_max and li == li_max
|
|
|
|
):
|
2019-07-08 13:21:20 +02:00
|
|
|
prev_date = line_date
|
2018-08-05 20:33:56 +02:00
|
|
|
break
|
2019-07-08 13:21:20 +02:00
|
|
|
else:
|
|
|
|
prev_date = line_date + relativedelta(days=1)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
if (
|
|
|
|
self.method == "degr-linear"
|
2022-11-29 23:30:33 +01:00
|
|
|
and currency.compare_amounts(
|
|
|
|
asset_sign * (fy_amount - fy_amount_check), 0
|
|
|
|
)
|
|
|
|
< 0
|
2020-01-29 18:34:31 +01:00
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
if i == 0 and li == 0:
|
2022-11-29 23:30:33 +01:00
|
|
|
if currency.compare_amounts(entry.get("day_amount"), 0) > 0:
|
2020-01-29 18:34:31 +01:00
|
|
|
amount = line_days * entry.get("day_amount")
|
2019-07-08 13:21:20 +02:00
|
|
|
else:
|
|
|
|
amount = self._get_first_period_amount(
|
2020-01-29 18:34:31 +01:00
|
|
|
table, entry, depreciation_start_date, line_dates
|
|
|
|
)
|
2022-11-29 23:30:33 +01:00
|
|
|
amount = currency.round(amount)
|
2018-08-05 20:33:56 +02:00
|
|
|
else:
|
2022-11-29 23:30:33 +01:00
|
|
|
if currency.compare_amounts(entry.get("day_amount"), 0) > 0:
|
2020-01-29 18:34:31 +01:00
|
|
|
amount = line_days * entry.get("day_amount")
|
2019-07-08 13:21:20 +02:00
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
amount = entry.get("period_amount")
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
# last year, last entry
|
|
|
|
# Handle rounding deviations.
|
|
|
|
if i == i_max and li == li_max:
|
|
|
|
amount = remaining_value
|
|
|
|
remaining_value = 0.0
|
|
|
|
else:
|
|
|
|
remaining_value -= amount
|
|
|
|
fy_amount_check += amount
|
|
|
|
line = {
|
2020-01-29 18:34:31 +01:00
|
|
|
"date": line_date,
|
|
|
|
"days": line_days,
|
|
|
|
"amount": amount,
|
|
|
|
"depreciated_value": depreciated_value,
|
|
|
|
"remaining_value": remaining_value,
|
2021-03-22 08:11:23 +01:00
|
|
|
"init": fiscalyear_lock_date >= line_date,
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
lines.append(line)
|
|
|
|
depreciated_value += amount
|
|
|
|
|
|
|
|
# Handle rounding and extended/shortened FY deviations.
|
|
|
|
#
|
|
|
|
# Remark:
|
|
|
|
# In account_asset_management version < 8.0.2.8.0
|
|
|
|
# the FY deviation for the first FY
|
|
|
|
# was compensated in the first FY depreciation line.
|
|
|
|
# The code has now been simplified with compensation
|
|
|
|
# always in last FT depreciation line.
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.method_time == "year" and not entry.get("day_amount"):
|
2022-11-29 23:30:33 +01:00
|
|
|
if not currency.is_zero(fy_amount_check - fy_amount):
|
2018-08-05 20:33:56 +02:00
|
|
|
diff = fy_amount_check - fy_amount
|
|
|
|
amount = amount - diff
|
|
|
|
remaining_value += diff
|
2020-01-29 18:34:31 +01:00
|
|
|
lines[-1].update(
|
|
|
|
{"amount": amount, "remaining_value": remaining_value}
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
depreciated_value -= diff
|
|
|
|
|
|
|
|
if not lines:
|
|
|
|
table.pop(i)
|
|
|
|
else:
|
2020-01-29 18:34:31 +01:00
|
|
|
entry["lines"] = lines
|
2018-08-05 20:33:56 +02:00
|
|
|
line_dates = line_dates[li:]
|
|
|
|
|
2020-01-29 18:37:08 +01:00
|
|
|
for entry in table:
|
2020-01-29 18:34:31 +01:00
|
|
|
if not entry["fy_amount"]:
|
2022-04-13 10:58:58 +02:00
|
|
|
entry["fy_amount"] = sum(line["amount"] for line in entry["lines"])
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2019-08-10 14:40:49 +02:00
|
|
|
def _get_fy_info(self, date):
|
|
|
|
"""Return an homogeneus data structure for fiscal years."""
|
|
|
|
fy_info = self.company_id.compute_fiscalyear_dates(date)
|
2020-01-29 18:34:31 +01:00
|
|
|
if "record" not in fy_info:
|
|
|
|
fy_info["record"] = DummyFy(
|
|
|
|
date_from=fy_info["date_from"], date_to=fy_info["date_to"]
|
2019-08-10 14:40:49 +02:00
|
|
|
)
|
|
|
|
return fy_info
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2019-08-10 14:40:49 +02:00
|
|
|
def _compute_depreciation_table(self):
|
2018-08-05 20:33:56 +02:00
|
|
|
table = []
|
2020-01-29 18:34:31 +01:00
|
|
|
if (
|
|
|
|
self.method_time in ["year", "number"]
|
|
|
|
and not self.method_number
|
|
|
|
and not self.method_end
|
|
|
|
):
|
2018-08-05 20:33:56 +02:00
|
|
|
return table
|
2019-01-11 15:24:15 +01:00
|
|
|
asset_date_start = self.date_start
|
2019-08-10 14:40:49 +02:00
|
|
|
depreciation_start_date = self._get_depreciation_start_date(
|
2020-01-29 18:34:31 +01:00
|
|
|
self._get_fy_info(asset_date_start)["record"]
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
depreciation_stop_date = self._get_depreciation_stop_date(
|
2020-01-29 18:34:31 +01:00
|
|
|
depreciation_start_date
|
|
|
|
)
|
2019-08-10 14:40:49 +02:00
|
|
|
fy_date_start = asset_date_start
|
2018-08-05 20:33:56 +02:00
|
|
|
while fy_date_start <= depreciation_stop_date:
|
2019-08-10 14:40:49 +02:00
|
|
|
fy_info = self._get_fy_info(fy_date_start)
|
2020-01-29 18:34:31 +01:00
|
|
|
table.append(
|
|
|
|
{
|
|
|
|
"fy": fy_info["record"],
|
|
|
|
"date_start": fy_info["date_from"],
|
|
|
|
"date_stop": fy_info["date_to"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
fy_date_start = fy_info["date_to"] + relativedelta(days=1)
|
2018-08-05 20:33:56 +02:00
|
|
|
# Step 1:
|
|
|
|
# Calculate depreciation amount per fiscal year.
|
|
|
|
# This is calculation is skipped for method_time != 'year'.
|
|
|
|
line_dates = self._compute_line_dates(
|
2020-01-29 18:34:31 +01:00
|
|
|
table, depreciation_start_date, depreciation_stop_date
|
|
|
|
)
|
2019-08-12 11:42:21 +02:00
|
|
|
table = self._compute_depreciation_amount_per_fiscal_year(
|
2019-07-08 13:21:20 +02:00
|
|
|
table, line_dates, depreciation_start_date, depreciation_stop_date
|
2019-08-12 11:42:21 +02:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
# Step 2:
|
|
|
|
# Spread depreciation amount per fiscal year
|
|
|
|
# over the depreciation periods.
|
|
|
|
self._compute_depreciation_table_lines(
|
2020-01-29 18:34:31 +01:00
|
|
|
table, depreciation_start_date, depreciation_stop_date, line_dates
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
return table
|
|
|
|
|
|
|
|
def _get_depreciation_entry_name(self, seq):
|
2022-03-30 21:38:04 +02:00
|
|
|
"""use this method to customise the name of the accounting entry"""
|
2020-01-29 18:34:31 +01:00
|
|
|
return (self.code or str(self.id)) + "/" + str(seq)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def _compute_entries(self, date_end, check_triggers=False):
|
2018-10-01 11:57:07 +02:00
|
|
|
# TODO : add ir_cron job calling this method to
|
2018-08-05 20:33:56 +02:00
|
|
|
# generate periodical accounting entries
|
|
|
|
result = []
|
2020-01-29 18:34:31 +01:00
|
|
|
error_log = ""
|
2018-08-05 20:33:56 +02:00
|
|
|
if check_triggers:
|
2020-01-29 18:34:31 +01:00
|
|
|
recompute_obj = self.env["account.asset.recompute.trigger"]
|
|
|
|
recomputes = recompute_obj.sudo().search([("state", "=", "open")])
|
2020-01-29 18:37:08 +01:00
|
|
|
if recomputes:
|
|
|
|
trigger_companies = recomputes.mapped("company_id")
|
|
|
|
for asset in self:
|
|
|
|
if asset.company_id.id in trigger_companies.ids:
|
|
|
|
asset.compute_depreciation_board()
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
depreciations = self.env["account.asset.line"].search(
|
|
|
|
[
|
|
|
|
("asset_id", "in", self.ids),
|
|
|
|
("type", "=", "depreciate"),
|
|
|
|
("init_entry", "=", False),
|
|
|
|
("line_date", "<=", date_end),
|
|
|
|
("move_check", "=", False),
|
|
|
|
],
|
|
|
|
order="line_date",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
for depreciation in depreciations:
|
|
|
|
try:
|
2018-10-01 11:57:07 +02:00
|
|
|
with self.env.cr.savepoint():
|
2018-08-05 20:33:56 +02:00
|
|
|
result += depreciation.create_move()
|
2023-12-04 13:46:15 +01:00
|
|
|
except Exception as e:
|
2020-01-29 18:34:31 +01:00
|
|
|
tb = "".join(format_exception(*exc_info()))
|
2019-10-28 15:38:58 +01:00
|
|
|
asset_ref = depreciation.asset_id.name
|
|
|
|
if depreciation.asset_id.code:
|
2020-01-29 18:34:31 +01:00
|
|
|
asset_ref = "[{}] {}".format(depreciation.asset_id.code, asset_ref)
|
2022-04-13 12:18:36 +02:00
|
|
|
error_log += _(
|
|
|
|
"\nError while processing asset '{ref}': {exception}"
|
2023-12-04 13:46:15 +01:00
|
|
|
).format(ref=asset_ref, exception=repr(e))
|
2022-04-13 12:18:36 +02:00
|
|
|
error_msg = _("Error while processing asset '{ref}': \n\n{tb}").format(
|
|
|
|
ref=asset_ref, tb=tb
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
_logger.error("%s, %s", self._name, error_msg)
|
|
|
|
|
|
|
|
if check_triggers and recomputes:
|
2020-01-29 18:34:31 +01:00
|
|
|
companies = recomputes.mapped("company_id")
|
|
|
|
triggers = recomputes.filtered(lambda r: r.company_id.id in companies.ids)
|
2018-08-05 20:33:56 +02:00
|
|
|
if triggers:
|
|
|
|
recompute_vals = {
|
2020-01-29 18:34:31 +01:00
|
|
|
"date_completed": fields.Datetime.now(),
|
|
|
|
"state": "done",
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
triggers.sudo().write(recompute_vals)
|
|
|
|
|
|
|
|
return (result, error_log)
|
2019-10-06 23:24:00 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _xls_acquisition_fields(self):
|
|
|
|
"""
|
|
|
|
Update list in custom module to add/drop columns or change order
|
|
|
|
"""
|
|
|
|
return [
|
|
|
|
"account",
|
|
|
|
"name",
|
|
|
|
"code",
|
|
|
|
"date_start",
|
2021-09-15 17:53:29 +02:00
|
|
|
"purchase_value",
|
2019-10-06 23:24:00 +02:00
|
|
|
"depreciation_base",
|
|
|
|
"salvage_value",
|
|
|
|
]
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _xls_active_fields(self):
|
|
|
|
"""
|
|
|
|
Update list in custom module to add/drop columns or change order
|
|
|
|
"""
|
|
|
|
return [
|
|
|
|
"account",
|
|
|
|
"name",
|
|
|
|
"code",
|
|
|
|
"date_start",
|
2021-09-15 17:53:29 +02:00
|
|
|
"purchase_value",
|
2019-10-06 23:24:00 +02:00
|
|
|
"depreciation_base",
|
|
|
|
"salvage_value",
|
|
|
|
"period_start_value",
|
|
|
|
"period_depr",
|
|
|
|
"period_end_value",
|
|
|
|
"period_end_depr",
|
|
|
|
"method",
|
|
|
|
"method_number",
|
|
|
|
"prorata",
|
|
|
|
"state",
|
|
|
|
]
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _xls_removal_fields(self):
|
|
|
|
"""
|
|
|
|
Update list in custom module to add/drop columns or change order
|
|
|
|
"""
|
|
|
|
return [
|
|
|
|
"account",
|
|
|
|
"name",
|
|
|
|
"code",
|
|
|
|
"date_remove",
|
2021-09-15 17:53:29 +02:00
|
|
|
"purchase_value",
|
2019-10-06 23:24:00 +02:00
|
|
|
"depreciation_base",
|
|
|
|
"salvage_value",
|
|
|
|
]
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _xls_asset_template(self):
|
|
|
|
"""
|
|
|
|
Template updates
|
|
|
|
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _xls_acquisition_template(self):
|
|
|
|
"""
|
|
|
|
Template updates
|
|
|
|
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _xls_active_template(self):
|
|
|
|
"""
|
|
|
|
Template updates
|
|
|
|
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def _xls_removal_template(self):
|
|
|
|
"""
|
|
|
|
Template updates
|
|
|
|
|
|
|
|
"""
|
|
|
|
return {}
|