diff --git a/addons/account_balance/account_move_line.py b/addons/account_balance/account_move_line.py index b5a4cab7960..817f09ee194 100644 --- a/addons/account_balance/account_move_line.py +++ b/addons/account_balance/account_move_line.py @@ -39,45 +39,54 @@ class account_move_line(osv.osv): _description = "Entry lines" def _query_get(self, cr, uid, obj='l', context={}): - - if not 'fiscalyear' in context: - context['fiscalyear'] = self.pool.get('account.fiscalyear').find(cr, uid, exception=False) - - strQuery = "" - if context.get('periods', False): - ids = ','.join([str(x) for x in context['periods']]) - - if not 'period_manner' in context: - strQuery = obj+".active AND "+obj+".state<>'draft' AND "+obj+".period_id in (SELECT id from account_period WHERE fiscalyear_id=%d)" % (context['fiscalyear'],) - else: - if context['period_manner']=='actual': - - strQuery = obj+".active AND "+obj+".state<>'draft' AND "+obj+".period_id in (SELECT id from account_period WHERE fiscalyear_id=%d AND id in (%s))" % (context['fiscalyear'], ids) - else: -# p_id="in (SELECT id from account_period WHERE fiscalyear_id=%d AND id in (%s)))" % (context['fiscalyear'], ids) - strQuery = obj+".active AND "+obj+".state<>'draft' AND(" - - p_id=self.pool.get('account.period').search(cr,uid,[('fiscalyear_id','=',context['fiscalyear']),('id','in',context['periods'])]) - - periods = self.pool.get('account.period').read(cr,uid,p_id,['date_start','date_stop']) - - count=1 - len_periods=len(p_id) - - - for period in periods: - strQuery += "("+obj+".create_date between to_date('" + period['date_start'] + "','yyyy-mm-dd') and to_date('" + period['date_stop'] + "','yyyy-mm-dd'))" - if len_periods!=1 and count!=len_periods: - strQuery+=" OR " - count=count+1 - if p_id==[]: - strQuery+=obj+".period_id in (SELECT id from account_period WHERE fiscalyear_id=%d))" % (context['fiscalyear'],) + query = super(account_move_line, self)._query_get(cr, uid, obj, context) + if 'period_manner' in context: + if context['period_manner'] == 'created': + if 'periods' in context: + #the query have to be build with no reference to periods but thanks to the creation date + if context['periods']: + #if one or more period are given, use them + p_ids = self.pool.get('account.period').search(cr,uid,[('id','in',context['periods'])]) else: - strQuery+=")" - else: - strQuery = obj+".active AND "+obj+".state<>'draft' AND "+obj+".period_id in (SELECT id from account_period WHERE fiscalyear_id=%d)" % (context['fiscalyear'],) + #else we have to consider all the periods of the selected fiscal year(s) + fiscalyear_obj = self.pool.get('account.fiscalyear') + if not context.get('fiscalyear', False): - return strQuery + #if there is no fiscal year, take all the fiscal years + fiscalyear_ids = fiscalyear_obj.search(cr, uid, [('state', '=', 'draft')]) + else: + fiscalyear_ids = [context['fiscalyear']] + p_ids = self.pool.get('account.period').search(cr,uid,[('fiscalyear_id','in',fiscalyear_ids)]) + + if p_ids == []: + return query + + #remove from the old query the clause related to the period selection + res = '' + count = 1 + clause_list = query.split('AND') + ref_string = ' '+obj+'.period_id in' + for clause in clause_list: + if count != 1 and not clause.startswith(ref_string): + res += "AND" + if not clause.startswith(ref_string): + res += clause + count += 1 + + #add to 'res' a new clause containing the creation date criterion + count = 1 + res += " AND (" + periods = self.pool.get('account.period').read(cr,uid,p_ids,['date_start','date_stop']) + for period in periods: + if count != 1: + res += " OR " + #creation date criterion: the creation date of the move_line has to be + # between the date_start and the date_stop of the selected periods + res += "("+obj+".create_date between to_date('" + period['date_start'] + "','yyyy-mm-dd') and to_date('" + period['date_stop'] + "','yyyy-mm-dd'))" + count += 1 + res += ")" + return res + return query account_move_line()