Minor code refactoring for readability

This commit is contained in:
Gregory Trullemans 2022-09-19 14:33:29 +02:00
parent c83103c592
commit 3eceee4a65
1 changed files with 28 additions and 84 deletions

View File

@ -50,26 +50,18 @@ INDENTED_X = X + INDENT
INDENTED_RIGHT_X = RIGHT_X - INDENT
MIDDLE = (RIGHT_X - X) / 2
PRESTATION_COLUMN_2 = INDENTED_X + 65
PRESTATION_COLUMN_3 = INDENTED_X + 400
PRESTATION_COLUMN_4 = INDENTED_X + 455
COMMON_LINE_HEIGHT = -15
SMALL_LINE_HEIGHT = COMMON_LINE_HEIGHT + 5
LARGE_LINE_HEIGHT = COMMON_LINE_HEIGHT - 5
SMALL_LINE_HEIGHT = -10
LARGE_LINE_HEIGHT = -20
DOUBLE_LINE_HEIGHT = COMMON_LINE_HEIGHT * 2
BIG_LINE_HEIGHT = COMMON_LINE_HEIGHT * 3
HUGE_LINE_HEIGHT = COMMON_LINE_HEIGHT * 4
class PDFDocument(object):
# Create the PDF object, using the response object as its "file."
# http://www.reportlab.com/docs/reportlab-userguide.pdf
# canvas.rect(x, y, width, height, stroke=1, fill=0)
# localhost:8000/billing/contract/pdf/2
""" Create the PDF object, using the response object as its "file" """
def __init__(self, response):
# Create the PDF object, using the response object as its "file."
self.document = Canvas(response, pagesize=A4)
self.y = Y - X
self.__load_config()
@ -87,7 +79,7 @@ class PDFDocument(object):
print(exc)
def add_vspace(self, height=COMMON_LINE_HEIGHT):
""" Passe à la ligne, la hauteur de la ligne étant passée en paramètre.
""" Passe à la ligne avec une hauteur de ligne passée en paramètre.
Args:
height (int): hauteur de la ligne.
@ -153,7 +145,7 @@ class PDFDocument(object):
self.add_vspace(BIG_LINE_HEIGHT)
def download(self):
# Close the PDF object cleanly, and we're done.
""" Close the PDF object cleanly, and send it to download. """
self.document.showPage()
self.document.save()
@ -508,6 +500,9 @@ class BillPaper(PDFDocument):
ne retourne rien
"""
self.amount = 0
self.styles = getSampleStyleSheet()
self.style = self.styles["BodyText"]
self.add_header(contract)
self.add_title(contract)
self.add_prestations(contract)
@ -516,7 +511,7 @@ class BillPaper(PDFDocument):
self.add_footer()
def add_title(self, contract):
""" Génère le "titre" de la facture.
""" Génère le titre de la facture.
Args:
contract (contract): instance de la class Contract.
@ -524,60 +519,13 @@ class BillPaper(PDFDocument):
Returns:
ne retourne rien
"""
res = len(re.findall(r'[fijlt]', contract.client.contact))
# print(res)
text = "A l'attention de <b>" + contract.client.contact + "</b><br />" + (("Pour <b>" + contract.client.name + "</b>") if contract.client.is_company else "") + "<br />" + contract.client.address + "<br />" + str(contract.client.postal_code) + " " + contract.client.city + "<br /><br />Concernant la/le <b>" + contract.title + "</b>"
self.add_string(TITLED_X, "A l'attention de")
self.add_string(194.25, contract.client.contact, font_decoration="Bold")
paragraph = Paragraph(text, self.style)
if contract.client.is_company:
self.add_string(
194.25 + ((len(contract.client.contact) - res / 1.5) * 5.8), " pour la"
)
self.add_new_line(TITLED_X, contract.client.name, font_decoration="Bold")
self.add_new_line(TITLED_X, contract.client.address)
self.add_new_line(
TITLED_X, str(contract.client.postal_code) + " " + contract.client.city
)
self.add_vspace()
self.add_new_line(TITLED_X, "Concernant la/le")
self.add_string(200, contract.title, font_decoration="Bold")
self.add_vspace()
def add_payement_informations(self, contract):
""" Génère les informations de payement.
Args:
contract (contract): instance de la class Contract.
Returns:
ne retourne rien
"""
self.add_vspace()
height = 40
self.document.rect(X, self.y - height, RIGHT_X - X, height, fill=0)
self.add_new_line(
INDENTED_X, "N° Entreprise : " + self.club_infos["BCE_NUMBER"]
)
if contract.client.company_number:
self.document.drawRightString(
INDENTED_RIGHT_X,
self.y,
"Votre N° Entreprise : " + contract.client.company_number,
)
self.add_new_line(
INDENTED_X,
"IBAN : "
+ self.club_infos["IBAN"]
+ " (BIC : "
+ self.club_infos["BIC"]
+ " - "
+ self.club_infos["BANK"]
+ ")",
)
self.add_vspace(DOUBLE_LINE_HEIGHT)
width, height = paragraph.wrap(6*cm, self.y)
paragraph.drawOn(self.document, 6*cm, self.y - height)
self.add_vspace(-height * 1.25)
def add_prestations(self, contract):
""" Génère l'affichage des prestations : tableau, liste des prestations, …
@ -588,16 +536,11 @@ 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")
text = "<b>Prestations</b>"
paragraph = Paragraph(text, self.style)
width, height = paragraph.wrap(6*cm, self.y)
paragraph.drawOn(self.document, INDENTED_X, self.y - height)
self.add_vspace(-height)
total = 0
elements = []
@ -630,8 +573,8 @@ class BillPaper(PDFDocument):
)
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
width, height = table.wrapOn(self.document, X, self.y)
self.y = self.y - height - 5
table.drawOn(self.document, X, self.y)
self.add_vspace(COMMON_LINE_HEIGHT)
@ -703,16 +646,17 @@ class BillPaper(PDFDocument):
def add_footer(self):
""" Ajoute les conditions générales de payement au bas de la facture """
self.y = 125
self.add_new_line(
INDENTED_X, "CONDITIONS GENERALES DE PAIEMENT", font_decoration="Bold"
)
self.add_vspace()
text = "<b>Conditions générales de paiement</b>"
paragraph = Paragraph(text, self.style)
width, height = paragraph.wrap(6*cm, self.y)
paragraph.drawOn(self.document, INDENTED_X, self.y)
self.add_vspace(-height)
lines = [
"Facture payable au comptant.",
"En cas de défaut de paiement à l'échéance, il est dû de plein droit et sans mise en demeure, un interêt fixé au taux de 15% l'an.",
"Tout réclamation, pour être admise, doit être faite dans les huit jours de la réception de la facture.",
"En cas de litige concernant la présente facture, seuls les tribunaux de MONS seront compétents."""
"En cas de litige concernant la présente facture, seuls les tribunaux de Mons seront compétents."
]
text_object = self.document.beginText()
@ -720,7 +664,7 @@ class BillPaper(PDFDocument):
text_object.setFont("Helvetica", 11)
for line in lines:
wraped_text = "\n".join(wrap(line, 108)) # 80 is line width
wraped_text = "\n".join(wrap(line, 108))
text_object.textLines(wraped_text)
self.document.drawText(text_object)