2018-08-05 20:33:56 +02:00
|
|
|
# Copyright 2009-2018 Noviat
|
2021-01-14 08:53:25 +01:00
|
|
|
# Copyright 2021 Tecnativa - João Marques
|
2018-08-05 20:33:56 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
from odoo import _, api, fields, models
|
2018-08-05 20:33:56 +02:00
|
|
|
from odoo.exceptions import UserError
|
|
|
|
|
|
|
|
|
|
|
|
class AccountAssetLine(models.Model):
|
2020-01-29 18:34:31 +01:00
|
|
|
_name = "account.asset.line"
|
|
|
|
_description = "Asset depreciation table line"
|
|
|
|
_order = "type, line_date"
|
2021-02-11 11:48:48 +01:00
|
|
|
_check_company_auto = True
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
name = fields.Char(string="Depreciation Name", size=64, readonly=True)
|
2018-08-05 20:33:56 +02:00
|
|
|
asset_id = fields.Many2one(
|
2021-02-23 13:59:08 +01:00
|
|
|
comodel_name="account.asset",
|
|
|
|
string="Asset",
|
|
|
|
required=True,
|
|
|
|
ondelete="cascade",
|
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
|
|
|
previous_id = fields.Many2one(
|
2020-01-29 18:34:31 +01:00
|
|
|
comodel_name="account.asset.line",
|
|
|
|
string="Previous Depreciation Line",
|
2018-10-01 11:57:07 +02:00
|
|
|
readonly=True,
|
|
|
|
)
|
2020-01-29 18:34:31 +01:00
|
|
|
parent_state = fields.Selection(
|
|
|
|
related="asset_id.state", string="State of Asset", readonly=True
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
depreciation_base = fields.Float(
|
2020-01-29 18:34:31 +01:00
|
|
|
related="asset_id.depreciation_base", string="Depreciation Base", readonly=True
|
2018-10-01 11:57:07 +02:00
|
|
|
)
|
2020-01-29 18:37:08 +01:00
|
|
|
amount = fields.Float(string="Amount", digits="Account", required=True)
|
2018-08-05 20:33:56 +02:00
|
|
|
remaining_value = fields.Float(
|
2020-01-29 18:34:31 +01:00
|
|
|
compute="_compute_values",
|
2020-01-29 18:37:08 +01:00
|
|
|
digits="Account",
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Next Period Depreciation",
|
|
|
|
store=True,
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
depreciated_value = fields.Float(
|
2020-01-29 18:34:31 +01:00
|
|
|
compute="_compute_values",
|
2020-01-29 18:37:08 +01:00
|
|
|
digits="Account",
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Amount Already Depreciated",
|
|
|
|
store=True,
|
2019-07-08 13:21:20 +02:00
|
|
|
)
|
2020-01-29 18:34:31 +01:00
|
|
|
line_date = fields.Date(string="Date", required=True)
|
|
|
|
line_days = fields.Integer(string="Days", readonly=True)
|
2018-08-05 20:33:56 +02:00
|
|
|
move_id = fields.Many2one(
|
2021-02-23 13:59:08 +01:00
|
|
|
comodel_name="account.move",
|
|
|
|
string="Depreciation Entry",
|
|
|
|
readonly=True,
|
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_check = fields.Boolean(
|
2020-01-29 18:34:31 +01:00
|
|
|
compute="_compute_move_check", string="Posted", store=True
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
type = fields.Selection(
|
|
|
|
selection=[
|
2020-01-29 18:34:31 +01:00
|
|
|
("create", "Depreciation Base"),
|
|
|
|
("depreciate", "Depreciation"),
|
|
|
|
("remove", "Asset Removal"),
|
|
|
|
],
|
|
|
|
readonly=True,
|
|
|
|
default="depreciate",
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
init_entry = fields.Boolean(
|
2020-01-29 18:34:31 +01:00
|
|
|
string="Initial Balance Entry",
|
2018-08-05 20:33:56 +02:00
|
|
|
help="Set this flag for entries of previous fiscal years "
|
2020-01-29 18:34:31 +01:00
|
|
|
"for which Odoo has not generated accounting entries.",
|
|
|
|
)
|
2021-02-11 11:48:48 +01:00
|
|
|
company_id = fields.Many2one(
|
2021-07-21 06:28:38 +02:00
|
|
|
"res.company",
|
|
|
|
store=True,
|
|
|
|
readonly=True,
|
|
|
|
related="asset_id.company_id",
|
2021-02-11 11:48:48 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.depends("amount", "previous_id", "type")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _compute_values(self):
|
2020-08-12 17:47:23 +02:00
|
|
|
self.depreciated_value = 0.0
|
|
|
|
self.remaining_value = 0.0
|
2018-08-05 20:33:56 +02:00
|
|
|
dlines = self
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.env.context.get("no_compute_asset_line_ids"):
|
2018-08-05 20:33:56 +02:00
|
|
|
# skip compute for lines in unlink
|
2020-01-29 18:34:31 +01:00
|
|
|
exclude_ids = self.env.context["no_compute_asset_line_ids"]
|
2018-10-01 11:57:07 +02:00
|
|
|
dlines = self.filtered(lambda l: l.id not in exclude_ids)
|
2020-01-29 18:34:31 +01:00
|
|
|
dlines = dlines.filtered(lambda l: l.type == "depreciate")
|
2018-08-05 20:33:56 +02:00
|
|
|
dlines = dlines.sorted(key=lambda l: l.line_date)
|
2020-07-09 02:11:10 +02:00
|
|
|
# Give value 0 to the lines that are not going to be calculated
|
|
|
|
# to avoid cache miss error
|
|
|
|
all_excluded_lines = self - dlines
|
|
|
|
all_excluded_lines.depreciated_value = 0
|
|
|
|
all_excluded_lines.remaining_value = 0
|
2019-05-24 08:48:58 +02:00
|
|
|
# Group depreciation lines per asset
|
2020-01-29 18:34:31 +01:00
|
|
|
asset_ids = dlines.mapped("asset_id")
|
2019-05-24 08:48:58 +02:00
|
|
|
grouped_dlines = []
|
|
|
|
for asset in asset_ids:
|
2020-01-29 18:34:31 +01:00
|
|
|
grouped_dlines.append(dlines.filtered(lambda l: l.asset_id.id == asset.id))
|
2019-05-24 08:48:58 +02:00
|
|
|
for dlines in grouped_dlines:
|
|
|
|
for i, dl in enumerate(dlines):
|
|
|
|
if i == 0:
|
|
|
|
depreciation_base = dl.depreciation_base
|
|
|
|
tmp = depreciation_base - dl.previous_id.remaining_value
|
|
|
|
depreciated_value = dl.previous_id and tmp or 0.0
|
2020-01-29 18:34:31 +01:00
|
|
|
remaining_value = depreciation_base - depreciated_value - dl.amount
|
2019-05-24 08:48:58 +02:00
|
|
|
else:
|
|
|
|
depreciated_value += dl.previous_id.amount
|
|
|
|
remaining_value -= dl.amount
|
|
|
|
dl.depreciated_value = depreciated_value
|
|
|
|
dl.remaining_value = remaining_value
|
2018-08-05 20:33:56 +02:00
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.depends("move_id")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _compute_move_check(self):
|
|
|
|
for line in self:
|
|
|
|
line.move_check = bool(line.move_id)
|
|
|
|
|
2020-01-29 18:34:31 +01:00
|
|
|
@api.onchange("amount")
|
2018-08-05 20:33:56 +02:00
|
|
|
def _onchange_amount(self):
|
2020-01-29 18:34:31 +01:00
|
|
|
if self.type == "depreciate":
|
|
|
|
self.remaining_value = (
|
|
|
|
self.depreciation_base - self.depreciated_value - self.amount
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def write(self, vals):
|
|
|
|
for dl in self:
|
2020-01-29 18:34:31 +01:00
|
|
|
line_date = vals.get("line_date") or dl.line_date
|
2018-08-05 20:33:56 +02:00
|
|
|
asset_lines = dl.asset_id.depreciation_line_ids
|
2020-01-29 18:34:31 +01:00
|
|
|
if list(vals.keys()) == ["move_id"] and not vals["move_id"]:
|
2018-08-05 20:33:56 +02:00
|
|
|
# allow to remove an accounting entry via the
|
|
|
|
# 'Delete Move' button on the depreciation lines.
|
2020-01-29 18:34:31 +01:00
|
|
|
if not self.env.context.get("unlink_from_asset"):
|
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"You are not allowed to remove an accounting entry "
|
|
|
|
"linked to an asset."
|
|
|
|
"\nYou should remove such entries from the asset."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif list(vals.keys()) == ["asset_id"]:
|
2018-08-05 20:33:56 +02:00
|
|
|
continue
|
2020-08-06 12:33:27 +02:00
|
|
|
elif (
|
|
|
|
dl.move_id
|
|
|
|
and not self.env.context.get("allow_asset_line_update")
|
|
|
|
and dl.type != "create"
|
|
|
|
):
|
2020-01-29 18:34:31 +01:00
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"You cannot change a depreciation line "
|
|
|
|
"with an associated accounting entry."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif vals.get("init_entry"):
|
2018-08-05 20:33:56 +02:00
|
|
|
check = asset_lines.filtered(
|
2020-01-29 18:34:31 +01:00
|
|
|
lambda l: l.move_check
|
|
|
|
and l.type == "depreciate"
|
|
|
|
and l.line_date <= line_date
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
if check:
|
2020-01-29 18:34:31 +01:00
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"You cannot set the 'Initial Balance Entry' flag "
|
|
|
|
"on a depreciation line "
|
|
|
|
"with prior posted entries."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif vals.get("line_date"):
|
|
|
|
if dl.type == "create":
|
2018-08-05 20:33:56 +02:00
|
|
|
check = asset_lines.filtered(
|
2020-01-29 18:34:31 +01:00
|
|
|
lambda l: l.type != "create"
|
|
|
|
and (l.init_entry or l.move_check)
|
|
|
|
and l.line_date < fields.Date.to_date(vals["line_date"])
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
if check:
|
|
|
|
raise UserError(
|
2020-01-29 18:34:31 +01:00
|
|
|
_(
|
|
|
|
"You cannot set the Asset Start Date "
|
|
|
|
"after already posted entries."
|
|
|
|
)
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
else:
|
|
|
|
check = asset_lines.filtered(
|
2020-01-29 18:37:08 +01:00
|
|
|
lambda al: al != dl
|
|
|
|
and (al.init_entry or al.move_check)
|
|
|
|
and al.line_date > fields.Date.to_date(vals["line_date"])
|
2020-01-29 18:34:31 +01:00
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
if check:
|
2020-01-29 18:34:31 +01:00
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"You cannot set the date on a depreciation line "
|
|
|
|
"prior to already posted entries."
|
|
|
|
)
|
|
|
|
)
|
2018-10-01 11:57:07 +02:00
|
|
|
return super().write(vals)
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def unlink(self):
|
|
|
|
for dl in self:
|
2020-01-29 18:34:31 +01:00
|
|
|
if dl.type == "create" and dl.amount:
|
|
|
|
raise UserError(
|
|
|
|
_("You cannot remove an asset line " "of type 'Depreciation Base'.")
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
elif dl.move_id:
|
2020-01-29 18:34:31 +01:00
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"You cannot delete a depreciation line with "
|
|
|
|
"an associated accounting entry."
|
|
|
|
)
|
|
|
|
)
|
2018-08-05 20:33:56 +02:00
|
|
|
previous = dl.previous_id
|
2018-10-01 11:57:07 +02:00
|
|
|
next_line = dl.asset_id.depreciation_line_ids.filtered(
|
2020-01-29 18:34:31 +01:00
|
|
|
lambda l: l.previous_id == dl and l not in self
|
|
|
|
)
|
2018-10-01 11:57:07 +02:00
|
|
|
if next_line:
|
|
|
|
next_line.previous_id = previous
|
2020-01-29 18:34:31 +01:00
|
|
|
return super(
|
|
|
|
AccountAssetLine, self.with_context(no_compute_asset_line_ids=self.ids)
|
|
|
|
).unlink()
|
2018-08-05 20:33:56 +02:00
|
|
|
|
|
|
|
def _setup_move_data(self, depreciation_date):
|
|
|
|
asset = self.asset_id
|
|
|
|
move_data = {
|
2020-01-29 18:34:31 +01:00
|
|
|
"date": depreciation_date,
|
2020-01-29 18:37:08 +01:00
|
|
|
"ref": "{} - {}".format(asset.name, self.name),
|
2020-01-29 18:34:31 +01:00
|
|
|
"journal_id": asset.profile_id.journal_id.id,
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
return move_data
|
|
|
|
|
2018-10-01 11:57:07 +02:00
|
|
|
def _setup_move_line_data(self, depreciation_date, account, ml_type, move):
|
2021-04-06 15:24:08 +02:00
|
|
|
"""Prepare data to be propagated to account.move.line"""
|
2018-08-05 20:33:56 +02:00
|
|
|
asset = self.asset_id
|
|
|
|
amount = self.amount
|
|
|
|
analytic_id = False
|
2021-04-06 15:24:08 +02:00
|
|
|
analytic_tags = self.env["account.analytic.tag"]
|
2020-01-29 18:34:31 +01:00
|
|
|
if ml_type == "depreciation":
|
2018-08-05 20:33:56 +02:00
|
|
|
debit = amount < 0 and -amount or 0.0
|
|
|
|
credit = amount > 0 and amount or 0.0
|
2020-01-29 18:34:31 +01:00
|
|
|
elif ml_type == "expense":
|
2018-08-05 20:33:56 +02:00
|
|
|
debit = amount > 0 and amount or 0.0
|
|
|
|
credit = amount < 0 and -amount or 0.0
|
|
|
|
analytic_id = asset.account_analytic_id.id
|
2021-04-06 15:24:08 +02:00
|
|
|
analytic_tags = asset.analytic_tag_ids
|
2018-08-05 20:33:56 +02:00
|
|
|
move_line_data = {
|
2020-01-29 18:34:31 +01:00
|
|
|
"name": asset.name,
|
|
|
|
"ref": self.name,
|
|
|
|
"move_id": move.id,
|
|
|
|
"account_id": account.id,
|
|
|
|
"credit": credit,
|
|
|
|
"debit": debit,
|
|
|
|
"journal_id": asset.profile_id.journal_id.id,
|
|
|
|
"partner_id": asset.partner_id.id,
|
|
|
|
"analytic_account_id": analytic_id,
|
2021-04-06 15:24:08 +02:00
|
|
|
"analytic_tag_ids": [(4, tag.id) for tag in analytic_tags],
|
2020-01-29 18:34:31 +01:00
|
|
|
"date": depreciation_date,
|
|
|
|
"asset_id": asset.id,
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
return move_line_data
|
|
|
|
|
|
|
|
def create_move(self):
|
|
|
|
created_move_ids = []
|
2018-10-01 11:57:07 +02:00
|
|
|
asset_ids = set()
|
2020-01-29 18:34:31 +01:00
|
|
|
ctx = dict(self.env.context, allow_asset=True, check_move_validity=False)
|
2018-08-05 20:33:56 +02:00
|
|
|
for line in self:
|
|
|
|
asset = line.asset_id
|
|
|
|
depreciation_date = line.line_date
|
|
|
|
am_vals = line._setup_move_data(depreciation_date)
|
2020-01-29 18:34:31 +01:00
|
|
|
move = self.env["account.move"].with_context(ctx).create(am_vals)
|
2018-08-05 20:33:56 +02:00
|
|
|
depr_acc = asset.profile_id.account_depreciation_id
|
|
|
|
exp_acc = asset.profile_id.account_expense_depreciation_id
|
|
|
|
aml_d_vals = line._setup_move_line_data(
|
2020-01-29 18:34:31 +01:00
|
|
|
depreciation_date, depr_acc, "depreciation", move
|
|
|
|
)
|
|
|
|
self.env["account.move.line"].with_context(ctx).create(aml_d_vals)
|
2018-08-05 20:33:56 +02:00
|
|
|
aml_e_vals = line._setup_move_line_data(
|
2020-01-29 18:34:31 +01:00
|
|
|
depreciation_date, exp_acc, "expense", move
|
|
|
|
)
|
|
|
|
self.env["account.move.line"].with_context(ctx).create(aml_e_vals)
|
2021-01-14 08:53:25 +01:00
|
|
|
move.action_post()
|
2020-01-29 18:34:31 +01:00
|
|
|
line.with_context(allow_asset_line_update=True).write({"move_id": move.id})
|
2018-08-05 20:33:56 +02:00
|
|
|
created_move_ids.append(move.id)
|
2018-10-01 11:57:07 +02:00
|
|
|
asset_ids.add(asset.id)
|
2018-08-05 20:33:56 +02:00
|
|
|
# we re-evaluate the assets to determine if we can close them
|
2020-01-29 18:34:31 +01:00
|
|
|
for asset in self.env["account.asset"].browse(list(asset_ids)):
|
2018-10-01 11:57:07 +02:00
|
|
|
if asset.company_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
|
|
|
return created_move_ids
|
|
|
|
|
|
|
|
def open_move(self):
|
|
|
|
self.ensure_one()
|
|
|
|
return {
|
2020-01-29 18:34:31 +01:00
|
|
|
"name": _("Journal Entry"),
|
2021-10-30 15:05:14 +02:00
|
|
|
"view_mode": "form",
|
|
|
|
"res_id": self.move_id.id,
|
2020-01-29 18:34:31 +01:00
|
|
|
"res_model": "account.move",
|
|
|
|
"view_id": False,
|
|
|
|
"type": "ir.actions.act_window",
|
|
|
|
"context": self.env.context,
|
2018-08-05 20:33:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 17:34:48 +02:00
|
|
|
def update_asset_line_after_unlink_move(self):
|
|
|
|
self.write({"move_id": False})
|
|
|
|
if self.parent_state == "close":
|
|
|
|
self.asset_id.write({"state": "open"})
|
|
|
|
elif self.parent_state == "removed" and self.type == "remove":
|
|
|
|
self.asset_id.write({"state": "close", "date_remove": False})
|
|
|
|
self.unlink()
|
|
|
|
|
2018-08-05 20:33:56 +02:00
|
|
|
def unlink_move(self):
|
|
|
|
for line in self:
|
2021-07-06 17:34:48 +02:00
|
|
|
if line.asset_id.profile_id.allow_reversal:
|
|
|
|
context = dict(self._context or {})
|
|
|
|
context.update(
|
|
|
|
{
|
|
|
|
"active_model": self._name,
|
|
|
|
"active_ids": line.ids,
|
|
|
|
"active_id": line.id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
"name": _("Reverse Move"),
|
|
|
|
"view_mode": "form",
|
|
|
|
"res_model": "wiz.asset.move.reverse",
|
|
|
|
"target": "new",
|
|
|
|
"type": "ir.actions.act_window",
|
|
|
|
"context": context,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
move = line.move_id
|
|
|
|
move.button_draft()
|
|
|
|
move.with_context(force_delete=True, unlink_from_asset=True).unlink()
|
|
|
|
line.with_context(
|
|
|
|
unlink_from_asset=True
|
|
|
|
).update_asset_line_after_unlink_move()
|
2018-08-05 20:33:56 +02:00
|
|
|
return True
|