2014-08-19 11:36:46 +00:00
|
|
|
import time
|
2014-10-01 10:52:21 +00:00
|
|
|
|
|
|
|
from netforce.model import Model, fields, get_model
|
2014-10-02 01:12:46 +00:00
|
|
|
from netforce.access import get_active_company, get_active_user, set_active_user
|
2014-08-19 11:36:46 +00:00
|
|
|
|
2015-01-13 07:56:30 +00:00
|
|
|
from . import utils
|
|
|
|
|
2014-10-02 11:50:11 +00:00
|
|
|
|
2014-08-19 11:36:46 +00:00
|
|
|
class Patient(Model):
|
|
|
|
_name="clinic.patient"
|
2014-09-11 03:21:52 +00:00
|
|
|
_string="Patient"
|
2014-08-19 11:36:46 +00:00
|
|
|
_audit_log=True
|
|
|
|
_multi_company=True
|
2015-01-13 07:56:30 +00:00
|
|
|
_key=['number']
|
2014-09-30 11:17:46 +00:00
|
|
|
|
|
|
|
def _get_age(self,ids,context):
|
|
|
|
res={}
|
|
|
|
year_now=int(time.strftime("%Y"))
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
age=0
|
|
|
|
if obj.birthday:
|
|
|
|
year_bd=int(obj.birthday[0:4])
|
|
|
|
age=year_now-year_bd
|
|
|
|
res[obj.id]=age
|
|
|
|
return res # -> {1: 30, 2: 45,.....}
|
2014-11-30 13:05:14 +00:00
|
|
|
|
|
|
|
def _get_last_cycle(self,ids,context={}):
|
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
cycle_id=None
|
|
|
|
for vs in obj.visits:
|
|
|
|
cycle=vs.cycle_id
|
|
|
|
cycle_id=cycle.id
|
|
|
|
res[obj.id]=cycle_id
|
|
|
|
return res
|
2014-12-02 08:34:28 +00:00
|
|
|
|
2015-01-09 05:19:52 +00:00
|
|
|
def _get_hn_no(self,ids,context={}):
|
2014-12-02 11:41:05 +00:00
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
2015-01-13 07:56:30 +00:00
|
|
|
hn=''.join(x for x in (obj.number or "") if x.isdigit())
|
2014-12-02 11:41:05 +00:00
|
|
|
res[obj.id]=hn
|
|
|
|
return res
|
|
|
|
|
2014-08-19 11:36:46 +00:00
|
|
|
_fields={
|
2014-12-02 07:08:20 +00:00
|
|
|
'type_id': fields.Many2One("clinic.patient.type","Type",search=True,required=True),
|
2015-01-13 07:56:30 +00:00
|
|
|
"number": fields.Char("HN",required=True,search=True),
|
2015-01-09 05:19:52 +00:00
|
|
|
"trt_no": fields.Char("TRT. No",search=True),
|
2015-01-13 07:56:30 +00:00
|
|
|
"hn_no": fields.Char("HN Number",function="_get_hn_no"),
|
2015-01-13 09:25:51 +00:00
|
|
|
"hn": fields.Char("REF/HN",search=False),
|
2014-10-01 10:52:21 +00:00
|
|
|
"name": fields.Char("Name",required=True,search=True),
|
2014-10-01 11:13:54 +00:00
|
|
|
"reg_date": fields.Date("Register Date",required=False,search=True),
|
2014-10-01 16:14:52 +00:00
|
|
|
"birthday": fields.Date("Birthday",required=False,search=True),
|
|
|
|
"phone": fields.Char("Phone",required=False,search=True),
|
2014-09-30 11:17:46 +00:00
|
|
|
"mobile": fields.Char("Mobile",required=False,search=True),
|
2014-10-01 11:13:54 +00:00
|
|
|
"job": fields.Char("Job/Position"),
|
2014-09-30 11:17:46 +00:00
|
|
|
"age": fields.Integer("Age", function="_get_age"),
|
2015-01-09 05:19:52 +00:00
|
|
|
'image': fields.File("Image"),
|
2014-10-01 16:14:52 +00:00
|
|
|
'email': fields.Char("Email"),
|
2014-10-01 11:13:54 +00:00
|
|
|
"weight": fields.Float("Weight (cm)"),
|
|
|
|
"height": fields.Float("Height (Kg)"),
|
2014-10-02 07:36:13 +00:00
|
|
|
"card_type": fields.Selection([("identification","Identification"),("passport","Passport")],"Card Type"),
|
2014-11-26 01:39:49 +00:00
|
|
|
'card_no' : fields.Char("Card Number"),
|
|
|
|
'card_exp' : fields.Date("Card Expire"),
|
2014-08-19 11:36:46 +00:00
|
|
|
"app_no": fields.Char("Application No."),
|
2014-10-01 07:49:24 +00:00
|
|
|
"salary": fields.Selection([["20000","5,001-20,000"],["50000","20,001-50,000"],["100000","50,001-100,000"],["100001","100,000+"]], "Salary"),
|
2014-10-26 01:34:10 +00:00
|
|
|
"addresses": fields.One2Many("address","patient_id","Addresses"),
|
2014-11-28 04:54:21 +00:00
|
|
|
"gender": fields.Selection([("male","Male"),("female","Female")],"Gender",search=True),
|
2014-09-30 11:17:46 +00:00
|
|
|
"marital_status": fields.Selection([("single","Single"),("marry","Marry"),("divorce","Divorce"),("separated","Saparated"),("widowed","Widowed")],"Marital Status",required=False),
|
2014-10-01 16:14:52 +00:00
|
|
|
"nation_id": fields.Many2One("clinic.nation","Nationality"),
|
|
|
|
"race_id": fields.Many2One("clinic.race","Race"),
|
|
|
|
"grad_id": fields.Many2One("clinic.graduation","Graduation"),
|
2014-09-30 11:17:46 +00:00
|
|
|
"smoke": fields.Selection([("never","Never"),("stopped","Stopped"),("smoked","Smoked")],"Smoking"),
|
|
|
|
"withdrawal" : fields.Selection([("social_security","Social Security"),("health_insurance","Health Insurance"),("etc","ETC.")],"Right of withdrawal"),
|
|
|
|
"first_treatment" : fields.Selection([("hd","HD"),("test","Test")], "First treatment"),
|
|
|
|
"first_hemodialysis": fields.Date("First time Hemodialysis",required=False),
|
|
|
|
"hemodialysis": fields.Char("First Hemodialysis",required=False),
|
|
|
|
"clinic_after": fields.Selection([("small","Small"),("medium","Medium"),("large","Large")],"Clinic Lastime",required=False),
|
|
|
|
"clinic_after_name": fields.Char("Clinic after name",required=False),
|
|
|
|
"first_permanent_vascular_access": fields.Date("First time Permanent Vascular",required=False),
|
|
|
|
"first_tenckhoff_catheters": fields.Date("First time Tenckhoff Catheters",required=False),
|
|
|
|
"start_date_clinic": fields.Date("Start Date Clinic",required=False),
|
|
|
|
"waiting_transplantation": fields.Selection([("yes","Yes"),("no","No")],"Kidney Transplantation Waiting ?"),
|
2014-08-19 11:36:46 +00:00
|
|
|
"who_transplantation": fields.Char("Who is Transplantation?"),
|
|
|
|
"reason_of_chronic_renal_failure": fields.Char("Reason chronic renal failure ?"),
|
2014-11-05 07:22:23 +00:00
|
|
|
'causes': fields.One2Many("clinic.patient.cause","patient_id","Cause Line"),
|
|
|
|
'comorbilities': fields.One2Many("clinic.patient.comorbidity","patient_id","Comorbility"),
|
|
|
|
'morbilities': fields.One2Many("clinic.patient.morbidity","patient_id","Morbility"),
|
2014-08-19 11:36:46 +00:00
|
|
|
"comments": fields.One2Many("message","related_id","Comments"),
|
2015-01-14 08:06:19 +00:00
|
|
|
"company_id": fields.Many2One("company","Company",search=True),
|
2014-08-19 11:36:46 +00:00
|
|
|
"visits": fields.One2Many("clinic.visit","patient_id","Visits"),
|
2014-08-21 07:29:37 +00:00
|
|
|
"hd_cases": fields.One2Many("clinic.hd.case","patient_id","HD Cases"),
|
2014-10-02 07:36:13 +00:00
|
|
|
"partner_id": fields.Many2One("partner","Contact"),
|
2014-10-03 02:00:47 +00:00
|
|
|
"dialyzers": fields.One2Many("clinic.dialyzer","patient_id","Dialyzers"),
|
2014-11-10 05:10:39 +00:00
|
|
|
"active":fields.Boolean("Uncheck as discountinue", search=True),
|
2014-10-29 09:10:19 +00:00
|
|
|
'note': fields.Text("Note"),
|
2014-11-01 08:49:27 +00:00
|
|
|
'categ_id': fields.Many2One("clinic.patient.categ","Category"),
|
2014-11-25 11:39:53 +00:00
|
|
|
'doctor_id': fields.Many2One("clinic.staff","Doctor",domain=[['type','=','doctor']]),
|
2014-11-05 05:06:43 +00:00
|
|
|
"documents": fields.One2Many("document","related_id","Documents"),
|
2014-11-10 05:10:39 +00:00
|
|
|
'resign_date': fields.Date("Resign Date"),
|
2014-11-18 06:02:47 +00:00
|
|
|
'rm_remain_visit': fields.Boolean("Auto Remove Remaining Visit"),
|
2014-11-27 07:36:27 +00:00
|
|
|
'department_id': fields.Many2One("clinic.department","Department"),
|
2014-11-30 13:05:14 +00:00
|
|
|
'cycle_id': fields.Many2One("clinic.cycle","Last Cycle",function="_get_last_cycle"),
|
2015-01-14 11:00:14 +00:00
|
|
|
'branch_id': fields.Many2One("clinic.branch","Branch"),
|
2014-08-19 11:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _get_number(self,context={}):
|
2014-09-30 11:17:46 +00:00
|
|
|
while 1:
|
2014-10-02 01:12:46 +00:00
|
|
|
seq_id=get_model("sequence").find_sequence(name="Clinic Patient")
|
|
|
|
num=get_model("sequence").get_next_number(seq_id,context=context)
|
2014-08-19 11:36:46 +00:00
|
|
|
if not num:
|
|
|
|
return None
|
2014-10-02 01:12:46 +00:00
|
|
|
user_id=get_active_user()
|
|
|
|
set_active_user(1)
|
2015-01-13 07:56:30 +00:00
|
|
|
# reformat number: month-running num-year thai(2 digit)
|
|
|
|
monthnow=int(time.strftime("%m"))
|
|
|
|
yearnow=utils.date2thai(time.strftime("%Y-%m-%d"),"%(By)s")
|
|
|
|
num="%s-%s-%s"%(monthnow,num,yearnow)
|
|
|
|
|
2014-08-19 11:36:46 +00:00
|
|
|
res=self.search([["number","=",num]])
|
2014-10-02 01:12:46 +00:00
|
|
|
set_active_user(user_id)
|
2014-08-19 11:36:46 +00:00
|
|
|
if not res:
|
|
|
|
return num
|
2014-10-02 01:12:46 +00:00
|
|
|
get_model("sequence").increment_number(seq_id,context=context)
|
2014-10-05 05:47:19 +00:00
|
|
|
|
|
|
|
def _get_cause_line(self,context={}):
|
|
|
|
cause_ids=get_model("clinic.cause.chronic").search([])
|
|
|
|
return cause_ids
|
2014-12-04 08:37:54 +00:00
|
|
|
|
|
|
|
def _get_type(self,context={}):
|
|
|
|
ptype_id=None
|
|
|
|
for ptype in get_model("clinic.patient.type").search_browse([]):
|
|
|
|
if ptype.default:
|
|
|
|
ptype_id=ptype.id
|
|
|
|
break
|
|
|
|
return ptype_id
|
2014-08-19 11:36:46 +00:00
|
|
|
|
|
|
|
_defaults={
|
2014-11-26 01:39:49 +00:00
|
|
|
"number": _get_number,
|
2014-10-01 11:13:54 +00:00
|
|
|
"reg_date": lambda *a: time.strftime("%Y-%m-%d"),
|
2014-08-19 11:36:46 +00:00
|
|
|
"company_id": lambda *a: get_active_company(),
|
2014-11-26 01:39:49 +00:00
|
|
|
'card_type': 'identification',
|
2014-12-04 08:37:54 +00:00
|
|
|
'type_id': _get_type,
|
2014-10-29 04:40:35 +00:00
|
|
|
"active" : True,
|
2014-08-19 11:36:46 +00:00
|
|
|
}
|
2014-11-25 11:39:53 +00:00
|
|
|
_order="resign_date desc,number desc"
|
2014-08-19 11:36:46 +00:00
|
|
|
|
2014-10-29 04:56:52 +00:00
|
|
|
def create(self, vals,**kw):
|
2014-10-02 19:12:52 +00:00
|
|
|
obj_id=super(Patient,self).create(vals,**kw)
|
|
|
|
obj=self.browse(obj_id)
|
|
|
|
partner_id=obj.partner_id
|
|
|
|
if not partner_id:
|
|
|
|
for partner in get_model("partner").search_browse([['name', '=', obj.name]]):
|
|
|
|
if partner.name==obj.name:
|
|
|
|
partner_id=partner.id
|
|
|
|
break
|
|
|
|
if not partner_id:
|
|
|
|
partner_id=get_model("partner").create({
|
|
|
|
'name': obj.name,
|
|
|
|
'last_name': obj.name,
|
|
|
|
'type': 'person',
|
|
|
|
})
|
2014-10-24 18:04:36 +00:00
|
|
|
address_id=get_model('address').create({
|
|
|
|
'type': 'shipping',
|
|
|
|
'partner_id': partner_id,
|
2014-11-29 14:56:15 +00:00
|
|
|
'patient_id': obj.id, #XXX
|
2014-10-24 18:04:36 +00:00
|
|
|
'address': 'your address',
|
|
|
|
'address2': 'your address2',
|
|
|
|
'city': 'your city',
|
|
|
|
'postal_code': 'your zip',
|
2014-11-27 07:36:27 +00:00
|
|
|
'country_id': 1,
|
2014-10-24 18:04:36 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
get_model('address').browse(address_id).write({
|
|
|
|
'related_id': "clinic.patient,%s"%obj.id,
|
|
|
|
})
|
2014-10-02 19:12:52 +00:00
|
|
|
obj.write({
|
|
|
|
'partner_id': partner_id,
|
|
|
|
})
|
|
|
|
return obj_id
|
|
|
|
|
2014-10-02 07:36:13 +00:00
|
|
|
def delete(self,ids,context={}):
|
2014-10-02 19:12:52 +00:00
|
|
|
partner_ids=[]
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
partner_ids.append(obj.partner_id.id)
|
2014-10-24 18:04:36 +00:00
|
|
|
address_ids=[addr.id for addr in obj.partner_id.addresses]
|
2014-10-02 19:12:52 +00:00
|
|
|
get_model("partner").delete(partner_ids)
|
2014-10-24 18:04:36 +00:00
|
|
|
get_model("address").delete(address_ids)
|
2014-10-02 19:12:52 +00:00
|
|
|
super().delete(ids)
|
|
|
|
|
|
|
|
def write(self,ids,vals,**kw):
|
2014-11-10 05:10:39 +00:00
|
|
|
if 'active' in vals.keys():
|
|
|
|
if not vals['active']:
|
|
|
|
vals['resign_date']=time.strftime("%Y-%m-%d")
|
2014-10-02 19:12:52 +00:00
|
|
|
for obj in self.browse(ids):
|
2015-01-14 03:36:39 +00:00
|
|
|
if not obj.addresses:
|
|
|
|
raise Exception("Address not found - %s!"%obj.name)
|
2014-10-03 02:00:47 +00:00
|
|
|
partner_id=obj.partner_id
|
|
|
|
if not partner_id:
|
2014-10-02 19:12:52 +00:00
|
|
|
for partner in get_model("partner").search_browse([['name', '=', obj.name]]):
|
|
|
|
if partner.name==obj.name:
|
|
|
|
partner_id=partner.id
|
|
|
|
break
|
|
|
|
if not partner_id:
|
|
|
|
partner_id=get_model("partner").create({
|
|
|
|
'name': obj.name,
|
2014-10-03 02:00:47 +00:00
|
|
|
'last_name': obj.name,
|
2014-10-02 19:12:52 +00:00
|
|
|
'type': 'person',
|
|
|
|
})
|
|
|
|
vals['partner_id']=partner_id
|
2014-10-26 01:34:10 +00:00
|
|
|
|
|
|
|
if not isinstance(partner_id,int):
|
|
|
|
partner_id=partner_id.id
|
|
|
|
if obj.addresses:
|
|
|
|
addr=obj.addresses[0]
|
|
|
|
get_model("address").browse(addr.id).write({
|
|
|
|
'partner_id': partner_id,
|
2014-11-29 14:56:15 +00:00
|
|
|
'patient_id': obj.id,
|
2014-10-26 01:34:10 +00:00
|
|
|
})
|
|
|
|
else:
|
|
|
|
if vals.get("addresses"):
|
|
|
|
addr_vals=vals.get("addresses")[0][1]
|
|
|
|
addr_id=get_model("address").create(addr_vals)
|
|
|
|
get_model("address").browse(addr_id).write({
|
|
|
|
'patient_id': obj.id,
|
|
|
|
'partner_id': partner_id,
|
|
|
|
})
|
|
|
|
del vals['addresses']
|
2014-11-27 15:14:31 +00:00
|
|
|
print("create address for %s"%obj.name, ' ', addr_vals)
|
2014-11-26 01:39:49 +00:00
|
|
|
if obj.rm_remain_visit:
|
|
|
|
visit_ids=get_model('clinic.visit').search([['patient_id','=',obj.id],['state','=','draft']])
|
|
|
|
get_model('clinic.visit').delete(visit_ids)
|
|
|
|
print('remove visit auto %s'%visit_ids)
|
2014-10-02 11:50:11 +00:00
|
|
|
super().write(ids,vals,**kw)
|
2014-10-14 03:57:20 +00:00
|
|
|
|
2015-01-09 05:19:52 +00:00
|
|
|
def name_get(self,ids,context={}):
|
2014-10-24 08:08:56 +00:00
|
|
|
vals=[]
|
|
|
|
for obj in self.browse(ids):
|
2015-01-09 05:19:52 +00:00
|
|
|
name=obj.name or ''
|
|
|
|
if obj.hn_no:
|
|
|
|
name+=" (HN: %s)"%obj.hn_no
|
2015-01-09 07:48:01 +00:00
|
|
|
vals.append((obj.id,name,obj.image))
|
2014-10-24 08:08:56 +00:00
|
|
|
return vals
|
|
|
|
|
|
|
|
def name_search(self,name,domain=None,context={},**kw):
|
2015-01-09 05:19:52 +00:00
|
|
|
dom=[["name","ilike","%"+name+"%"]]
|
2014-10-24 08:08:56 +00:00
|
|
|
if domain:
|
|
|
|
dom=[dom,domain]
|
|
|
|
ids1=self.search(dom)
|
2015-01-13 07:56:30 +00:00
|
|
|
dom=[["number","ilike","%"+name+"%"]]
|
2014-10-24 08:08:56 +00:00
|
|
|
if domain:
|
|
|
|
dom=[dom,domain]
|
2015-01-09 05:19:52 +00:00
|
|
|
ids2=self.search(dom)
|
|
|
|
ids=list(set(ids1+ids2))
|
|
|
|
#dom=[["hn","ilike","%"+name+"%"]]
|
|
|
|
#if domain:
|
|
|
|
#dom=[dom,domain]
|
|
|
|
#ids3=self.search(dom)
|
|
|
|
#ids=list(set(ids1+ids2+ids3))
|
2014-10-24 08:08:56 +00:00
|
|
|
return self.name_get(ids,context=context)
|
2014-10-27 19:01:18 +00:00
|
|
|
|
|
|
|
def simple_address(self,ids,context={}):
|
|
|
|
print("call simple address ")
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
if not obj.addresses:
|
2014-10-29 04:56:52 +00:00
|
|
|
address_id=get_model("address").create({
|
2014-10-27 19:01:18 +00:00
|
|
|
'patient_id': obj.id,
|
|
|
|
'partner_id': obj.partner_id.id,
|
|
|
|
'type': 'shipping',
|
|
|
|
'address': 'xxxx',
|
|
|
|
'address2': 'xxxx',
|
|
|
|
'city': 'BKK',
|
|
|
|
'country_id': 1,
|
|
|
|
'postal_code': '12000',
|
|
|
|
})
|
|
|
|
if context.get('called'):
|
|
|
|
return obj.id
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_patient',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},'flash': 'Simple Address OK',
|
|
|
|
}
|
|
|
|
|
|
|
|
def new_dialyzer(self,ids,context={}):
|
|
|
|
dlz_id=None
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
dlz_vals=get_model("clinic.dialyzer").default_get()
|
|
|
|
dlz_vals['patient_id']=obj.id
|
|
|
|
dlz_vals['company_id']=dlz_vals['company_id'][0]
|
|
|
|
dlz_vals['product_id']=dlz_vals['product_id'][0]
|
|
|
|
dlz_id=get_model('clinic.dialyzer').create(dlz_vals)
|
|
|
|
dialyzer=get_model("clinic.dialyzer").browse(dlz_id)
|
|
|
|
dialyzer.confirm()
|
|
|
|
if context.get('called'):
|
|
|
|
return dlz_id
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_patient',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},'flash': 'New Dialyzer successfully',
|
|
|
|
}
|
2014-10-20 06:22:31 +00:00
|
|
|
|
2014-08-19 11:36:46 +00:00
|
|
|
Patient.register()
|