Пример #1
0
def print_timesheets(period: str, path: str, uids: List = []) -> None:    

    for uid in uids:
        qry = (t.tracking_date.year()==int(period[:4])) &(t.tracking_date.month()==int(period[5:])) &(t.user_id==uid)

        rows = db(qry).select()
        tothours = tot_hours(rows)[:-3]
        data = rows.as_list()

        if False:        
            import sys
            print(tothours)
            sys.exit()

        user = db(u.id==uid).select().first()
        
        pdf_file = os.path.join(path, f"{period[:2]}{period[5:]}-timesheet {user.last_name} {user.first_name}.pdf")

        cols = [30, 130, 170, 210, 250, 310, 350]

        rpt = Report(datasource = data, 
                    detailband = Band([
                        Element((cols[0], 0),  ("Helvetica", 10), key = 'tracking_date', format = lambda x: x.strftime("%Y-%m-%d %a")),
                        Element((cols[1], 0), ("Helvetica", 10), key = 'time_start', format = lambda x: x.strftime("%H:%M")),
                        Element((cols[2], 0), ("Helvetica", 10), key = 'time_end', format = lambda x: x.strftime("%H:%M")),
                        Element((cols[3], 0), ("Helvetica", 10), key = 'time_break', format = lambda x: x.strftime("%H:%M")),
                        Element((cols[4], 0), ("Helvetica-Bold", 10), key = 'total_time', format = lambda x: x[:-3]),
                        Element((cols[5], 0), ("Helvetica", 10), key = 'place'),
                        # Element((cols[6], 0), ("Helvetica", 10), key = 'comments'),
                    ]))

        rpt.pageheader = Band([
            Element((cols[0], 0), ("Times-Bold", 18), text = f"{user.last_name} {user.first_name}"),
            Element((cols[0]+150, 7), ("Helvetica", 11), text = "timesheet"),
            Element((cols[0]+150+52, 0), ("Times-Bold", 18), text = f"{period}"),
            Element((cols[0]+150+52+125, 7), ("Helvetica", 11), text = "total hours: "),
            Element((cols[0]+150+52+125+60, 0), ("Times-Bold", 18), text = f"{tothours}"),

            Element((cols[0], 45), ("Helvetica", 11), text = "Date"),
            Element((cols[1], 45), ("Helvetica", 11), text = "Start"),
            Element((cols[2], 45), ("Helvetica", 11), text = "End"),
            Element((cols[3], 45), ("Helvetica", 11), text = "Break"),
            Element((cols[4], 45), ("Helvetica", 11), text = "Total"),
            Element((cols[5], 45), ("Helvetica", 11), text = "Place"),
            Rule((cols[0], 63), 6*72, thickness = 1),
            Element((cols[0], 67), ("Helvetica", 6), text = " "),
        ])


        if False:
            rpt.pagefooter = Band([
                Element((36, 16), ("Helvetica-Bold", 12), sysvar = "pagenumber", format = lambda x: "Page %d" % x),
                Element((300, 16), ("Helvetica-Bold", 12), text = f"{tothours}", align = "right")
            ])
        
        canvas = Canvas(pdf_file,  pagesize=A4)
        rpt.generate(canvas)
        canvas.save()
Пример #2
0
def funit_dscd_er(self, format, param):
    print(param)
    print(format)
    report_data = UnitSancDesg.objects.values(
        'u_id', 'd5', 'u_name', 'd_gdesig', 'd_id', 'd_rank', 'd_discp',
        'd_name', 'd_grade', 'd_gcode', 'tot', 'san', 'req', 'comment',
        'd_cadre').annotate(total_count=ExpressionWrapper(
            F('san') + F('tot') +
            F('req'), output_field=models.IntegerField())).filter(
                u_id=param['u_id'],
                total_count__gt=0).order_by('d5', 'd_gcode')
    print(report_data)
    rpt = Report(report_data)

    pos_x_start = 36
    pos_y_start = 24
    col_1_pos = (pos_x_start, pos_y_start)
    col_2_pos = (col_1_pos[0] + 200, pos_y_start)
    col_3_pos = (col_2_pos[0] + 100, pos_y_start)
    col_4_pos = (col_3_pos[0] + 50, pos_y_start)
    col_5_pos = (col_4_pos[0] + 50, pos_y_start)
    col_6_pos = (col_5_pos[0] + 50, pos_y_start)
    col_7_pos = (col_6_pos[0] + 50, pos_y_start)

    rpt.pageheader = Band([
        Element((36, 0), ("Times-Bold", 20),
                getvalue=lambda x: x["u_name"].upper(),
                format=lambda x: "Unit Summary : %s" % x),
        Element(col_1_pos, ("Helvetica-Bold", 12), text="Designation"),
        Element(col_2_pos, ("Helvetica-Bold", 12),
                text="Grade",
                align="center"),
        Element(col_3_pos, ("Helvetica-Bold", 12),
                text="Cadre",
                align="center"),
        Element(col_4_pos, ("Helvetica-Bold", 12),
                text="Ext",
                align="center",
                width=50),
        Element(col_5_pos, ("Helvetica-Bold", 12),
                text="Req",
                align="center",
                width=50),
        Element(col_6_pos, ("Helvetica-Bold", 12),
                text="San",
                align="center",
                width=50),
        # Element(col_7_pos, ("Helvetica-Bold", 12), text="San", align="center", width=50),
        Rule((36, 56), 7.5 * 72, thickness=2),
    ])

    rpt.groupheaders = [
        Band(
            [
                # Rule((36, 20), 7.5 * 72),
                Element(
                    (36, 4),
                    ("Helvetica-Bold", 10),
                    getvalue=lambda x: x["d_gdesig"].upper(),
                    # format=lambda x: "Group Designation : %s" % x
                ),
            ],
            getvalue=lambda x: x["d_gdesig"].upper()),
    ]

    comment_band = Band([
        Element(
            (col_1_pos[0], 0),
            ("Helvetica", 10),
            key="comment",
            align="left",
        )
    ])
    rpt.detailband = Band([
        Element(
            (col_1_pos[0], 0),
            ("Helvetica", 10),
            getvalue=lambda x: x["d_name"].upper(),
        ),
        Element((col_2_pos[0], 0), ("Helvetica", 10),
                key="d_grade",
                align="left"),
        Element((col_3_pos[0], 0), ("Helvetica", 10),
                key="d_cadre",
                align="right"),
        Element((col_4_pos[0], 0), ("Helvetica", 10),
                getvalue=lambda x: x["tot"] or 0,
                align="right"),
        Element((col_5_pos[0], 0), ("Helvetica", 10),
                getvalue=lambda x: x["req"] or 0,
                align="right"),
        Element((col_6_pos[0], 0), ("Helvetica", 10),
                getvalue=lambda x: x["san"] or 0,
                align="right")
    ])  #, additionalbands=[comment_band]

    rpt.groupfooters = [
        Band(
            [
                Rule((col_4_pos[0] - 24, 4), 30),
                Rule((col_5_pos[0] - 24, 4), 30),
                Rule((col_6_pos[0] - 24, 4), 30),
                Element((col_1_pos[0], 4), ("Helvetica-Bold", 10),
                        getvalue=lambda x: x["d_gdesig"].upper(),
                        format=lambda x: "Subtotal"
                        # format=lambda x: "Subtotal for %s" % x
                        ),
                SumElement((col_4_pos[0], 4), ("Helvetica-Bold", 10),
                           key="tot",
                           align="right"),
                SumElement((col_5_pos[0], 4), ("Helvetica-Bold", 10),
                           key="req",
                           align="right"),
                SumElement((col_6_pos[0], 4), ("Helvetica-Bold", 10),
                           key="san",
                           align="right"),
                Element((36, 16), ("Helvetica-Bold", 10), text=""),
            ],
            getvalue=lambda x: x["d_gdesig"].upper(),
            newpageafter=0),
    ]

    rpt.reportfooter = Band([
        Rule((col_4_pos[0] - 24, 4), 30),
        Rule((col_5_pos[0] - 24, 4), 30),
        Rule((col_6_pos[0] - 24, 4), 30),
        Element((240, 4), ("Helvetica-Bold", 10), text="Unit Total"),
        SumElement((col_4_pos[0], 4), ("Helvetica-Bold", 10),
                   key="tot",
                   align="right"),
        SumElement((col_5_pos[0], 4), ("Helvetica-Bold", 10),
                   key="req",
                   align="right"),
        SumElement((col_6_pos[0], 4), ("Helvetica-Bold", 10),
                   key="san",
                   align="right"),
        Element((36, 16), ("Helvetica-Bold", 10), text=""),
    ])

    rpt.pagefooter = Band([
        Element((400, 16), ("Times-Bold", 10),
                text="Industrial Engineering Dept, WCL HQ",
                align="right"),
        Element((36, 16), ("Helvetica-Bold", 10),
                sysvar="pagenumber",
                format=lambda x: "Page %d" % x),
    ])

    file_loc = "./report_folder/" + param['u_id'] + ".pdf"
    canvas = Canvas(file_loc, (210 * mm, 297 * mm))
    rpt.generate(canvas)
    print("report generated at %s" % file_loc)
    canvas.save()

    return file_loc