-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
93 lines (77 loc) 路 3.3 KB
/
Copy pathutils.py
File metadata and controls
93 lines (77 loc) 路 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from fpdf import FPDF
def generate_invoice_pdf(invoice, client, supplier, target):
pdf = FPDF()
pdf.add_page()
# --- TES INFOS (L'interm茅diaire / Ta boutique) ---
# Tu peux aussi mettre 莽a dans ton fichier .env
MY_COMPANY_NAME = "MA BOUTIQUE E-COMMERCE"
MY_COMPANY_ADDRESS = "123 Rue du Commerce, 75001 Paris, France"
# --- STYLE & COULEURS ---
primary_color = (41, 128, 185) # Bleu pro
# --- LOGIQUE DE L'EN-T脢TE ---
if target == "client":
title = "FACTURE CLIENT"
sender_name = MY_COMPANY_NAME
sender_address = MY_COMPANY_ADDRESS
receiver_name = client.name
receiver_address = client.address if client.address else ""
amount = invoice.amount_client
else:
title = "FACTURE FOURNISSEUR"
sender_name = supplier.name
sender_address = supplier.address if supplier.address else ""
receiver_name = MY_COMPANY_NAME
receiver_address = MY_COMPANY_ADDRESS
amount = invoice.amount_supplier
# --- DESSIN DU PDF ---
pdf.set_font("Arial", "B", 20)
pdf.set_text_color(*primary_color)
pdf.cell(190, 15, txt=title, ln=True, align='L') # type: ignore
pdf.set_line_width(0.5)
pdf.set_draw_color(*primary_color)
pdf.line(10, 25, 200, 25)
pdf.ln(10)
# --- INFOS 脡METTEUR & R脡CEPTEUR ---
pdf.set_text_color(0, 0, 0)
pdf.set_font("Arial", "B", 10)
pdf.cell(95, 5, "DE LA PART DE :", ln=0)
pdf.cell(95, 5, "DESTIN脡 脌 :", ln=1)
pdf.set_font("Arial", "", 10)
pdf.cell(95, 5, f"{sender_name}", ln=0)
pdf.cell(95, 5, f"{receiver_name}", ln=1)
# Gestion des adresses (si vides)
pdf.multi_cell(95, 5, f"{sender_address}")
# Positionnement manuel pour la deuxi猫me colonne de l'adresse destinataire
curr_y = pdf.get_y()
pdf.set_xy(105, curr_y - 5)
pdf.multi_cell(95, 5, f"{receiver_address}")
pdf.ln(10)
# --- D脡TAILS FACTURE ---
pdf.set_fill_color(245, 245, 245)
pdf.set_font("Arial", "B", 10)
pdf.cell(60, 10, f"Num茅ro : {invoice.invoice_number}", border=0, fill=True)
pdf.cell(70, 10, f"Date : {invoice.created_at.strftime('%d/%m/%Y')}", border=0, fill=True)
pdf.cell(60, 10, f"Statut : {invoice.status.upper()}", border=0, fill=True, ln=1)
pdf.ln(10)
# --- TABLEAU DES PRIX ---
pdf.set_font("Arial", "B", 11)
pdf.set_text_color(255, 255, 255)
pdf.set_fill_color(*primary_color)
pdf.cell(140, 10, " Description des services", border=0, fill=True)
pdf.cell(50, 10, "Total HT ", border=0, fill=True, ln=1, align='R')
pdf.set_text_color(0, 0, 0)
pdf.set_font("Arial", "", 11)
desc = "Prestation de service / Commande Shopify" if target == "client" else f"Achat de marchandises / Commande {invoice.invoice_number}"
pdf.cell(140, 12, f" {desc}", border="B")
pdf.cell(50, 12, f"{amount:.2f} EUR ", border="B", ln=1, align='R')
# --- TOTAL ---
pdf.ln(10)
pdf.set_font("Arial", "B", 12)
pdf.cell(140, 10, "TOTAL A PAYER", align='R')
pdf.set_text_color(*primary_color)
pdf.cell(50, 10, f"{amount:.2f} EUR", align='R')
# FIX FINAL :
# 1. On r茅cup猫re le contenu (qui est d茅j脿 un bytearray en fpdf2)
pdf_output = pdf.output()
# 2. On s'assure que c'est bien au format 'bytes' pour FastAPI
return bytes(pdf_output)