2
0

[FIX] account_asset_management: Don't depend on account.fiscal.year created records

* There's no need of forcing to create such records if your fiscal years are regular ones.
* FY date range computation duplicates code that is already on core.
* DummyFY is a good idea, but was not used at all. Now it is.
This commit is contained in:
Pedro M. Baeza 2019-08-10 14:40:49 +02:00 committed by Rodrigo
parent fabe61b412
commit 095a205ab1
3 changed files with 41 additions and 120 deletions

View File

@ -4,7 +4,7 @@
{ {
'name': 'Assets Management', 'name': 'Assets Management',
'version': '12.0.1.1.1', 'version': '12.0.1.1.2',
'license': 'AGPL-3', 'license': 'AGPL-3',
'depends': [ 'depends': [
'account', 'account',

View File

@ -14,7 +14,7 @@ msgstr ""
"Plural-Forms: \n" "Plural-Forms: \n"
#. module: account_asset_management #. module: account_asset_management
#: code:addons/account_asset_management/models/account_asset.py:1080 #: code:addons/account_asset_management/models/account_asset.py:1014
#, python-format #, python-format
msgid "\n" msgid "\n"
"Error while processing asset '%s': %s" "Error while processing asset '%s': %s"
@ -512,7 +512,7 @@ msgid "Error ! You can not create recursive assets."
msgstr "" msgstr ""
#. module: account_asset_management #. module: account_asset_management
#: code:addons/account_asset_management/models/account_asset.py:1083 #: code:addons/account_asset_management/models/account_asset.py:1017
#, python-format #, python-format
msgid "Error while processing asset '%s': \n" msgid "Error while processing asset '%s': \n"
"\n" "\n"
@ -577,7 +577,7 @@ msgid "ID"
msgstr "" msgstr ""
#. module: account_asset_management #. module: account_asset_management
#: code:addons/account_asset_management/models/account_asset.py:771 #: code:addons/account_asset_management/models/account_asset.py:760
#, python-format #, python-format
msgid "Illegal value %s in asset.method." msgid "Illegal value %s in asset.method."
msgstr "" msgstr ""
@ -707,12 +707,6 @@ msgstr ""
msgid "Next Period Depreciation" msgid "Next Period Depreciation"
msgstr "" msgstr ""
#. module: account_asset_management
#: code:addons/account_asset_management/models/account_asset.py:972
#, python-format
msgid "No Fiscal Year defined."
msgstr ""
#. module: account_asset_management #. module: account_asset_management
#: selection:account.asset,type:0 #: selection:account.asset,type:0
msgid "Normal" msgid "Normal"
@ -931,7 +925,7 @@ msgid "Status"
msgstr "" msgstr ""
#. module: account_asset_management #. module: account_asset_management
#: code:addons/account_asset_management/models/account_asset.py:744 #: code:addons/account_asset_management/models/account_asset.py:733
#, python-format #, python-format
msgid "The '_compute_year_amount' method is only intended for Time Method 'Number of Years." msgid "The '_compute_year_amount' method is only intended for Time Method 'Number of Years."
msgstr "" msgstr ""
@ -1116,13 +1110,6 @@ msgstr ""
msgid "You cannot change the profile of an asset with accounting entries." msgid "You cannot change the profile of an asset with accounting entries."
msgstr "" msgstr ""
#. module: account_asset_management
#: code:addons/account_asset_management/models/account_asset.py:979
#, python-format
msgid "You cannot compute a depreciation table for an asset starting in an undefined future fiscal year.\n"
"Please correct the start date for asset '%s'."
msgstr ""
#. module: account_asset_management #. module: account_asset_management
#: code:addons/account_asset_management/models/account_asset_line.py:166 #: code:addons/account_asset_management/models/account_asset_line.py:166
#, python-format #, python-format

View File

@ -611,16 +611,15 @@ class AccountAsset(models.Model):
return True return True
def _get_fy_duration(self, fy_id, option='days'): def _get_fy_duration(self, fy, option='days'):
""" """Returns fiscal year duration.
Returns fiscal year duration.
@param option: @param option:
- days: duration in days - days: duration in days
- months: duration in months, - months: duration in months,
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['account.fiscal.year'].browse(fy_id)
fy_date_start = fy.date_from fy_date_start = fy.date_from
fy_date_stop = fy.date_to fy_date_stop = fy.date_to
days = (fy_date_stop - fy_date_start).days + 1 days = (fy_date_stop - fy_date_start).days + 1
@ -657,33 +656,23 @@ class AccountAsset(models.Model):
calculate the impact of extended/shortened fiscal years calculate the impact of extended/shortened fiscal years
""" """
duration_factor = 1.0 duration_factor = 1.0
fy_id = entry['fy_id'] fy = entry['fy']
if self.prorata: if self.prorata:
if firstyear: if firstyear:
depreciation_date_start = self.date_start depreciation_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
if fy_id: first_fy_duration = self._get_fy_duration(fy, option='days')
first_fy_duration = self._get_fy_duration( first_fy_year_factor = self._get_fy_duration(
fy_id, option='days') fy, option='years')
first_fy_year_factor = self._get_fy_duration( duration_factor = \
fy_id, option='years') float(first_fy_asset_days) / first_fy_duration \
duration_factor = \ * first_fy_year_factor
float(first_fy_asset_days) / first_fy_duration \ else:
* first_fy_year_factor duration_factor = self._get_fy_duration(fy, option='years')
else: else:
first_fy_duration = \ fy_months = self._get_fy_duration(fy, option='months')
calendar.isleap(entry['date_start'].year) \
and 366 or 365
duration_factor = \
float(first_fy_asset_days) / first_fy_duration
elif fy_id:
duration_factor = self._get_fy_duration(
fy_id, option='years')
elif fy_id:
fy_months = self._get_fy_duration(
fy_id, option='months')
duration_factor = float(fy_months) / 12 duration_factor = float(fy_months) / 12
return duration_factor return duration_factor
@ -938,93 +927,38 @@ class AccountAsset(models.Model):
entry['fy_amount'] = sum( entry['fy_amount'] = sum(
[l['amount'] for l in entry['lines']]) [l['amount'] for l in entry['lines']])
def _compute_depreciation_table(self): def _get_fy_info(self, date):
"""Return an homogeneus data structure for fiscal years."""
fy_info = self.company_id.compute_fiscalyear_dates(date)
if 'record' not in fy_info:
fy_info['record'] = DummyFy(
date_from=fy_info['date_from'],
date_to=fy_info['date_to'],
)
return fy_info
def _compute_depreciation_table(self):
table = [] table = []
if self.method_time in ['year', 'number'] and not self.method_number: if self.method_time in ['year', 'number'] and not self.method_number:
return table return table
company = self.company_id company = self.company_id
init_flag = False
asset_date_start = self.date_start asset_date_start = self.date_start
fy = self.env['account.fiscal.year'].search([ fiscalyear_lock_date = (
('date_from', '<=', asset_date_start), company.fiscalyear_lock_date or fields.Date.to_date('1901-01-01'))
('date_to', '>=', asset_date_start), depreciation_start_date = self._get_depreciation_start_date(
('company_id', '=', company.id)]) self._get_fy_info(asset_date_start)['record'])
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 = 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['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 = 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)' \
% (self.name, self.code) or self.name
raise UserError(
_("You cannot compute a depreciation table for an asset "
"starting in an undefined future fiscal year."
"\nPlease correct the start date for asset '%s'.")
% asset_ref)
while asset_date_start < fy_date_start:
fy_date_start = fy_date_start - relativedelta(years=1)
fy_date_stop = fy_date_start + relativedelta(years=1, days=-1)
fy_id = False
fy = DummyFy(
date_start=fy_date_start,
date_end=fy_date_stop,
id=False,
state='done',
dummy=True)
init_flag = True
depreciation_start_date = self._get_depreciation_start_date(fy)
depreciation_stop_date = self._get_depreciation_stop_date( depreciation_stop_date = self._get_depreciation_stop_date(
depreciation_start_date) depreciation_start_date)
fy_date_start = asset_date_start
while fy_date_start <= depreciation_stop_date: while fy_date_start <= depreciation_stop_date:
fy_info = self._get_fy_info(fy_date_start)
table.append({ table.append({
'fy_id': fy_id, 'fy': fy_info['record'],
'date_start': fy_date_start, 'date_start': fy_info['date_from'],
'date_stop': fy_date_stop, 'date_stop': fy_info['date_to'],
'init': init_flag}) 'init': fiscalyear_lock_date >= fy_info['date_from'],
fy_date_start = fy_date_stop + relativedelta(days=1) })
fy = self.env['account.fiscal.year'].search([ fy_date_start = fy_info['date_to'] + relativedelta(days=1)
('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_to
):
init_flag = True
else:
init_flag = False
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
):
init_flag = True
else:
init_flag = False
# Step 1: # Step 1:
# Calculate depreciation amount per fiscal year. # Calculate depreciation amount per fiscal year.
# This is calculation is skipped for method_time != 'year'. # This is calculation is skipped for method_time != 'year'.