clinic/netforce_clinic/models/report_discontinue_patient.py

132 lines
4.7 KiB
Python

import time
from calendar import monthrange
from netforce.model import Model,fields,get_model
from netforce.access import get_active_company
from . import utils
class ReportDiscontinuePatient(Model):
_name="clinic.report.discontinue.patient"
_string="Report Discontinue Patient"
_transient=True
_fields={
"date": fields.Date("Month"),
"date_from": fields.Date("From", required=True),
"date_to": fields.Date("To", required=True),
"branch_id": fields.Many2One("clinic.branch","Branch"),
"department_id": fields.Many2One("clinic.department","Department"),
}
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))
branch_id=defaults.get('branch_id',None)
print('defaults ', defaults)
if branch_id:
branch_id=int(branch_id)
department_id=defaults.get('department_id',None)
if department_id:
department_id=int(department_id)
select_dpt=get_model('select.company').get_select()
if select_dpt:
if not branch_id:
branch_id=select_dpt['branch_id']
if not department_id:
if select_dpt.get('department_ids'):
department_id=select_dpt['department_ids'][0]
else:
department_id=select_dpt['department_id']
res={
'date': date,
'date_from': date_from,
'date_to': date_to,
'branch_id': branch_id,
'department_id': department_id,
}
return res
def get_report_data(self,ids,context={}):
company_id=get_active_company()
company=get_model('company').browse(company_id)
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")
if ids:
obj=self.browse(ids)[0]
month=obj.date_from.split("-")[1]
time_start=obj.date_from
time_stop=obj.date_to
branch_id=obj.branch_id.id
department_id=obj.department_id.id
# new patient of this month
dom=[]
dom.append(['resign_date','>=',time_start])
dom.append(['resign_date','<=',time_stop])
dom.append(['walkin','=',"no"])
dom.append(['dispose','=',True])
if branch_id:
dom.append(['branch_id','=',branch_id])
if department_id:
dom.append(['department_id','=',department_id])
print("dom ", dom)
records=get_model('clinic.patient').search_browse(dom)
lines=[]
no=1
for record in records:
lines.append({
'no': no,
'name': record.name or '',
'pid': record.id,
'note': record.note or '',
'resign_date': record.resign_date or '',
})
no+=1
month_str=utils.MONTHS['th_TH'][int(month)]
start=int(time_start[8:10])
stop=int(time_stop[8:10])
diff=stop-start
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 ""
data={
'company_name': '%s %s' % (company.name or "", sub_name),
'parent_company_name': company.parent_id.name or "",
'lines': lines,
'month': month_str,
'from': time_start,
'to': time_stop,
'is_duration': diff+1 < total_day,
'year': year,
}
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
ReportDiscontinuePatient.register()