[REF]refactoring of search function to find vehicle with due or overdue contracts

bzr revid: csn@openerp.com-20121107161932-mv2jpfjftw0fpbmp
This commit is contained in:
Cedric Snauwaert 2012-11-07 17:19:32 +01:00
parent 2665f36934
commit 7e3222f924
1 changed files with 11 additions and 13 deletions

View File

@ -248,25 +248,23 @@ class fleet_vehicle(osv.Model):
def _search_get_overdue_contract_reminder(self, cr, uid, obj, name, args, context):
res = []
for field, operator, value in args:
vehicle_ids = self.search(cr, uid, [])
contracts_needed = self._get_contract_reminder_fnc(cr,uid,vehicle_ids,['contract_renewal_total', 'contract_renewal_due_soon', 'contract_renewal_overdue', 'contract_renewal_name'],None,context=context)
res_ids = []
for renew_key,renew_value in contracts_needed.items():
if eval(str(renew_value['contract_renewal_overdue']) + " " + str(operator) + " " + str(value)):
res_ids.append(renew_key)
assert operator == '>' and value == 0, 'Operation not supported'
today = fields.date.context_today(self, cr, uid, context=context)
cr.execute('select cost.vehicle_id, count(contract.id) as contract_number FROM fleet_vehicle_cost cost left join fleet_vehicle_log_contract contract on contract.cost_id = cost.id WHERE contract.expiration_date is not null AND contract.expiration_date < %s AND contract.state IN (\'open\', \'toclose\') GROUP BY cost.vehicle_id', (today,))
res_ids = [x[0] for x in cr.fetchall()]
res.append(('id', 'in', res_ids))
return res
def _search_contract_renewal_due_soon(self, cr, uid, obj, name, args, context):
res = []
for field, operator, value in args:
vehicle_ids = self.search(cr, uid, [])
contracts_needed = self._get_contract_reminder_fnc(cr,uid,vehicle_ids,['contract_renewal_total', 'contract_renewal_due_soon', 'contract_renewal_overdue', 'contract_renewal_name'],None,context=context)
res_ids = []
for renew_key,renew_value in contracts_needed.items():
if eval(str(renew_value['contract_renewal_due_soon']) + " " + str(operator) + " " + str(value)):
res_ids.append(renew_key)
res.append(('id', 'in', res_ids))
assert operator == '>' and value == 0, 'Operation not supported'
today = fields.date.context_today(self, cr, uid, context=context)
datetime_today = datetime.datetime.strptime(today, tools.DEFAULT_SERVER_DATE_FORMAT)
limit_date = str((datetime_today + relativedelta(days=+15)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT))
cr.execute('select cost.vehicle_id, count(contract.id) as contract_number FROM fleet_vehicle_cost cost left join fleet_vehicle_log_contract contract on contract.cost_id = cost.id WHERE contract.expiration_date is not null AND contract.expiration_date > %s AND contract.expiration_date < %s AND contract.state IN (\'open\', \'toclose\') GROUP BY cost.vehicle_id', (today, limit_date))
res_ids = [x[0] for x in cr.fetchall()]
res.append(('id', 'in', res_ids))
return res
def _get_contract_reminder_fnc(self, cr, uid, ids, prop, unknow_none, context=None):