clinic/netforce_clinic/models/dialyzer.py

285 lines
9.8 KiB
Python
Raw Normal View History

2014-08-21 10:03:45 +00:00
import time
2014-10-02 10:10:57 +00:00
from netforce.model import Model, fields, get_model
from netforce.access import get_active_company, get_active_user, set_active_user
2014-08-21 10:03:45 +00:00
class Dialyzer(Model):
_name="clinic.dialyzer"
_string="Dialyzer"
_audit_log=True
_name_field="number"
_multi_company=True
2014-10-26 00:15:13 +00:00
2014-08-21 10:03:45 +00:00
_fields={
2015-01-13 08:58:47 +00:00
"name": fields.Char("Name",search=True),
2014-08-21 10:03:45 +00:00
"number": fields.Char("Number",required=True,search=True),
2014-11-29 00:48:00 +00:00
'ref': fields.Char("Ref."),
2015-01-15 06:44:58 +00:00
"note": fields.Text("Note",search=True),
2015-02-12 08:52:35 +00:00
"date": fields.DateTime("Create Date",search=True),
2014-08-21 10:03:45 +00:00
"use_time": fields.Integer("Use Time"),
"max_use_time": fields.Integer("Max Use Time"),
2014-10-02 10:10:57 +00:00
"exp_date": fields.Date("Expiry Date",search=True),
"dialyzer_type": fields.Selection([("low","low flux"),("high","high flux"),("dbl","dbl hifulx")],"Dialyzer Type"),
2014-10-03 07:27:57 +00:00
"state": fields.Selection([("new","New"),("active","Active"),("drop","Drop"),("expire","Expire"),('cancelled','Cancelled')],"Status"),
2014-08-21 10:03:45 +00:00
"comments": fields.One2Many("message","related_id","Comments"),
"company_id": fields.Many2One("company","Company"),
'product_id': fields.Many2One("product", "Product",required=True,search=True),
2014-10-02 07:36:13 +00:00
"pickings": fields.One2Many("stock.picking","related_id","Pickings"),
2015-03-02 04:03:07 +00:00
"patient_id": fields.Many2One("clinic.patient","Patient",search=True,domain=[['state','=','admit']]),
2015-01-20 13:58:38 +00:00
"visit_id": fields.Many2One("clinic.visit","Visit",search=True),
"hd_case_id": fields.Many2One("clinic.hd.case","HD Case",search=True),
2014-10-26 00:15:13 +00:00
"hd_cases": fields.One2Many("clinic.hd.case","dlz_id","HD Case"), #TODO funtion to get hd case
2015-01-19 06:42:57 +00:00
'department_id': fields.Many2One("clinic.department","Department",search=True),
2015-01-29 14:55:05 +00:00
"membrane_type": fields.Selection([("unsub","Unsub cellul"),("sub","Sub cellul"),("synthetic","Synthetic")],"Membrane Type"),
2014-08-21 10:03:45 +00:00
}
def _get_number(self,context={}):
while 1:
seq_name='Clinic Dialyzer'
seq_id=get_model("sequence").find_sequence(name=seq_name)
if not seq_id:
raise Exception("Can not found sequence %s"%seq_name)
2014-10-02 10:10:57 +00:00
num=get_model("sequence").get_next_number(seq_id,context=context)
2014-08-21 10:03:45 +00:00
if not num:
return None
2014-10-02 10:10:57 +00:00
user_id=get_active_user()
set_active_user(1)
2014-08-21 10:03:45 +00:00
res=self.search([["number","=",num]])
2014-10-02 10:10:57 +00:00
set_active_user(user_id)
2014-08-21 10:03:45 +00:00
if not res:
return num
2014-10-02 10:10:57 +00:00
get_model("sequence").increment_number(seq_id,context=context)
2014-10-02 15:26:14 +00:00
def _get_product(self,context={}):
categ_ids=get_model("product.categ").search([['name','=','Dialyzer']])
product_id=None
2014-10-26 02:23:27 +00:00
found=False
2014-10-02 15:26:14 +00:00
for prod in get_model("product").search_browse([]):
2014-10-26 02:23:27 +00:00
if found:
2014-10-02 15:26:14 +00:00
break
2014-11-26 11:22:17 +00:00
if prod.categ_id.id in categ_ids:
product_id=prod.id
found=True
break
if not product_id:
raise Exception("No Product with category 'Dialyzer'")
2014-10-02 15:26:14 +00:00
return product_id
2014-08-21 10:03:45 +00:00
2015-01-19 06:42:57 +00:00
def _get_product_name(self,context={}):
categ_ids=get_model("product.categ").search([['name','=','Dialyzer']])
product_name=''
found=False
for prod in get_model("product").search_browse([]):
if found:
break
if prod.categ_id.id in categ_ids:
product_name=prod.name
found=True
break
return product_name
2014-08-21 10:03:45 +00:00
_defaults={
2014-10-02 10:10:57 +00:00
"state": "new",
2015-02-12 08:52:35 +00:00
"date": lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
'number': '/',
2014-08-21 10:03:45 +00:00
"max_use_time": 10,
"use_time": 0,
"company_id": lambda *a: get_active_company(),
2014-10-02 15:26:14 +00:00
'product_id': _get_product,
2015-01-19 06:42:57 +00:00
'name': _get_product_name,
2014-10-26 01:34:10 +00:00
'dialyzer_type': 'low',
2014-08-21 10:03:45 +00:00
}
_order="date desc,number desc"
def void(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({"state":"voided"})
2015-02-10 06:41:47 +00:00
def validate(self,ids,context={}):
2014-10-02 10:10:57 +00:00
id=ids[0]
obj=self.browse(id)
2014-10-02 15:26:14 +00:00
patient=obj.patient_id
2014-12-19 18:19:19 +00:00
#if not patient:
#raise Exception("Patient is required")
2014-10-02 15:26:14 +00:00
partner=patient.partner_id
if not partner:
raise Exception("Contact not for this patient")
ship_address_id=None
for address in partner.addresses:
if address.type=="shipping":
ship_address_id=address.id
break
if not ship_address_id:
2014-12-19 18:43:56 +00:00
patient.simple_address()
2015-01-13 08:58:47 +00:00
#XXX
2014-12-14 11:15:14 +00:00
st=get_model("clinic.setting").browse(1)
2015-01-13 08:58:47 +00:00
stock_journal=st.stock_journal_id
wh_loc_id=None
cust_loc_id=None
department=obj.department_id
if department:
stock_journal=department.pick_out_journal_id
if stock_journal:
wh_loc_id=stock_journal.location_from_id.id
cust_loc_id=stock_journal.location_to_id.id
print("get location from stock journal %s "%(stock_journal.name))
if not stock_journal:
2014-12-14 11:15:14 +00:00
raise Exception("Not found stock journal")
2014-10-02 10:10:57 +00:00
pick_vals={
"type": "out",
2015-01-13 08:58:47 +00:00
'journal_id': stock_journal.id,
2015-02-12 08:52:35 +00:00
'date': obj.date,
2014-10-02 10:10:57 +00:00
"ref": obj.number,
"related_id": "clinic.dialyzer,%s"%obj.id,
"partner_id": obj.patient_id.partner_id.id,
"ship_address_id": ship_address_id,
"lines": [],
"state": "draft",
}
2014-10-02 15:26:14 +00:00
2015-01-13 08:58:47 +00:00
if not cust_loc_id:
res=get_model("stock.location").search([["type","=","customer"]])
if not res:
raise Exception("Customer location not found")
cust_loc_id=res[0]
2014-10-02 15:26:14 +00:00
prod=obj.product_id
2014-10-02 10:10:57 +00:00
if not wh_loc_id:
2015-01-13 08:58:47 +00:00
wh_loc_id=prod.location_id.id # product -> tab inventory -> warehouse filed
if not wh_loc_id:
res=get_model("stock.location").search([["type","=","internal"]])
if not res:
raise Exception("Warehouse not found")
wh_loc_id=res[0]
if prod.type=='stock':
line_vals={
"product_id": prod.id,
"qty": 1,
"uom_id": prod.uom_id.id,
"location_from_id": wh_loc_id,
"location_to_id": cust_loc_id,
2014-10-02 10:10:57 +00:00
}
pick_vals["lines"].append(("create",line_vals))
picking_obj=get_model("stock.picking")
2015-03-17 14:37:18 +00:00
context={
'pick_type': 'out',
'journal_id': pick_vals['journal_id'],
}
pick_id=picking_obj.create(pick_vals,context=context)
pick=picking_obj.browse(pick_id)
pick.set_done([pick_id])
number=obj.number.replace("/","")
if not number:
number=self._get_number(context)
obj.write({
"number": number,
"state": "active",
})
2014-10-02 15:26:14 +00:00
return {
'next':{
'name': 'clinic_dialyzer',
'mode': 'form',
'active_id': obj.id
},
2015-01-19 06:42:57 +00:00
'flash': 'Dialyzer %s is confirmed'%(number),
2014-10-02 15:26:14 +00:00
}
2014-10-02 10:10:57 +00:00
print("Done!")
2014-10-02 15:26:14 +00:00
def delete(self,ids,context={}):
for obj in self.browse(ids):
if obj.state != 'new':
raise Exception("Can not delete Dializer %s because state is not 'new'"%obj.number)
2014-10-15 07:52:15 +00:00
super().delete(ids)
2014-10-02 15:26:14 +00:00
def cancel(self,ids,context={}):
obj=self.browse(ids[0])
for pick in obj.pickings:
pick.to_draft(context)
pick.delete()
obj.write({
'state': 'cancelled',
})
return {
'next':{
'name': 'clinic_dialyzer',
'mode': 'form',
'active_id': obj.id
},
'flash': 'Dializer %s is cancelled'%(obj.number),
}
2015-02-10 06:41:47 +00:00
def to_draft(self,ids,context={}):
2014-10-02 15:26:14 +00:00
obj=self.browse(ids[0])
2015-02-10 06:41:47 +00:00
#if obj.use_time > obj.max_use_time:
#raise Exception("Dialyzer %s is expired. (use time > max use time)"%obj.number or '')
2014-10-03 06:09:10 +00:00
obj.cancel()
2014-10-02 15:26:14 +00:00
obj.write({"state": "new"})
def drop(self,ids,context={}):
obj=self.browse(ids[0])
obj.write({"state": "drop"})
def copy(self,ids,context={}):
obj=self.browse(ids[0])
vals={
'patient_id': obj.patient_id.id,
'pickings':[],
}
new_id=self.create(vals,context=context)
new_obj=self.browse(new_id)
return {
'next':{
'name': 'clinic_dialyzer',
'mode': 'form',
'active_id': new_id,
},
'flash': 'Dializer %s is copy to %s'%(obj.number,new_obj.number),
}
2015-01-13 08:58:47 +00:00
def onchange_patient(self,context={}):
data=context['data']
patient_id=data['patient_id']
patient=get_model("clinic.patient").browse(patient_id)
department=patient.department_id
if department:
data['department_id']=department.id
return data
2015-01-15 06:44:58 +00:00
def onchange_product(self,context={}):
data=context['data']
prod_id=data['product_id']
prod=get_model("product").browse(prod_id)
if prod:
data['name']=prod.name
return data
2015-01-13 08:58:47 +00:00
def name_get(self,ids,context={}):
vals=[]
for obj in self.browse(ids):
name=obj.number or ''
if obj.name:
name+=" (%s)"%obj.name
vals.append((obj.id,name))
return vals
def name_search(self,name,domain=None,context={},**kw):
dom=[["name","ilike","%"+name+"%"]]
if domain:
dom=[dom,domain]
ids1=self.search(dom)
dom=[["number","ilike","%"+name+"%"]]
if domain:
dom=[dom,domain]
ids2=self.search(dom)
ids=list(set(ids1+ids2))
return self.name_get(ids,context=context)
2014-08-21 10:03:45 +00:00
Dialyzer.register()