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 ReportNewPatient(Model): _name="clinic.report.new.patient" _string="Report New Patient" _transient=True _fields={ "date": fields.Date("Month", required=True), } _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d"), } 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("-") defaults=context.get('defaults') if defaults: year,month,day=defaults['date'].split("-") if ids: obj=self.browse(ids)[0] year,month,day=obj.date.split("-") # new patient of this month year=int(year) month=int(month) dom=[] weekday, total_day=monthrange(year, month) time_start='%s-%s-01'%(year,str(month).zfill(2)) time_stop='%s-%s-%s'%(year,str(month).zfill(2),total_day) dom.append(['reg_date','>=',time_start]) dom.append(['reg_date','<=',time_stop]) 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 '', }) no+=1 month_str=utils.MONTHS['th_TH'][month] data={ 'company_name': company.name or "", 'parent_company_name': company.parent_id.name or "", 'lines': lines, 'month': month_str, 'year': year, } return data ReportNewPatient.register()