clinic/netforce_clinic/models/sickbed.py

88 lines
3.0 KiB
Python

from netforce.model import Model, fields, get_model
from netforce.access import get_active_company, get_active_user
class SickBed(Model):
_name="clinic.sickbed"
_string="Sickbed"
_key=['name','department_id','branch_id']
def _get_all(self,ids,context={}):
res={}
for obj in self.browse(ids):
hd_case=None
patient_id=None
image=None
date=''
for hd_case in sorted(obj.hd_cases, key=lambda a: a.id):
patient=hd_case.patient_id
patient_id=patient.id
image=patient.image
date=hd_case.date
res[obj.id]={
'patient_id': patient_id,
'image': image,
'date': date,
}
return res
_fields={
"name": fields.Char("Name",required=True,search=True),
"available": fields.Boolean("Available"),
'hd_cases': fields.One2Many("clinic.hd.case",'sickbed_id','HDCases'),
'company_id': fields.Many2One("company","Company"),
'patient_id': fields.Many2One("clinic.patient","Lasted Patient",function="_get_all",function_multi=True,domain=[['state','=','admit']]),
'image': fields.File("Image",function="_get_all",function_multi=True),
'date': fields.Date("Lasted Date",function="_get_all",function_multi=True),
"state": fields.Selection([("available","Available"),("not_available","Not Available")],"Status"),
'sequence': fields.Integer("Sequence"),
'note': fields.Text("Note"),
'branch_id': fields.Many2One("clinic.branch","Branch",required=True, search=True),
'department_id': fields.Many2One("clinic.department","Department",required=True, search=True),
'active': fields.Boolean("Active"),
}
def _get_branch(self,context={}):
user_id=get_active_user()
staffs=get_model("clinic.staff").search_browse([['user_id','=',user_id]])
branch_id=None
if staffs:
staff=staffs[0]
if staff.branch_id:
branch_id=staff.branch_id.id
return branch_id
_defaults={
'available': True,
"company_id": lambda *a: get_active_company(),
'sequence': 0,
'state': 'available',
'branch_id': _get_branch,
'active': True,
}
_order="branch_id,department_id,sequence,name"
def copy(self,ids,context={}):
obj=self.browse(ids)[0]
new_id=get_model("clinic.sickbed").create({
'name': '%s(copy)' % obj.name,
})
return {
'next': {
'name': 'clinic_sickbed',
'mode': 'form',
'active_id': new_id,
},
'flash': 'Copy succesfully',
}
def write(self,ids,vals,**kw):
if 'available' in vals.keys():
if vals['available']:
vals['state']='available'
else:
vals['state']='not_available'
super().write(ids,vals,**kw)
SickBed.register()