Skip to content

Commit

Permalink
[IMP] Improve smile_account_asset
Browse files Browse the repository at this point in the history
- Fix depreciation board line rounding
- Fix args in Odoo builtin function call
- Ajust previous amounts on reports
- Display sale date only if asset is closed
- Fix sign of purchase refund asset
  • Loading branch information
isabellerichard committed Nov 6, 2020
1 parent a57f05d commit 20891c8
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<field name="base_value">book_value</field>
<field name="use_salvage_value" eval="False"/>
<field name="depreciation_start_date">first_day_of_purchase_month</field>
<field name="depreciation_stop_date">last_day_of_previous_sale_month</field>
<field name="use_manual_rate" eval="True"/>
<field name="rate_formula">max(rate, annuity_number &lt;= length and 100 / (length - annuity_number + 1) or 0)</field>
<field name="prorata" eval="True"/>
Expand Down
14 changes: 5 additions & 9 deletions smile_account_asset/models/account_asset_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,6 @@ def _compute_depreciation_lines(self, depreciation_type='accounting'):
# Create new lines
line_infos = self.env['account.asset.depreciation.method']. \
compute_depreciation_board(**kwargs)
# Round dot because of Python float limit decimal
rounding = self.currency_id.display_rounding
for line_info in line_infos:
for key, value in line_info.items():
if isinstance(value, (int, float)):
value = int(value * (1 / rounding)) / (1 / rounding)
line_info[key] = value
return self._update_or_create_depreciation_lines(
line_infos, depreciation_type)

Expand Down Expand Up @@ -533,8 +526,9 @@ def _get_depreciation_arguments(self, depreciation_type):
'last_day_of_previous_sale_month':
last_day_of_previous_sale_month = True
sale_date = fields.Date.to_string(
fields.Date.from_string(sale_date, '%Y-%m-%d') +
relativedelta(day=1) + relativedelta(days=-1))
fields.Date.from_string(sale_date)
+ relativedelta(day=1)
+ relativedelta(days=-1))
return {
'code': method,
'purchase_value': self.purchase_value,
Expand Down Expand Up @@ -706,6 +700,7 @@ def _get_move_line_vals(self, journal_type, amount_excl_tax, tax_amount,
lines = []
if amount_excl_tax:
debit, credit = abs(amount_excl_tax), 0.0
# TODO: fix journal type `purchase_refund` that is deprecated
if (amount_excl_tax < 0.0) ^ (
journal_type in ('sale', 'purchase_refund')):
debit, credit = abs(credit), abs(debit)
Expand Down Expand Up @@ -739,6 +734,7 @@ def _get_move_line_vals(self, journal_type, amount_excl_tax, tax_amount,
accounts['analytic_account_id'], default))
if amount_excl_tax + tax_amount:
debit, credit = 0.0, abs(amount_excl_tax + tax_amount)
# TODO: fix journal type `purchase_refund` that is deprecated
if (amount_excl_tax + tax_amount < 0.0) ^ \
(journal_type in ('sale', 'purchase_refund')):
debit, credit = credit, debit
Expand Down
4 changes: 2 additions & 2 deletions smile_account_asset/models/account_invoice_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def _get_asset_vals(self):
asset_type = 'purchase'
amount = quantity = 0.0
for line in self:
sign = line.invoice_id.journal_id.type == 'purchase_refund' and \
-1.0 or 1.0
sign = (line.invoice_id.journal_id.type == 'purchase'
and line.invoice_id.type == 'in_refund') and -1.0 or 1.0
amount += line.price_subtotal * sign
quantity += line.quantity * sign
if amount < 0.0:
Expand Down
39 changes: 38 additions & 1 deletion smile_account_asset/report/account_asset_depreciations_report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from odoo import api, models
from odoo import api, fields, models


class ReportAccountAssetDepreciations(models.AbstractModel):
Expand Down Expand Up @@ -93,5 +93,42 @@ def _get_asset_infos(
asset_infos['fiscal_total'] = last_fiscal_line. \
previous_years_accumulated_value_sign
asset_infos['fiscal_year'] = 0.0
self._adjust_previous_amounts(asset_infos, asset, date_to, is_posted)
return self._convert_to_currency(
asset_infos, from_currency, to_currency)

def _adjust_previous_amounts(self, asset_infos, asset, date_to, is_posted):
"""
Ensure that depreciation lines having depreciation date on
a previous year but that related to an asset having Entry
into service date on the current year are displayed
with a null amount on the previous year.
"""
DepreciationLine = self.env['account.asset.depreciation.line']
domain = [
('asset_id', '=', asset.id),
('depreciation_date', '<=', date_to),
]
if is_posted:
domain += [('is_posted', '=', True)]
depreciation_lines = DepreciationLine.search(
domain, order='depreciation_date asc')
if not depreciation_lines:
return
in_service_account_date_year = fields.Date.from_string(
asset.in_service_account_date).year
first_line_year = fields.Date.from_string(
depreciation_lines[0].depreciation_date).year
date_to_year = fields.Date.from_string(date_to).year
if first_line_year == date_to_year - 1 and \
in_service_account_date_year == date_to_year:
accounting_total = asset_infos['accounting_total']
fiscal_total = asset_infos['fiscal_total']
asset_infos.update({
'accounting_previous': 0.0,
'fiscal_previous': 0.0,
'accounting_period': fiscal_total,
'fiscal_period': fiscal_total,
'accounting_year': accounting_total,
'fiscal_year': fiscal_total,
})
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<td class="text-left"><span t-field="asset.code"/></td>
<td class="text-left"><span t-field="asset.name"/></td>
<td class="text-left"><span t-field="asset.in_service_date"/></td>
<td class="text-left"><span t-field="asset.sale_date"/></td>
<td class="text-left"><span t-field="asset.sale_date" t-if="asset.state == 'close'"/></td>
<td class="text-right"><span t-esc="asset_infos['purchase']" t-esc-options='{"widget": "monetary", "display_currency": currency}'/></td>
<td class="text-right"><span t-esc="asset_infos['accounting_previous']" t-esc-options='{"widget": "monetary", "display_currency": currency}'/></td>
<td class="text-right"><span t-esc="asset_infos['fiscal_previous']" t-esc-options='{"widget": "monetary", "display_currency": currency}'/></td>
Expand Down
37 changes: 36 additions & 1 deletion smile_account_asset/report/account_asset_report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from odoo import api, models
from odoo import api, fields, models

from ..tools import get_fiscalyear_start_date

Expand Down Expand Up @@ -80,6 +80,10 @@ def _get_asset_infos(self, asset, to_currency, date_to, is_posted):
current_year_accumulated_value_sign,
'book': depreciation_line.book_value_sign,
}
# We only have to adjust amounts in this case, because
# when we read values inside asset history, previous value
# is already set to 0
self._adjust_previous_amounts(res, asset, date_to, is_posted)
else:
for history in asset.asset_history_ids.sorted('date_to'):
if history.date_to > date_to:
Expand All @@ -101,3 +105,34 @@ def _get_asset_infos(self, asset, to_currency, date_to, is_posted):
}
res['next'] = res['previous'] + res['current']
return self._convert_to_currency(res, from_currency, to_currency)

def _adjust_previous_amounts(self, asset_infos, asset, date_to, is_posted):
"""
Ensure that depreciation lines having depreciation date on
a previous year but that related to an asset having Entry
into service date on the current year are displayed
with a null amount on the previous year.
"""
DepreciationLine = self.env['account.asset.depreciation.line']
domain = [
('asset_id', '=', asset.id),
('depreciation_date', '<=', date_to),
]
if is_posted:
domain += [('is_posted', '=', True)]
depreciation_lines = DepreciationLine.search(
domain, order='depreciation_date asc')
if not depreciation_lines:
return
in_service_account_date_year = fields.Date.from_string(
asset.in_service_account_date).year
first_line_year = fields.Date.from_string(
depreciation_lines[0].depreciation_date).year
date_to_year = fields.Date.from_string(date_to).year
if first_line_year == date_to_year - 1 and \
in_service_account_date_year == date_to_year:
current = asset_infos['previous'] + asset_infos['current']
asset_infos.update({
'previous': 0.0,
'current': current,
})
2 changes: 1 addition & 1 deletion smile_account_asset/report/account_asset_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<td class="text-center"><span t-field="asset.accounting_method"/></td>
<td class="text-center"><span t-field="asset.purchase_date"/></td>
<td class="text-left"><span t-field="asset.accounting_annuities"/></td>
<td class="text-center"><span t-field="asset.sale_date"/></td>
<td class="text-center"><span t-field="asset.sale_date" t-if="asset.state == 'close'"/></td>
<td class="text-right" t-esc="asset_infos['purchase']" t-esc-options='{"widget": "monetary", "display_currency": currency}'/>
<td class="text-right" t-esc="asset_infos['salvage']" t-esc-options='{"widget": "monetary", "display_currency": currency}'/>
<td class="text-right" t-esc="asset_infos['previous']" t-esc-options='{"widget": "monetary", "display_currency": currency}'/>
Expand Down
2 changes: 1 addition & 1 deletion smile_account_asset/views/account_invoice_line_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<field name="view_mode">tree,form</field>
<field name="view_id" ref="account.view_invoice_line_tree"/>
<field name="search_view_id" ref="view_account_invoice_line_search"/>
<field name="domain">[('invoice_id.state', 'not in', ('draft', 'cancel')),('asset_category_id','!=',False),('invoice_id.journal_id.type', 'in',('purchase','purchase_refund'))]</field>
<field name="domain">[('invoice_id.state', 'not in', ('draft', 'cancel')),('asset_category_id','!=',False),('invoice_id.journal_id.type', '=', 'purchase')]</field>
<field name="context">{'search_default_asset': True}</field>
</record>
<record id="action_account_invoice_line_view1" model="ir.actions.act_window.view">
Expand Down

0 comments on commit 20891c8

Please sign in to comment.