Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE - ONLY FOR TESTING PURPOSE #3345

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b433e34
[FIX][14.0] depreciable amount computation by date
sergiocorato May 19, 2023
0967321
[14.0][IMP] remove unuseful error message when delete asset depreciation
sergiocorato Mar 27, 2023
3963c25
[14.0][FIX] do not create depreciation lines with zero amount
sergiocorato Jan 6, 2023
a37cd0c
[14.0][FIX] get only not null dates
sergiocorato May 30, 2023
e613af7
[FIX] previsional report create only needed depreciation lines
sergiocorato May 19, 2023
64f60de
[14.0][FIX] currency convert function
sergiocorato May 30, 2023
4b1f23a
[14.0][FIX] assets_management view html report
sergiocorato May 31, 2023
c38afbb
[14.0][FIX] assets_management get in-out values
sergiocorato Jan 6, 2023
fc45d99
[IMP] autodelete accounting info when delete asset
sergiocorato Apr 5, 2023
b2aa573
[FIX][14.0] assets_management do not report assets sold before report…
sergiocorato May 25, 2023
c0d1c6c
[14.0][FIX] assets_management: includere nella stampa cespiti non anc…
sergiocorato Jun 19, 2023
ee85e4a
Merge branch '14.0-fix-assets_management-depreciation-with-incremente…
sergiocorato Jun 19, 2023
bacabb8
Merge branch '14.0-imp-assets_management-auto-delete-asset_accounting…
sergiocorato Jun 19, 2023
95f0e0f
Merge branch '14.0-fix-assets_management-hide-sold-assets' of git+ssh…
sergiocorato Jun 19, 2023
3a83b59
Merge branch '14.0-fix-assets_management-get-in-out-values' of git+ss…
sergiocorato Jun 19, 2023
a5fe820
Merge branch '14.0-fix-assets_management-do-not-create-zero-dep-line'…
sergiocorato Jun 19, 2023
609e3ff
Merge branch '14.0-fix-assets_management-last_depreciation_date' of g…
sergiocorato Jun 19, 2023
72f6340
Merge branch '14.0-fix-assets_management-currency_convert' of git+ssh…
sergiocorato Jun 19, 2023
cd06e41
Merge branch '14.0-fix-assets_management-view-html' of git+ssh://gith…
sergiocorato Jun 19, 2023
fae532a
Merge branch '14.0-fix-assets_management-show-asset-not-depreciated' …
sergiocorato Jun 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions assets_management/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,7 @@ def write(self, vals):
return super().write(vals)

def unlink(self):
if self.mapped("asset_accounting_info_ids"):
assets = self.filtered("asset_accounting_info_ids")
name_list = "\n".join([a[-1] for a in assets.name_get()])
raise ValidationError(
_(
"The assets you are trying to delete are currently linked"
" to accounting info. Please remove them if necessary"
" before removing these assets:\n"
)
+ name_list
)
self.mapped("asset_accounting_info_ids").unlink()
self.mapped("depreciation_ids").unlink()
return super().unlink()

Expand Down
17 changes: 15 additions & 2 deletions assets_management/models/asset_accounting_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ class AssetAccountingInfo(models.Model):
"asset.depreciation.line", ondelete="set null", string="Depreciation Line"
)
move_type = fields.Selection(related="dep_line_id.move_type")
move_id = fields.Many2one("account.move", ondelete="set null", string="Move")
move_id = fields.Many2one(
"account.move", related="move_line_id.move_id", string="Move"
)

move_line_id = fields.Many2one(
"account.move.line", ondelete="set null", string="Move Line"
"account.move.line", ondelete="cascade", string="Move Line"
)

relation_type = fields.Selection(
Expand Down Expand Up @@ -107,7 +109,18 @@ def get_main_fields(self):

def button_unlink(self):
"""Button action: deletes a.a.info"""
other_asset_accounting_info = self.env["asset.accounting.info"].search(
[
("id", "!=", self.id),
("dep_line_id", "=", self.dep_line_id.id),
]
)
dep_line_to_delete = self.env["asset.depreciation.line"]
if not other_asset_accounting_info:
dep_line_to_delete = self.dep_line_id
self.unlink()
if dep_line_to_delete:
dep_line_to_delete.unlink()

def check_and_normalize(self):
for info in self:
Expand Down
2 changes: 1 addition & 1 deletion assets_management/models/asset_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def copy(self, default=None):
def unlink(self):
if self.env["asset.asset"].sudo().search([("category_id", "in", self.ids)]):
raise UserError(
_("Cannot delete categories while they're still linked" " to an asset.")
_("Cannot delete categories while they're still linked to an asset.")
)
return super().unlink()

Expand Down
55 changes: 27 additions & 28 deletions assets_management/models/asset_depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,34 +255,11 @@ def check_before_generate_depreciation_lines(self, dep_date):
# Check if self is a valid recordset
if not self:
raise ValidationError(
_("Cannot create any depreciation according to current" " settings.")
_("Cannot create any depreciation according to current settings.")
)

lines = self.mapped("line_ids")

# Check if any depreciation already has newer depreciation lines
# than the given date
newer_lines = lines.filtered(
lambda line: line.move_type == "depreciated"
and not line.partial_dismissal
and line.date > dep_date
)
if newer_lines:
asset_names = ", ".join(
[
asset_name
for asset_id, asset_name in newer_lines.mapped(
"depreciation_id.asset_id"
).name_get()
]
)
raise ValidationError(
_(
"Cannot update the following assets which contain"
" newer depreciations for the chosen types:\n{}"
).format(asset_names)
)

posted_lines = lines.filtered(
lambda line: line.date == dep_date
and line.move_id
Expand Down Expand Up @@ -310,20 +287,27 @@ def generate_depreciation_lines(self, dep_date):

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

return new_lines

def generate_depreciation_lines_single(self, dep_date):
self.ensure_one()

res = self.env["asset.depreciation.line"]
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_amount = dep.get_depreciation_amount(dep_date)
if not dep_amount:
return False
dep = dep.with_context(dep_amount=dep_amount)

vals = dep.prepare_depreciation_line_vals(dep_date)
return self.env["asset.depreciation.line"].create(vals)
res = self.env["asset.depreciation.line"].create(vals)
return res

def generate_dismiss_account_move(self):
self.ensure_one()
Expand Down Expand Up @@ -378,13 +362,28 @@ def get_computed_amounts(self):

def get_depreciable_amount(self, dep_date=None):
types = self.line_ids.get_update_move_types()
return self.amount_depreciable + sum(
depreciable_amount = self.amount_depreciable
update_depreciable_amount = sum(
[
line.balance
for line in self.line_ids
if line.move_type in types and (not dep_date or line.date <= dep_date)
]
)
depreciable_amount += update_depreciable_amount
depreciated_amount = sum(
[
line.balance
for line in self.line_ids
if line.move_type == "depreciated"
and (not dep_date or line.date <= dep_date)
]
)
# If the asset is fully depreciated in the dep_date requested, gives 0 as
# depreciable amount
if float_is_zero(depreciable_amount + depreciated_amount, precision_digits=2):
depreciable_amount = 0
return depreciable_amount

def get_depreciation_amount(self, dep_date):
self.ensure_one()
Expand Down
53 changes: 35 additions & 18 deletions assets_management/models/asset_depreciation_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,6 @@ def write(self, vals):
return res

def unlink(self):
if self.mapped("asset_accounting_info_ids"):
lines = self.filtered("asset_accounting_info_ids")
name_list = "\n".join([line[-1] for line in lines.name_get()])
raise ValidationError(
_(
"The lines you you are trying to delete are currently"
" linked to accounting info. Please remove them if"
" necessary before removing these lines:\n"
)
+ name_list
)
if any([m.state != "draft" for m in self.mapped("move_id")]):
lines = self.filtered(
lambda line: line.move_id and line.move_id.state != "draft"
Expand All @@ -168,6 +157,7 @@ def unlink(self):
)
+ name_list
)
self.mapped("asset_accounting_info_ids").unlink()
self.mapped("move_id").unlink()
return super().unlink()

Expand Down Expand Up @@ -447,13 +437,27 @@ def get_gain_account_move_line_vals(self):

def get_historical_account_move_line_vals(self):
raise NotImplementedError(
_("Cannot create account move lines for lines of type" " `Historical`")
_("Cannot create account move lines for lines of type `Historical`")
)

def get_in_account_move_line_vals(self):
raise NotImplementedError(
_("Cannot create account move lines for lines of type `In`")
)
self.ensure_one()
credit_line_vals = {
"account_id": self.asset_id.category_id.gain_account_id.id,
"credit": self.amount,
"debit": 0.0,
"currency_id": self.currency_id.id,
"name": " - ".join((self.asset_id.make_name(), self.name)),
}

debit_line_vals = {
"account_id": self.asset_id.category_id.asset_account_id.id,
"credit": 0.0,
"debit": self.amount,
"currency_id": self.currency_id.id,
"name": " - ".join((self.asset_id.make_name(), self.name)),
}
return [credit_line_vals, debit_line_vals]

def get_loss_account_move_line_vals(self):
self.ensure_one()
Expand All @@ -474,9 +478,22 @@ def get_loss_account_move_line_vals(self):
return [credit_line_vals, debit_line_vals]

def get_out_account_move_line_vals(self):
raise NotImplementedError(
_("Cannot create account move lines for lines of type `Out`")
)
self.ensure_one()
credit_line_vals = {
"account_id": self.asset_id.category_id.asset_account_id.id,
"credit": self.amount,
"debit": 0.0,
"currency_id": self.currency_id.id,
"name": " - ".join((self.asset_id.make_name(), self.name)),
}
debit_line_vals = {
"account_id": self.asset_id.category_id.loss_account_id.id,
"credit": 0.0,
"debit": self.amount,
"currency_id": self.currency_id.id,
"name": " - ".join((self.asset_id.make_name(), self.name)),
}
return [credit_line_vals, debit_line_vals]

def needs_account_move(self):
self.ensure_one()
Expand Down
62 changes: 33 additions & 29 deletions assets_management/report/asset_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.misc import format_amount
from odoo.tools.safe_eval import safe_eval


def format_date(rec, field_name, fmt):
Expand Down Expand Up @@ -56,6 +55,7 @@ class Report(models.TransientModel):
show_totals = fields.Boolean()

show_category_totals = fields.Boolean()
show_sold_assets = fields.Boolean()

type_ids = fields.Many2many(
"asset.depreciation.type",
Expand Down Expand Up @@ -95,10 +95,8 @@ def print_report(self, report_type=None):
"""
self.ensure_one()
report_type = report_type or "qweb-pdf"
if report_type in ("qweb-pdf", "xlsx"):
if report_type in ("qweb-pdf", "xlsx", "qweb-html"):
res = self.do_print(report_type)
elif report_type == "qweb-html":
res = self.view_report()
elif report_type:
raise ValidationError(
_("No report has been defined for type `{}`.").format(report_type)
Expand All @@ -113,25 +111,13 @@ def do_print(self, report_type):
self.ensure_one()
if report_type == "qweb-pdf":
xml_id = "assets_management.report_asset_journal_pdf"
elif report_type == "qweb-html":
xml_id = "assets_management.report_asset_journal_html"
else:
xml_id = "assets_management.report_asset_journal_xlsx"
report = self.env.ref(xml_id)
return report.report_action(self)

def view_report(self):
"""Launches view for HTML report"""
self.ensure_one()
xmlid = "assets_management.act_client_asset_journal_report"
[act] = self.env.ref(xmlid).read()
ctx = act.get("context", {})
if isinstance(ctx, str):
ctx = safe_eval(ctx)
# Call update twice to force 'active_id(s)' values to be overridden
ctx.update(dict(self._context))
ctx.update(active_id=self.id, active_ids=self.ids)
act["context"] = ctx
return act

@api.model
def get_html(self, given_context=None):
"""Method needed from JavaScript widget to render HTML view"""
Expand Down Expand Up @@ -177,7 +163,7 @@ def generate_structure(self):
dep_lines = dep_lines.filtered(lambda dl: dl.date <= self.date)
categories = assets.mapped("category_id")

if not (categories and assets and deps and dep_lines):
if not (categories and assets and deps):
raise ValidationError(
_("There is nothing to print according to current settings!")
)
Expand Down Expand Up @@ -255,8 +241,8 @@ def generate_totals(self):
total_curr = total.get_currency()
total_type = total.type_id
for fname in fnames:
totals_by_dep_type[total_type][fname] += total_curr.compute(
total[fname], curr
totals_by_dep_type[total_type][fname] += total_curr._convert(
total[fname], curr, self.company_id, self.date
)
self.write(
{
Expand Down Expand Up @@ -288,6 +274,12 @@ def get_depreciations(self):
domain += [("date_start", "<=", self.date)]
if self.type_ids:
domain += [("type_id", "in", self.type_ids.ids)]
if not self.show_sold_assets:
domain += [
"|",
("asset_id.sale_date", "=", False),
("asset_id.sale_date", ">=", self.date.replace(month=1, day=1)),
]
return self.env["asset.depreciation"].search(domain)

def set_report_name(self):
Expand Down Expand Up @@ -356,28 +348,40 @@ def generate_totals(self):
for fname in fnames:
if fname == "amount_depreciation_fund_prev_year":
if fy_start <= report_date <= fy_end:
totals_by_dep_type[dep_type][fname] += line_curr.compute(
last_line[fname], curr
totals_by_dep_type[dep_type][fname] += line_curr._convert(
last_line[fname],
curr,
categ.report_id.company_id,
report_date,
)
elif fname in (
"amount_in_total",
"amount_out_total",
"gain_loss_total",
):
if fy_start <= report_date <= fy_end:
totals_by_dep_type[dep_type][fname] += line_curr.compute(
last_line[fname], curr
totals_by_dep_type[dep_type][fname] += line_curr._convert(
last_line[fname],
curr,
categ.report_id.company_id,
report_date,
)
elif report_date < fy_start:
totals_by_dep_type[dep_type][fname] = 0
elif fname == "amount_depreciated":
if fy_start <= report_date <= fy_end:
totals_by_dep_type[dep_type][fname] += line_curr.compute(
last_line[fname], curr
totals_by_dep_type[dep_type][fname] += line_curr._convert(
last_line[fname],
curr,
categ.report_id.company_id,
report_date,
)
else:
totals_by_dep_type[dep_type][fname] += line_curr.compute(
last_line[fname], curr
totals_by_dep_type[dep_type][fname] += line_curr._convert(
last_line[fname],
curr,
categ.report_id.company_id,
report_date,
)
categ.write(
{
Expand Down
Loading