forked from seb_vallee/BillManager
génération du fichier XML pour le format factur-x
This commit is contained in:
@@ -11,6 +11,7 @@ from numerotation import generer_numero_facture
|
||||
from config import settings
|
||||
from auth import get_current_user
|
||||
from template_helper import render
|
||||
from generate_facturx_basic import Address, Party, Invoice, InvoiceLine, build_facturx
|
||||
|
||||
router = APIRouter(prefix="/factures", tags=["factures"], dependencies=[Depends(get_current_user)])
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
@@ -171,6 +172,11 @@ def telecharger_pdf(facture_id: int, db: Session = Depends(get_db)):
|
||||
pdf_bytes = HTML(string=html_content, base_url=".").write_pdf()
|
||||
|
||||
filename = f"facture-{facture.numero}.pdf"
|
||||
|
||||
tree = build_facturx(generate_xml (facture_id, db))
|
||||
output_file = "factur-x.xml"
|
||||
tree.write(output_file, xml_declaration=True, encoding="UTF-8", pretty_print=True)
|
||||
|
||||
return Response(
|
||||
content=pdf_bytes,
|
||||
media_type="application/pdf",
|
||||
@@ -187,3 +193,57 @@ def apercu_pdf(request: Request, facture_id: int, db: Session = Depends(get_db))
|
||||
"facture": facture,
|
||||
"settings": settings,
|
||||
})
|
||||
|
||||
def generate_xml(facture_id: int, db: Session):
|
||||
facture = db.query(Facture).get(facture_id)
|
||||
invoice = Invoice(
|
||||
number=facture.numero,
|
||||
issue_date=facture.date_emission,
|
||||
due_date=facture.date_echeance,
|
||||
currency="EUR",
|
||||
type_code="380",
|
||||
note=("Facture établie suite au devis " + facture.devis_origine.numero),
|
||||
|
||||
seller=Party(
|
||||
name=settings.asso_nom,
|
||||
#vat_id="FR12345678901",
|
||||
siret=settings.asso_siret,
|
||||
address=Address(
|
||||
line1=settings.asso_adresse,
|
||||
city=settings.asso_ville,
|
||||
postcode=settings.asso_code_postal,
|
||||
country_code="FR",
|
||||
),
|
||||
),
|
||||
|
||||
buyer=Party(
|
||||
name=facture.client.nom,
|
||||
#vat_id="FR98765432100",
|
||||
siret=facture.client.siret,
|
||||
address=Address(
|
||||
line1=facture.client.adresse,
|
||||
city=facture.client.ville,
|
||||
postcode=facture.client.code_postal,
|
||||
country_code="FR",
|
||||
),
|
||||
),
|
||||
|
||||
|
||||
|
||||
payment_means_code="30", # 30 = virement bancaire
|
||||
iban=settings.asso_iban,
|
||||
bic=settings.asso_bic,
|
||||
payment_reference=facture.devis_origine.numero,
|
||||
)
|
||||
|
||||
for l in facture.lignes:
|
||||
invoice.lines.append(InvoiceLine(
|
||||
line_id=l.id,
|
||||
description=l.description,
|
||||
quantity=l.quantite,
|
||||
unit_code="C62",
|
||||
unit_price=l.prix_unitaire_ht,
|
||||
vat_rate=0.0,
|
||||
))
|
||||
|
||||
return invoice
|
||||
|
||||
Reference in New Issue
Block a user