clinic/netforce_clinic/models/dialyzer.py

208 lines
7.3 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={
"number": fields.Char("Number",required=True,search=True),
2014-10-02 10:10:57 +00:00
"description": fields.Text("Description",search=True),
2014-08-21 10:03:45 +00:00
"date": fields.Date("Create Date",search=True),
"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),
"member_type": fields.Selection([("unsub","Unsub cellul"),("sub","Sub cellul"),("synthetic","Synthetic")],"Membrane Type"),
"dialyzer_type": fields.Selection([("low","low flux"),("high","high flux"),("dbl","dbl hifulx")],"Dialyzer Type"),
"bid_flow_rate": fields.Integer("Bid Flow Rate (ml/min)",search=True),
"ultrafittration": fields.Float("Ultrafittration Kg.",search=True),
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"),
2014-10-02 10:10:57 +00:00
'product_id': fields.Many2One("product", "Product",required=True),
2014-10-02 07:36:13 +00:00
"pickings": fields.One2Many("stock.picking","related_id","Pickings"),
2014-10-04 18:33:18 +00:00
"patient_id": fields.Many2One("clinic.patient","Patient"),
"visit_id": fields.Many2One("clinic.visit","Visit"),
"hd_case_id": fields.Many2One("clinic.hd.case","HD Case"),
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
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
stop=False
for prod in get_model("product").search_browse([]):
if stop:
break
for categ in prod.categs:
if categ.id in categ_ids:
product_id=prod.id
stop=True
break
return product_id
2014-08-21 10:03:45 +00:00
_defaults={
2014-10-02 10:10:57 +00:00
"state": "new",
2014-08-21 10:03:45 +00:00
"date": lambda *a: time.strftime("%Y-%m-%d"),
#"number": _get_number,
'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,
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"})
2014-10-02 10:10:57 +00:00
def confirm(self,ids,context={}):
id=ids[0]
obj=self.browse(id)
2014-10-02 15:26:14 +00:00
patient=obj.patient_id
if not patient:
raise Exception("Patient is required")
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:
raise Exception("contact %s dont'have address with type shipping"%partner.name)
2014-10-02 10:10:57 +00:00
pick_vals={
"type": "out",
"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
2014-10-02 10:10:57 +00:00
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
wh_loc_id=prod.location_id.id # product -> tab inventory -> warehouse filed
2014-10-02 10:10:57 +00:00
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]
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,
}
pick_vals["lines"].append(("create",line_vals))
if not pick_vals["lines"]:
return {
"flash": "Nothing left to deliver",
}
2014-10-02 15:26:14 +00:00
picking_obj=get_model("stock.picking")
pick_id=picking_obj.create(pick_vals,context={"pick_type": "out"})
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
},
'flash': 'Dializer %s is confirmed'%(obj.number),
}
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),
}
def renew(self,ids,context={}):
obj=self.browse(ids[0])
2014-10-03 06:09:10 +00:00
# XXX
if obj.use_time > obj.max_use_time:
raise Exception("No long to use it (use time > max use time")
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),
}
2014-08-21 10:03:45 +00:00
Dialyzer.register()