Display prestation code refactoring.

This commit is contained in:
Gregory Trullemans 2022-09-19 12:59:53 +02:00
parent a0b744859f
commit c83103c592
1 changed files with 54 additions and 80 deletions

View File

@ -1,10 +1,14 @@
from reportlab.pdfgen import canvas
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.platypus import Table, TableStyle, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from django.conf import settings
from datetime import date, datetime, timedelta
from django.db.models import Q
from django.db.models import Q, Sum
import os
import locale
@ -66,7 +70,7 @@ class PDFDocument(object):
def __init__(self, response):
# Create the PDF object, using the response object as its "file."
self.document = canvas.Canvas(response, pagesize=A4)
self.document = Canvas(response, pagesize=A4)
self.y = Y - X
self.__load_config()
@ -521,7 +525,7 @@ class BillPaper(PDFDocument):
ne retourne rien
"""
res = len(re.findall(r'[fijlt]', contract.client.contact))
print(res)
# print(res)
self.add_string(TITLED_X, "A l'attention de")
self.add_string(194.25, contract.client.contact, font_decoration="Bold")
@ -584,9 +588,53 @@ class BillPaper(PDFDocument):
Returns:
ne retourne rien.
"""
# titre de section
# styles = getSampleStyleSheet()
# style = styles["BodyText"]
# header = Paragraph("<bold><font size=12>Prestations</font></bold>", style)
# w, h = header.wrap(X + 50, self.y)
# header.drawOn(self.document, 72, self.y)
self.add_new_line(X, "Prestations", font_decoration="Bold")
total = self.draw_prestations_table(contract.get_prestation.all())
self.add_vspace(DOUBLE_LINE_HEIGHT)
total = 0
elements = []
prestations_list = contract.get_prestation.all()
for prestation in prestations_list:
total += prestation.total_amount
data = [['Date', 'Libellé', 'Nbre', 'Prix Unit.', 'Total']]
for prestation in prestations_list:
data.append(
[
str(prestation.date),
prestation.label,
str(prestation.unit),
str(prestation.unit_price),
str(prestation.total_amount)
]
)
data.append([' ', ' ', ' ', 'Total', str(total)])
style = TableStyle(
[
('BACKGROUND', (0,0), (-1,0), '#317BB5'), # première ligne bleue
('ALIGN', (2,0), (-1, 0), 'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
('BACKGROUND', (0,-1), (-1,-1), '#317BB5'), # dernière ligne bleue
('ALIGN', (2,1), (-1, -1), 'RIGHT'),
('FONTNAME', (0,-1), (-1,-1), 'Helvetica-Bold'),
]
)
table = Table(data, [2.6*cm, 8.2*cm, 2.6*cm, 2.6*cm, 2.6*cm])
table.setStyle(style)
w, h = table.wrapOn(self.document, X, self.y)
self.y = self.y - h - 5
table.drawOn(self.document, X, self.y)
self.add_vspace(COMMON_LINE_HEIGHT)
self.document.setFont("Helvetica", 10)
self.document.drawRightString(INDENTED_X + 445, self.y, "Acompte")
self.document.drawRightString(INDENTED_RIGHT_X, self.y, str(contract.advance))
@ -596,80 +644,6 @@ class BillPaper(PDFDocument):
self.amount = total - contract.advance
self.document.drawRightString(INDENTED_RIGHT_X, self.y, str(self.amount))
def draw_colored_row(self, c=1, m=0, y=0, k=0.1):
""" Génère une ligne colorée. """
self.document.setFillColorCMYK(c, y, m, k)
self.document.rect(
X, self.y - 4, RIGHT_X - X, COMMON_LINE_HEIGHT, fill=True, stroke=False
)
self.document.setFillColorCMYK(0, 0, 0, 1)
def draw_prestations_table_header(self):
""" Génère le header de la table des prestations. """
self.draw_colored_row()
self.add_vspace()
self.add_string(INDENTED_X, "Date")
self.add_string(PRESTATION_COLUMN_2, "Libellé")
self.add_string(INDENTED_X + 365, "Nbre")
self.add_string(INDENTED_X + 420, "Prix Unit.")
self.document.setFont("Helvetica-Bold", 10)
self.document.drawRightString(INDENTED_X + 510, self.y, "Total")
def draw_prestations_table_footer(self, total):
""" Génère le footer de la table des prestations.
Args:
total (int): somme totale des prestations.
Returns:
ne retourne rien.
"""
self.draw_colored_row()
self.add_vspace()
self.document.setFont("Helvetica-Bold", 10)
self.document.drawRightString(INDENTED_X + 445, self.y, "Total")
self.document.drawRightString(INDENTED_RIGHT_X, self.y, str(total))
def display_prestations(self, prestation, total):
""" Affiche une ligne de prestation dans le tableau.
Args:
prestation (prestation): instance de la classe Prestation.
total (int): somme totale des prestations.
Returns:
total (int): somme totale des prestations.
"""
total += prestation.total_amount
self.add_new_line(INDENTED_X, str(prestation.date))
self.add_string(PRESTATION_COLUMN_2, prestation.label)
self.document.drawRightString(PRESTATION_COLUMN_3, self.y, str(prestation.unit))
self.document.drawRightString(
PRESTATION_COLUMN_4, self.y, str(prestation.unit_price)
)
self.document.drawRightString(
INDENTED_RIGHT_X, self.y, str(prestation.total_amount)
)
return total
def draw_prestations_table(self, prestations_list):
""" Génère le tableau des prestations.
Args:
prestations_list (list): liste des prestations d'un contrat.
Returns:
ne retourne rien.
"""
self.draw_prestations_table_header()
total = 0
for prestation in prestations_list:
total = self.display_prestations(prestation, total)
self.draw_prestations_table_footer(total)
return total
def add_conclusion(self, contract):
""" Affiche la add_add_conclusion de la facture.