2015-03-24 06:51:21 +00:00
|
|
|
import time
|
|
|
|
from calendar import monthrange
|
|
|
|
|
|
|
|
from netforce.model import Model,fields,get_model
|
|
|
|
from netforce.access import get_active_company
|
|
|
|
|
|
|
|
class ReportMedicalDetail(Model):
|
|
|
|
_name="clinic.report.medical.detail"
|
|
|
|
_string="Report Medical Detail"
|
|
|
|
_transient=True
|
|
|
|
|
|
|
|
_fields={
|
|
|
|
"date": fields.Date("Month", required=True),
|
|
|
|
"date_from": fields.Date("From", required=True),
|
|
|
|
"date_to": fields.Date("To", required=True),
|
|
|
|
"prod_categ_id": fields.Many2One("product.categ","Category",required=True),
|
|
|
|
"product_id": fields.Many2One("product","Product"),
|
|
|
|
'types': fields.Many2Many("clinic.patient.type","Types"),
|
|
|
|
"branch_id": fields.Many2One("clinic.branch","Branch"),
|
|
|
|
"department_id": fields.Many2One("clinic.department","Department"),
|
2015-04-30 15:10:55 +00:00
|
|
|
'report_type': fields.Selection([['completed','Completed'],['not_completed','Not Completed']],"Report Type"),
|
2015-03-24 06:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# in case link from another report
|
|
|
|
def default_get(self,field_names=None,context={},**kw):
|
|
|
|
defaults=context.get("defaults",{})
|
|
|
|
date=defaults.get('date',time.strftime("%Y-%m-%d"))
|
|
|
|
year,month=time.strftime("%Y-%m").split("-")
|
|
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
|
|
date_from=defaults.get('date_from','%s-%s-01'%(year,month))
|
|
|
|
date_to=defaults.get('date_to',"%s-%s-%s"%(year,month,total_day))
|
|
|
|
categ_id=defaults.get('categ_id',None)
|
2016-03-01 04:08:44 +00:00
|
|
|
report_type=defaults.get('report_type') or "completed"
|
2015-03-24 06:51:21 +00:00
|
|
|
if not categ_id:
|
2015-04-30 15:10:55 +00:00
|
|
|
categ_ids=get_model("product.categ").search([['code','=','MDC']])
|
2015-03-24 06:51:21 +00:00
|
|
|
if categ_ids:
|
|
|
|
categ_id=categ_ids[0]
|
|
|
|
branch_id=defaults.get('branch_id',None)
|
|
|
|
select_dpt=get_model('select.company').get_select()
|
|
|
|
if not branch_id:
|
|
|
|
if select_dpt:
|
|
|
|
branch_id=select_dpt['branch_id']
|
|
|
|
else:
|
|
|
|
branch_id=int(branch_id or "0")
|
|
|
|
department_id=defaults.get('department_id',None)
|
|
|
|
if not department_id:
|
|
|
|
if select_dpt.get('department_ids'):
|
|
|
|
department_id=select_dpt['department_ids'][0]
|
|
|
|
else:
|
|
|
|
department_id=select_dpt['department_id']
|
|
|
|
else:
|
|
|
|
department_id=int(department_id or "0")
|
|
|
|
types=defaults.get('types',[])
|
|
|
|
if types:
|
|
|
|
types=[int(t) for t in types.split(",") if t]
|
|
|
|
product_id=defaults.get("product_id",None)
|
|
|
|
if product_id:
|
|
|
|
product_id=int(product_id)
|
|
|
|
res={
|
|
|
|
'date': date,
|
|
|
|
'date_from': date_from,
|
|
|
|
'date_to': date_to,
|
|
|
|
'prod_categ_id': categ_id,
|
|
|
|
'branch_id': branch_id,
|
|
|
|
'department_id': department_id,
|
|
|
|
'types': types,
|
|
|
|
'product_id': product_id,
|
2015-04-30 15:10:55 +00:00
|
|
|
'report_type': report_type,
|
2015-03-24 06:51:21 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
def get_report_data(self,ids,context={}):
|
|
|
|
year, month=time.strftime("%Y-%m").split("-")
|
|
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
|
|
defaults=self.default_get(context=context)
|
|
|
|
time_start=defaults.get("date_from")
|
|
|
|
time_stop=defaults.get("date_to")
|
|
|
|
branch_id=defaults.get("branch_id")
|
|
|
|
department_id=defaults.get("department_id")
|
|
|
|
product_id=defaults.get('product_id')
|
|
|
|
types=defaults.get('types')
|
2015-04-30 15:10:55 +00:00
|
|
|
report_type=defaults.get('report_type','completed')
|
2015-03-24 06:51:21 +00:00
|
|
|
if ids:
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
product_id=obj.product_id.id
|
|
|
|
types=obj.types
|
|
|
|
branch_id=obj.branch_id.id
|
|
|
|
department_id=obj.department_id.id
|
|
|
|
month=obj.date_from.split("-")[1]
|
|
|
|
time_start=obj.date_from
|
|
|
|
time_stop=obj.date_to
|
2015-04-30 15:10:55 +00:00
|
|
|
report_type=obj.report_type or 'completed'
|
2015-03-24 06:51:21 +00:00
|
|
|
dom=[
|
|
|
|
['hd_case_id.date','>=',time_start],
|
|
|
|
['hd_case_id.date','<=',time_stop],
|
|
|
|
]
|
2016-03-01 04:08:44 +00:00
|
|
|
shop_dom=[
|
|
|
|
['shop_id.date','>=',time_start],
|
|
|
|
['shop_id.date','<=',time_stop],
|
|
|
|
]
|
2015-03-24 06:51:21 +00:00
|
|
|
if branch_id:
|
|
|
|
dom.append(['hd_case_id.branch_id','=',branch_id])
|
2016-03-01 04:08:44 +00:00
|
|
|
shop_dom.append(['shop_id.branch_id','=',branch_id])
|
2015-03-24 06:51:21 +00:00
|
|
|
if department_id:
|
|
|
|
dom.append(['hd_case_id.department_id','=',department_id])
|
2016-03-01 04:08:44 +00:00
|
|
|
shop_dom.append(['shop_id.department_id','=',department_id])
|
2015-08-21 04:39:38 +00:00
|
|
|
st=get_model("clinic.setting").browse(1)
|
|
|
|
ct_ids=[]
|
|
|
|
for categ in st.product_categ_view:
|
|
|
|
ct_ids.append(categ.id)
|
|
|
|
if ct_ids:
|
|
|
|
dom.append(['product_categ_id.id','in',ct_ids])
|
2015-03-24 06:51:21 +00:00
|
|
|
if product_id:
|
|
|
|
dom.append(['product_id','=',product_id])
|
|
|
|
if types and ids:
|
|
|
|
dom.append(['hd_case_id.patient_type_id','in',[t.id for t in types]])
|
|
|
|
elif types:
|
|
|
|
dom.append(['hd_case_id.patient_type_id','in',[t_id for t_id in types]])
|
2015-04-30 15:10:55 +00:00
|
|
|
if report_type=='completed':
|
|
|
|
dom.append(["hd_case_id.state","in",["waiting_payment","paid"]])
|
|
|
|
else:
|
|
|
|
dom.append(["hd_case_id.state","not in",["waiting_payment","paid"]])
|
2015-03-24 06:51:21 +00:00
|
|
|
lines=[]
|
|
|
|
total_qty=0
|
2016-11-30 18:52:00 +00:00
|
|
|
print('DOM ', dom)
|
2015-03-24 06:51:21 +00:00
|
|
|
for line in get_model('clinic.hd.case.line').search_browse(dom):
|
|
|
|
hdcase=line.hd_case_id
|
|
|
|
patient=hdcase.patient_id
|
|
|
|
cycle=hdcase.cycle_id
|
|
|
|
department=hdcase.department_id
|
|
|
|
qty=line.qty or 0
|
|
|
|
lines.append({
|
|
|
|
'date': hdcase.date or '',
|
|
|
|
'tname': hdcase.patient_type_id.name or '',
|
|
|
|
'pname': patient.name or '',
|
|
|
|
'qty': qty,
|
|
|
|
'cname': cycle.name,
|
|
|
|
'cseq': cycle.sequence,
|
|
|
|
'dpt_name': department.name,
|
|
|
|
'hid': hdcase.id,
|
|
|
|
'hname': hdcase.number,
|
|
|
|
})
|
|
|
|
total_qty+=qty
|
2016-03-01 04:08:44 +00:00
|
|
|
|
|
|
|
for line in get_model("clinic.shop.line").search_browse(shop_dom):
|
|
|
|
shop=line.shop_id
|
|
|
|
patient=shop.patient_id
|
|
|
|
department=shop.department_id
|
|
|
|
qty=line.qty or 0
|
|
|
|
lines.append({
|
|
|
|
'date': shop.date or '',
|
|
|
|
'tname': patient.type_id.name or '',
|
|
|
|
'pname': patient.name or '',
|
|
|
|
'qty': qty,
|
|
|
|
'cname': 'RD Shop',
|
|
|
|
'cseq': 999,
|
|
|
|
'dpt_name': department.name,
|
|
|
|
'hid': shop.id,
|
|
|
|
'hname': shop.number,
|
|
|
|
'is_shop': True,
|
|
|
|
})
|
|
|
|
total_qty+=qty
|
|
|
|
|
2015-03-24 06:51:21 +00:00
|
|
|
sub_name=''
|
|
|
|
if department_id:
|
|
|
|
dpt=get_model("clinic.department").browse(department_id)
|
|
|
|
sub_name="(%s)" % dpt.name or ""
|
|
|
|
elif branch_id:
|
|
|
|
branch=get_model("clinic.branch").browse(branch_id)
|
|
|
|
sub_name="(%s)" % branch.name or ""
|
|
|
|
company_id=get_active_company()
|
|
|
|
company=get_model("company").browse(company_id)
|
2016-03-01 04:08:44 +00:00
|
|
|
lines=sorted(lines,key=lambda x: (x['date'],x['cseq']))
|
2015-03-24 06:51:21 +00:00
|
|
|
no=1
|
2016-03-01 04:08:44 +00:00
|
|
|
for line in lines:
|
2015-03-24 06:51:21 +00:00
|
|
|
line['no']=no
|
|
|
|
no+=1
|
|
|
|
data={
|
|
|
|
'company_name': '%s %s' % (company.name or "", sub_name),
|
|
|
|
'lines': lines,
|
|
|
|
'year': year,
|
|
|
|
'total_qty': total_qty,
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
|
|
|
def onchange_date(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
date=data['date']
|
|
|
|
year,month,day=date.split("-")
|
|
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
|
|
data['date_from']="%s-%s-01"%(year,month)
|
|
|
|
data['date_to']="%s-%s-%s"%(year,month,total_day)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def onchange_branch(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
data['department_id']=None
|
|
|
|
return data
|
|
|
|
|
|
|
|
ReportMedicalDetail.register()
|