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), } def _get_date_from(self,context={}): year,month=time.strftime("%Y-%m").split("-") return '%s-%s-01'%(year,month) def _get_date_to(self,context={}): year,month,day=time.strftime("%Y-%m-%d").split("-") weekday, total_day=monthrange(int(year), int(month)) return "%s-%s-%s"%(year,month,total_day) _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d"), 'date_from': _get_date_from, 'date_to': _get_date_to, } 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)) time_start='%s-%s-01'%(year,str(month).zfill(2)) time_stop='%s-%s-%s'%(year,str(month).zfill(2),total_day) defaults=context.get('defaults') if defaults: year,month,day=defaults['date'].split("-") time_start='%s-%s-01'%(year,str(month).zfill(2)) time_stop='%s-%s-%s'%(year,str(month).zfill(2),total_day) if ids: obj=self.browse(ids)[0] month=obj.date_from.split("-")[1] time_start=obj.date_from time_stop=obj.date_to # new patient of this month dom=[] dom.append(['resign_date','>=',time_start]) dom.append(['resign_date','<=',time_stop]) dom.append(['active','=',False]) 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 data={ 'company_name': company.name or "", '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 ReportDiscontinuePatient.register()