2012-09-26 09:28:58 +00:00
from itertools import chain
2012-09-19 16:13:27 +00:00
from osv import osv , fields
2012-09-26 14:13:52 +00:00
import time
2012-09-20 15:21:21 +00:00
class fleet_vehicle_model_type ( osv . Model ) :
2012-09-20 13:07:14 +00:00
_name = ' fleet.vehicle.type '
_description = ' Type of the vehicle '
_columns = {
' name ' : fields . char ( ' Name ' , size = 32 , required = True ) ,
}
2012-09-27 08:09:12 +00:00
class fleet_vehicle_tag ( osv . Model ) :
_name = ' fleet.vehicle.tag '
_columns = {
' name ' : fields . char ( ' Name ' , required = True , translate = True ) ,
}
2012-09-27 13:24:50 +00:00
class fleet_vehicle_state ( osv . Model ) :
_name = ' fleet.vehicle.state '
_columns = {
' name ' : fields . char ( ' Name ' , required = True ) ,
' sequence ' : fields . integer ( ' Order ' , help = " Used to order the note stages " )
}
_order = ' sequence asc '
2012-09-19 16:13:27 +00:00
class fleet_vehicle_model ( osv . Model ) :
2012-09-21 09:57:15 +00:00
2012-09-21 15:42:44 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
if context is None :
context = { }
if not ids :
return [ ]
reads = self . browse ( cr , uid , ids , context = context )
res = [ ]
for record in reads :
name = record . modelname
2012-09-25 07:47:20 +00:00
if record . brand . name :
2012-09-21 15:42:44 +00:00
name = record . brand . name + ' / ' + name
res . append ( ( record . id , name ) )
return res
def _model_name_get_fnc ( self , cr , uid , ids , prop , unknow_none , context = None ) :
res = self . name_get ( cr , uid , ids , context = context )
return dict ( res )
2012-09-19 16:13:27 +00:00
_name = ' fleet.vehicle.model '
2012-09-20 15:21:21 +00:00
_description = ' Model of a vehicle '
2012-09-19 16:13:27 +00:00
_columns = {
2012-09-21 15:42:44 +00:00
' name ' : fields . function ( _model_name_get_fnc , type = " char " , string = ' Name ' , store = True ) ,
' modelname ' : fields . char ( ' Model name ' , size = 32 , required = True ) ,
2012-09-26 12:35:12 +00:00
' brand ' : fields . many2one ( ' fleet.vehicle.model.brand ' , ' Model brand ' , required = True , help = ' Brand of the vehicle ' ) ,
2012-09-21 14:34:15 +00:00
' vendors ' : fields . many2many ( ' res.partner ' , ' fleet_vehicle_model_vendors ' , ' model_id ' , ' partner_id ' , string = ' Vendors ' , required = False ) ,
2012-09-26 13:09:01 +00:00
' image ' : fields . related ( ' brand ' , ' image ' , type = " binary " , string = " Logo " , store = False )
2012-09-19 16:13:27 +00:00
}
2012-09-20 13:11:32 +00:00
class fleet_vehicle_model_brand ( osv . Model ) :
_name = ' fleet.vehicle.model.brand '
_description = ' Brand model of the vehicle '
_columns = {
' name ' : fields . char ( ' Brand Name ' , size = 32 , required = True ) ,
2012-09-26 13:09:01 +00:00
' image ' : fields . binary ( " Logo " , help = " This field holds the image used as logo for the brand, limited to 128x128px. " ) ,
2012-09-20 13:11:32 +00:00
}
2012-09-19 16:13:27 +00:00
class fleet_vehicle ( osv . Model ) :
2012-09-24 09:07:05 +00:00
2012-09-24 11:57:39 +00:00
_inherit = ' mail.thread '
2012-09-24 09:07:05 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
if context is None :
context = { }
if not ids :
return [ ]
reads = self . browse ( cr , uid , ids , context = context )
res = [ ]
for record in reads :
2012-09-27 08:00:59 +00:00
if record . license_plate :
name = record . license_plate
2012-09-25 07:47:20 +00:00
if record . model_id . modelname :
name = record . model_id . modelname + ' / ' + name
if record . model_id . brand . name :
name = record . model_id . brand . name + ' / ' + name
2012-09-24 09:07:05 +00:00
res . append ( ( record . id , name ) )
return res
def _vehicle_name_get_fnc ( self , cr , uid , ids , prop , unknow_none , context = None ) :
res = self . name_get ( cr , uid , ids , context = context )
return dict ( res )
2012-09-27 07:57:26 +00:00
def act_show_log_services ( self , cr , uid , ids , context = None ) :
2012-09-25 13:48:39 +00:00
""" This opens log view to view and add new log for this vehicle
2012-09-27 07:57:26 +00:00
@return : the service log view
2012-09-25 13:48:39 +00:00
"""
2012-09-27 07:57:26 +00:00
res = self . pool . get ( ' ir.actions.act_window ' ) . for_xml_id ( cr , uid , ' fleet ' , ' act_show_log_services ' , context )
2012-09-27 07:48:17 +00:00
res [ ' context ' ] = {
2012-09-27 07:57:26 +00:00
' default_vehicle_id ' : ids [ 0 ]
2012-09-27 07:48:17 +00:00
}
res [ ' domain ' ] = [ ( ' vehicle_id ' , ' = ' , ids [ 0 ] ) ]
2012-09-25 15:14:50 +00:00
return res
2012-09-25 13:48:39 +00:00
2012-09-28 08:08:02 +00:00
def act_show_log_insurance ( self , cr , uid , ids , context = None ) :
""" This opens log view to view and add new log for this vehicle
@return : the insurance log view
"""
res = self . pool . get ( ' ir.actions.act_window ' ) . for_xml_id ( cr , uid , ' fleet ' , ' act_show_log_insurance ' , context )
res [ ' context ' ] = {
' default_vehicle_id ' : ids [ 0 ]
}
res [ ' domain ' ] = [ ( ' vehicle_id ' , ' = ' , ids [ 0 ] ) ]
return res
2012-09-27 07:57:26 +00:00
def act_show_log_fuel ( self , cr , uid , ids , context = None ) :
""" This opens log view to view and add new log for this vehicle
2012-09-28 08:08:02 +00:00
@return : the fuel log view
2012-09-27 07:57:26 +00:00
"""
res = self . pool . get ( ' ir.actions.act_window ' ) . for_xml_id ( cr , uid , ' fleet ' , ' act_show_log_fuel ' , context )
res [ ' context ' ] = {
' default_vehicle_id ' : ids [ 0 ]
}
res [ ' domain ' ] = [ ( ' vehicle_id ' , ' = ' , ids [ 0 ] ) ]
2012-09-27 13:24:50 +00:00
return res
2012-09-27 07:57:26 +00:00
2012-09-19 16:13:27 +00:00
_name = ' fleet.vehicle '
_description = ' Fleet Vehicle '
_columns = {
2012-09-24 09:07:05 +00:00
' name ' : fields . function ( _vehicle_name_get_fnc , type = " char " , string = ' Name ' , store = True ) ,
2012-09-27 08:00:59 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' ) ,
' license_plate ' : fields . char ( ' License plate ' , size = 32 , required = True , help = ' License plate number of the vehicle (ie: plate number for a car) ' ) ,
2012-09-21 11:32:28 +00:00
' vin_sn ' : fields . char ( ' Chassis Number ' , size = 32 , required = False , help = ' Unique number written on the vehicle motor (VIN/SN number) ' ) ,
2012-09-27 13:28:44 +00:00
' driver ' : fields . many2one ( ' res.partner ' , ' Driver ' , required = False , help = ' Driver of the vehicle ' ) ,
2012-09-21 11:32:28 +00:00
' model_id ' : fields . many2one ( ' fleet.vehicle.model ' , ' Model ' , required = True , help = ' Model of the vehicle ' ) ,
2012-09-24 15:58:24 +00:00
' log_ids ' : fields . one2many ( ' fleet.vehicle.log ' , ' vehicle_id ' , ' Other Logs ' ) ,
2012-09-24 07:42:39 +00:00
' log_fuel ' : fields . one2many ( ' fleet.vehicle.log.fuel ' , ' vehicle_id ' , ' Fuel Logs ' ) ,
2012-09-24 12:51:11 +00:00
' log_services ' : fields . one2many ( ' fleet.vehicle.log.services ' , ' vehicle_id ' , ' Services Logs ' ) ,
2012-09-24 15:38:26 +00:00
' log_insurances ' : fields . one2many ( ' fleet.vehicle.log.insurance ' , ' vehicle_id ' , ' Insurances ' ) ,
2012-09-21 11:32:28 +00:00
' acquisition_date ' : fields . date ( ' Acquisition date ' , required = False , help = ' Date when the vehicle has been bought ' ) ,
' acquisition_price ' : fields . integer ( ' Price ' , help = ' Price of the bought vehicle ' ) ,
' color ' : fields . char ( ' Color ' , size = 32 , help = ' Color of the vehicle ' ) ,
2012-09-27 13:24:50 +00:00
' state ' : fields . many2one ( ' fleet.vehicle.state ' , ' State ' , help = ' Current state of the vehicle ' , domain = ' [] ' ) ,
2012-09-21 11:32:28 +00:00
' location ' : fields . char ( ' Location ' , size = 32 , help = ' Location of the vehicle (garage, ...) ' ) ,
2012-09-21 14:34:15 +00:00
' doors ' : fields . integer ( ' Number of doors ' , help = ' Number of doors of the vehicle ' ) ,
2012-09-27 08:09:12 +00:00
' tag_ids ' : fields . many2many ( ' fleet.vehicle.tag ' , ' vehicle_vehicle_tag_rel ' , ' vehicle_tag_id ' , ' tag_id ' , ' Tags ' ) ,
2012-09-19 16:13:27 +00:00
2012-09-21 11:32:28 +00:00
' transmission ' : fields . selection ( [ ( ' manual ' , ' Manual ' ) , ( ' automatic ' , ' Automatic ' ) ] , ' Transmission ' , help = ' Transmission Used by the vehicle ' , required = False ) ,
' fuel_type ' : fields . selection ( [ ( ' gasoline ' , ' Gasoline ' ) , ( ' diesel ' , ' Diesel ' ) , ( ' electric ' , ' Electric ' ) , ( ' hybrid ' , ' Hybrid ' ) ] , ' Fuel Type ' , help = ' Fuel Used by the vehicle ' , required = False ) ,
2012-09-21 14:34:15 +00:00
' horsepower ' : fields . integer ( ' Horsepower ' , required = False ) ,
' horsepower_tax ' : fields . float ( ' Horsepower Taxation ' ) ,
2012-09-21 11:32:28 +00:00
' power ' : fields . integer ( ' Power (kW) ' , required = False , help = ' Power in kW of the vehicle ' ) ,
' co2 ' : fields . float ( ' CO2 Emissions ' , required = False , help = ' CO2 emissions of the vehicle ' ) ,
2012-09-26 13:09:01 +00:00
' image ' : fields . related ( ' model_id ' , ' image ' , type = " binary " , string = " Logo " , store = False )
2012-09-19 16:13:27 +00:00
}
2012-09-27 08:00:59 +00:00
_defaults = {
' doors ' : 5 ,
}
2012-09-19 16:13:27 +00:00
def on_change_model ( self , cr , uid , ids , model_id , context = None ) :
# print "ids: %r" % (ids,)
# print "model_id: %r" % (model_id,)
# print "context: %r" % (context,)
# import logging
# logger = logging.getLogger('fleet.vehicle')
# logger.info('Hello')
# import ipdb
# ipdb.set_trace()
if not model_id :
return { }
model = self . pool . get ( ' fleet.vehicle.model ' ) . browse ( cr , uid , model_id , context = context )
print " model: %r " % ( model . name , )
return {
' value ' : {
' message ' : " You have selected this %s model " % ( model . name , ) ,
}
}
2012-09-25 11:47:14 +00:00
def create ( self , cr , uid , data , context = None ) :
vehicle_id = super ( fleet_vehicle , self ) . create ( cr , uid , data , context = context )
try :
vehicle = self . browse ( cr , uid , vehicle_id , context = context )
2012-09-27 08:00:59 +00:00
self . message_post ( cr , uid , [ vehicle_id ] , body = ' Vehicle %s has been added to the fleet! ' % ( vehicle . license_plate ) , context = context )
2012-09-25 11:47:14 +00:00
except :
pass # group deleted: do not push a message
return vehicle_id
2012-09-19 16:13:27 +00:00
2012-09-25 13:49:41 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
vehicle_id = super ( fleet_vehicle , self ) . write ( cr , uid , ids , vals , context )
try :
2012-09-26 09:28:58 +00:00
changes = { }
for key , value in vals . items ( ) :
2012-09-27 08:00:59 +00:00
if key == ' license_plate ' or key == ' driver ' :
2012-09-26 09:28:58 +00:00
changes [ key ] = value
if len ( changes ) > 0 :
self . message_post ( cr , uid , [ vehicle_id ] , body = ' Vehicle edited. Changes : ' + str ( changes ) , context = context )
#self.message_post(cr, uid, [vehicle_id], body='Vehicle edited. Changes : '+ ','.join(chain(*str(changes.items()))), context=context)
except Exception as e :
print e
2012-09-25 13:49:41 +00:00
pass
return vehicle_id
2012-09-26 14:13:52 +00:00
class fleet_vehicle_odometer ( osv . Model ) :
_name = ' fleet.vehicle.odometer '
_description = ' Odometer log for a vehicle '
2012-09-27 15:05:24 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
if context is None :
context = { }
if not ids :
return [ ]
reads = self . browse ( cr , uid , ids , context = context )
res = [ ]
for record in reads :
if record . vehicle_id . name :
name = str ( record . vehicle_id . name )
if record . date :
name = name + ' / ' + str ( record . date )
print record . date
res . append ( ( record . id , name ) )
return res
def _vehicle_log_name_get_fnc ( self , cr , uid , ids , prop , unknow_none , context = None ) :
res = self . name_get ( cr , uid , ids , context = context )
return dict ( res )
2012-09-26 14:13:52 +00:00
_columns = {
2012-09-27 15:05:24 +00:00
' name ' : fields . function ( _vehicle_log_name_get_fnc , type = " char " , string = ' Name ' , store = True ) ,
2012-09-26 14:13:52 +00:00
' date ' : fields . date ( ' Date ' ) ,
' value ' : fields . float ( ' Odometer Value ' ) ,
' unit ' : fields . selection ( [ ( ' kilometers ' , ' Kilometers ' ) , ( ' miles ' , ' Miles ' ) ] , ' Odometer Unit ' , help = ' Unit of the measurement ' , required = False ) ,
' vehicle_id ' : fields . many2one ( ' fleet.vehicle ' , ' Vehicle ' , required = True ) ,
}
_defaults = {
' date ' : time . strftime ( ' % Y- % m- %d ' )
}
2012-09-21 15:10:22 +00:00
class fleet_vehicle_log_fuel ( osv . Model ) :
2012-09-24 10:17:20 +00:00
2012-09-26 14:56:03 +00:00
_inherits = { ' fleet.vehicle.odometer ' : ' odometer_id ' }
2012-09-24 14:49:27 +00:00
2012-09-25 08:40:05 +00:00
def on_change_liter ( self , cr , uid , ids , liter , price_per_liter , amount , context = None ) :
2012-09-24 10:17:20 +00:00
2012-09-25 08:40:05 +00:00
if liter > 0 and price_per_liter > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' amount ' : float ( liter ) * float ( price_per_liter ) , } }
2012-09-25 08:40:05 +00:00
elif liter > 0 and amount > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' price_per_liter ' : float ( amount ) / float ( liter ) , } }
2012-09-25 08:40:05 +00:00
elif price_per_liter > 0 and amount > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' liter ' : float ( amount ) / float ( price_per_liter ) , } }
2012-09-25 08:40:05 +00:00
else :
return { }
2012-09-24 10:17:20 +00:00
2012-09-25 08:40:05 +00:00
def on_change_price_per_liter ( self , cr , uid , ids , liter , price_per_liter , amount , context = None ) :
2012-09-24 10:17:20 +00:00
2012-09-25 09:20:22 +00:00
liter = float ( liter ) ;
price_per_liter = float ( price_per_liter ) ;
2012-09-25 08:40:05 +00:00
if price_per_liter > 0 and liter > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' amount ' : float ( liter ) * float ( price_per_liter ) , } }
2012-09-25 08:40:05 +00:00
elif price_per_liter > 0 and amount > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' liter ' : float ( amount ) / float ( price_per_liter ) , } }
2012-09-25 08:40:05 +00:00
elif liter > 0 and amount > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' price_per_liter ' : float ( amount ) / float ( liter ) , } }
2012-09-25 08:40:05 +00:00
else :
return { }
def on_change_amount ( self , cr , uid , ids , liter , price_per_liter , amount , context = None ) :
if amount > 0 and liter > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' price_per_liter ' : float ( amount ) / float ( liter ) , } }
2012-09-25 08:40:05 +00:00
elif amount > 0 and price_per_liter > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' liter ' : float ( amount ) / float ( price_per_liter ) , } }
2012-09-25 08:40:05 +00:00
elif liter > 0 and price_per_liter > 0 :
2012-09-25 09:20:22 +00:00
return { ' value ' : { ' amount ' : float ( liter ) * float ( price_per_liter ) , } }
2012-09-25 08:40:05 +00:00
else :
return { }
2012-09-24 10:17:20 +00:00
2012-09-21 15:10:22 +00:00
_name = ' fleet.vehicle.log.fuel '
2012-09-26 14:13:52 +00:00
2012-09-21 15:10:22 +00:00
_columns = {
2012-09-27 15:05:24 +00:00
#'name' : fields.char('Name',size=64),
2012-09-26 14:56:03 +00:00
2012-09-24 15:58:24 +00:00
' liter ' : fields . float ( ' Liter ' ) ,
2012-09-24 07:42:39 +00:00
' price_per_liter ' : fields . float ( ' Price per liter ' ) ,
2012-09-25 08:40:05 +00:00
' amount ' : fields . float ( ' Total price ' ) ,
2012-09-26 14:13:52 +00:00
' purchaser_id ' : fields . many2one ( ' res.partner ' , ' Purchaser ' ) ,
2012-09-26 14:56:03 +00:00
' inv_ref ' : fields . char ( ' Invoice Reference ' , size = 64 ) ,
2012-09-26 07:49:15 +00:00
' vendor_id ' : fields . many2one ( ' res.partner ' , ' Vendor ' , domain = " [( ' supplier ' , ' = ' ,True)] " ) ,
2012-09-27 14:53:35 +00:00
' notes ' : fields . text ( ' Notes ' ) ,
2012-09-26 14:56:03 +00:00
}
2012-09-27 14:34:30 +00:00
_defaults = {
' purchaser_id ' : lambda self , cr , uid , ctx : uid ,
}
2012-09-26 14:56:03 +00:00
class fleet_vehicle_log_services ( osv . Model ) :
_inherits = { ' fleet.vehicle.odometer ' : ' odometer_id ' }
_name = ' fleet.vehicle.log.services '
_columns = {
2012-09-27 15:05:24 +00:00
#'name' : fields.char('Name',size=64),
2012-09-26 14:56:03 +00:00
' amount ' : fields . float ( ' Cost ' , help = " Total cost of the service " ) ,
' service_ids ' : fields . many2many ( ' fleet.service.type ' , ' vehicle_service_type_rel ' , ' vehicle_service_type_id ' , ' service_id ' , ' Services completed ' ) ,
' purchaser_id ' : fields . many2one ( ' res.partner ' , ' Purchaser ' ) ,
' inv_ref ' : fields . char ( ' Invoice Reference ' , size = 64 ) ,
' vendor_id ' : fields . many2one ( ' res.partner ' , ' Vendor ' , domain = " [( ' supplier ' , ' = ' ,True)] " ) ,
2012-09-27 14:53:35 +00:00
' notes ' : fields . text ( ' Notes ' ) ,
2012-09-21 15:10:22 +00:00
}
2012-09-27 14:34:30 +00:00
_defaults = {
' purchaser_id ' : lambda self , cr , uid , ctx : uid ,
}
2012-09-21 15:10:22 +00:00
2012-09-24 15:38:26 +00:00
class fleet_insurance_type ( osv . Model ) :
_name = ' fleet.insurance.type '
_columns = {
' name ' : fields . char ( ' Name ' , required = True , translate = True ) ,
}
2012-09-21 15:10:22 +00:00
class fleet_vehicle_log_insurance ( osv . Model ) :
2012-09-26 15:11:50 +00:00
_inherits = { ' fleet.vehicle.odometer ' : ' odometer_id ' }
2012-09-21 15:10:22 +00:00
_name = ' fleet.vehicle.log.insurance '
_columns = {
2012-09-26 15:11:50 +00:00
2012-09-27 15:05:24 +00:00
#'name' : fields.char('Name',size=64),
2012-09-26 15:11:50 +00:00
2012-09-24 15:38:26 +00:00
' insurance_type ' : fields . many2one ( ' fleet.insurance.type ' , ' Type ' , required = False , help = ' Type of the insurance ' ) ,
' start_date ' : fields . date ( ' Start date ' , required = False , help = ' Date when the coverage of the insurance begins ' ) ,
' expiration_date ' : fields . date ( ' Expiration date ' , required = False , help = ' Date when the coverage of the insurance expirates ' ) ,
' price ' : fields . float ( ' Price ' , help = " Cost of the insurance for the specified period " ) ,
' insurer_id ' : fields . many2one ( ' res.partner ' , ' Insurer ' , domain = " [( ' supplier ' , ' = ' ,True)] " ) ,
2012-09-27 14:34:30 +00:00
' purchaser_id ' : fields . many2one ( ' res.partner ' , ' Purchaser ' ) ,
2012-09-26 15:11:50 +00:00
' ins_ref ' : fields . char ( ' Insurance Reference ' , size = 64 ) ,
2012-09-27 14:53:35 +00:00
' notes ' : fields . text ( ' Notes ' ) ,
2012-09-21 15:10:22 +00:00
}
2012-09-27 14:34:30 +00:00
_defaults = {
' purchaser_id ' : lambda self , cr , uid , ctx : uid ,
}
2012-09-21 15:10:22 +00:00
2012-09-25 10:40:57 +00:00
class fleet_service_type ( osv . Model ) :
2012-09-24 12:51:11 +00:00
_name = ' fleet.service.type '
_columns = {
' name ' : fields . char ( ' Name ' , required = True , translate = True ) ,
}
2012-09-27 13:28:44 +00:00
#class hr_employee(osv.Model):
# _inherit = 'hr.employee'
2012-09-19 16:13:27 +00:00
2012-09-27 13:28:44 +00:00
# _columns = {
# 'vehicle_id' : fields.one2many('fleet.vehicle','driver', 'Vehicle',type="char"),
# 'log_ids' : fields.one2many('fleet.vehicle.log', 'employee_id', 'Logs'),
# }
2012-09-24 14:49:27 +00:00