Skip to content

Commit

Permalink
[IMP] l10n_it_asset_management: refactoring for compatibility with Od…
Browse files Browse the repository at this point in the history
…oo Enterprise
  • Loading branch information
LorenzoC0 authored and eLBati committed Jun 13, 2024
1 parent 82c0f17 commit 3376878
Show file tree
Hide file tree
Showing 25 changed files with 215 additions and 185 deletions.
10 changes: 5 additions & 5 deletions l10n_it_asset_management/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AccountMove(models.Model):
"asset.accounting.info", "move_id", string="Assets Accounting Info"
)

asset_ids = fields.Many2many(
l10n_it_asset_ids = fields.Many2many(
"asset.asset", compute="_compute_asset_data", store=True, string="Assets"
)

Expand Down Expand Up @@ -59,19 +59,19 @@ def button_cancel(self):

@api.depends(
"asset_accounting_info_ids",
"asset_accounting_info_ids.asset_id",
"asset_accounting_info_ids.l10n_it_asset_id",
"asset_accounting_info_ids.dep_line_id",
)
def _compute_asset_data(self):
for move in self:
aa_info = move.get_linked_aa_info_records()
assets = aa_info.mapped("asset_id")
assets = aa_info.mapped("l10n_it_asset_id")
dep_lines = aa_info.mapped("dep_line_id")
if dep_lines:
assets += dep_lines.mapped("asset_id")
assets += dep_lines.mapped("l10n_it_asset_id")
move.update(
{
"asset_ids": [Command.set(assets.ids)],
"l10n_it_asset_ids": [Command.set(assets.ids)],
"dep_line_ids": [Command.set(dep_lines.ids)],
}
)
Expand Down
10 changes: 5 additions & 5 deletions l10n_it_asset_management/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AccountMoveLine(models.Model):
"asset.accounting.info", "move_line_id", string="Assets Accounting Info"
)

asset_ids = fields.Many2many(
l10n_it_asset_ids = fields.Many2many(
"asset.asset", compute="_compute_asset_data", store=True, string="Assets"
)

Expand All @@ -41,19 +41,19 @@ def check_company(self):

@api.depends(
"asset_accounting_info_ids",
"asset_accounting_info_ids.asset_id",
"asset_accounting_info_ids.l10n_it_asset_id",
"asset_accounting_info_ids.dep_line_id",
)
def _compute_asset_data(self):
for line in self:
aa_info = line.get_linked_aa_info_records()
assets = aa_info.mapped("asset_id")
assets = aa_info.mapped("l10n_it_asset_id")
dep_lines = aa_info.mapped("dep_line_id")
if dep_lines:
assets += dep_lines.mapped("asset_id")
assets += dep_lines.mapped("l10n_it_asset_id")
line.update(
{
"asset_ids": [Command.set(assets.ids)],
"l10n_it_asset_ids": [Command.set(assets.ids)],
"dep_line_ids": [Command.set(dep_lines.ids)],
}
)
Expand Down
10 changes: 5 additions & 5 deletions l10n_it_asset_management/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_default_company_id(self):
return self.env.company

asset_accounting_info_ids = fields.One2many(
"asset.accounting.info", "asset_id", string="Accounting Info"
"asset.accounting.info", "l10n_it_asset_id", string="Accounting Info"
)

category_id = fields.Many2one(
Expand Down Expand Up @@ -50,7 +50,7 @@ def get_default_company_id(self):

depreciation_ids = fields.One2many(
"asset.depreciation",
"asset_id",
"l10n_it_asset_id",
string="Depreciations",
)

Expand Down Expand Up @@ -205,7 +205,7 @@ def launch_wizard_generate_depreciations(self):
ctx = dict(self._context)
ctx.update(
{
"default_asset_ids": [Command.set(self.ids)],
"default_l10n_it_asset_ids": [Command.set(self.ids)],
"default_category_ids": [Command.set(self.category_id.ids)],
"default_company_id": self.company_id.id,
"default_date": fields.Date.today(),
Expand Down Expand Up @@ -236,8 +236,8 @@ def get_linked_aa_info_records(self):
return self.env["asset.accounting.info"].search(
[
"|",
("asset_id", "=", self.id),
("dep_line_id.asset_id", "=", self.id),
("l10n_it_asset_id", "=", self.id),
("dep_line_id.l10n_it_asset_id", "=", self.id),
]
)

Expand Down
23 changes: 13 additions & 10 deletions l10n_it_asset_management/models/asset_accounting_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class AssetAccountingInfo(models.Model):
_description = "Asset Accounting Relations"
_table = "asset_accounting_info"

asset_id = fields.Many2one("asset.asset", ondelete="set null", string="Asset")
l10n_it_asset_id = fields.Many2one(
"asset.asset", ondelete="set null", string="Asset"
)

company_id = fields.Many2one(
"res.company",
Expand Down Expand Up @@ -102,7 +104,7 @@ def cron_vacuum_table(self):
@api.model
def get_main_fields(self):
return [
"asset_id",
"l10n_it_asset_id",
"dep_line_id",
"move_id",
"move_line_id",
Expand Down Expand Up @@ -143,12 +145,13 @@ def check_company_coherence(self):
def check_data_coherence(self):
self.ensure_one()

# If dep_line_id and asset_id are set, check whether the depreciation
# If dep_line_id and l10n_it_asset_id are set, check whether the depreciation
# line belongs to the given asset
if (
self.asset_id
self.l10n_it_asset_id
and self.dep_line_id
and self.asset_id != self.dep_line_id.depreciation_id.asset_id
and self.l10n_it_asset_id
!= self.dep_line_id.depreciation_id.l10n_it_asset_id
):
raise ValidationError(_("Incoherent asset data."))

Expand Down Expand Up @@ -179,8 +182,8 @@ def get_normalized_info_vals(self):
vals = {}

# Set asset as dep line's asset if dep line is set
if not self.asset_id and self.dep_line_id:
vals["asset_id"] = self.dep_line_id.asset_id.id
if not self.l10n_it_asset_id and self.dep_line_id:
vals["l10n_it_asset_id"] = self.dep_line_id.l10n_it_asset_id.id

# Set move_id as move line's move if move line is set
if not self.move_id and self.move_line_id:
Expand All @@ -206,7 +209,7 @@ def get_records_to_delete_by_cron(self):
[
"|",
"&",
("asset_id", "=", False),
("l10n_it_asset_id", "=", False),
("dep_line_id", "=", False),
"&",
("move_id", "=", False),
Expand All @@ -216,8 +219,8 @@ def get_records_to_delete_by_cron(self):

def make_name(self):
self.ensure_one()
if self.asset_id:
name = self.asset_id.make_name()
if self.l10n_it_asset_id:
name = self.l10n_it_asset_id.make_name()

Check warning on line 223 in l10n_it_asset_management/models/asset_accounting_info.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_management/models/asset_accounting_info.py#L223

Added line #L223 was not covered by tests
else:
name = _("Unknown Asset")
relation_name = dict(self._fields["relation_type"].selection).get(
Expand Down
45 changes: 26 additions & 19 deletions l10n_it_asset_management/models/asset_depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AssetDepreciation(models.Model):
string="Residual Amount",
)

asset_id = fields.Many2one(
l10n_it_asset_id = fields.Many2one(
"asset.asset",
ondelete="cascade",
readonly=True,
Expand All @@ -78,11 +78,17 @@ class AssetDepreciation(models.Model):
)

company_id = fields.Many2one(
"res.company", readonly=True, related="asset_id.company_id", string="Company"
"res.company",
readonly=True,
related="l10n_it_asset_id.company_id",
string="Company",
)

currency_id = fields.Many2one(
"res.currency", readonly=True, related="asset_id.currency_id", string="Currency"
"res.currency",
readonly=True,
related="l10n_it_asset_id.currency_id",
string="Currency",
)

date_start = fields.Date()
Expand Down Expand Up @@ -196,9 +202,9 @@ def _compute_state(self):
for dep in self:
dep.state = dep.get_depreciation_state()

@api.onchange("asset_id", "base_coeff")
@api.onchange("l10n_it_asset_id", "base_coeff")
def onchange_base_coeff(self):
purchase_amount = self.asset_id.purchase_amount
purchase_amount = self.l10n_it_asset_id.purchase_amount

Check warning on line 207 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#L207

Added line #L207 was not covered by tests
self.amount_depreciable = self.base_coeff * purchase_amount

@api.onchange("first_dep_nr")
Expand Down Expand Up @@ -235,8 +241,8 @@ def onchange_force_dep_nrs(self):
"line_ids.amount",
"line_ids.balance",
"line_ids.move_type",
"asset_id.sold",
"asset_id.dismissed",
"l10n_it_asset_id.sold",
"l10n_it_asset_id.dismissed",
)
def _compute_amounts(self):
for dep in self:
Expand Down Expand Up @@ -275,8 +281,8 @@ def check_before_generate_depreciation_lines(self, dep_date):
draft_names = ", ".join(
[
asset_name
for asset_id, asset_name in draft_lines.mapped(
"depreciation_id.asset_id"
for l10n_it_asset_id, asset_name in draft_lines.mapped(
"depreciation_id.l10n_it_asset_id"
).name_get()
]
)
Expand Down Expand Up @@ -307,7 +313,7 @@ def generate_depreciation_lines_single(self, dep_date):
if self.last_depreciation_date and self.last_depreciation_date > dep_date:
return res
dep_nr = self.get_max_depreciation_nr() + 1
dep = self.with_context(dep_nr=dep_nr, used_asset=self.asset_id.used)
dep = self.with_context(dep_nr=dep_nr, used_asset=self.l10n_it_asset_id.used)
dep_amount = dep.get_depreciation_amount(dep_date)
if not dep_amount:
return res
Expand Down Expand Up @@ -339,7 +345,7 @@ def get_computed_amounts(self):
if f"amount_{k}" in self._fields
}

if self.asset_id.sold or self.asset_id.dismissed:
if self.l10n_it_asset_id.sold or self.l10n_it_asset_id.dismissed:
vals.update({"amount_depreciable_updated": 0, "amount_residual": 0})
else:
non_residual_types = self.line_ids.get_non_residual_move_types()
Expand Down Expand Up @@ -477,29 +483,30 @@ def get_depreciation_state(self):
def get_dismiss_account_move_line_vals(self):
self.ensure_one()
credit_line_vals = {
"account_id": self.asset_id.category_id.asset_account_id.id,
"account_id": self.l10n_it_asset_id.category_id.asset_account_id.id,
"credit": self.amount_depreciated,
"debit": 0.0,
"currency_id": self.currency_id.id,
"name": _("Asset dismissal: ") + self.asset_id.make_name(),
"name": _("Asset dismissal: ") + self.l10n_it_asset_id.make_name(),
}
debit_line_vals = {
"account_id": self.asset_id.category_id.fund_account_id.id,
"account_id": self.l10n_it_asset_id.category_id.fund_account_id.id,
"credit": 0.0,
"debit": self.amount_depreciated,
"currency_id": self.currency_id.id,
"name": _("Asset dismissal: ") + self.asset_id.make_name(),
"name": _("Asset dismissal: ") + self.l10n_it_asset_id.make_name(),
}
return [credit_line_vals, debit_line_vals]

def get_dismiss_account_move_vals(self):
self.ensure_one()
return {
"company_id": self.company_id.id,
"date": self._context.get("dismiss_date") or self.asset_id.sale_date,
"journal_id": self.asset_id.category_id.journal_id.id,
"date": self._context.get("dismiss_date")
or self.l10n_it_asset_id.sale_date,
"journal_id": self.l10n_it_asset_id.category_id.journal_id.id,
"line_ids": [],
"ref": _("Asset dismissal: ") + self.asset_id.make_name(),
"ref": _("Asset dismissal: ") + self.l10n_it_asset_id.make_name(),
"move_type": "entry",
}

Expand Down Expand Up @@ -580,7 +587,7 @@ def get_pro_rata_temporis_multiplier(self, date=None, mode="std"):

def make_name(self):
self.ensure_one()
return " - ".join((self.asset_id.make_name(), self.type_id.name or ""))
return " - ".join((self.l10n_it_asset_id.make_name(), self.type_id.name or ""))

Check warning on line 590 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#L590

Added line #L590 was not covered by tests

def need_normalize_first_dep_nr(self):
self.ensure_one()
Expand Down
Loading

0 comments on commit 3376878

Please sign in to comment.