From 67b94fa511311cdd28549de4a89e6659d1934b38 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 8 May 2013 12:50:44 +0200 Subject: [PATCH 1/6] [FIX] opw584557: account: better default fiscalyear dates for fiscalyears not 1/1-12/31 bzr revid: mat@openerp.com-20130508105044-fr3tf2t1zwmd6hko --- addons/account/res_config.py | 51 ++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/addons/account/res_config.py b/addons/account/res_config.py index 53c604161ea..bae18ec9476 100644 --- a/addons/account/res_config.py +++ b/addons/account/res_config.py @@ -132,11 +132,58 @@ class account_config_settings(osv.osv_memory): count = self.pool.get('res.company').search_count(cr, uid, [], context=context) return bool(count == 1) + def _default_start_date(self, cr, uid, context=None): + """Compute default starting date for fiscalyear + - if no fiscal year, use 1st jan of this year + - if in a fiscal year, use its starting date + - if past fiscal year, use the ending date of the latest +1 day + """ + company_id = self._default_company(cr, uid, context=context) + fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, + [('date_start', '<=', time.strftime('%Y-%m-%d')), ('date_stop', '>=', time.strftime('%Y-%m-%d')), + ('company_id', '=', company_id)]) + if fiscalyear_ids: + # has a current fiscal year, use this one + return self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context).date_start + else: + past_fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, + [('date_stop', '<=', time.strftime('%Y-%m-%d')), ('company_id', '=', company_id)]) + if past_fiscalyear_ids: + # use the latest fiscal year+1 day, sorted by start_date + latest_stop = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context).date_stop + return (datetime.datetime.strptime(latest_stop, "%Y-%m-%d") + datetime.timedelta(days=1)).strftime('%Y-%m-%d') + else: + return time.strftime('%Y-01-01') + + def _default_stop_date(self, cr, uid, context=None): + """Compute default ending date for fiscalyear + - if no fiscal year, use 31th dec of this year + - if in a fiscal year, use its ending date + - if past fiscal year, use 31th dec of year of the latest fiscalyear +1 day + """ + company_id = self._default_company(cr, uid, context=context) + fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, + [('date_start', '<=', time.strftime('%Y-%m-%d')), ('date_stop', '>=', time.strftime('%Y-%m-%d')), + ('company_id', '=', company_id)]) + if fiscalyear_ids: + # has a current fiscal year, use this one + return self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context).date_stop + else: + past_fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, + [('date_stop', '<=', time.strftime('%Y-%m-%d')), ('company_id', '=', company_id)]) + if past_fiscalyear_ids: + # use the latest fiscal year+1 day, sorted by start_date + latest_stop = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context).date_stop + return latest_stop.replace(year= latest_stop.year+1).strftime('%Y-%m-%d') + else: + return time.strftime('%Y-12-31') + + _defaults = { 'company_id': _default_company, 'has_default_company': _default_has_default_company, - 'date_start': lambda *a: time.strftime('%Y-01-01'), - 'date_stop': lambda *a: time.strftime('%Y-12-31'), + 'date_start': _default_start_date, + 'date_stop': _default_stop_date, 'period': 'month', } From bf8fc82ecb4a663f51f60077e6109c046247361d Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 8 May 2013 13:57:08 +0200 Subject: [PATCH 2/6] [FIX] use correctly datetime objects bzr revid: mat@openerp.com-20130508115708-ut4jlp572dhxntsd --- addons/account/res_config.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/addons/account/res_config.py b/addons/account/res_config.py index bae18ec9476..ed0355f77e7 100644 --- a/addons/account/res_config.py +++ b/addons/account/res_config.py @@ -173,8 +173,9 @@ class account_config_settings(osv.osv_memory): [('date_stop', '<=', time.strftime('%Y-%m-%d')), ('company_id', '=', company_id)]) if past_fiscalyear_ids: # use the latest fiscal year+1 day, sorted by start_date - latest_stop = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context).date_stop - return latest_stop.replace(year= latest_stop.year+1).strftime('%Y-%m-%d') + latest_year = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context) + latest_stop = datetime.datetime.strptime(latest_year.date_stop, "%Y-%m-%d") + return latest_stop.replace(year=latest_stop.year+1).strftime('%Y-%m-%d') else: return time.strftime('%Y-12-31') From dff5615b9ee7882e05121858d405a96f117cf664 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 8 May 2013 14:06:26 +0200 Subject: [PATCH 3/6] [IMP] better comment bzr revid: mat@openerp.com-20130508120626-tz68kaunfdv45paj --- addons/account/res_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/account/res_config.py b/addons/account/res_config.py index ed0355f77e7..7d164f73f94 100644 --- a/addons/account/res_config.py +++ b/addons/account/res_config.py @@ -159,7 +159,7 @@ class account_config_settings(osv.osv_memory): """Compute default ending date for fiscalyear - if no fiscal year, use 31th dec of this year - if in a fiscal year, use its ending date - - if past fiscal year, use 31th dec of year of the latest fiscalyear +1 day + - if past fiscal year, use the ending date of latest fiscalyear +1 year """ company_id = self._default_company(cr, uid, context=context) fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, From f2c5aee290db938fa084513a1bdfb5bee3cee783 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 8 May 2013 14:34:40 +0200 Subject: [PATCH 4/6] [IMP] account: use DEFAULT_SERVER_DATETIME_FORMAT bzr revid: mat@openerp.com-20130508123440-kmtbv1vq4x03gr8k --- addons/account/res_config.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/addons/account/res_config.py b/addons/account/res_config.py index 7d164f73f94..adce6814b25 100644 --- a/addons/account/res_config.py +++ b/addons/account/res_config.py @@ -25,6 +25,7 @@ from dateutil.relativedelta import relativedelta from operator import itemgetter from os.path import join as opj +from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT as DF from openerp.tools.translate import _ from openerp.osv import fields, osv from openerp import tools @@ -140,18 +141,18 @@ class account_config_settings(osv.osv_memory): """ company_id = self._default_company(cr, uid, context=context) fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, - [('date_start', '<=', time.strftime('%Y-%m-%d')), ('date_stop', '>=', time.strftime('%Y-%m-%d')), + [('date_start', '<=', time.strftime(DF)), ('date_stop', '>=', time.strftime(DF)), ('company_id', '=', company_id)]) if fiscalyear_ids: # has a current fiscal year, use this one return self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context).date_start else: past_fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, - [('date_stop', '<=', time.strftime('%Y-%m-%d')), ('company_id', '=', company_id)]) + [('date_stop', '<=', time.strftime(DF)), ('company_id', '=', company_id)]) if past_fiscalyear_ids: # use the latest fiscal year+1 day, sorted by start_date latest_stop = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context).date_stop - return (datetime.datetime.strptime(latest_stop, "%Y-%m-%d") + datetime.timedelta(days=1)).strftime('%Y-%m-%d') + return (datetime.datetime.strptime(latest_stop, DF) + datetime.timedelta(days=1)).strftime(DF) else: return time.strftime('%Y-01-01') @@ -163,19 +164,19 @@ class account_config_settings(osv.osv_memory): """ company_id = self._default_company(cr, uid, context=context) fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, - [('date_start', '<=', time.strftime('%Y-%m-%d')), ('date_stop', '>=', time.strftime('%Y-%m-%d')), + [('date_start', '<=', time.strftime(DF)), ('date_stop', '>=', time.strftime(DF)), ('company_id', '=', company_id)]) if fiscalyear_ids: # has a current fiscal year, use this one return self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context).date_stop else: past_fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, - [('date_stop', '<=', time.strftime('%Y-%m-%d')), ('company_id', '=', company_id)]) + [('date_stop', '<=', time.strftime(DF)), ('company_id', '=', company_id)]) if past_fiscalyear_ids: # use the latest fiscal year+1 day, sorted by start_date latest_year = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context) - latest_stop = datetime.datetime.strptime(latest_year.date_stop, "%Y-%m-%d") - return latest_stop.replace(year=latest_stop.year+1).strftime('%Y-%m-%d') + latest_stop = datetime.datetime.strptime(latest_year.date_stop, DF) + return latest_stop.replace(year=latest_stop.year+1).strftime(DF) else: return time.strftime('%Y-12-31') From e15811a6a1a698adc21465e4a7d69389661cff5a Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 8 May 2013 14:55:20 +0200 Subject: [PATCH 5/6] [IMP] account: use DEFAULT_SERVER_DATETIME_FORMAT and merging in one method bzr revid: mat@openerp.com-20130508125520-kf0pbgfislf34vay --- addons/account/res_config.py | 41 +++++++++--------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/addons/account/res_config.py b/addons/account/res_config.py index adce6814b25..ef16638905f 100644 --- a/addons/account/res_config.py +++ b/addons/account/res_config.py @@ -133,42 +133,20 @@ class account_config_settings(osv.osv_memory): count = self.pool.get('res.company').search_count(cr, uid, [], context=context) return bool(count == 1) - def _default_start_date(self, cr, uid, context=None): - """Compute default starting date for fiscalyear + def _get_default_fiscalyear_dates(self, cr, uid, company_id, context=None): + """Compute default starting and ending date for fiscalyear - if no fiscal year, use 1st jan of this year - if in a fiscal year, use its starting date - if past fiscal year, use the ending date of the latest +1 day + :return: (date_start, date_stop) at format DEFAULT_SERVER_DATETIME_FORMAT """ - company_id = self._default_company(cr, uid, context=context) fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, [('date_start', '<=', time.strftime(DF)), ('date_stop', '>=', time.strftime(DF)), ('company_id', '=', company_id)]) if fiscalyear_ids: # has a current fiscal year, use this one - return self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context).date_start - else: - past_fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, - [('date_stop', '<=', time.strftime(DF)), ('company_id', '=', company_id)]) - if past_fiscalyear_ids: - # use the latest fiscal year+1 day, sorted by start_date - latest_stop = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context).date_stop - return (datetime.datetime.strptime(latest_stop, DF) + datetime.timedelta(days=1)).strftime(DF) - else: - return time.strftime('%Y-01-01') - - def _default_stop_date(self, cr, uid, context=None): - """Compute default ending date for fiscalyear - - if no fiscal year, use 31th dec of this year - - if in a fiscal year, use its ending date - - if past fiscal year, use the ending date of latest fiscalyear +1 year - """ - company_id = self._default_company(cr, uid, context=context) - fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, - [('date_start', '<=', time.strftime(DF)), ('date_stop', '>=', time.strftime(DF)), - ('company_id', '=', company_id)]) - if fiscalyear_ids: - # has a current fiscal year, use this one - return self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context).date_stop + fiscalyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context) + return (fiscalyear.date_start, fiscalyear.date_stop) else: past_fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, [('date_stop', '<=', time.strftime(DF)), ('company_id', '=', company_id)]) @@ -176,16 +154,14 @@ class account_config_settings(osv.osv_memory): # use the latest fiscal year+1 day, sorted by start_date latest_year = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context) latest_stop = datetime.datetime.strptime(latest_year.date_stop, DF) - return latest_stop.replace(year=latest_stop.year+1).strftime(DF) + return ((latest_stop+datetime.timedelta(days=1)).strftime(DF), latest_stop.replace(year=latest_stop.year+1).strftime(DF)) else: - return time.strftime('%Y-12-31') + return (time.strftime('%Y-01-01'), time.strftime('%Y-12-31')) _defaults = { 'company_id': _default_company, 'has_default_company': _default_has_default_company, - 'date_start': _default_start_date, - 'date_stop': _default_stop_date, 'period': 'month', } @@ -210,6 +186,7 @@ class account_config_settings(osv.osv_memory): fiscalyear_count = self.pool.get('account.fiscalyear').search_count(cr, uid, [('date_start', '<=', time.strftime('%Y-%m-%d')), ('date_stop', '>=', time.strftime('%Y-%m-%d')), ('company_id', '=', company_id)]) + date_start, date_stop = self._get_default_fiscalyear_dates(cr, uid, company_id, context=context) values = { 'expects_chart_of_accounts': company.expects_chart_of_accounts, 'currency_id': company.currency_id.id, @@ -219,6 +196,8 @@ class account_config_settings(osv.osv_memory): 'has_fiscal_year': bool(fiscalyear_count), 'chart_template_id': False, 'tax_calculation_rounding_method': company.tax_calculation_rounding_method, + 'date_start': date_start, + 'date_stop': date_stop, } # update journals and sequences for journal_type in ('sale', 'sale_refund', 'purchase', 'purchase_refund'): From b9ee794a2bbe1d851764948a40f81128473fd946 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Wed, 8 May 2013 15:14:38 +0200 Subject: [PATCH 6/6] [IMP] account: compute default period for fiscal year bzr revid: mat@openerp.com-20130508131438-nyzi49wbn61giecf --- addons/account/res_config.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/addons/account/res_config.py b/addons/account/res_config.py index ef16638905f..f1f1aee7d41 100644 --- a/addons/account/res_config.py +++ b/addons/account/res_config.py @@ -133,36 +133,43 @@ class account_config_settings(osv.osv_memory): count = self.pool.get('res.company').search_count(cr, uid, [], context=context) return bool(count == 1) - def _get_default_fiscalyear_dates(self, cr, uid, company_id, context=None): - """Compute default starting and ending date for fiscalyear - - if no fiscal year, use 1st jan of this year - - if in a fiscal year, use its starting date - - if past fiscal year, use the ending date of the latest +1 day - :return: (date_start, date_stop) at format DEFAULT_SERVER_DATETIME_FORMAT + def _get_default_fiscalyear_data(self, cr, uid, company_id, context=None): + """Compute default period, starting and ending date for fiscalyear + - if in a fiscal year, use its period, starting and ending date + - if past fiscal year, use its period, and new dates [ending date of the latest +1 day ; ending date of the latest +1 year] + - if no fiscal year, use monthly, 1st jan, 31th dec of this year + :return: (date_start, date_stop, period) at format DEFAULT_SERVER_DATETIME_FORMAT """ fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, [('date_start', '<=', time.strftime(DF)), ('date_stop', '>=', time.strftime(DF)), ('company_id', '=', company_id)]) if fiscalyear_ids: - # has a current fiscal year, use this one + # is in a current fiscal year, use this one fiscalyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_ids[0], context=context) - return (fiscalyear.date_start, fiscalyear.date_stop) + if len(fiscalyear.period_ids) == 5: # 4 periods of 3 months + opening period + period = '3months' + else: + period = 'month' + return (fiscalyear.date_start, fiscalyear.date_stop, period) else: past_fiscalyear_ids = self.pool.get('account.fiscalyear').search(cr, uid, [('date_stop', '<=', time.strftime(DF)), ('company_id', '=', company_id)]) if past_fiscalyear_ids: - # use the latest fiscal year+1 day, sorted by start_date + # use the latest fiscal, sorted by (start_date, id) latest_year = self.pool.get('account.fiscalyear').browse(cr, uid, past_fiscalyear_ids[-1], context=context) latest_stop = datetime.datetime.strptime(latest_year.date_stop, DF) - return ((latest_stop+datetime.timedelta(days=1)).strftime(DF), latest_stop.replace(year=latest_stop.year+1).strftime(DF)) + if len(latest_year.period_ids) == 5: + period = '3months' + else: + period = 'month' + return ((latest_stop+datetime.timedelta(days=1)).strftime(DF), latest_stop.replace(year=latest_stop.year+1).strftime(DF), period) else: - return (time.strftime('%Y-01-01'), time.strftime('%Y-12-31')) + return (time.strftime('%Y-01-01'), time.strftime('%Y-12-31'), 'month') _defaults = { 'company_id': _default_company, 'has_default_company': _default_has_default_company, - 'period': 'month', } def create(self, cr, uid, values, context=None): @@ -186,7 +193,7 @@ class account_config_settings(osv.osv_memory): fiscalyear_count = self.pool.get('account.fiscalyear').search_count(cr, uid, [('date_start', '<=', time.strftime('%Y-%m-%d')), ('date_stop', '>=', time.strftime('%Y-%m-%d')), ('company_id', '=', company_id)]) - date_start, date_stop = self._get_default_fiscalyear_dates(cr, uid, company_id, context=context) + date_start, date_stop, period = self._get_default_fiscalyear_data(cr, uid, company_id, context=context) values = { 'expects_chart_of_accounts': company.expects_chart_of_accounts, 'currency_id': company.currency_id.id, @@ -198,6 +205,7 @@ class account_config_settings(osv.osv_memory): 'tax_calculation_rounding_method': company.tax_calculation_rounding_method, 'date_start': date_start, 'date_stop': date_stop, + 'period': period, } # update journals and sequences for journal_type in ('sale', 'sale_refund', 'purchase', 'purchase_refund'):