From 886abfe610430851643deac6a68aa61b62bd0d11 Mon Sep 17 00:00:00 2001 From: Henrik Norlin Date: Fri, 11 Jan 2019 15:24:15 +0100 Subject: [PATCH] [MIG] account_asset_management: Migration to 12.0 --- account_asset_management/__manifest__.py | 2 +- account_asset_management/models/__init__.py | 2 +- .../models/account_asset.py | 66 +++++++++---------- .../models/account_asset_line.py | 11 +--- .../readme/CONTRIBUTORS.rst | 1 + account_asset_management/readme/HISTORY.rst | 5 ++ .../tests/account_asset_test_data.xml | 14 ++-- .../views/account_asset.xml | 4 +- .../wizard/account_asset_remove.py | 10 +-- 9 files changed, 52 insertions(+), 63 deletions(-) create mode 100644 account_asset_management/readme/HISTORY.rst diff --git a/account_asset_management/__manifest__.py b/account_asset_management/__manifest__.py index 080336d4..fc8fa453 100644 --- a/account_asset_management/__manifest__.py +++ b/account_asset_management/__manifest__.py @@ -3,7 +3,7 @@ { 'name': 'Assets Management', - 'version': '11.0.1.0.0', + 'version': '12.0.1.0.0', 'license': 'AGPL-3', 'depends': [ 'account_fiscal_year', diff --git a/account_asset_management/models/__init__.py b/account_asset_management/models/__init__.py index f7fea15d..85eb8750 100644 --- a/account_asset_management/models/__init__.py +++ b/account_asset_management/models/__init__.py @@ -5,5 +5,5 @@ from . import account_asset_line from . import account_asset_recompute_trigger from . import account_invoice from . import account_move -from . import date_range +#from . import date_range from . import res_config_settings diff --git a/account_asset_management/models/account_asset.py b/account_asset_management/models/account_asset.py index b46e0fb8..65b73a19 100644 --- a/account_asset_management/models/account_asset.py +++ b/account_asset_management/models/account_asset.py @@ -86,8 +86,7 @@ class AccountAsset(models.Model): ondelete='restrict', index=True, ) - parent_left = fields.Integer(index=True) - parent_right = fields.Integer(index=True) + parent_path = fields.Char(index=True) child_ids = fields.One2many( comodel_name='account.asset', inverse_name='parent_id', @@ -510,8 +509,7 @@ class AccountAsset(models.Model): continue # group lines prior to depreciation start period - depreciation_start_date = fields.Datetime.from_string( - asset.date_start) + depreciation_start_date = asset.date_start lines = table[0]['lines'] lines1 = [] lines2 = [] @@ -532,8 +530,7 @@ class AccountAsset(models.Model): # recompute in case of deviation depreciated_value_posted = depreciated_value = 0.0 if posted_lines: - last_depreciation_date = fields.Datetime.from_string( - last_line.line_date) + last_depreciation_date = last_line.line_date last_date_in_table = table[-1]['lines'][-1]['date'] if last_date_in_table <= last_depreciation_date: raise UserError( @@ -604,7 +601,7 @@ class AccountAsset(models.Model): 'amount': amount, 'asset_id': asset.id, 'name': name, - 'line_date': line['date'].strftime('%Y-%m-%d'), + 'line_date': line['date'], 'init_entry': entry['init'], } depreciated_value += amount @@ -624,9 +621,9 @@ class AccountAsset(models.Model): a started month is counted as a full month - years: duration in calendar years, considering also leap years """ - fy = self.env['date.range'].browse(fy_id) - fy_date_start = fields.Datetime.from_string(fy.date_start) - fy_date_stop = fields.Datetime.from_string(fy.date_end) + fy = self.env['account.fiscal.year'].browse(fy_id) + fy_date_start = fy.date_from + fy_date_stop = fy.date_to days = (fy_date_stop - fy_date_start).days + 1 months = (fy_date_stop.year - fy_date_start.year) * 12 \ + (fy_date_stop.month - fy_date_start.month) + 1 @@ -664,8 +661,7 @@ class AccountAsset(models.Model): fy_id = entry['fy_id'] if self.prorata: if firstyear: - depreciation_date_start = fields.Datetime.from_string( - self.date_start) + depreciation_date_start = self.date_start fy_date_stop = entry['date_stop'] first_fy_asset_days = \ (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 self.prorata: - depreciation_start_date = fields.Datetime.from_string( - self.date_start) + depreciation_start_date = self.date_start else: - fy_date_start = fields.Datetime.from_string(fy.date_start) - depreciation_start_date = datetime( - fy_date_start.year, fy_date_start.month, 1) + depreciation_start_date = fy.date_from return 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 + \ relativedelta(years=self.method_number, days=-1) elif self.method_time == 'end': - depreciation_stop_date = fields.Datetime.from_string( - self.method_end) + depreciation_stop_date = self.method_end return depreciation_stop_date def _get_first_period_amount(self, table, entry, depreciation_start_date, @@ -908,30 +900,31 @@ class AccountAsset(models.Model): company = self.company_id init_flag = False - asset_date_start = datetime.strptime(self.date_start, '%Y-%m-%d') - fy = company.find_daterange_fy(asset_date_start) + asset_date_start = self.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 if fiscalyear_lock_date and fiscalyear_lock_date >= self.date_start: init_flag = True if fy: fy_id = fy.id - fy_date_start = datetime.strptime(fy.date_start, '%Y-%m-%d') - fy_date_stop = datetime.strptime(fy.date_end, '%Y-%m-%d') + fy_date_start = fy.date_from + fy_date_stop = fy.date_to else: # The following logic is used when no fiscal year # is defined for the asset start date: # - We lookup the first fiscal year defined in the system # - The 'undefined' fiscal years are assumed to be years # with a duration equal to a calendar year - first_fy = self.env['date.range'].search( - [('company_id', '=', self.company_id.id), - ('type_id.fiscal_year', '=', True)], - order='date_end ASC', limit=1) + first_fy = self.env['account.fiscal.year'].search( + [('company_id', '=', self.company_id.id)], + order='date_to ASC', limit=1) if not first_fy: raise UserError( _("No Fiscal Year defined.")) - first_fy_date_start = datetime.strptime( - first_fy.date_start, '%Y-%m-%d') + first_fy_date_start = first_fy.date_from fy_date_start = first_fy_date_start if asset_date_start > fy_date_start: 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_id = False fy = DummyFy( - date_start=fy_date_start.strftime('%Y-%m-%d'), - date_end=fy_date_stop.strftime('%Y-%m-%d'), + date_start=fy_date_start, + date_end=fy_date_stop, id=False, state='done', dummy=True) @@ -964,21 +957,24 @@ class AccountAsset(models.Model): 'date_stop': fy_date_stop, 'init': init_flag}) 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 ( fiscalyear_lock_date and - fiscalyear_lock_date >= fy.date_end + fiscalyear_lock_date >= fy.date_to ): init_flag = True else: init_flag = False - fy_date_stop = datetime.strptime(fy.date_end, '%Y-%m-%d') + fy_date_stop = fy.date_to else: fy_date_stop = fy_date_stop + relativedelta(years=1) if ( fiscalyear_lock_date and - fiscalyear_lock_date >= fy_date_stop.strftime('%Y-%m-%d') + fiscalyear_lock_date >= fy_date_stop ): init_flag = True else: diff --git a/account_asset_management/models/account_asset_line.py b/account_asset_management/models/account_asset_line.py index 47faa538..b85ef0c7 100644 --- a/account_asset_management/models/account_asset_line.py +++ b/account_asset_management/models/account_asset_line.py @@ -1,8 +1,6 @@ # Copyright 2009-2018 Noviat # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -import datetime - from odoo import api, fields, models, _ import odoo.addons.decimal_precision as dp from odoo.exceptions import UserError @@ -103,10 +101,6 @@ class AccountAssetLine(models.Model): @api.multi def write(self, vals): 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 asset_lines = dl.asset_id.depreciation_line_ids if list(vals.keys()) == ['move_id'] and not vals['move_id']: @@ -145,8 +139,9 @@ class AccountAssetLine(models.Model): "after already posted entries.")) else: check = asset_lines.filtered( - lambda l: (l.init_entry or l.move_check) and - l.line_date > vals['line_date'] and l != dl) + lambda l: l != dl and + (l.init_entry or l.move_check) and + l.line_date > fields.Date.to_date(vals['line_date'])) if check: raise UserError(_( "You cannot set the date on a depreciation line " diff --git a/account_asset_management/readme/CONTRIBUTORS.rst b/account_asset_management/readme/CONTRIBUTORS.rst index fb458b39..b2cacd6d 100644 --- a/account_asset_management/readme/CONTRIBUTORS.rst +++ b/account_asset_management/readme/CONTRIBUTORS.rst @@ -5,3 +5,4 @@ - Stéphane Bidoul (Acsone) - Adrien Peiffer (Acsone) - Akim Juillerat +- Henrik Norlin (Apps2GROW) \ No newline at end of file diff --git a/account_asset_management/readme/HISTORY.rst b/account_asset_management/readme/HISTORY.rst new file mode 100644 index 00000000..c192efa6 --- /dev/null +++ b/account_asset_management/readme/HISTORY.rst @@ -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) diff --git a/account_asset_management/tests/account_asset_test_data.xml b/account_asset_management/tests/account_asset_test_data.xml index 594f76f5..e65a276d 100644 --- a/account_asset_management/tests/account_asset_test_data.xml +++ b/account_asset_management/tests/account_asset_test_data.xml @@ -2,19 +2,17 @@ - - + FY_assets - - + + - - + FY_assets previous - - + + diff --git a/account_asset_management/views/account_asset.xml b/account_asset_management/views/account_asset.xml index 76c1af26..93e002ef 100644 --- a/account_asset_management/views/account_asset.xml +++ b/account_asset_management/views/account_asset.xml @@ -205,8 +205,8 @@ - - + + diff --git a/account_asset_management/wizard/account_asset_remove.py b/account_asset_management/wizard/account_asset_remove.py index fde524c8..498d2a1f 100644 --- a/account_asset_management/wizard/account_asset_remove.py +++ b/account_asset_management/wizard/account_asset_remove.py @@ -2,7 +2,6 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from dateutil.relativedelta import relativedelta -from datetime import datetime import logging from odoo import api, fields, models, _ @@ -236,14 +235,9 @@ class AccountAssetRemove(models.TransientModel): [('asset_id', '=', asset.id), ('type', '=', 'create')]) last_depr_date = create_dl.line_date - period_number_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') + period_number_days = (first_date - last_depr_date).days new_line_date = date_remove + relativedelta(days=-1) - to_depreciate_days = ( - new_line_date - - datetime.strptime(last_depr_date, '%Y-%m-%d')).days + to_depreciate_days = (new_line_date - last_depr_date).days to_depreciate_amount = round( float(to_depreciate_days) / float(period_number_days) * first_to_depreciate_dl.amount, digits)