clinic/netforce_clinic/models/cycle_item.py

90 lines
2.9 KiB
Python
Raw Normal View History

2014-10-26 08:48:51 +00:00
import time
2014-11-02 07:13:20 +00:00
from netforce.model import Model, fields, get_model
2014-10-26 08:48:51 +00:00
from netforce.access import get_active_company
class CycleItem(Model):
_name="clinic.cycle.item"
_string="Cycle Item"
2014-10-26 10:11:19 +00:00
_name_field="name"
2014-10-26 08:48:51 +00:00
def _get_all(self,ids,context):
res={}
for obj in self.browse(ids):
2014-10-26 10:11:19 +00:00
name="%s-%s"%(obj.cycle_id.name,obj.date)
2014-10-26 08:48:51 +00:00
res[obj.id]={
2014-10-26 10:11:19 +00:00
'name': name,
2014-10-26 08:48:51 +00:00
}
return res
_fields={
2014-11-17 00:59:19 +00:00
'name': fields.Char("Name", function="_get_all",function_multi=True),
2014-10-26 08:48:51 +00:00
'date': fields.Date("Date",search=True),
2014-11-17 00:59:19 +00:00
'cycle_id': fields.Many2One("clinic.cycle", "Cycle",search=True),
'cycle_dialy_id': fields.Many2One("clinic.cycle.dialy", "Cycle Dialy",search=True),
2014-11-01 08:49:27 +00:00
'visits': fields.One2Many("clinic.visit","cycle_item_id", "Visits"),
2014-11-17 00:59:19 +00:00
'hd_cases': fields.One2Many("clinic.hd.case","cycle_item_id", "HD Cases"),
2014-11-01 18:17:40 +00:00
'nurses': fields.One2Many("clinic.cycle.item.nurse",'cycle_item_id','Nurses'),
2014-11-17 00:59:19 +00:00
'sequence': fields.Char("Sequence"), # for sort item
'company_id': fields.Many2One("company", "Company"),
"state": fields.Selection([("draft","Draft"),("done","Done")],"Status",required=True),
2014-10-26 08:48:51 +00:00
}
_defaults={
'state': 'draft',
'company_id': lambda *a: get_active_company(),
'date': lambda *a: time.strftime("%Y-%m-%d"),
}
2014-11-02 07:13:20 +00:00
_order="sequence"
2014-11-11 00:51:44 +00:00
_sql_constraints=[
("cycle_item_uniq","unique (cycle_id,date,company_id)","Cycle item should be unique"),
]
2014-11-17 00:59:19 +00:00
def get_cycle_diary(self,date):
dom=[]
print("get ", date)
dom.append(['date','=',date])
cd_ids=get_model('clinic.cycle.dialy').search(dom)
cd_id=None
if cd_ids:
cd_id=cd_ids[0]
else:
cd_id=get_model('clinic.cycle.dialy').create({
'date':date,
'name':date,
})
return cd_id
2014-11-02 07:13:20 +00:00
def create(self, vals,**kw):
date=vals['date']
cycle_id=vals['cycle_id']
2014-11-17 00:59:19 +00:00
vals['cycle_dialy_id']=self.get_cycle_diary(date)
2014-11-02 07:13:20 +00:00
cycle=get_model("clinic.cycle").browse(cycle_id)
vals['sequence']='%s-%s'%(date,cycle.sequence) #date-sequence
obj_id=super().create(vals,**kw)
return obj_id
def write(self,ids,vals,**kw):
obj=self.browse(ids)[0]
cycle=obj.cycle_id
2014-11-17 00:59:19 +00:00
date=vals.get('date') and vals.get('date') or obj.date
vals['cycle_dialy_id']=self.get_cycle_diary(date)
2014-11-02 07:13:20 +00:00
vals['sequence']='%s-%s'%(obj.date,cycle.sequence) #date-sequence
super().write(ids,vals,**kw)
2014-11-03 00:21:37 +00:00
def validate(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'done',
})
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'draft',
})
2014-11-01 17:30:48 +00:00
2014-10-26 08:48:51 +00:00
CycleItem.register()