Example #1
0
def patients_by_chw(request, object_id=None, per_page="0", rformat="pdf"):
    '''List of Cases/Patient per CHW'''
    today = datetime.now().strftime("%d %B,%Y")
    pdfrpt = PDFReport()
    pdfrpt.setLandscape(True)
    pdfrpt.setPrintOnBothSides(True)
    
    pdfrpt.setTitle(_("%(app_name)s: Cases Reports by CHW as of %(date)s") % {'date': today,'app_name':Cfg.get("app_name")})
    
    pdfrpt.setNumOfColumns(2)
    #pdfrpt.setRowsPerPage(88)
    if object_id is None:
        if request.POST and request.POST['zone']:
         #   providers = Case.objects.filter(location=request.POST['zone']).\
          #      values('reporter', 'location').distinct()
            providers = Reporter.objects.filter(location=request.POST['zone'])
            #per_page = "1"
        else:
            providers = Reporter.objects.all()
         #   providers = Case.objects.order_by("location").\
         #                   values('reporter', 'location').distinct()
        
            
        for reporter in providers:
            queryset, fields = ReportAllPatients.by_provider(reporter)
            if queryset:
                cinfo = {'loc': reporter.location,
                         'lname': reporter.last_name,
                         'fname': reporter.first_name,
                         'date': today}
                c = _("%(loc)s: %(lname)s %(fname)s: %(date)s" % cinfo)
                pdfrpt.setTableData(queryset, fields, c, \
                            [0.3 * inch, 0.4 * inch, 1 * inch, 0.4 * inch, \
                             0.3 * inch, 0.8 * inch, 0.5 * inch, 1 * inch, \
                             0.8 * inch])
                if (int(per_page) == 1) is True:
                    pdfrpt.setPageBreak()
                pdfrpt.setFilename("/tmp/report_per_page")
    else:
        if request.POST and request.POST['provider']:
            object_id = request.POST['provider']
        reporter = Reporter.objects.get(id=object_id)
        queryset, fields = ReportAllPatients.by_provider(reporter)
        if queryset:
            cinfo = {'loc': reporter.location,
                     'lname': reporter.last_name,
                     'fname': reporter.first_name,
                     'date': today}
            c = _("%(loc)s: %(lname)s %(fname)s: %(date)s" % cinfo)
            if rformat == "csv" or (request.POST \
                                and request.POST["format"].lower() == "csv"):
                file_name = reporter.last_name + ".csv"
                file_name = file_name.replace(" ", "_").replace("'", "")
                return handle_csv(request, queryset, fields, file_name)

            pdfrpt.setTableData(queryset, fields, c, \
                            [0.3 * inch, 0.4 * inch, 1 * inch, 0.4 * inch, \
                             0.3 * inch, 0.4 * inch, 0.5 * inch, 1 * inch, \
                             1 * inch])
            if (int(per_page) == 1) is True:
                pdfrpt.setPageBreak()
            pdfrpt.setFilename("/tmp/report_per_page")

    return pdfrpt.render()