2
0

[MIG] account_asset_management: Migration to 12.0

This commit is contained in:
Henrik Norlin 2019-01-11 15:24:15 +01:00 committed by Rodrigo
parent 0cece3a4b7
commit 886abfe610
9 changed files with 52 additions and 63 deletions

View File

@ -3,7 +3,7 @@
{ {
'name': 'Assets Management', 'name': 'Assets Management',
'version': '11.0.1.0.0', 'version': '12.0.1.0.0',
'license': 'AGPL-3', 'license': 'AGPL-3',
'depends': [ 'depends': [
'account_fiscal_year', 'account_fiscal_year',

View File

@ -5,5 +5,5 @@ from . import account_asset_line
from . import account_asset_recompute_trigger from . import account_asset_recompute_trigger
from . import account_invoice from . import account_invoice
from . import account_move from . import account_move
from . import date_range #from . import date_range
from . import res_config_settings from . import res_config_settings

View File

@ -86,8 +86,7 @@ class AccountAsset(models.Model):
ondelete='restrict', ondelete='restrict',
index=True, index=True,
) )
parent_left = fields.Integer(index=True) parent_path = fields.Char(index=True)
parent_right = fields.Integer(index=True)
child_ids = fields.One2many( child_ids = fields.One2many(
comodel_name='account.asset', comodel_name='account.asset',
inverse_name='parent_id', inverse_name='parent_id',
@ -510,8 +509,7 @@ class AccountAsset(models.Model):
continue continue
# group lines prior to depreciation start period # group lines prior to depreciation start period
depreciation_start_date = fields.Datetime.from_string( depreciation_start_date = asset.date_start
asset.date_start)
lines = table[0]['lines'] lines = table[0]['lines']
lines1 = [] lines1 = []
lines2 = [] lines2 = []
@ -532,8 +530,7 @@ class AccountAsset(models.Model):
# recompute in case of deviation # recompute in case of deviation
depreciated_value_posted = depreciated_value = 0.0 depreciated_value_posted = depreciated_value = 0.0
if posted_lines: if posted_lines:
last_depreciation_date = fields.Datetime.from_string( last_depreciation_date = last_line.line_date
last_line.line_date)
last_date_in_table = table[-1]['lines'][-1]['date'] last_date_in_table = table[-1]['lines'][-1]['date']
if last_date_in_table <= last_depreciation_date: if last_date_in_table <= last_depreciation_date:
raise UserError( raise UserError(
@ -604,7 +601,7 @@ class AccountAsset(models.Model):
'amount': amount, 'amount': amount,
'asset_id': asset.id, 'asset_id': asset.id,
'name': name, 'name': name,
'line_date': line['date'].strftime('%Y-%m-%d'), 'line_date': line['date'],
'init_entry': entry['init'], 'init_entry': entry['init'],
} }
depreciated_value += amount depreciated_value += amount
@ -624,9 +621,9 @@ class AccountAsset(models.Model):
a started month is counted as a full month a started month is counted as a full month
- years: duration in calendar years, considering also leap years - years: duration in calendar years, considering also leap years
""" """
fy = self.env['date.range'].browse(fy_id) fy = self.env['account.fiscal.year'].browse(fy_id)
fy_date_start = fields.Datetime.from_string(fy.date_start) fy_date_start = fy.date_from
fy_date_stop = fields.Datetime.from_string(fy.date_end) fy_date_stop = fy.date_to
days = (fy_date_stop - fy_date_start).days + 1 days = (fy_date_stop - fy_date_start).days + 1
months = (fy_date_stop.year - fy_date_start.year) * 12 \ months = (fy_date_stop.year - fy_date_start.year) * 12 \
+ (fy_date_stop.month - fy_date_start.month) + 1 + (fy_date_stop.month - fy_date_start.month) + 1
@ -664,8 +661,7 @@ class AccountAsset(models.Model):
fy_id = entry['fy_id'] fy_id = entry['fy_id']
if self.prorata: if self.prorata:
if firstyear: if firstyear:
depreciation_date_start = fields.Datetime.from_string( depreciation_date_start = self.date_start
self.date_start)
fy_date_stop = entry['date_stop'] fy_date_stop = entry['date_stop']
first_fy_asset_days = \ first_fy_asset_days = \
(fy_date_stop - depreciation_date_start).days + 1 (fy_date_stop - depreciation_date_start).days + 1
@ -698,12 +694,9 @@ class AccountAsset(models.Model):
if the fiscal year starts in the middle of a month. if the fiscal year starts in the middle of a month.
""" """
if self.prorata: if self.prorata:
depreciation_start_date = fields.Datetime.from_string( depreciation_start_date = self.date_start
self.date_start)
else: else:
fy_date_start = fields.Datetime.from_string(fy.date_start) depreciation_start_date = fy.date_from
depreciation_start_date = datetime(
fy_date_start.year, fy_date_start.month, 1)
return depreciation_start_date return depreciation_start_date
def _get_depreciation_stop_date(self, depreciation_start_date): def _get_depreciation_stop_date(self, depreciation_start_date):
@ -726,8 +719,7 @@ class AccountAsset(models.Model):
depreciation_stop_date = depreciation_start_date + \ depreciation_stop_date = depreciation_start_date + \
relativedelta(years=self.method_number, days=-1) relativedelta(years=self.method_number, days=-1)
elif self.method_time == 'end': elif self.method_time == 'end':
depreciation_stop_date = fields.Datetime.from_string( depreciation_stop_date = self.method_end
self.method_end)
return depreciation_stop_date return depreciation_stop_date
def _get_first_period_amount(self, table, entry, depreciation_start_date, def _get_first_period_amount(self, table, entry, depreciation_start_date,
@ -908,30 +900,31 @@ class AccountAsset(models.Model):
company = self.company_id company = self.company_id
init_flag = False init_flag = False
asset_date_start = datetime.strptime(self.date_start, '%Y-%m-%d') asset_date_start = self.date_start
fy = company.find_daterange_fy(asset_date_start) fy = self.env['account.fiscal.year'].search([
('date_from', '<=', asset_date_start),
('date_to', '>=', asset_date_start),
('company_id', '=', company.id)])
fiscalyear_lock_date = company.fiscalyear_lock_date fiscalyear_lock_date = company.fiscalyear_lock_date
if fiscalyear_lock_date and fiscalyear_lock_date >= self.date_start: if fiscalyear_lock_date and fiscalyear_lock_date >= self.date_start:
init_flag = True init_flag = True
if fy: if fy:
fy_id = fy.id fy_id = fy.id
fy_date_start = datetime.strptime(fy.date_start, '%Y-%m-%d') fy_date_start = fy.date_from
fy_date_stop = datetime.strptime(fy.date_end, '%Y-%m-%d') fy_date_stop = fy.date_to
else: else:
# The following logic is used when no fiscal year # The following logic is used when no fiscal year
# is defined for the asset start date: # is defined for the asset start date:
# - We lookup the first fiscal year defined in the system # - We lookup the first fiscal year defined in the system
# - The 'undefined' fiscal years are assumed to be years # - The 'undefined' fiscal years are assumed to be years
# with a duration equal to a calendar year # with a duration equal to a calendar year
first_fy = self.env['date.range'].search( first_fy = self.env['account.fiscal.year'].search(
[('company_id', '=', self.company_id.id), [('company_id', '=', self.company_id.id)],
('type_id.fiscal_year', '=', True)], order='date_to ASC', limit=1)
order='date_end ASC', limit=1)
if not first_fy: if not first_fy:
raise UserError( raise UserError(
_("No Fiscal Year defined.")) _("No Fiscal Year defined."))
first_fy_date_start = datetime.strptime( first_fy_date_start = first_fy.date_from
first_fy.date_start, '%Y-%m-%d')
fy_date_start = first_fy_date_start fy_date_start = first_fy_date_start
if asset_date_start > fy_date_start: if asset_date_start > fy_date_start:
asset_ref = self.code and '%s (ref: %s)' \ asset_ref = self.code and '%s (ref: %s)' \
@ -946,8 +939,8 @@ class AccountAsset(models.Model):
fy_date_stop = fy_date_start + relativedelta(years=1, days=-1) fy_date_stop = fy_date_start + relativedelta(years=1, days=-1)
fy_id = False fy_id = False
fy = DummyFy( fy = DummyFy(
date_start=fy_date_start.strftime('%Y-%m-%d'), date_start=fy_date_start,
date_end=fy_date_stop.strftime('%Y-%m-%d'), date_end=fy_date_stop,
id=False, id=False,
state='done', state='done',
dummy=True) dummy=True)
@ -964,21 +957,24 @@ class AccountAsset(models.Model):
'date_stop': fy_date_stop, 'date_stop': fy_date_stop,
'init': init_flag}) 'init': init_flag})
fy_date_start = fy_date_stop + relativedelta(days=1) fy_date_start = fy_date_stop + relativedelta(days=1)
fy = company.find_daterange_fy(fy_date_start) fy = self.env['account.fiscal.year'].search([
('date_from', '<=', fy_date_start),
('date_to', '>=', fy_date_start),
('company_id', '=', company.id)])
if fy: if fy:
if ( if (
fiscalyear_lock_date and fiscalyear_lock_date and
fiscalyear_lock_date >= fy.date_end fiscalyear_lock_date >= fy.date_to
): ):
init_flag = True init_flag = True
else: else:
init_flag = False init_flag = False
fy_date_stop = datetime.strptime(fy.date_end, '%Y-%m-%d') fy_date_stop = fy.date_to
else: else:
fy_date_stop = fy_date_stop + relativedelta(years=1) fy_date_stop = fy_date_stop + relativedelta(years=1)
if ( if (
fiscalyear_lock_date and fiscalyear_lock_date and
fiscalyear_lock_date >= fy_date_stop.strftime('%Y-%m-%d') fiscalyear_lock_date >= fy_date_stop
): ):
init_flag = True init_flag = True
else: else:

View File

@ -1,8 +1,6 @@
# Copyright 2009-2018 Noviat # Copyright 2009-2018 Noviat
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import datetime
from odoo import api, fields, models, _ from odoo import api, fields, models, _
import odoo.addons.decimal_precision as dp import odoo.addons.decimal_precision as dp
from odoo.exceptions import UserError from odoo.exceptions import UserError
@ -103,10 +101,6 @@ class AccountAssetLine(models.Model):
@api.multi @api.multi
def write(self, vals): def write(self, vals):
for dl in self: for dl in self:
if vals.get('line_date'):
if isinstance(vals['line_date'], datetime.date):
vals['line_date'] = fields.Date.to_string(
vals['line_date'])
line_date = vals.get('line_date') or dl.line_date line_date = vals.get('line_date') or dl.line_date
asset_lines = dl.asset_id.depreciation_line_ids asset_lines = dl.asset_id.depreciation_line_ids
if list(vals.keys()) == ['move_id'] and not vals['move_id']: if list(vals.keys()) == ['move_id'] and not vals['move_id']:
@ -145,8 +139,9 @@ class AccountAssetLine(models.Model):
"after already posted entries.")) "after already posted entries."))
else: else:
check = asset_lines.filtered( check = asset_lines.filtered(
lambda l: (l.init_entry or l.move_check) and lambda l: l != dl and
l.line_date > vals['line_date'] and l != dl) (l.init_entry or l.move_check) and
l.line_date > fields.Date.to_date(vals['line_date']))
if check: if check:
raise UserError(_( raise UserError(_(
"You cannot set the date on a depreciation line " "You cannot set the date on a depreciation line "

View File

@ -5,3 +5,4 @@
- Stéphane Bidoul (Acsone) - Stéphane Bidoul (Acsone)
- Adrien Peiffer (Acsone) - Adrien Peiffer (Acsone)
- Akim Juillerat <akim.juillerat@camptocamp.com> - Akim Juillerat <akim.juillerat@camptocamp.com>
- Henrik Norlin (Apps2GROW)

View File

@ -0,0 +1,5 @@
12.0.1.0.0 (2019-01-13)
~~~~~~~~~~~~~~~~~~~~~~~
* [BREAKING] account.asset: parent_path has replaced parent_left & parent_right (TODO: migration script)
* [BREAKING] account.asset.recompute.trigger: depends on date_range.py (TODO: re-implement in account_fiscal_year.py)

View File

@ -2,19 +2,17 @@
<odoo> <odoo>
<data noupdate="1"> <data noupdate="1">
<record id="date_range_fy" model="date.range"> <record id="account_fiscal_year_current" model="account.fiscal.year">
<field name="type_id" ref="account_fiscal_year.fiscalyear" />
<field name="name">FY_assets</field> <field name="name">FY_assets</field>
<field name="date_start" eval="time.strftime('%Y-01-01')"/> <field name="date_from" eval="time.strftime('%Y-01-01')"/>
<field name="date_end" eval="time.strftime('%Y-12-31')"/> <field name="date_to" eval="time.strftime('%Y-12-31')"/>
<field name="company_id" ref="base.main_company"/> <field name="company_id" ref="base.main_company"/>
</record> </record>
<record id="date_range_fy_previous" model="date.range"> <record id="account_fiscal_year_previous" model="account.fiscal.year">
<field name="type_id" ref="account_fiscal_year.fiscalyear" />
<field name="name">FY_assets previous</field> <field name="name">FY_assets previous</field>
<field name="date_start" eval="(datetime.now() - relativedelta(years=1, month=1, day=1)).strftime('%Y-%m-%d')"/> <field name="date_from" eval="(datetime.now() - relativedelta(years=1, month=1, day=1)).strftime('%Y-%m-%d')"/>
<field name="date_end" eval="(datetime.now() - relativedelta(years=1, month=12, day=31)).strftime('%Y-%m-%d')"/> <field name="date_to" eval="(datetime.now() - relativedelta(years=1, month=12, day=31)).strftime('%Y-%m-%d')"/>
<field name="company_id" ref="base.main_company"/> <field name="company_id" ref="base.main_company"/>
</record> </record>

View File

@ -205,8 +205,8 @@
<field name="profile_id"/> <field name="profile_id"/>
<field name="partner_id" filter_domain="[('partner_id', 'child_of', self)]"/> <field name="partner_id" filter_domain="[('partner_id', 'child_of', self)]"/>
<group expand="0" string="Group By..."> <group expand="0" string="Group By...">
<filter string="Type" domain="" context="{'group_by': 'type'}"/> <filter string="Type" name="type" domain="" context="{'group_by': 'type'}"/>
<filter string="Profile" domain="" context="{'group_by': 'profile_id'}"/> <filter string="Profile" name="profile" domain="" context="{'group_by': 'profile_id'}"/>
</group> </group>
</search> </search>
</field> </field>

View File

@ -2,7 +2,6 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from datetime import datetime
import logging import logging
from odoo import api, fields, models, _ from odoo import api, fields, models, _
@ -236,14 +235,9 @@ class AccountAssetRemove(models.TransientModel):
[('asset_id', '=', asset.id), ('type', '=', 'create')]) [('asset_id', '=', asset.id), ('type', '=', 'create')])
last_depr_date = create_dl.line_date last_depr_date = create_dl.line_date
period_number_days = ( period_number_days = (first_date - last_depr_date).days
datetime.strptime(first_date, '%Y-%m-%d') -
datetime.strptime(last_depr_date, '%Y-%m-%d')).days
date_remove = datetime.strptime(date_remove, '%Y-%m-%d')
new_line_date = date_remove + relativedelta(days=-1) new_line_date = date_remove + relativedelta(days=-1)
to_depreciate_days = ( to_depreciate_days = (new_line_date - last_depr_date).days
new_line_date -
datetime.strptime(last_depr_date, '%Y-%m-%d')).days
to_depreciate_amount = round( to_depreciate_amount = round(
float(to_depreciate_days) / float(period_number_days) * float(to_depreciate_days) / float(period_number_days) *
first_to_depreciate_dl.amount, digits) first_to_depreciate_dl.amount, digits)