1 from itertools import chain
2 from osv import osv, fields
5 class fleet_vehicle_model_type(osv.Model):
6 _name = 'fleet.vehicle.type'
7 _description = 'Type of the vehicle'
9 'name' : fields.char('Name', size=32, required=True),
12 class fleet_vehicle_tag(osv.Model):
13 _name = 'fleet.vehicle.tag'
15 'name': fields.char('Name', required=True, translate=True),
18 class fleet_vehicle_state(osv.Model):
19 _name = 'fleet.vehicle.state'
21 'name': fields.char('Name', required=True),
22 'sequence': fields.integer('Order',help="Used to order the note stages")
24 _order = 'sequence asc'
26 class fleet_vehicle_model(osv.Model):
28 def name_get(self, cr, uid, ids, context=None):
33 reads = self.browse(cr, uid, ids, context=context)
36 name = record.modelname
38 name = record.brand.name+' / '+name
39 res.append((record.id, name))
42 def _model_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
43 res = self.name_get(cr, uid, ids, context=context)
46 _name = 'fleet.vehicle.model'
47 _description = 'Model of a vehicle'
50 'name' : fields.function(_model_name_get_fnc, type="char", string='Name', store=True),
51 'modelname' : fields.char('Model name', size=32, required=True),
52 'brand' : fields.many2one('fleet.vehicle.model.brand', 'Model brand', required=True, help='Brand of the vehicle'),
53 'vendors': fields.many2many('res.partner','fleet_vehicle_model_vendors','model_id', 'partner_id',string='Vendors',required=False),
54 'image': fields.related('brand','image',type="binary",string="Logo",store=False)
57 class fleet_vehicle_model_brand(osv.Model):
58 _name = 'fleet.vehicle.model.brand'
59 _description = 'Brand model of the vehicle'
61 'name' : fields.char('Brand Name',size=32, required=True),
62 'image': fields.binary("Logo",help="This field holds the image used as logo for the brand, limited to 128x128px."),
65 class fleet_vehicle(osv.Model):
67 _inherit = 'mail.thread'
69 def name_get(self, cr, uid, ids, context=None):
74 reads = self.browse(cr, uid, ids, context=context)
77 if record.license_plate:
78 name = record.license_plate
79 if record.model_id.modelname:
80 name = record.model_id.modelname + ' / ' + name
81 if record.model_id.brand.name:
82 name = record.model_id.brand.name+' / '+ name
83 res.append((record.id, name))
86 def _vehicle_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
87 res = self.name_get(cr, uid, ids, context=context)
90 def act_show_log_services(self, cr, uid, ids, context=None):
91 """ This opens log view to view and add new log for this vehicle
92 @return: the service log view
94 res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid ,'fleet','act_show_log_services', context)
96 'default_vehicle_id': ids[0]
98 res['domain']=[('vehicle_id','=', ids[0])]
101 def act_show_log_fuel(self, cr, uid, ids, context=None):
102 """ This opens log view to view and add new log for this vehicle
103 @return: the service log view
105 res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid ,'fleet','act_show_log_fuel', context)
107 'default_vehicle_id': ids[0]
109 res['domain']=[('vehicle_id','=', ids[0])]
112 _name = 'fleet.vehicle'
113 _description = 'Fleet Vehicle'
116 'name' : fields.function(_vehicle_name_get_fnc, type="char", string='Name', store=True),
117 'company_id': fields.many2one('res.company', 'Company'),
118 'license_plate' : fields.char('License plate', size=32, required=True, help='License plate number of the vehicle (ie: plate number for a car)'),
119 'vin_sn' : fields.char('Chassis Number', size=32, required=False, help='Unique number written on the vehicle motor (VIN/SN number)'),
120 'driver' : fields.many2one('res.partner', 'Driver',required=False, help='Driver of the vehicle'),
121 'model_id' : fields.many2one('fleet.vehicle.model', 'Model', required=True, help='Model of the vehicle'),
122 'log_ids' : fields.one2many('fleet.vehicle.log', 'vehicle_id', 'Other Logs'),
123 'log_fuel' : fields.one2many('fleet.vehicle.log.fuel','vehicle_id', 'Fuel Logs'),
124 'log_services' : fields.one2many('fleet.vehicle.log.services','vehicle_id', 'Services Logs'),
125 'log_insurances' : fields.one2many('fleet.vehicle.log.insurance','vehicle_id', 'Insurances'),
126 'acquisition_date' : fields.date('Acquisition date', required=False, help='Date when the vehicle has been bought'),
127 'acquisition_price' : fields.integer('Price', help='Price of the bought vehicle'),
128 'color' : fields.char('Color',size=32, help='Color of the vehicle'),
129 'state': fields.many2one('fleet.vehicle.state', 'State', help='Current state of the vehicle', domain='[]'),
130 'location' : fields.char('Location',size=32, help='Location of the vehicle (garage, ...)'),
131 'doors' : fields.integer('Number of doors', help='Number of doors of the vehicle'),
132 'tag_ids' :fields.many2many('fleet.vehicle.tag','vehicle_vehicle_tag_rel','vehicle_tag_id','tag_id','Tags'),
134 'transmission' : fields.selection([('manual', 'Manual'),('automatic','Automatic')], 'Transmission', help='Transmission Used by the vehicle',required=False),
135 'fuel_type' : fields.selection([('gasoline', 'Gasoline'),('diesel','Diesel'),('electric','Electric'),('hybrid','Hybrid')], 'Fuel Type', help='Fuel Used by the vehicle',required=False),
136 'horsepower' : fields.integer('Horsepower',required=False),
137 'horsepower_tax': fields.float('Horsepower Taxation'),
138 'power' : fields.integer('Power (kW)',required=False,help='Power in kW of the vehicle'),
139 'co2' : fields.float('CO2 Emissions',required=False,help='CO2 emissions of the vehicle'),
141 'image': fields.related('model_id','image',type="binary",string="Logo",store=False)
147 def on_change_model(self, cr, uid, ids, model_id, context=None):
148 # print "ids: %r" % (ids,)
149 # print "model_id: %r" % (model_id,)
150 # print "context: %r" % (context,)
152 # logger = logging.getLogger('fleet.vehicle')
153 # logger.info('Hello')
161 model = self.pool.get('fleet.vehicle.model').browse(cr, uid, model_id, context=context)
163 print "model: %r" % (model.name,)
167 'message' : "You have selected this %s model" % (model.name,),
170 def create(self, cr, uid, data, context=None):
171 vehicle_id = super(fleet_vehicle, self).create(cr, uid, data, context=context)
173 vehicle = self.browse(cr, uid, vehicle_id, context=context)
174 self.message_post(cr, uid, [vehicle_id], body='Vehicle %s has been added to the fleet!' % (vehicle.license_plate), context=context)
176 pass # group deleted: do not push a message
179 def write(self, cr, uid, ids, vals, context=None):
180 vehicle_id = super(fleet_vehicle,self).write(cr, uid, ids, vals, context)
183 for key,value in vals.items():
184 if key == 'license_plate' or key == 'driver':
187 self.message_post(cr, uid, [vehicle_id], body='Vehicle edited. Changes : '+ str(changes), context=context)
188 #self.message_post(cr, uid, [vehicle_id], body='Vehicle edited. Changes : '+ ','.join(chain(*str(changes.items()))), context=context)
189 except Exception as e:
194 class fleet_vehicle_odometer(osv.Model):
195 _name='fleet.vehicle.odometer'
196 _description='Odometer log for a vehicle'
199 'name' : fields.char('Name',size=64),
201 'date' : fields.date('Date'),
202 'value' : fields.float('Odometer Value'),
203 'unit' : fields.selection([('kilometers', 'Kilometers'),('miles','Miles')], 'Odometer Unit', help='Unit of the measurement',required=False),
204 'vehicle_id' : fields.many2one('fleet.vehicle', 'Vehicle', required=True),
207 'date' : time.strftime('%Y-%m-%d')
210 class fleet_vehicle_log_fuel(osv.Model):
212 _inherits = {'fleet.vehicle.odometer': 'odometer_id'}
214 def on_change_liter(self, cr, uid, ids, liter, price_per_liter, amount, context=None):
216 if liter > 0 and price_per_liter > 0:
217 return {'value' : {'amount' : float(liter) * float(price_per_liter),}}
218 elif liter > 0 and amount > 0:
219 return {'value' : {'price_per_liter' : float(amount) / float(liter),}}
220 elif price_per_liter > 0 and amount > 0:
221 return {'value' : {'liter' : float(amount) / float(price_per_liter),}}
225 def on_change_price_per_liter(self, cr, uid, ids, liter, price_per_liter, amount, context=None):
227 liter = float(liter);
228 price_per_liter = float(price_per_liter);
229 if price_per_liter > 0 and liter > 0:
230 return {'value' : {'amount' : float(liter) * float(price_per_liter),}}
231 elif price_per_liter > 0 and amount > 0:
232 return {'value' : {'liter' : float(amount) / float(price_per_liter),}}
233 elif liter > 0 and amount > 0:
234 return {'value' : {'price_per_liter' : float(amount) / float(liter),}}
238 def on_change_amount(self, cr, uid, ids, liter, price_per_liter, amount, context=None):
240 if amount > 0 and liter > 0:
241 return {'value' : {'price_per_liter' : float(amount) / float(liter),}}
242 elif amount > 0 and price_per_liter > 0:
243 return {'value' : {'liter' : float(amount) / float(price_per_liter),}}
244 elif liter > 0 and price_per_liter > 0:
245 return {'value' : {'amount' : float(liter) * float(price_per_liter),}}
250 _name = 'fleet.vehicle.log.fuel'
253 'name' : fields.char('Name',size=64),
255 'liter' : fields.float('Liter'),
256 'price_per_liter' : fields.float('Price per liter'),
257 'amount': fields.float('Total price'),
258 'purchaser_id' : fields.many2one('res.partner', 'Purchaser'),
259 'inv_ref' : fields.char('Invoice Reference', size=64),
260 'vendor_id' : fields.many2one('res.partner', 'Vendor', domain="[('supplier','=',True)]"),
261 'notes' : fields.text('Notes'),
264 'purchaser_id': lambda self, cr, uid, ctx: uid,
267 class fleet_vehicle_log_services(osv.Model):
269 _inherits = {'fleet.vehicle.odometer': 'odometer_id'}
271 _name = 'fleet.vehicle.log.services'
274 'name' : fields.char('Name',size=64),
276 'amount' :fields.float('Cost', help="Total cost of the service"),
277 'service_ids' :fields.many2many('fleet.service.type','vehicle_service_type_rel','vehicle_service_type_id','service_id','Services completed'),
278 'purchaser_id' : fields.many2one('res.partner', 'Purchaser'),
279 'inv_ref' : fields.char('Invoice Reference', size=64),
280 'vendor_id' :fields.many2one('res.partner', 'Vendor', domain="[('supplier','=',True)]"),
281 'notes' : fields.text('Notes'),
284 'purchaser_id': lambda self, cr, uid, ctx: uid,
287 class fleet_insurance_type(osv.Model):
288 _name = 'fleet.insurance.type'
290 'name': fields.char('Name', required=True, translate=True),
293 class fleet_vehicle_log_insurance(osv.Model):
294 _inherits = {'fleet.vehicle.odometer': 'odometer_id'}
296 _name = 'fleet.vehicle.log.insurance'
299 'name' : fields.char('Name',size=64),
301 'insurance_type' : fields.many2one('fleet.insurance.type', 'Type', required=False, help='Type of the insurance'),
302 'start_date' : fields.date('Start date', required=False, help='Date when the coverage of the insurance begins'),
303 'expiration_date' : fields.date('Expiration date', required=False, help='Date when the coverage of the insurance expirates'),
304 'price' : fields.float('Price', help="Cost of the insurance for the specified period"),
305 'insurer_id' :fields.many2one('res.partner', 'Insurer', domain="[('supplier','=',True)]"),
306 'purchaser_id' : fields.many2one('res.partner', 'Purchaser'),
307 'ins_ref' : fields.char('Insurance Reference', size=64),
308 'notes' : fields.text('Notes'),
311 'purchaser_id': lambda self, cr, uid, ctx: uid,
314 class fleet_service_type(osv.Model):
315 _name = 'fleet.service.type'
317 'name': fields.char('Name', required=True, translate=True),
320 #class hr_employee(osv.Model):
321 # _inherit = 'hr.employee'
324 # 'vehicle_id' : fields.one2many('fleet.vehicle','driver', 'Vehicle',type="char"),
325 # 'log_ids' : fields.one2many('fleet.vehicle.log', 'employee_id', 'Logs'),