2
0

[IMP] : pre-commit stuff

This commit is contained in:
Alexis de Lattre 2022-11-29 23:30:33 +01:00 committed by Rodrigo
parent 0466dc9c43
commit 3c5d26b456
10 changed files with 129 additions and 91 deletions

View File

@ -11,7 +11,8 @@ class AccountAccount(models.Model):
asset_profile_id = fields.Many2one(
comodel_name="account.asset.profile",
string="Asset Profile",
help="Default Asset Profile when creating invoice lines " "with this account.",
check_company=True,
help="Default Asset Profile when creating invoice lines with this account.",
)
@api.constrains("asset_profile_id")

View File

@ -58,37 +58,41 @@ class AccountAsset(models.Model):
size=32,
states=READONLY_STATES,
)
purchase_value = fields.Float(
purchase_value = fields.Monetary(
string="Purchase Value",
required=True,
states=READONLY_STATES,
currency_field="company_currency_id",
help="This amount represent the initial value of the asset."
"\nThe Depreciation Base is calculated as follows:"
"\nPurchase Value - Salvage Value.",
)
salvage_value = fields.Float(
digits="Account",
salvage_value = fields.Monetary(
string="Salvage Value",
states=READONLY_STATES,
currency_field="company_currency_id",
help="The estimated value that an asset will realize upon "
"its sale at the end of its useful life.\n"
"This value is used to determine the depreciation amounts.",
)
depreciation_base = fields.Float(
depreciation_base = fields.Monetary(
compute="_compute_depreciation_base",
digits="Account",
string="Depreciation Base",
store=True,
currency_field="company_currency_id",
help="This amount represent the depreciation base "
"of the asset (Purchase Value - Salvage Value).",
)
value_residual = fields.Float(
value_residual = fields.Monetary(
compute="_compute_depreciation",
digits="Account",
string="Residual Value",
currency_field="company_currency_id",
store=True,
)
value_depreciated = fields.Float(
value_depreciated = fields.Monetary(
compute="_compute_depreciation",
digits="Account",
string="Depreciated Value",
currency_field="company_currency_id",
store=True,
)
note = fields.Text()
@ -266,7 +270,6 @@ class AccountAsset(models.Model):
related="company_id.currency_id",
string="Company Currency",
store=True,
readonly=True,
)
account_analytic_id = fields.Many2one(
comodel_name="account.analytic.account",
@ -607,8 +610,8 @@ class AccountAsset(models.Model):
last_line,
posted_lines,
):
digits = self.env["decimal.precision"].precision_get("Account")
company = self.company_id
currency = company.currency_id
fiscalyear_lock_date = company.fiscalyear_lock_date or fields.Date.to_date(
"1901-01-01"
)
@ -639,14 +642,14 @@ class AccountAsset(models.Model):
if amount or self.carry_forward_missed_depreciations:
vals = {
"previous_id": depr_line.id,
"amount": round(amount, digits),
"amount": currency.round(amount),
"asset_id": self.id,
"name": name,
"line_date": line["date"],
"line_days": line["days"],
"init_entry": fiscalyear_lock_date >= line["date"],
}
depreciated_value += round(amount, digits)
depreciated_value += currency.round(amount)
depr_line = self.env["account.asset.line"].create(vals)
else:
seq -= 1
@ -655,10 +658,10 @@ class AccountAsset(models.Model):
def compute_depreciation_board(self):
line_obj = self.env["account.asset.line"]
digits = self.env["decimal.precision"].precision_get("Account")
for asset in self:
if asset.value_residual == 0.0:
currency = asset.company_id.currency_id
if currency.is_zero(asset.value_residual):
continue
domain = [
("asset_id", "=", asset.id),
@ -740,7 +743,7 @@ class AccountAsset(models.Model):
posted_line.amount for posted_line in posted_lines
)
residual_amount = asset.depreciation_base - depreciated_value
amount_diff = round(residual_amount_table - residual_amount, digits)
amount_diff = currency.round(residual_amount_table - residual_amount)
if amount_diff:
# We will auto-create a new line because the number of lines in
# the tables are the same as the posted depreciations and there
@ -987,7 +990,8 @@ class AccountAsset(models.Model):
def _compute_depreciation_amount_per_fiscal_year(
self, table, line_dates, depreciation_start_date, depreciation_stop_date
):
digits = self.env["decimal.precision"].precision_get("Account")
self.ensure_one()
currency = self.company_id.currency_id
fy_residual_amount = self.depreciation_base
i_max = len(table) - 1
asset_sign = self.depreciation_base >= 0 and 1 or -1
@ -1019,17 +1023,22 @@ class AccountAsset(models.Model):
firstyear = i == 0 and True or False
fy_factor = self._get_fy_duration_factor(entry, firstyear)
fy_amount = year_amount * fy_factor
if asset_sign * (fy_amount - fy_residual_amount) > 0:
if (
currency.compare_amounts(
asset_sign * (fy_amount - fy_residual_amount), 0
)
> 0
):
fy_amount = fy_residual_amount
period_amount = round(period_amount, digits)
fy_amount = round(fy_amount, digits)
period_amount = currency.round(period_amount)
fy_amount = currency.round(fy_amount)
else:
fy_amount = False
if self.method_time == "number":
number = self.method_number
else:
number = len(line_dates)
period_amount = round(self.depreciation_base / number, digits)
period_amount = currency.round(self.depreciation_base / number)
entry.update(
{
"period_amount": period_amount,
@ -1039,7 +1048,7 @@ class AccountAsset(models.Model):
)
if self.method_time == "year":
fy_residual_amount -= fy_amount
if round(fy_residual_amount, digits) == 0:
if currency.is_zero(fy_residual_amount):
break
i_max = i
table = table[: i_max + 1]
@ -1049,7 +1058,8 @@ class AccountAsset(models.Model):
self, table, depreciation_start_date, depreciation_stop_date, line_dates
):
digits = self.env["decimal.precision"].precision_get("Account")
self.ensure_one()
currency = self.company_id.currency_id
asset_sign = 1 if self.depreciation_base >= 0 else -1
i_max = len(table) - 1
remaining_value = self.depreciation_base
@ -1068,7 +1078,7 @@ class AccountAsset(models.Model):
prev_date = max(entry["date_start"], depreciation_start_date)
for li, line_date in enumerate(line_dates):
line_days = (line_date - prev_date).days + 1
if round(remaining_value, digits) == 0.0:
if currency.is_zero(remaining_value):
break
if line_date > min(entry["date_stop"], depreciation_stop_date) and not (
@ -1081,20 +1091,23 @@ class AccountAsset(models.Model):
if (
self.method == "degr-linear"
and asset_sign * (fy_amount - fy_amount_check) < 0
and currency.compare_amounts(
asset_sign * (fy_amount - fy_amount_check), 0
)
< 0
):
break
if i == 0 and li == 0:
if entry.get("day_amount") > 0.0:
if currency.compare_amounts(entry.get("day_amount"), 0) > 0:
amount = line_days * entry.get("day_amount")
else:
amount = self._get_first_period_amount(
table, entry, depreciation_start_date, line_dates
)
amount = round(amount, digits)
amount = currency.round(amount)
else:
if entry.get("day_amount") > 0.0:
if currency.compare_amounts(entry.get("day_amount"), 0) > 0:
amount = line_days * entry.get("day_amount")
else:
amount = entry.get("period_amount")
@ -1127,7 +1140,7 @@ class AccountAsset(models.Model):
# The code has now been simplified with compensation
# always in last FT depreciation line.
if self.method_time == "year" and not entry.get("day_amount"):
if round(fy_amount_check - fy_amount, digits) != 0:
if not currency.is_zero(fy_amount_check - fy_amount):
diff = fy_amount_check - fy_amount
amount = amount - diff
remaining_value += diff

View File

@ -27,23 +27,28 @@ class AccountAssetLine(models.Model):
readonly=True,
)
parent_state = fields.Selection(
related="asset_id.state", string="State of Asset", readonly=True
related="asset_id.state",
string="State of Asset",
)
depreciation_base = fields.Float(
related="asset_id.depreciation_base", string="Depreciation Base", readonly=True
depreciation_base = fields.Monetary(
related="asset_id.depreciation_base",
string="Depreciation Base",
currency_field="company_currency_id",
)
amount = fields.Float(digits="Account", required=True)
remaining_value = fields.Float(
amount = fields.Monetary(
string="Amount", required=True, currency_field="company_currency_id"
)
remaining_value = fields.Monetary(
compute="_compute_values",
digits="Account",
string="Next Period Depreciation",
store=True,
currency_field="company_currency_id",
)
depreciated_value = fields.Float(
depreciated_value = fields.Monetary(
compute="_compute_values",
digits="Account",
string="Amount Already Depreciated",
store=True,
currency_field="company_currency_id",
)
line_date = fields.Date(string="Date", required=True)
line_days = fields.Integer(string="Days", readonly=True)
@ -70,11 +75,9 @@ class AccountAssetLine(models.Model):
help="Set this flag for entries of previous fiscal years "
"for which Odoo has not generated accounting entries.",
)
company_id = fields.Many2one(
"res.company",
store=True,
readonly=True,
related="asset_id.company_id",
company_id = fields.Many2one(related="asset_id.company_id", store=True)
company_currency_id = fields.Many2one(
related="asset_id.company_id.currency_id", store=True, string="Company Currency"
)
@api.depends("amount", "previous_id", "type")
@ -229,15 +232,17 @@ class AccountAssetLine(models.Model):
def _setup_move_line_data(self, depreciation_date, account, ml_type, move):
"""Prepare data to be propagated to account.move.line"""
asset = self.asset_id
currency = asset.company_id.currency_id
amount = self.amount
amount_comp = currency.compare_amounts(amount, 0)
analytic_id = False
analytic_tags = self.env["account.analytic.tag"]
if ml_type == "depreciation":
debit = amount < 0 and -amount or 0.0
credit = amount > 0 and amount or 0.0
debit = amount_comp < 0 and -amount or 0.0
credit = amount_comp > 0 and amount or 0.0
elif ml_type == "expense":
debit = amount > 0 and amount or 0.0
credit = amount < 0 and -amount or 0.0
debit = amount_comp > 0 and amount or 0.0
credit = amount_comp < 0 and -amount or 0.0
analytic_id = asset.account_analytic_id.id
analytic_tags = asset.analytic_tag_ids
move_line_data = {

View File

@ -33,13 +33,12 @@ class AccountMove(models.Model):
asset_count = fields.Integer(compute="_compute_asset_count")
def _compute_asset_count(self):
for rec in self:
assets = (
self.env["account.asset.line"]
.search([("move_id", "=", rec.id)])
.mapped("asset_id")
rg_res = self.env["account.asset.line"].read_group(
[("move_id", "in", self.ids)], ["move_id"], ["move_id"]
)
rec.asset_count = len(assets)
mapped_data = {x["move_id"][0]: x["move_id_count"] for x in rg_res}
for move in self:
move.asset_count = mapped_data.get(move.id, 0)
def unlink(self):
# for move in self:
@ -179,6 +178,7 @@ class AccountMoveLine(models.Model):
comodel_name="account.asset",
string="Asset",
ondelete="restrict",
check_company=True,
)
@api.depends("account_id", "asset_id")

View File

@ -5,8 +5,18 @@
<field name="model">account.account</field>
<field name="inherit_id" ref="account.view_account_form" />
<field name="arch" type="xml">
<field name="user_type_id" position="before">
<group name="options_group" position="inside">
<field name="asset_profile_id" />
</group>
</field>
</record>
<record id="view_account_list" model="ir.ui.view">
<field name="model">account.account</field>
<field name="inherit_id" ref="account.view_account_list" />
<field name="arch" type="xml">
<field name="tag_ids" position="after">
<field name="asset_profile_id" optional="hide" />
</field>
</field>
</record>

View File

@ -70,21 +70,9 @@
/>
<field name="move_line_check" invisible="1" />
<newline />
<field
name="depreciation_base"
widget="monetary"
options="{'currency_field': 'company_currency_id'}"
/>
<field
name="value_depreciated"
widget="monetary"
options="{'currency_field': 'company_currency_id'}"
/>
<field
name="value_residual"
widget="monetary"
options="{'currency_field': 'company_currency_id'}"
/>
<field name="depreciation_base" />
<field name="value_depreciated" />
<field name="value_residual" />
<field name="active" invisible="1" />
</group>
<notebook colspan="4">
@ -93,8 +81,6 @@
<group>
<field
name="purchase_value"
widget="monetary"
options="{'currency_field': 'currency_id'}"
attrs="{'readonly':['|', ('move_line_check', '=', True), ('state', '!=', 'draft')]}"
/>
<field
@ -105,8 +91,6 @@
<group>
<field
name="salvage_value"
widget="monetary"
options="{'currency_field': 'company_currency_id'}"
attrs="{'readonly': [('state', '!=', 'draft')]}"
/>
<field name="date_remove" />
@ -186,6 +170,7 @@
<field name="init_entry" string="Init" />
<field name="move_check" />
<field name="parent_state" invisible="1" />
<field name="company_currency_id" invisible="1" />
<button
name="create_move"
icon="fa-cog"
@ -218,6 +203,10 @@
name="depreciation_base"
invisible="1"
/>
<field
name="company_currency_id"
invisible="1"
/>
<field name="type" />
<field name="name" />
<field
@ -315,6 +304,7 @@
decoration-info="state == 'draft'"
decoration-muted="state == 'close'"
/>
<field name="company_currency_id" invisible="1" />
</tree>
</field>
</record>

View File

@ -24,6 +24,9 @@ class AccountAssetRemove(models.TransientModel):
required=True,
default=lambda self: self._default_company_id(),
)
company_currency_id = fields.Many2one(
related="company_id.currency_id", string="Company Currency"
)
date_remove = fields.Date(
string="Asset Removal Date",
required=True,
@ -32,7 +35,11 @@ class AccountAssetRemove(models.TransientModel):
"in case of early removal",
)
force_date = fields.Date(string="Force accounting date")
sale_value = fields.Float(default=lambda self: self._default_sale_value())
sale_value = fields.Monetary(
string="Sale Value",
default=lambda self: self._default_sale_value(),
currency_field="company_currency_id",
)
account_sale_id = fields.Many2one(
comodel_name="account.account",
string="Asset Sale Account",
@ -70,9 +77,9 @@ class AccountAssetRemove(models.TransientModel):
)
note = fields.Text("Notes")
@api.constrains("sale_value")
@api.constrains("sale_value", "company_id")
def _check_sale_value(self):
if self.sale_value < 0:
if self.company_id.currency_id.compare_amounts(self.sale_value, 0) < 0:
raise ValidationError(_("The Sale Value must be positive!"))
@api.model
@ -105,10 +112,11 @@ class AccountAssetRemove(models.TransientModel):
inv_curr = inv.currency_id
if line.move_id.payment_state == "paid" or line.parent_state == "draft":
account_sale_id = line.account_id.id
amount = line.price_subtotal
if inv_curr != comp_curr:
amount = comp_curr.compute(amount)
sale_value += amount
amount_inv_cur = line.price_subtotal
amount_comp_cur = inv_curr._convert(
amount_inv_cur, comp_curr, inv.company_id, inv.date
)
sale_value += amount_comp_cur
return {"sale_value": sale_value, "account_sale_id": account_sale_id}
@api.model
@ -234,7 +242,7 @@ class AccountAssetRemove(models.TransientModel):
date_remove = self.date_remove
asset_line_obj = self.env["account.asset.line"]
digits = self.env["decimal.precision"].precision_get("Account")
currency = asset.company_id.currency_id
def _dlines(asset):
lines = asset.depreciation_line_ids
@ -279,11 +287,10 @@ class AccountAssetRemove(models.TransientModel):
period_number_days = (first_date - last_depr_date).days + same_month
new_line_date = date_remove + relativedelta(days=-1)
to_depreciate_days = (new_line_date - last_depr_date).days + same_month
to_depreciate_amount = round(
to_depreciate_amount = currency.round(
float(to_depreciate_days)
/ float(period_number_days)
* first_to_depreciate_dl.amount,
digits,
)
residual_value = asset.value_residual - to_depreciate_amount
if to_depreciate_amount:
@ -302,25 +309,28 @@ class AccountAssetRemove(models.TransientModel):
move_lines = []
partner_id = asset.partner_id and asset.partner_id.id or False
profile = asset.profile_id
currency = asset.company_id.currency_id
# asset and asset depreciation account reversal
depr_amount = asset.depreciation_base - residual_value
depr_amount_comp = currency.compare_amounts(depr_amount, 0)
if depr_amount:
move_line_vals = {
"name": asset.name,
"account_id": profile.account_depreciation_id.id,
"debit": depr_amount > 0 and depr_amount or 0.0,
"credit": depr_amount < 0 and -depr_amount or 0.0,
"debit": depr_amount_comp > 0 and depr_amount or 0.0,
"credit": depr_amount_comp < 0 and -depr_amount or 0.0,
"partner_id": partner_id,
"asset_id": asset.id,
}
move_lines.append((0, 0, move_line_vals))
depreciation_base_comp = currency.compare_amounts(asset.depreciation_base, 0)
move_line_vals = {
"name": asset.name,
"account_id": profile.account_asset_id.id,
"debit": (asset.depreciation_base < 0 and -asset.depreciation_base or 0.0),
"credit": (asset.depreciation_base > 0 and asset.depreciation_base or 0.0),
"debit": (depreciation_base_comp < 0 and -asset.depreciation_base or 0.0),
"credit": (depreciation_base_comp > 0 and asset.depreciation_base or 0.0),
"partner_id": partner_id,
"asset_id": asset.id,
}
@ -356,9 +366,10 @@ class AccountAssetRemove(models.TransientModel):
}
move_lines.append((0, 0, move_line_vals))
balance = self.sale_value - residual_value
balance_comp = currency.compare_amounts(balance, 0)
account_id = (
self.account_plus_value_id.id
if balance > 0
if balance_comp > 0
else self.account_min_value_id.id
)
move_line_vals = {
@ -366,8 +377,8 @@ class AccountAssetRemove(models.TransientModel):
"account_id": account_id,
"analytic_account_id": asset.account_analytic_id.id,
"analytic_tag_ids": [(4, tag.id) for tag in asset.analytic_tag_ids],
"debit": balance < 0 and -balance or 0.0,
"credit": balance > 0 and balance or 0.0,
"debit": balance_comp < 0 and -balance or 0.0,
"credit": balance_comp > 0 and balance or 0.0,
"partner_id": partner_id,
"asset_id": asset.id,
}

View File

@ -1,2 +1,3 @@
# generated from manifests external_dependencies
freezegun
python-dateutil

View File

@ -0,0 +1 @@
../../../../account_asset_management

View File

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)