Esempio n. 1
0
def ramassagePdf(request, id_ramassage, parMois=0, full=0):
    """Renvoie le fichier PDF du ramassage par année/effectif correspondant au ramassage dont l'id est id_ramassage"""
    parMois = int(parMois) // 2
    full = int(full)
    ramassage = get_object_or_404(Ramassage, pk=id_ramassage)
    LISTE_MOIS = [
        "", "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet",
        "Août", "Septembre", "Octobre", "Novembre", "Décembre"
    ]
    LISTE_MOIS_COURT = [
        "jan", "fev", "mar", "avr", "mai", "juin", "juil", "aou", "sep", "oct",
        "nov", "dec"
    ]
    response = HttpResponse(content_type='application/pdf')
    if Ramassage.objects.filter(moisFin__lt=ramassage.moisFin).exists(
    ) and not full:  #s'il existe un ramassage antérieur
        debut = Ramassage.objects.filter(
            moisFin__lt=ramassage.moisFin).aggregate(
                Max('moisFin'))['moisFin__max'] + timedelta(days=1)
    else:
        debut = Semaine.objects.aggregate(Min('lundi'))['lundi__min']
    fin = ramassage.moisFin
    moisdebut = 12 * debut.year + debut.month - 1
    listeDecompte, effectifs = Ramassage.objects.decompteRamassage(
        ramassage, csv=False, parClasse=False, parMois=parMois, full=full)
    nomfichier = "ramassage{}_{}-{}_{}.pdf".format(debut.month, debut.year,
                                                   fin.month, fin.year)
    response['Content-Disposition'] = "attachment; filename={}".format(
        nomfichier)
    pdf = easyPdf(titre="Ramassage des colles de {} {} à {} {}".format(
        LISTE_MOIS[debut.month], debut.year, LISTE_MOIS[fin.month], fin.year),
                  marge_x=30,
                  marge_y=30)
    largeurcel = (pdf.format[0] - 2 * pdf.marge_x) / (9 + parMois +
                                                      len(effectifs))
    hauteurcel = 30
    nbKolleurs = sum([z for x, y, z in listeDecompte])
    pdf.debutDePage()
    LIST_STYLE = TableStyle([('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                             ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                             ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                             ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                             ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                             ('SIZE', (0, 0), (-1, -1), 8)])
    data = [["Matière", "Établissement", "Grade", "Colleur"] +
            (["Mois"] if parMois else []) + [
                "{}è. ann.\n{}".format(annee, effectif)
                for annee, effectif in effectifs
            ]] + [
                [""] * (4 + parMois + len(effectifs))
                for i in range(min(23, nbKolleurs))
            ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
    ligneMat = ligneEtab = ligneGrade = ligneColleur = ligneMois = 1
    for matiere, listeEtabs, nbEtabs in listeDecompte:
        data[ligneMat][0] = matiere.title()
        if nbEtabs > 1:
            LIST_STYLE.add('SPAN', (0, ligneMat),
                           (0, min(ligneMat + nbEtabs - 1, 23)))
        ligneMat += nbEtabs
        for etablissement, listeGrades, nbGrades in listeEtabs:
            data[ligneEtab][1] = etablissement.title()
            if nbGrades > 1:
                LIST_STYLE.add('SPAN', (1, ligneEtab),
                               (1, min(ligneEtab + nbGrades - 1, 23)))
            ligneEtab += nbGrades
            for grade, listeColleurs, nbColleurs in listeGrades:
                data[ligneGrade][2] = grade
                if nbColleurs > 1:
                    LIST_STYLE.add('SPAN', (2, ligneGrade),
                                   (2, min(ligneGrade + nbColleurs - 1, 23)))
                ligneGrade += nbColleurs
                if parMois:
                    for colleur, listeMois, nbMois in listeColleurs:
                        data[ligneColleur][3] = colleur
                        if nbMois > 1:
                            LIST_STYLE.add(
                                'SPAN', (3, ligneColleur),
                                (3, min(ligneColleur + nbMois - 1, 23)))
                        ligneColleur += nbMois
                        for moi, decomptes in listeMois:
                            if moi < moisdebut:
                                LIST_STYLE.add('TEXTCOLOR', (4, ligneMois),
                                               (4 + len(effectifs), ligneMois),
                                               (1, 0, 0))
                            data[ligneMois][4] = LISTE_MOIS_COURT[moi % 12]
                            for i in range(len(effectifs)):
                                data[ligneMois][i + 5] = "{:.02f}h".format(
                                    decomptes[i] / 60).replace('.', ',')
                            ligneMois += 1
                            if ligneMois == 24 and nbKolleurs > 23:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                                t = Table(
                                    data,
                                    colWidths=[
                                        2 * largeurcel, 3 * largeurcel,
                                        largeurcel, 3 * largeurcel, largeurcel
                                    ] + [largeurcel] * len(effectifs),
                                    rowHeights=min(
                                        (1 + nbKolleurs), 24) * [hauteurcel])
                                t.setStyle(LIST_STYLE)
                                w, h = t.wrapOn(pdf, 0, 0)
                                t.drawOn(pdf, (pdf.format[0] - w) / 2,
                                         pdf.y - h - hauteurcel / 2)
                                pdf.finDePage()
                                # on redémarre sur une nouvelle page
                                pdf.debutDePage()
                                LIST_STYLE = TableStyle([
                                    ('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                                    ('BACKGROUND', (0, 0), (-1, 0), (.6, .6,
                                                                     .6)),
                                    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                    ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                                    ('FACE', (0, 0), (-1, -1),
                                     "Helvetica-Bold"),
                                    ('SIZE', (0, 0), (-1, -1), 8)
                                ])
                                nbKolleurs -= 23
                                data = [[
                                    "Matière", "Établissement", "Grade",
                                    "Colleur", "Mois"
                                ] + [
                                    "{}è. ann.\n{}".format(annee, effectif)
                                    for annee, effectif in effectifs
                                ]] + [
                                    [""] * (5 + len(effectifs))
                                    for i in range(min(23, nbKolleurs))
                                ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                                ligneEtab -= 23
                                ligneGrade -= 23
                                ligneMat -= 23
                                ligneColleur -= 23
                                ligneMois = 1
                                if ligneMat > 1:
                                    data[1][0] = matiere.title()
                                    if ligneMat > 2:
                                        LIST_STYLE.add(
                                            'SPAN', (0, 1),
                                            (0, min(ligneMat - 1, 23)))
                                    if ligneEtab > 1:
                                        data[1][1] = etablissement.title()
                                        if ligneEtab > 2:
                                            LIST_STYLE.add(
                                                'SPAN', (1, 1),
                                                (1, min(ligneEtab - 1, 23)))
                                        if ligneGrade > 1:
                                            data[1][2] = grade
                                            if ligneGrade > 2:
                                                LIST_STYLE.add(
                                                    'SPAN', (2, 1),
                                                    (2, min(
                                                        ligneGrade - 1, 23)))
                                            if ligneColleur > 1:
                                                data[1][3] = colleur
                                                if ligneColleur > 2:
                                                    LIST_STYLE.add(
                                                        'SPAN', (3, 1),
                                                        (3,
                                                         min(
                                                             ligneColleur - 1,
                                                             23)))
                else:
                    for colleur, decomptes in listeColleurs:
                        data[ligneColleur][3] = colleur
                        for i in range(len(effectifs)):
                            data[ligneColleur][i + 4] = "{:.02f}h".format(
                                decomptes[i] / 60).replace('.', ',')
                        ligneColleur += 1
                        if ligneColleur == 24 and nbKolleurs > 23:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                            t = Table(data,
                                      colWidths=[
                                          2 * largeurcel, 3 * largeurcel,
                                          largeurcel, 3 * largeurcel
                                      ] + [largeurcel] * len(effectifs),
                                      rowHeights=min(
                                          (1 + nbKolleurs), 24) * [hauteurcel])
                            t.setStyle(LIST_STYLE)
                            w, h = t.wrapOn(pdf, 0, 0)
                            t.drawOn(pdf, (pdf.format[0] - w) / 2,
                                     pdf.y - h - hauteurcel / 2)
                            pdf.finDePage()
                            # on redémarre sur une nouvelle page
                            pdf.debutDePage()
                            LIST_STYLE = TableStyle([
                                ('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                                ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                                ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                                ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                                ('SIZE', (0, 0), (-1, -1), 8)
                            ])
                            nbKolleurs -= 23
                            data = [[
                                "Matière", "Établissement", "Grade", "Colleur"
                            ] + [
                                "{}è. ann.\n{}".format(annee, effectif)
                                for annee, effectif in effectifs
                            ]] + [
                                [""] * (4 + len(effectifs))
                                for i in range(min(23, nbKolleurs))
                            ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                            ligneEtab -= 23
                            ligneGrade -= 23
                            ligneMat -= 23
                            ligneColleur = 1
                            if ligneMat > 1:
                                data[1][0] = matiere.title()
                                if ligneMat > 2:
                                    LIST_STYLE.add('SPAN', (0, 1),
                                                   (0, min(ligneMat - 1, 23)))
                                if ligneEtab > 1:
                                    data[1][1] = etablissement.title()
                                    if ligneEtab > 2:
                                        LIST_STYLE.add(
                                            'SPAN', (1, 1),
                                            (1, min(ligneEtab - 1, 23)))
                                    if ligneGrade > 1:
                                        data[1][2] = grade
                                        if ligneGrade > 2:
                                            LIST_STYLE.add(
                                                'SPAN', (2, 1),
                                                (2, min(ligneGrade - 1, 23)))
    t = Table(
        data,
        colWidths=[2 * largeurcel, 3 * largeurcel, largeurcel, 3 * largeurcel
                   ] + [largeurcel] * (parMois + len(effectifs)),
        rowHeights=min((1 + nbKolleurs), 24) * [hauteurcel])
    t.setStyle(LIST_STYLE)
    w, h = t.wrapOn(pdf, 0, 0)
    t.drawOn(pdf, (pdf.format[0] - w) / 2, pdf.y - h - hauteurcel / 2)
    pdf.finDePage()
    pdf.save()
    fichier = pdf.buffer.getvalue()
    pdf.buffer.close()
    response.write(fichier)
    return response
Esempio n. 2
0
def ramassagePdf(request, id_ramassage):
    """Renvoie le fichier PDF du ramassage correspondant au ramassage dont l'id est id_ramassage"""
    ramassage = get_object_or_404(Ramassage, pk=id_ramassage)
    LISTE_MOIS = [
        "", "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet",
        "Août", "Septembre", "Octobre", "Novembre", "Décembre"
    ]
    response = HttpResponse(content_type='application/pdf')
    debut = ramassage.moisDebut
    fin = Ramassage.incremente_mois(ramassage.moisFin) - timedelta(days=1)
    listeDecompte, effectifs = Ramassage.objects.decompte(debut, fin)
    nomfichier = "ramassage{}_{}-{}_{}.pdf".format(debut.month, debut.year,
                                                   fin.month, fin.year)
    response['Content-Disposition'] = "attachment; filename={}".format(
        nomfichier)
    pdf = easyPdf(titre="Ramassage des colles de {} {} à {} {}".format(
        LISTE_MOIS[debut.month], debut.year, LISTE_MOIS[fin.month], fin.year),
                  marge_x=30,
                  marge_y=30)
    largeurcel = (pdf.format[0] - 2 * pdf.marge_x) / (9 + len(effectifs))
    hauteurcel = 30
    nbKolleurs = sum([z for x, y, z in listeDecompte])
    pdf.debutDePage()
    LIST_STYLE = TableStyle([('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                             ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                             ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                             ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                             ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                             ('SIZE', (0, 0), (-1, -1), 8)])
    data = [["Matière", "Établissement", "Grade", "Colleur"] + [
        "{}è. ann.\n{}".format(annee, effectif)
        for annee, effectif in effectifs
    ]] + [[""] * (4 + len(effectifs)) for i in range(min(23, nbKolleurs))
          ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
    ligneMat = ligneEtab = ligneGrade = ligneColleur = 1
    for matiere, listeEtabs, nbEtabs in listeDecompte:
        data[ligneMat][0] = matiere.title()
        if nbEtabs > 1:
            LIST_STYLE.add('SPAN', (0, ligneMat),
                           (0, min(ligneMat + nbEtabs - 1, 23)))
        ligneMat += nbEtabs
        for etablissement, listeGrades, nbGrades in listeEtabs:
            data[ligneEtab][
                1] = 'Inconnu' if not etablissement else etablissement.title()
            if nbGrades > 1:
                LIST_STYLE.add('SPAN', (1, ligneEtab),
                               (1, min(ligneEtab + nbGrades - 1, 23)))
            ligneEtab += nbGrades
            for grade, listeColleurs, nbColleurs in listeGrades:
                data[ligneGrade][2] = grade
                if nbColleurs > 1:
                    LIST_STYLE.add('SPAN', (2, ligneGrade),
                                   (2, min(ligneGrade + nbColleurs - 1, 23)))
                ligneGrade += nbColleurs
                for colleur, decomptes in listeColleurs:
                    data[ligneColleur][3] = colleur
                    for i in range(len(effectifs)):
                        data[ligneColleur][i + 4] = "{}h{:02d}".format(
                            decomptes[i] // 60, decomptes[i] % 60)
                    ligneColleur += 1
                    if ligneColleur == 24 and nbKolleurs > 23:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                        t = Table(data,
                                  colWidths=[
                                      2 * largeurcel, 3 * largeurcel,
                                      largeurcel, 3 * largeurcel
                                  ] + [largeurcel] * len(effectifs),
                                  rowHeights=min(
                                      (1 + nbKolleurs), 24) * [hauteurcel])
                        t.setStyle(LIST_STYLE)
                        w, h = t.wrapOn(pdf, 0, 0)
                        t.drawOn(pdf, (pdf.format[0] - w) / 2,
                                 pdf.y - h - hauteurcel / 2)
                        pdf.finDePage()
                        # on redémarre sur une nouvelle page
                        pdf.debutDePage()
                        LIST_STYLE = TableStyle([
                            ('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                            ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                            ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                            ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                            ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                            ('SIZE', (0, 0), (-1, -1), 8)
                        ])
                        nbKolleurs -= 23
                        data = [
                            ["Matière", "Établissement", "Grade", "Colleur"] +
                            [
                                "{}è. ann.\n{}".format(annee, effectif)
                                for annee, effectif in effectifs
                            ]
                        ] + [
                            [""] * (4 + len(effectifs))
                            for i in range(min(23, nbKolleurs))
                        ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                        ligneEtab -= 23
                        ligneGrade -= 23
                        ligneMat -= 23
                        ligneColleur = 1
                        if ligneMat > 1:
                            data[1][0] = matiere.title()
                            if ligneMat > 2:
                                LIST_STYLE.add('SPAN', (0, 1),
                                               (0, min(ligneMat - 1, 23)))
                            if ligneEtab > 1:
                                data[1][
                                    1] = 'Inconnu' if not etablissement else etablissement.title(
                                    )
                                if ligneEtab > 2:
                                    LIST_STYLE.add('SPAN', (1, 1),
                                                   (1, min(ligneEtab - 1, 23)))
                                if ligneGrade > 1:
                                    data[1][2] = grade
                                    if ligneGrade > 2:
                                        LIST_STYLE.add(
                                            'SPAN', (2, 1),
                                            (2, min(ligneGrade - 1, 23)))
    t = Table(
        data,
        colWidths=[2 * largeurcel, 3 * largeurcel, largeurcel, 3 * largeurcel
                   ] + [largeurcel] * len(effectifs),
        rowHeights=min((1 + nbKolleurs), 24) * [hauteurcel])
    t.setStyle(LIST_STYLE)
    w, h = t.wrapOn(pdf, 0, 0)
    t.drawOn(pdf, (pdf.format[0] - w) / 2, pdf.y - h - hauteurcel / 2)
    pdf.finDePage()
    pdf.save()
    fichier = pdf.buffer.getvalue()
    pdf.buffer.close()
    response.write(fichier)
    return response
Esempio n. 3
0
def mixteRamassagePdfParClasse(ramassage, total, parmois, full, colleur=False):
    """Renvoie le fichier PDF du ramassage par classe correspondant au ramassage dont l'id est id_ramassage
    si total vaut 1, les totaux par classe et matière sont calculés"""
    LISTE_MOIS = [
        "", "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet",
        "Août", "Septembre", "Octobre", "Novembre", "Décembre"
    ]
    response = HttpResponse(content_type='application/pdf')
    if Ramassage.objects.filter(moisFin__lt=ramassage.moisFin).exists(
    ) and not full:  # s'il existe un ramassage antérieur
        debut = Ramassage.objects.filter(
            moisFin__lt=ramassage.moisFin).aggregate(
                Max('moisFin'))['moisFin__max'] + timedelta(days=1)
    else:
        debut = Semaine.objects.aggregate(Min('lundi'))['lundi__min']
    fin = ramassage.moisFin
    moisdebut = 12 * debut.year + debut.month - 1
    decomptes = Ramassage.objects.decompteRamassage(ramassage,
                                                    csv=False,
                                                    parClasse=True,
                                                    parMois=bool(parmois),
                                                    full=full)
    nomfichier = "ramassagePdfParclasse{}_{}-{}_{}.pdf".format(
        debut.month, debut.year, fin.month, fin.year)
    response['Content-Disposition'] = "attachment; filename={}".format(
        nomfichier)
    pdf = easyPdf(titre="Ramassage des colles de {} {} à {} {}".format(
        LISTE_MOIS[debut.month], debut.year, LISTE_MOIS[fin.month], fin.year),
                  marge_x=30,
                  marge_y=30)
    largeurcel = (pdf.format[0] - 2 * pdf.marge_x) / (10 + parmois)
    hauteurcel = 30
    total = int(total)
    for classe, listeClasse, nbMatieres in decomptes:
        totalclasse = 0
        pdf.debutDePage(soustitre=classe)
        nbKolleurs = nbMatieres
        if total:
            nbKolleurs += 1 + len([
                x for x, y, z in listeClasse
            ])  # on rajoute le nombre de matières et 1 pour la classe
        LIST_STYLE = TableStyle([('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                                 ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                                 ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                 ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                                 ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                                 ('SIZE', (0, 0), (-1, -1), 8)])
        data = [
            ["Matière", "Établissement", "Grade", "Colleur"] +
            (["mois"] if parmois else []) + ["heures"]
        ] + [
            [""] * (5 + parmois) for i in range(min(22, nbKolleurs))
        ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
        ligneMat = ligneEtab = ligneGrade = ligneColleur = ligneMois = 1
        for matiere, listeEtabs, nbEtabs in listeClasse:
            totalmatiere = 0
            data[ligneMat][0] = matiere
            if nbEtabs > 1:
                LIST_STYLE.add('SPAN', (0, ligneMat),
                               (0, min(ligneMat + nbEtabs - 1, 22)))
            ligneMat += nbEtabs
            for etablissement, listeGrades, nbGrades in listeEtabs:
                data[ligneEtab][
                    1] = 'Inconnu' if not etablissement else etablissement.title(
                    )
                if nbGrades > 1:
                    LIST_STYLE.add('SPAN', (1, ligneEtab),
                                   (1, min(ligneEtab + nbGrades - 1, 22)))
                ligneEtab += nbGrades
                for grade, listeColleurs, nbColleurs in listeGrades:
                    data[ligneGrade][2] = grade
                    if nbColleurs > 1:
                        LIST_STYLE.add(
                            'SPAN', (2, ligneGrade),
                            (2, min(ligneGrade + nbColleurs - 1, 22)))
                    ligneGrade += nbColleurs
                    if parmois:  # si on ramassage pour chaque mois
                        for colleur, listeMois, nbMois in listeColleurs:
                            data[ligneColleur][3] = colleur
                            if nbMois > 1:
                                LIST_STYLE.add(
                                    'SPAN', (3, ligneColleur),
                                    (3, min(ligneColleur + nbMois - 1, 22)))
                            ligneColleur += nbMois
                            for moi, heures in listeMois:
                                totalmatiere += heures
                                if moi < moisdebut:
                                    LIST_STYLE.add('TEXTCOLOR', (4, ligneMois),
                                                   (5, ligneMois), (1, 0, 0))
                                data[ligneMois][4] = LISTE_MOIS[moi % 12 + 1]
                                data[ligneMois][5] = "{:.02f}h".format(
                                    heures / 60).replace('.', ',')
                                ligneMois += 1
                                if ligneMois == 23 and nbKolleurs > 22:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                                    t = Table(data,
                                              colWidths=[
                                                  2 * largeurcel,
                                                  3 * largeurcel, largeurcel,
                                                  3 * largeurcel, largeurcel,
                                                  largeurcel
                                              ],
                                              rowHeights=min(
                                                  (1 + nbKolleurs), 23) *
                                              [hauteurcel])
                                    t.setStyle(LIST_STYLE)
                                    w, h = t.wrapOn(pdf, 0, 0)
                                    t.drawOn(pdf, (pdf.format[0] - w) / 2,
                                             pdf.y - h - hauteurcel / 2)
                                    pdf.finDePage()
                                    # on redémarre sur une nouvelle page
                                    pdf.debutDePage(soustitre=classe)
                                    LIST_STYLE = TableStyle([
                                        ('GRID', (0, 0), (-1, -1), 1, (0, 0,
                                                                       0)),
                                        ('BACKGROUND', (0, 0), (-1, 0),
                                         (.6, .6, .6)),
                                        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                        ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                                        ('FACE', (0, 0), (-1, -1),
                                         "Helvetica-Bold"),
                                        ('SIZE', (0, 0), (-1, -1), 8)
                                    ])
                                    nbKolleurs -= 22
                                    data = [[
                                        "Matière", "Établissement", "Grade",
                                        "Colleur", "mois", "heures"
                                    ]] + [
                                        [""] * 6
                                        for i in range(min(22, nbKolleurs))
                                    ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                                    ligneEtab -= 22
                                    ligneGrade -= 22
                                    ligneMat -= 22
                                    ligneColleur -= 22
                                    ligneMois = 1
                                    if ligneMat > 1:
                                        data[1][0] = matiere
                                        if ligneMat > 2:
                                            LIST_STYLE.add(
                                                'SPAN', (0, 1),
                                                (0, min(ligneMat - 1, 22)))
                                        if ligneEtab > 1:
                                            data[1][
                                                1] = 'Inconnu' if not etablissement else etablissement.title(
                                                )
                                            if ligneEtab > 2:
                                                LIST_STYLE.add(
                                                    'SPAN', (1, 1),
                                                    (1, min(ligneEtab - 1,
                                                            22)))
                                            if ligneGrade > 1:
                                                data[1][2] = grade
                                                if ligneGrade > 2:
                                                    LIST_STYLE.add(
                                                        'SPAN', (2, 1),
                                                        (2,
                                                         min(
                                                             ligneGrade - 1,
                                                             22)))
                                                if ligneColleur > 1:
                                                    data[1][3] = colleur
                                                    if ligneColleur > 2:
                                                        LIST_STYLE.add(
                                                            'SPAN', (3, 1),
                                                            (3,
                                                             min(
                                                                 ligneColleur -
                                                                 1, 22)))
            # fin matière
                    else:  # si on ne ramasse pas pour chaque mois mais globalement sur la période de ramassage
                        for colleur, heures in listeColleurs:
                            totalmatiere += heures
                            data[ligneColleur][3] = colleur
                            data[ligneColleur][4] = "{:.02f}h".format(
                                heures / 60).replace('.', ',')
                            ligneColleur += 1
                            if ligneColleur == 23 and nbKolleurs > 22:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                                t = Table(
                                    data,
                                    colWidths=[
                                        2 * largeurcel, 3 * largeurcel,
                                        largeurcel, 3 * largeurcel, largeurcel
                                    ],
                                    rowHeights=min(
                                        (1 + nbKolleurs), 23) * [hauteurcel])
                                t.setStyle(LIST_STYLE)
                                w, h = t.wrapOn(pdf, 0, 0)
                                t.drawOn(pdf, (pdf.format[0] - w) / 2,
                                         pdf.y - h - hauteurcel / 2)
                                pdf.finDePage()
                                # on redémarre sur une nouvelle page
                                pdf.debutDePage(soustitre=classe)
                                LIST_STYLE = TableStyle([
                                    ('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                                    ('BACKGROUND', (0, 0), (-1, 0), (.6, .6,
                                                                     .6)),
                                    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                    ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                                    ('FACE', (0, 0), (-1, -1),
                                     "Helvetica-Bold"),
                                    ('SIZE', (0, 0), (-1, -1), 8)
                                ])
                                nbKolleurs -= 22
                                data = [[
                                    "Matière", "Établissement", "Grade",
                                    "Colleur", "heures"
                                ]] + [
                                    [""] * 5
                                    for i in range(min(22, nbKolleurs))
                                ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                                ligneEtab -= 22
                                ligneGrade -= 22
                                ligneMat -= 22
                                ligneColleur = 1
                                if ligneMat > 1:
                                    data[1][0] = matiere.title()
                                    if ligneMat > 2:
                                        LIST_STYLE.add(
                                            'SPAN', (0, 1),
                                            (0, min(ligneMat - 1, 22)))
                                    if ligneEtab > 1:
                                        data[1][
                                            1] = 'Inconnu' if not etablissement else etablissement.title(
                                            )
                                        if ligneEtab > 2:
                                            LIST_STYLE.add(
                                                'SPAN', (1, 1),
                                                (1, min(ligneEtab - 1, 22)))
                                        if ligneGrade > 1:
                                            data[1][2] = grade
                                            if ligneGrade > 2:
                                                LIST_STYLE.add(
                                                    'SPAN', (2, 1),
                                                    (2, min(
                                                        ligneGrade - 1, 22)))
            # fin matière
            totalclasse += totalmatiere
            if total:
                LIST_STYLE.add('SPAN', (0, ligneColleur),
                               (3 + parmois, ligneColleur))
                LIST_STYLE.add('BACKGROUND', (0, ligneColleur),
                               (-1, ligneColleur), (.8, .8, .8))
                data[ligneColleur] = ["total {}".format(
                    matiere.title())] + [""] * (3 + parmois) + [
                        "{:.02f}h".format(totalmatiere / 60).replace('.', ',')
                    ]
                ligneEtab += 1
                ligneGrade += 1
                ligneMat += 1
                ligneColleur += 1
                ligneMois += 1
                if ligneColleur == 23 and nbKolleurs > 22:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                    t = Table(data,
                              colWidths=[
                                  2 * largeurcel, 3 * largeurcel, largeurcel,
                                  3 * largeurcel, largeurcel
                              ],
                              rowHeights=min(
                                  (1 + nbKolleurs), 23) * [hauteurcel])
                    t.setStyle(LIST_STYLE)
                    w, h = t.wrapOn(pdf, 0, 0)
                    t.drawOn(pdf, (pdf.format[0] - w) / 2,
                             pdf.y - h - hauteurcel / 2)
                    pdf.finDePage()
                    # on redémarre sur une nouvelle page
                    pdf.debutDePage(soustitre=classe)
                    LIST_STYLE = TableStyle([
                        ('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                        ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                        ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                        ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                        ('SIZE', (0, 0), (-1, -1), 8)
                    ])
                    nbKolleurs -= 22
                    data = [
                        ["Matière", "Établissement", "Grade", "Colleur"] +
                        (["mois"] if parmois else []) + ["heures"]
                    ] + [
                        [""] * (5 + parmois)
                        for i in range(min(22, nbKolleurs))
                    ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                    ligneEtab -= 22
                    ligneGrade -= 22
                    ligneMat -= 22
                    if parmois:
                        ligneColleur -= 22
                        ligneMois = 1
                    else:
                        ligneColleur = 1
        # fin classe
        if total:
            LIST_STYLE.add('SPAN', (0, ligneColleur),
                           (3 + parmois, ligneColleur))
            LIST_STYLE.add('BACKGROUND', (0, ligneColleur), (-1, ligneColleur),
                           (.7, .7, .7))
            data[ligneColleur] = [
                "total {}".format(classe)
            ] + [""] * (3 + parmois) + [
                "{:.02f}h".format(totalclasse / 60).replace('.', ',')
            ]
            ligneEtab += 1
            ligneGrade += 1
            ligneMat += 1
            ligneColleur += 1
        t = Table(data,
                  colWidths=[
                      2 * largeurcel, 3 * largeurcel, largeurcel,
                      3 * largeurcel, largeurcel
                  ],
                  rowHeights=min((1 + nbKolleurs), 23) * [hauteurcel])
        t.setStyle(LIST_STYLE)
        w, h = t.wrapOn(pdf, 0, 0)
        t.drawOn(pdf, (pdf.format[0] - w) / 2, pdf.y - h - hauteurcel / 2)
        pdf.finDePage()
    pdf.save()
    fichier = pdf.buffer.getvalue()
    pdf.buffer.close()
    response.write(fichier)
    return response
Esempio n. 4
0
def ramassagePdfParClasse(request, id_ramassage, total):
    """Renvoie le fichier PDF du ramassage par classe correspondant au ramassage dont l'id est id_ramassage
	si total vaut 1, les totaux par classe et matière sont calculés"""
    ramassage = get_object_or_404(Ramassage, pk=id_ramassage)
    LISTE_MOIS = [
        "", "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet",
        "Août", "Septembre", "Octobre", "Novembre", "Décembre"
    ]
    response = HttpResponse(content_type='application/pdf')
    debut = ramassage.moisDebut
    fin = Ramassage.incremente_mois(ramassage.moisFin) - timedelta(days=1)
    listeClasses, classes = Ramassage.objects.decompteParClasse(debut, fin)
    nomfichier = "ramassagePdfParclasse{}_{}-{}_{}.pdf".format(
        debut.month, debut.year, fin.month, fin.year)
    response['Content-Disposition'] = "attachment; filename={}".format(
        nomfichier)
    pdf = easyPdf(titre="Ramassage des colles de {} {} à {} {}".format(
        LISTE_MOIS[debut.month], debut.year, LISTE_MOIS[fin.month], fin.year),
                  marge_x=30,
                  marge_y=30)
    largeurcel = (pdf.format[0] - 2 * pdf.marge_x) / 10
    hauteurcel = 30
    total = int(total)
    for classe, listeClasse in zip(classes, listeClasses):
        totalclasse = 0
        pdf.debutDePage(soustitre=classe.nom)
        nbKolleurs = sum([z for x, y, z in listeClasse])
        if total:
            nbKolleurs += 1 + len([
                x for x, y, z in listeClasse
            ])  # on rajoute le nombre de matières et 1 pour la classe
        LIST_STYLE = TableStyle([('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                                 ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                                 ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                 ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                                 ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                                 ('SIZE', (0, 0), (-1, -1), 8)])
        data = [["Matière", "Établissement", "Grade", "Colleur", "heures"]] + [
            [""] * 5 for i in range(min(22, nbKolleurs))
        ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
        ligneMat = ligneEtab = ligneGrade = ligneColleur = 1
        for matiere, listeEtabs, nbEtabs in listeClasse:
            totalmatiere = 0
            data[ligneMat][0] = matiere.title()
            if nbEtabs > 1:
                LIST_STYLE.add('SPAN', (0, ligneMat),
                               (0, min(ligneMat + nbEtabs - 1, 22)))
            ligneMat += nbEtabs
            for etablissement, listeGrades, nbGrades in listeEtabs:
                data[ligneEtab][
                    1] = 'Inconnu' if not etablissement else etablissement.title(
                    )
                if nbGrades > 1:
                    LIST_STYLE.add('SPAN', (1, ligneEtab),
                                   (1, min(ligneEtab + nbGrades - 1, 22)))
                ligneEtab += nbGrades
                for grade, listeColleurs, nbColleurs in listeGrades:
                    data[ligneGrade][2] = grade
                    if nbColleurs > 1:
                        LIST_STYLE.add(
                            'SPAN', (2, ligneGrade),
                            (2, min(ligneGrade + nbColleurs - 1, 22)))
                    ligneGrade += nbColleurs
                    for colleur, decomptes in listeColleurs:
                        print(ligneColleur)
                        totalmatiere += decomptes
                        data[ligneColleur][3] = colleur
                        data[ligneColleur][4] = "{},{:02d}h".format(
                            decomptes // 60, (1 + decomptes % 60 * 5) // 3)
                        ligneColleur += 1
                        if ligneColleur == 23 and nbKolleurs > 22:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                            t = Table(data,
                                      colWidths=[
                                          2 * largeurcel, 3 * largeurcel,
                                          largeurcel, 3 * largeurcel,
                                          largeurcel
                                      ],
                                      rowHeights=min(
                                          (1 + nbKolleurs), 23) * [hauteurcel])
                            t.setStyle(LIST_STYLE)
                            w, h = t.wrapOn(pdf, 0, 0)
                            t.drawOn(pdf, (pdf.format[0] - w) / 2,
                                     pdf.y - h - hauteurcel / 2)
                            pdf.finDePage()
                            # on redémarre sur une nouvelle page
                            pdf.debutDePage(soustitre=classe.nom)
                            LIST_STYLE = TableStyle([
                                ('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                                ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                                ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                                ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                                ('SIZE', (0, 0), (-1, -1), 8)
                            ])
                            nbKolleurs -= 22
                            data = [[
                                "Matière", "Établissement", "Grade", "Colleur",
                                "heures"
                            ]] + [
                                [""] * 5 for i in range(min(22, nbKolleurs))
                            ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                            ligneEtab -= 22
                            ligneGrade -= 22
                            ligneMat -= 22
                            ligneColleur = 1
                            if ligneMat > 1:
                                data[1][0] = matiere.title()
                                if ligneMat > 2:
                                    LIST_STYLE.add('SPAN', (0, 1),
                                                   (0, min(ligneMat - 1, 22)))
                                if ligneEtab > 1:
                                    data[1][
                                        1] = 'Inconnu' if not etablissement else etablissement.title(
                                        )
                                    if ligneEtab > 2:
                                        LIST_STYLE.add('SPAN', (1, 1),
                                                       (1, min(ligneEtab, 22)))
                                    if ligneGrade > 1:
                                        data[1][2] = grade
                                        if ligneGrade > 2:
                                            LIST_STYLE.add(
                                                'SPAN', (2, 1),
                                                (2, min(ligneGrade, 22)))
            # fin matière
            totalclasse += totalmatiere
            if total:
                LIST_STYLE.add('SPAN', (0, ligneColleur), (3, ligneColleur))
                LIST_STYLE.add('BACKGROUND', (0, ligneColleur),
                               (-1, ligneColleur), (.8, .8, .8))
                data[ligneColleur] = [
                    "total {}".format(matiere.title()), "", "", "",
                    "{},{:02d}h".format(totalmatiere // 60,
                                        (1 + totalmatiere % 60 * 5) // 3)
                ]
                ligneEtab += 1
                ligneGrade += 1
                ligneMat += 1
                ligneColleur += 1
                if ligneColleur == 23 and nbKolleurs > 22:  # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
                    t = Table(data,
                              colWidths=[
                                  2 * largeurcel, 3 * largeurcel, largeurcel,
                                  3 * largeurcel, largeurcel
                              ],
                              rowHeights=min(
                                  (1 + nbKolleurs), 23) * [hauteurcel])
                    t.setStyle(LIST_STYLE)
                    w, h = t.wrapOn(pdf, 0, 0)
                    t.drawOn(pdf, (pdf.format[0] - w) / 2,
                             pdf.y - h - hauteurcel / 2)
                    pdf.finDePage()
                    # on redémarre sur une nouvelle page
                    pdf.debutDePage(soustitre=classe.nom)
                    LIST_STYLE = TableStyle([
                        ('GRID', (0, 0), (-1, -1), 1, (0, 0, 0)),
                        ('BACKGROUND', (0, 0), (-1, 0), (.6, .6, .6)),
                        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                        ('ALIGN', (0, 0), (-1, -1), 'CENTRE'),
                        ('FACE', (0, 0), (-1, -1), "Helvetica-Bold"),
                        ('SIZE', (0, 0), (-1, -1), 8)
                    ])
                    nbKolleurs -= 22
                    data = [[
                        "Matière", "Établissement", "Grade", "Colleur",
                        "heures"
                    ]] + [
                        [""] * 5 for i in range(min(22, nbKolleurs))
                    ]  # on créé un tableau de la bonne taille, rempli de chaînes vides
                    ligneEtab -= 22
                    ligneGrade -= 22
                    ligneMat -= 22
                    ligneColleur = 1
        # fin classe
        if total:
            LIST_STYLE.add('SPAN', (0, ligneColleur), (3, ligneColleur))
            LIST_STYLE.add('BACKGROUND', (0, ligneColleur), (-1, ligneColleur),
                           (.7, .7, .7))
            data[ligneColleur] = [
                "total {}".format(classe.nom), "", "", "",
                "{},{:02d}h".format(totalclasse // 60,
                                    (1 + totalclasse % 60 * 5) // 3)
            ]
            ligneEtab += 1
            ligneGrade += 1
            ligneMat += 1
            ligneColleur += 1
        t = Table(data,
                  colWidths=[
                      2 * largeurcel, 3 * largeurcel, largeurcel,
                      3 * largeurcel, largeurcel
                  ],
                  rowHeights=min((1 + nbKolleurs), 23) * [hauteurcel])
        t.setStyle(LIST_STYLE)
        w, h = t.wrapOn(pdf, 0, 0)
        t.drawOn(pdf, (pdf.format[0] - w) / 2, pdf.y - h - hauteurcel / 2)
        pdf.finDePage()
    pdf.save()
    fichier = pdf.buffer.getvalue()
    pdf.buffer.close()
    response.write(fichier)
    return response
Esempio n. 5
0
def ramassagePdfParClasse(request,id_ramassage):
	"""Renvoie le fichier PDF du ramassage par classe correspondant au ramassage dont l'id est id_ramassage"""
	ramassage=get_object_or_404(Ramassage,pk=id_ramassage)
	LISTE_MOIS=["","Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"]
	response = HttpResponse(content_type='application/pdf')
	debut=ramassage.moisDebut
	fin=Ramassage.incremente_mois(ramassage.moisFin)-timedelta(days=1)
	listeClasses, classes = Ramassage.objects.decompteParClasse(debut,fin)
	nomfichier="ramassageParclasse{}_{}-{}_{}.pdf".format(debut.month,debut.year,fin.month,fin.year)
	response['Content-Disposition'] = "attachment; filename={}".format(nomfichier)
	pdf = easyPdf(titre="Ramassage des colles de {} {} à {} {}".format(LISTE_MOIS[debut.month],debut.year,LISTE_MOIS[fin.month],fin.year),marge_x=30,marge_y=30)
	largeurcel=(pdf.format[0]-2*pdf.marge_x)/10
	hauteurcel=30
	for classe, listeClasse in zip(classes,listeClasses):
		pdf.debutDePage(soustitre = classe.nom)
		nbKolleurs=sum([z for x,y,z in listeClasse])
		LIST_STYLE = TableStyle([('GRID',(0,0),(-1,-1),1,(0,0,0))
											,('BACKGROUND',(0,0),(-1,0),(.6,.6,.6))
											,('VALIGN',(0,0),(-1,-1),'MIDDLE')
											,('ALIGN',(0,0),(-1,-1),'CENTRE')
											,('FACE',(0,0),(-1,-1),"Helvetica-Bold")
											,('SIZE',(0,0),(-1,-1),8)])
		data = [["Matière","Établissement","Grade","Colleur", "heures"]]+[[""]*5 for i in range(min(22,nbKolleurs))] # on créé un tableau de la bonne taille, rempli de chaînes vides
		ligneMat=ligneEtab=ligneGrade=ligneColleur=1
		for matiere, listeEtabs, nbEtabs in listeClasse:
			data[ligneMat][0]=matiere.title()
			if nbEtabs>1:
				LIST_STYLE.add('SPAN',(0,ligneMat),(0,min(ligneMat+nbEtabs-1,22)))
			ligneMat+=nbEtabs
			for etablissement, listeGrades, nbGrades in listeEtabs:
				data[ligneEtab][1]='Inconnu' if not etablissement else etablissement.title()
				if nbGrades>1:
					LIST_STYLE.add('SPAN',(1,ligneEtab),(1,min(ligneEtab+nbGrades-1,22)))
				ligneEtab+=nbGrades
				for grade, listeColleurs, nbColleurs in listeGrades:
					data[ligneGrade][2]=grade
					if nbColleurs>1:
						LIST_STYLE.add('SPAN',(2,ligneGrade),(2,min(ligneGrade+nbColleurs-1,22)))
					ligneGrade+=nbColleurs
					for colleur, decomptes in listeColleurs:
						data[ligneColleur][3]=colleur
						data[ligneColleur][4]="{},{:02d}h".format(decomptes//60,(1+decomptes%60*5)//3)
						ligneColleur+=1
						if ligneColleur==23 and nbKolleurs>22: # si le tableau prend toute une page (et qu'il doit continuer), on termine la page et on recommence un autre tableau
							t=Table(data,colWidths=[2*largeurcel,3*largeurcel,largeurcel,3*largeurcel, largeurcel],rowHeights=min((1+nbKolleurs),23)*[hauteurcel])
							t.setStyle(LIST_STYLE)
							w,h=t.wrapOn(pdf,0,0)
							t.drawOn(pdf,(pdf.format[0]-w)/2,pdf.y-h-hauteurcel/2)
							pdf.finDePage()
							# on redémarre sur une nouvelle page
							pdf.debutDePage(soustitre = classe.nom)
							LIST_STYLE = TableStyle([('GRID',(0,0),(-1,-1),1,(0,0,0))
											,('BACKGROUND',(0,0),(-1,0),(.6,.6,.6))
											,('VALIGN',(0,0),(-1,-1),'MIDDLE')
											,('ALIGN',(0,0),(-1,-1),'CENTRE')
											,('FACE',(0,0),(-1,-1),"Helvetica-Bold")
											,('SIZE',(0,0),(-1,-1),8)])
							nbKolleurs-=22
							data = [["Matière","Établissement","Grade","Colleur", "heures"]]+[[""]*5 for i in range(min(22,nbKolleurs))] # on créé un tableau de la bonne taille, rempli de chaînes vides
							ligneEtab-=22
							ligneGrade-=22
							ligneMat-=22
							ligneColleur=1
							if ligneMat>1:
								data[1][0]=matiere.title()
								if ligneMat>2:
									LIST_STYLE.add('SPAN',(0,1),(0,min(ligneMat-1,22)))
								if ligneEtab>1:
									data[1][1]='Inconnu' if not etablissement else etablissement.title()
									if ligneEtab>2:
										LIST_STYLE.add('SPAN',(1,1),(1,min(ligneEtab-1,22)))
									if ligneGrade>1:
										data[1][2]=grade
										if ligneGrade>2:
											LIST_STYLE.add('SPAN',(2,1),(2,min(ligneGrade-1,22)))
		t=Table(data,colWidths=[2*largeurcel,3*largeurcel,largeurcel,3*largeurcel,largeurcel],rowHeights=min((1+nbKolleurs),23)*[hauteurcel])
		t.setStyle(LIST_STYLE)
		w,h=t.wrapOn(pdf,0,0)
		t.drawOn(pdf,(pdf.format[0]-w)/2,pdf.y-h-hauteurcel/2)
		pdf.finDePage()
	pdf.save()
	fichier = pdf.buffer.getvalue()
	pdf.buffer.close()
	response.write(fichier)
	return response