[ADD]wizard for renewing contracts

bzr revid: csn@openerp.com-20121022152736-fiqiyp2sh1pr3lbw
This commit is contained in:
Cedric Snauwaert
2012-10-22 17:27:36 +02:00
parent 183646f3df
commit a1da4c7fad
3 changed files with 211 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
import renew_contract
+140
View File
@@ -0,0 +1,140 @@
from osv import osv, fields
import datetime
class renew_contract(osv.TransientModel):
_name = "fleet.vehicle.contract.renew"
_description = "wizard to renew a contract"
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.cost_type.name:
name = name+ ' / '+ str(record.cost_type.name)
if record.date:
name = name+ ' / '+ record.date
res.append((record.id, name))
return res
def _vehicle_contract_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
res = self.name_get(cr, uid, ids, context=context)
return dict(res)
def _get_odometer(self, cr, uid, ids, odometer_id, arg, context):
res = dict.fromkeys(ids, False)
for record in self.browse(cr,uid,ids,context=context):
if record.odometer_id:
res[record.id] = record.odometer_id.value
return res
def _set_odometer(self, cr, uid, id, name, value, args=None, context=None):
if value:
try:
value = float(value)
except ValueError:
#_logger.exception(value+' is not a correct odometer value. Please, fill a float for this field')
raise except_orm(_('Error!'), value+' is not a correct odometer value. Please, fill a float for this field')
date = self.browse(cr, uid, id, context=context).date
if not(date):
date = time.strftime('%Y-%m-%d')
vehicle_id = self.browse(cr, uid, id, context=context).vehicle_id
data = {'value' : value,'date' : date,'vehicle_id' : vehicle_id.id}
odometer_id = self.pool.get('fleet.vehicle.odometer').create(cr, uid, data, context=context)
self.write(cr, uid, id, {'odometer_id': odometer_id})
return value
self.write(cr, uid, id, {'odometer_id': ''})
return False
def on_change_vehicle(self, cr, uid, ids, vehicle_id, context=None):
if not vehicle_id:
return {}
odometer_unit = self.pool.get('fleet.vehicle').browse(cr, uid, vehicle_id, context=context).odometer_unit
return {
'value' : {
'odometer_unit' : odometer_unit,
}
}
def compute_next_year_date(self, strdate):
oneyear=datetime.timedelta(days=365)
curdate = self.str_to_date(strdate)
nextyear=curdate+oneyear#int(strdate[:4])+1
return str(nextyear)#+strdate[4:]
#def on_change_start_date(self, cr, uid, ids, strdate, context=None):
# if (strdate):
# return {'value' : {'expiration_date' : self.compute_next_year_date(strdate),}}
# else:
# return {}
def str_to_date(self,strdate):
return datetime.datetime(int(strdate[:4]),int(strdate[5:7]),int(strdate[8:]))
def get_warning_date(self,cr,uid,ids,prop,unknow_none,context=None):
if context is None:
context={}
if not ids:
return dict([])
reads = self.browse(cr,uid,ids,context=context)
res=[]
for record in reads:
#if (record.reminder==True):
if (record.expiration_date and record.state=='open'):
today=self.str_to_date(time.strftime('%Y-%m-%d'))
renew_date = self.str_to_date(record.expiration_date)
diff_time=int((renew_date-today).days)
if (diff_time<=0):
res.append((record.id,0))
else:
res.append((record.id,diff_time))
else:
res.append((record.id,-1))
#else:
# res.append((record.id,-1))
return dict(res)
_columns = {
'name' : fields.function(_vehicle_contract_name_get_fnc, type="text", string='Name', store=True),
'vehicle_id': fields.many2one('fleet.vehicle', 'Vehicle', required=True, help='Vehicle concerned by this fuel log'),
'cost_type': fields.many2one('fleet.service.type', 'Service type', required=False, help='Service type purchased with this cost'),
'amount': fields.float('Total Price'),
'parent_id': fields.many2one('fleet.vehicle.cost', 'Parent', required=False, help='Parent cost to this current cost'),
'cost_ids' : fields.one2many('fleet.vehicle.cost', 'parent_id', 'Included Services'),
'date' :fields.date('Date',help='Date when the cost has been executed'),
'start_date' : fields.date('Contract Start Date', required=False, help='Date when the coverage of the contract begins'),
'expiration_date' : fields.date('Contract Expiration Date', required=False, help='Date when the coverage of the contract expirates (by default, one year after begin date)'),
'warning_date' : fields.function(get_warning_date,type='integer',string='Warning Date',store=False),
'insurer_id' :fields.many2one('res.partner', 'Supplier', domain="[('supplier','=',True)]"),
'purchaser_id' : fields.many2one('res.partner', 'Contractor',domain="['|',('customer','=',True),('employee','=',True)]",help='Person to which the contract is signed for'),
'ins_ref' : fields.char('Contract Reference', size=64),
'state' : fields.selection([('open', 'In Progress'), ('closed', 'Terminated')], 'Status', readonly=True, help='Choose wheter the contract is still valid or not'),
#'reminder' : fields.boolean('Renewal Reminder', help="Warn the user a few days before the expiration date of this contract"),
'notes' : fields.text('Terms and Conditions', help='Write here all supplementary informations relative to this contract'),
'odometer_id' : fields.many2one('fleet.vehicle.odometer', 'Odometer', required=False, help='Odometer measure of the vehicle at the moment of this log'),
'odometer' : fields.function(_get_odometer,fnct_inv=_set_odometer,type='char',string='Odometer Value',store=False,help='Odometer measure of the vehicle at the moment of this log'),
'odometer_unit': fields.related('vehicle_id','odometer_unit',type="char",string="Unit",store=False, readonly=True),
#'cost_amount': fields.related('cost_id','amount',type="float",string="Amount",store=True, readonly=True),
}
def renew(self, cr, uid, ids, context=None):
print '-------------------------------'
print 'renew contract'
print '-------------------------------'
return {}
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record model='ir.ui.view' id='fleet_vehicle_contract_wizard_form'>
<field name="name">fleet.vehicle.contract.wizard.form</field>
<field name="model">fleet.vehicle.contract.renew</field>
<field name="arch" type="xml">
<form string="Contract logs" version="7.0">
<sheet>
<header>
<button string="Cancel" icon="gtk-cancel" special="cancel"/>
<button string="Validate" icon="gtk-ok"
type="object" name="renew"/>
</header>
<group col="2" string="Please check that all informations are correct">
<group string="Contract details">
<field name="vehicle_id" on_change="on_change_vehicle(vehicle_id)"/>
<field name="cost_type" string="Contract Type" required="1" domain="['|',('category','=','contract'),('category','=','both')]"/>
<field name="amount" string="Invoice Amount"/>
</group>
<group string="Odometer details">
<label for="odometer"/>
<div>
<field name="odometer" class="oe_inline"/>
<field name="odometer_unit" class="oe_inline"/>
</div>
</group>
</group>
<group col="2">
<group>
<field name="date" string="Invoice Date"/>
<field name="start_date"/>
<field name="expiration_date" />
</group>
<group>
<field name="insurer_id" />
<field name="purchaser_id" />
<field name="ins_ref" />
</group>
</group>
<group string="Included services">
<field name="cost_ids" context="{'vehicle_id': vehicle_id}" nolabel="1">
<tree version="7.0" editable="bottom">
<field name="cost_type" domain="[('category','=','service')]"/>
<field name="amount" sum="Price" string="Indicative Cost" />
</tree>
</field>
</group>
<group string="Terms and Conditions">
<field name="notes" nolabel="1" placeholder="Write here all other information relative to this contract" />
</group>
</sheet>
</form>
</field>
</record>
<act_window
id="act_renew_contract_wizard"
name="Renew Contract"
res_model="fleet.vehicle.contract.renew"
src_model="fleet.vehicle.log.contract"
view_mode="form"
view_type="form"
target="new"
/>
</data>
</openerp>