import os import time import urllib from calendar import monthrange from netforce.model import Model,fields,get_model from netforce.database import get_active_db, get_connection class PrintLaborCost(Model): _name="clinic.print.labor.cost" _string="Print Report Labor Cost" _fields={ "date": fields.Date("Month"), "period_id": fields.Many2One("clinic.period.line","Period"), "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"), "staff_type": fields.Selection([["doctor","Doctor"],["nurse","Nurse"]],"Type"), 'staff_id': fields.Many2One("clinic.staff","Staff"), 'lines': fields.One2Many("clinic.print.labor.cost.line","print_labor_cost_id","Lines"), 'sum_report_id': fields.Many2One("clinic.report.labor.cost.summary","Summary Report"), 'lc_report_id': fields.Many2One("clinic.report.labor.cost","Labor Cost Report"), 'dt_report_id': fields.Many2One("clinic.report.labor.cost.detail","Detail Report"), 'sub_dt_report_id': fields.Many2One("clinic.report.labor.cost.sub.detail","Sub Detail Report"), 'dl_report_id': fields.Many2One("clinic.report.labor.cost.daily","Daily Report"), 'ot_report_id': fields.Many2One("clinic.report.labor.cost.overtime","Overtime Report"), 'report_summary': fields.File("Summary"), 'report_labor_cost': fields.File("Labor Cost"), } def reload(self,ids,context={}): obj=self.browse(ids)[0] context['defaults']={ 'date_from': obj.date_from, 'date_to': obj.date_to, 'cycle_id': obj.cycle_id.id, 'branch_id': obj.branch_id.id, 'department_id': obj.department_id.id, 'staff_type': obj.staff_type, } cost_lines=get_model('clinic.report.labor.cost.summary').get_report_data(ids=[],context=context)['lines'] for line in obj.lines: line.delete() lines=[] for cost_line in cost_lines: staff_id=cost_line['staff_id'] staff_name=cost_line['staff_name'] total_amount=0 total_qty=0 for sub_line in cost_line['sub_lines']: total_qty+=sub_line['qty'] total_amount+=sub_line['amt'] lines.append(('create',{ 'staff_id': staff_id, 'staff_name': staff_name, 'total_amount': total_amount, 'total_qty': total_qty, })) obj.write({ 'lines': lines, }) def generate_report(self,ids,context={}): obj=self.browse(ids)[0] # clear and reload obj.reload() def load_report(fname,link): req=urllib.request.Request(link) response=urllib.request.urlopen(req) data=response.read() dbname=get_active_db() fdir=os.path.join("static","db",dbname,"files") open(os.path.join(fdir,fname),"wb").write(data) db=get_connection() sum_id=obj.sum_report_id.id if not sum_id: sum_id=get_model('clinic.report.labor.cost.summary').create({ 'date': obj.date_from, 'date_from': obj.date_from, 'date_to': obj.date_to, 'staff_type': obj.staff_type, 'cycle_id': obj.cycle_id.id, 'branch_id': obj.branch_id.id, 'department_id': obj.department_id.id, }) obj.write({ 'sum_report_id': sum_id, }) report_sum=get_model("clinic.report.labor.cost.summary").browse(sum_id) report_sum.write({ 'date': obj.date_from, 'date_from': obj.date_from, 'date_to': obj.date_to, 'staff_type': obj.staff_type, 'cycle_id': obj.cycle_id.id, 'branch_id': obj.branch_id.id, 'department_id': obj.department_id.id, 'staff_id': None, }) lc_id=obj.lc_report_id.id if not lc_id: lc_vals={ 'date': obj.date_from, 'period_id': obj.period_id.id, 'date_from': obj.date_from, 'date_to': obj.date_to, 'cycle_id': obj.cycle_id.id, 'branch_id': obj.branch_id.id, 'report_type': 'cross', 'department_id': obj.department_id.id, } lc_id=get_model('clinic.report.labor.cost').create(lc_vals) obj.write({ 'lc_report_id': lc_id, }) report_lc=get_model("clinic.report.labor.cost").browse(lc_id) report_lc.write({ 'date': obj.date_from, 'period_id': obj.period_id.id, 'date_from': obj.date_from, 'date_to': obj.date_to, 'cycle_id': obj.cycle_id.id, 'branch_id': obj.branch_id.id, 'report_type': 'cross', 'department_id': obj.department_id.id, }) db.commit() #lcname='labor.cost.xlsx' #link='http://localhost:9999/report_export_xls?model=clinic.report.labor.cost&template=report_labor_cost&active_id=%s'%(lc_id) #load_report(lcname,link) #sum_name='labor.cost-summary.xlsx' #link='http://localhost:9999/report_export_xls?model=clinic.report.labor.cost.summary&template=report_labor_cost_summary&active_id=%s'%(sum_id) #load_report(sum_name,link) #obj.write({ #'report_summary': sum_name, #'report_labor_cost': lcname, #}) #db.commit() report_lines=[ {'report_file': 'report_detail','model': 'clinic.report.labor.cost.detail', 'report_id': obj.dt_report_id.id,'field_name': 'dt_report_id'}, {'report_file': 'report_sub_detail','model': 'clinic.report.labor.cost.sub.detail', 'report_id': obj.sub_dt_report_id.id,'field_name': 'sub_dt_report_id'}, {'report_file': 'report_daily','model': 'clinic.report.labor.cost.daily', 'report_id': obj.dl_report_id.id,'field_name': 'dl_report_id'}, {'report_file': 'report_ot','model': 'clinic.report.labor.cost.overtime', 'report_id': obj.ot_report_id.id,'field_name': 'ot_report_id'}, ] count=0 for line in obj.lines: if count >= 2: print("Break!") break staff=line.staff_id for report_line in report_lines: name=staff.number or staff.name or staff.id fname='%s-%s.xlsx'%(name,report_line['report_file']) report_file=report_line['report_file'] report_model=report_line['model'] report_template=report_model.replace(".","_").replace("clinic_","") report_id=report_line['report_id'] if not report_id: report_id=get_model(report_model).create({ 'date': obj.date_from, 'date_from': obj.date_from, 'date_to': obj.date_to, 'cycle_id': obj.cycle_id.id, 'branch_id': obj.branch_id.id, 'department_id': obj.department_id.id, 'staff_id': staff.id, }) report_obj=get_model(report_model).browse(report_id) db.commit() else: report_obj=get_model(report_model).browse(report_id) report_obj.write({ 'date': obj.date_from, 'date_from': obj.date_from, 'date_to': obj.date_to, 'cycle_id': obj.cycle_id.id, 'branch_id': obj.branch_id.id, 'department_id': obj.department_id.id, 'staff_id': staff.id, }) db.commit() obj.write({ report_line['field_name']: report_id, }) db.commit() link='http://localhost:9999/report_export_xls?model=%s&template=%s&active_id=%s'%(report_model,report_template,report_obj.id) load_report(fname,link) line.write({ report_file: fname, }) db.commit() count+=1 print("Done!") def _get_date_from(self,context={}): year,month,day=time.strftime("%Y-%m-%d").split("-") #return '%s-%s-01'%(year,month) return '%s-%s-%s'%(year,month,day) 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) return "%s-%s-%s"%(year,month,day) def default_get(self,field_names=None,context={},**kw): defaults=context.get("defaults",{}) date_from=defaults.get("date_from", self._get_date_from()) date_to=defaults.get("date_to", self._get_date_to()) print('defaults ', defaults) yearnow=date_from.split("-")[0] for period in get_model('clinic.period').search_browse([['name','=',yearnow]]): for line in period.lines: if line.state=='open': period_id=line.id date_from=line.date_start date_to=line.date_stop break res={ 'period_id': period_id, 'date': time.strftime("%Y-%m-%d"), 'date_from': date_from, 'date_to': date_to, } return res 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 def onchange_from(self,context={}): data=context['data'] data['date_to']=data['date_from'] return data def onchange_period(self,context={}): data=context['data'] period_id=data['period_id'] period=get_model('clinic.period.line').browse(period_id) data['date_from']=period.date_start data['date_to']=period.date_stop return data PrintLaborCost.register()