clinic/netforce_clinic/models/period_line.py

82 lines
2.2 KiB
Python
Raw Permalink Normal View History

2014-12-15 17:46:43 +00:00
from datetime import datetime
2014-11-27 15:14:31 +00:00
from netforce.model import Model, fields
class PeriodLine(Model):
_name="clinic.period.line"
_string="Period Line"
2014-12-16 09:53:36 +00:00
2014-12-15 17:46:43 +00:00
def _get_total(self,ids,context={}):
res={}
fmt='%Y-%m-%d'
for obj in self.browse(ids):
start=datetime.strptime(obj.date_start,fmt)
stop=datetime.strptime(obj.date_stop,fmt)
days=(stop-start).days
res[obj.id]=days
return res
2014-12-16 09:53:36 +00:00
def _get_name(self,ids,context={}):
res={}
for obj in self.browse(ids):
2015-06-19 14:31:45 +00:00
res[obj.id]={
'name': '%s - %s'%(obj.date_start, obj.date_stop),
}
2014-12-16 09:53:36 +00:00
return res
2014-11-27 15:14:31 +00:00
_fields={
2015-06-19 14:31:45 +00:00
'name': fields.Char("Name",function="_get_name", function_multi=True, store=True),
2014-11-27 15:14:31 +00:00
"period_id": fields.Many2One("clinic.period", "Period"),
2014-12-15 17:46:43 +00:00
'date_start': fields.Date("Date Start"),
2015-06-19 14:31:45 +00:00
'date_stop': fields.Date("Date End"),
2014-12-16 09:53:36 +00:00
'day_total': fields.Integer("Duration (Day)",function="_get_total"),
2015-06-19 14:31:45 +00:00
'state': fields.Selection([['open','Open'],['close','Close']],"State"),
2014-11-27 15:14:31 +00:00
}
_defaults={
2015-06-19 14:31:45 +00:00
'state': 'open',
2014-11-27 15:14:31 +00:00
}
2014-12-16 09:53:36 +00:00
2015-06-20 10:01:37 +00:00
def delete(self,ids,context={}):
for obj in self.browse(ids):
if obj.state=='close':
raise Exception("Can delete object which state is closed!")
super().delete(ids,context)
2014-12-16 09:53:36 +00:00
def create(self,vals,**kw):
id=super().create(vals,**kw)
self.function_store([id])
return id
def write(self,ids,vals,**kw):
super().write(ids,vals,**kw)
self.function_store(ids)
2015-06-19 14:31:45 +00:00
def do_close(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'close',
})
return {
'next': {
'name': 'clinic_period_line',
'mode': 'form',
'active_id': obj.id,
},
}
def do_open(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'open',
})
return {
'next': {
'name': 'clinic_period_line',
'mode': 'form',
'active_id': obj.id,
},
}
2014-11-27 15:14:31 +00:00
PeriodLine.register()