- _inherits = {'fleet.vehicle.cost': 'cost_id'}
-
- def run_scheduler(self,cr,uid,context=None):
-
- d = datetime.datetime.now()
- #d = datetime.datetime(2012, 12, 01)
-
- contract_ids = self.pool.get('fleet.vehicle.log.contract').search(cr, uid, [('state','=','open')], offset=0, limit=None, order=None,context=None, count=False)
- for contract in self.pool.get('fleet.vehicle.log.contract').browse(cr,uid,contract_ids,context=context):
- if contract.generated_cost_ids != []:
- last_cost_id = self.pool.get('fleet.vehicle.cost').search(cr, uid, ['&',('contract_id','=',contract.id),('auto_generated','=',True)], offset=0, limit=1, order='date desc',context=None, count=False)
- last_cost_date = self.pool.get('fleet.vehicle.cost').browse(cr,uid,last_cost_id[0],context=None).date
- else :
- last_cost_date = False
-
- if contract.cost_frequency == 'yearly':
- delta = relativedelta(years=+1)
- if last_cost_date:
- startdate = datetime.datetime(int(last_cost_date[:4]),1,1) + relativedelta(years=+1)
- else:
- startdate = datetime.datetime(int(contract.start_date[:4]),1,1)
- enddate = datetime.datetime(d.year,1,1)
- if contract.cost_frequency == 'monthly':
- delta = relativedelta(months=+1)
- if last_cost_date:
- startdate = datetime.datetime(int(last_cost_date[:4]),int(last_cost_date[5:7]),1) + relativedelta(months=+1)
- else:
- startdate = datetime.datetime(int(contract.start_date[:4]),int(contract.start_date[5:7]),1)
- enddate = datetime.datetime(d.year,d.month,1)
- if contract.cost_frequency == 'weekly':
- delta = relativedelta(weeks=+1)
- if last_cost_date:
- startdate = datetime.datetime.strptime(last_cost_date[:4]+'-'+last_cost_date[5:7]+'-'+str(self.str_to_date(last_cost_date).weekday()),'%Y-%m-%w') + relativedelta(weeks=+1)
- else:
- startdate = datetime.datetime.strptime(contract.start_date[:4]+'-'+contract.start_date[5:7]+'-'+str(self.str_to_date(contract.start_date).weekday()),'%Y-%m-%w')
- enddate = datetime.datetime.strptime(d.year+'-'+d.month+'-'+d.weekday(),'%Y-%m-%w')
- if contract.cost_frequency == 'daily':
- delta = relativedelta(days=+1)
- if last_cost_date:
- startdate = datetime.datetime.strptime(last_cost_date,'%Y-%m-%d') + relativedelta(days=+1)
- else:
- startdate = datetime.datetime.strptime(contract.start_date,'%Y-%m-%d')
- enddate = d
- print 'Start : '+str(startdate)+', End : '+str(enddate)
- while startdate <= enddate:
- data = {'amount' : contract.cost_generated,'date' : startdate,'vehicle_id' : contract.vehicle_id.id,'cost_type' : contract.cost_type.id,'contract_id' : contract.id,'auto_generated' : True}
- print data
+ def scheduler_manage_auto_costs(self, cr, uid, context=None):
+ #This method is called by a cron task
+ #It creates costs for contracts having the "recurring cost" field setted, depending on their frequency
+ #For example, if a contract has a reccuring cost of 200 with a weekly frequency, this method creates a cost of 200 on the first day of each week, from the date of the last recurring costs in the database to today
+ #If the contract has not yet any recurring costs in the database, the method generates the recurring costs from the start_date to today
+ #The created costs are associated to a contract thanks to the many2one field contract_id
+ #If the contract has no start_date, no cost will be created, even if the contract has recurring costs
+ vehicle_cost_obj = self.pool.get('fleet.vehicle.cost')
+ d = datetime.datetime.strptime(fields.date.context_today(self, cr, uid, context=context), tools.DEFAULT_SERVER_DATE_FORMAT).date()
+ contract_ids = self.pool.get('fleet.vehicle.log.contract').search(cr, uid, [('state','!=','closed')], offset=0, limit=None, order=None,context=None, count=False)
+ deltas = {'yearly': relativedelta(years=+1), 'monthly': relativedelta(months=+1), 'weekly': relativedelta(weeks=+1), 'daily': relativedelta(days=+1)}
+ for contract in self.pool.get('fleet.vehicle.log.contract').browse(cr, uid, contract_ids, context=context):
+ if not contract.start_date or contract.cost_frequency == 'no':
+ continue
+ found = False
+ last_cost_date = contract.start_date
+ if contract.generated_cost_ids:
+ last_autogenerated_cost_id = vehicle_cost_obj.search(cr, uid, ['&', ('contract_id','=',contract.id), ('auto_generated','=',True)], offset=0, limit=1, order='date desc',context=context, count=False)
+ if last_autogenerated_cost_id:
+ found = True
+ last_cost_date = vehicle_cost_obj.browse(cr, uid, last_autogenerated_cost_id[0], context=context).date
+ startdate = datetime.datetime.strptime(last_cost_date, tools.DEFAULT_SERVER_DATE_FORMAT).date()
+ if found:
+ startdate += deltas.get(contract.cost_frequency)
+ while (startdate <= d) & (startdate <= datetime.datetime.strptime(contract.expiration_date, tools.DEFAULT_SERVER_DATE_FORMAT).date()):
+ data = {
+ 'amount': contract.cost_generated,
+ 'date': startdate.strftime(tools.DEFAULT_SERVER_DATE_FORMAT),
+ 'vehicle_id': contract.vehicle_id.id,
+ 'cost_subtype_id': contract.cost_subtype_id.id,
+ 'contract_id': contract.id,
+ 'auto_generated': True
+ }