import time from netforce.model import Model,fields,get_model from netforce.access import get_active_company from . import utils class ReportCycleItem(Model): _name="clinic.report.cycle.item" _string="Report Cycle Item" _transient=True _fields={ "date": fields.Date("Month"), "date_from": fields.Date("From", required=True), "date_to": fields.Date("To", required=True), "cycle_id": fields.Many2One("clinic.cycle","Cycle"), '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("-") date_from=defaults.get('date_from','%s-%s-01'%(year,month)) date_to=defaults.get('date_to','%s-%s-01'%(year,month)) res={ 'date': date, 'date_from': date_from, 'date_to': date_to, } return res def get_report_data(self,ids,context={}): company_id=get_active_company() company=get_model('company').browse(company_id) defaults=self.default_get(context=context) date_from=defaults.get("date_from") date_to=defaults.get("date_to") month=date_from.split("-")[1] branch_id=None department_id=None cycle_id=None if ids: obj=self.browse(ids)[0] month=obj.date_from.split("-")[1] date_from=obj.date_from date_to=obj.date_to branch_id=obj.branch_id.id department_id=obj.department_id.id cycle_id=obj.cycle_id.id # new patient of this month dom=[] dom.append(['date','>=',date_from]) dom.append(['date','<=',date_to]) dom.append(['state','=','validated']) if cycle_id: dom.append(['cycle_id','=',cycle_id]) if branch_id: dom.append(['branch_id','=',branch_id]) if department_id: dom.append(['department_id','=',department_id]) no=1 lines=[] for citem in get_model('clinic.cycle.item').search_browse(dom,order="date"): for hdcase in citem.hd_cases: patient=hdcase.patient_id ptype=patient.type_id doctor=hdcase.doctor_id cycle=hdcase.cycle_id dpt=hdcase.department_id lines.append({ 'no': no, 'pname': patient.name or '', 'pid': patient.id or '', 'hn': patient.hn_no, 'did': doctor.id, 'dname': doctor.name or "", #'date': utils.date2thai(hdcase.date or '',format='%(Td)s %(d)s/%(m)s/%(BY)s'), 'date': hdcase.date, 'epo': hdcase.epo, 'cname': cycle.name or '', 'cid': citem.id or '', 'hct': hdcase.hct or 0, 'tname': ptype.name or '', 'tid': ptype.id, 'dpt_id': dpt.id, 'dpt_name': dpt.name or "", }) no+=1 month_str=utils.MONTHS['th_TH'][int(month)] company_name=company.name or "" if department_id: department=get_model("clinic.department").browse(department_id) company_name+=" ("+department.name+")" elif branch_id: branch=get_model("clinic.branch").browse(branch_id) company_name+=" ("+branch.name+")" data={ 'company_name': company_name or "", 'lines': lines, 'month': month_str, 'date_from': date_from, 'date_to': date_to, } 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 ReportCycleItem.register()