From 0d467eeea01a357ff900af7e3c1985980995335f Mon Sep 17 00:00:00 2001 From: Mustufa Rangwala Date: Wed, 8 Sep 2010 09:44:22 +0530 Subject: [PATCH] [FIX] Account: Fiscal year overlap problem (Add constraint) bzr revid: mra@mra-laptop-20100908041422-vpxitbhcjbaisfyz --- addons/account/account.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/addons/account/account.py b/addons/account/account.py index df54affe012..dbbf8400318 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -758,14 +758,45 @@ class account_fiscalyear(osv.osv): } _order = "date_start" + def _check_fiscal_year(self, cr, uid, ids, context=None): + current_fiscal_yr = self.browse(cr, uid, ids, context=context)[0] + obj_fiscal_ids = self.search(cr, uid, [('company_id', '=', current_fiscal_yr.company_id.id)], context=context) + obj_fiscal_ids.remove(ids[0]) + data_fiscal_yr = self.browse(cr, uid, obj_fiscal_ids, context=context) + +# # To check for gap between fiscal year +# old_latest_fy = self.search(cr, uid, [('date_stop', '<', datetime.strptime(current_fiscal_yr['date_start'], '%Y-%m-%d') ), ('company_id', '=', current_fiscal_yr.company_id.id), ('id', '!=', current_fiscal_yr.id )], order='date_stop desc', limit=1, context=context) +# data_old_latest_fy = self.browse(cr, uid, old_latest_fy, context=context) +# if data_old_latest_fy and data_old_latest_fy[0].company_id.id == current_fiscal_yr['company_id'].id and \ +# datetime.strptime(data_old_latest_fy[0].date_stop, '%Y-%m-%d') + timedelta(days=1) != datetime.strptime(current_fiscal_yr['date_start'], '%Y-%m-%d'): +# return False +# +# next_latest_fy = self.search(cr, uid, [('date_start', '>', datetime.strptime(current_fiscal_yr['date_stop'], '%Y-%m-%d') ), ('company_id', '=', current_fiscal_yr.company_id.id), ('id', '!=', current_fiscal_yr.id )], order='date_start', limit=1, context=context) +# data_next_latest_fy = self.browse(cr, uid, next_latest_fy, context=context) +# if data_next_latest_fy and data_next_latest_fy[0].company_id.id == current_fiscal_yr['company_id'].id and \ +# datetime.strptime(current_fiscal_yr['date_stop'], '%Y-%m-%d') + timedelta(days=1) != datetime.strptime(data_next_latest_fy[0].date_start, '%Y-%m-%d'): +# return False + + for old_fy in data_fiscal_yr: + if old_fy.company_id.id == current_fiscal_yr['company_id'].id: + # Condition to check if the current fiscal year falls in between any previously defined fiscal year + if old_fy.date_start <= current_fiscal_yr['date_start'] <= old_fy.date_stop or \ + old_fy.date_start <= current_fiscal_yr['date_stop'] <= old_fy.date_stop: + return False + # Condition to check whether there exist any previously defined fiscal year falling in between the newly created fiscal year + # elif current_fiscal_yr['date_start'] <= old_fy.date_start <= current_fiscal_yr['date_stop'] or current_fiscal_yr['date_start'] <= old_fy.date_stop <= current_fiscal_yr['date_stop']: +# return False + return True + def _check_duration(self,cr,uid,ids): - obj_fy=self.browse(cr,uid,ids[0]) + obj_fy = self.browse(cr,uid,ids[0]) if obj_fy.date_stop < obj_fy.date_start: return False return True _constraints = [ - (_check_duration, 'Error ! The duration of the Fiscal Year is invalid. ', ['date_stop']) + (_check_duration, 'Error ! The duration of the Fiscal Year is invalid. ', ['date_stop']), + (_check_fiscal_year, 'Error ! This Fiscal Year overlaps an existing Fiscal Year',['date_start', 'date_stop']) ] def create_period3(self,cr, uid, ids, context={}):