haikal/inventory/pdf_generator.py
Marwan Alwali 92e94e7265 update
2024-12-23 17:41:24 +03:00

123 lines
4.7 KiB
Python

from nltk.app.wordnet_app import page_from_reference
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from decimal import Decimal
def generate_quotation_pdf(response, quotation, services):
# Initialize PDF
doc = SimpleDocTemplate(response, pagesize=A4)
elements = []
# Styles
styles = getSampleStyleSheet()
title_style = styles['Heading1']
heading_style = styles['Heading2']
normal_style = styles['Normal']
# Title
title = Paragraph(f"Quotation Details - {quotation.display_quotation_number}", title_style)
elements.append(title)
elements.append(Spacer(1, 12))
# Customer Details
elements.append(Paragraph("Customer Details", heading_style))
customer_details = [
["Name:", quotation.customer.get_full_name],
["Address:", quotation.customer.address],
["VAT No:", quotation.customer.national_id]
]
customer_table = Table(customer_details, colWidths=[100, 400])
customer_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (0, -1), colors.lightgrey),
('TEXTCOLOR', (0, 0), (0, -1), colors.black),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 0), (-1, -1), 10),
('BOTTOMPADDING', (0, 0), (-1, -1), 6),
]))
elements.append(customer_table)
elements.append(Spacer(1, 12))
# Quotation Info
elements.append(Paragraph("Quotation Information", heading_style))
quotation_details = [
["Quotation No:", quotation.display_quotation_number],
["Date:", quotation.created_at.strftime("%Y-%m-%d")],
["Remarks:", quotation.remarks or "-"]
]
quotation_table = Table(quotation_details, colWidths=[100, 400])
quotation_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (0, -1), colors.lightgrey),
('TEXTCOLOR', (0, 0), (0, -1), colors.black),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 0), (-1, -1), 10),
('BOTTOMPADDING', (0, 0), (-1, -1), 6),
]))
elements.append(quotation_table)
elements.append(Spacer(1, 12))
# Car Details Table
elements.append(Paragraph("Car Details", heading_style))
car_data = [["رقم الهيكل", "الموديل", "سنة الصنع", "الكمية", "السعر", "الضريبة", "الإجمالي"]]
for item in quotation.quotation_cars.all():
car_data.append([
item.car.vin,
item.car.id_car_model.name,
item.car.year,
item.quantity,
f"{item.car.finances.selling_price:.2f}",
"15%",
f"{item.total_vat:.2f}"
])
car_table = Table(car_data, colWidths=[120, 70, 50, 50, 70, 50, 70])
car_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, -1), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, -1), 10),
('BOTTOMPADDING', (0, 0), (-1, 0), 6),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('TEXTCOLOR', (0, 1), (-1, -1), colors.black),
('ALIGN', (0, 1), (-1, -1), 'CENTER'),
('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 1), (-1, -1), 9),
('GRID', (0, 0), (-1, -1), 0.5, colors.black)
]))
elements.append(car_table)
elements.append(Spacer(1, 12))
# Additional Costs Table
elements.append(Paragraph("Additional Costs", heading_style))
additional_data = [["Additions", "Cost", "VAT Amount", "Total Cost with VAT"]]
for service in services:
additional_data.append([
service.name,
f"{service.price:.2f}",
])
additional_table = Table(additional_data, colWidths=[120, 100, 100, 120])
additional_table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, -1), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, -1), 10),
('BOTTOMPADDING', (0, 0), (-1, 0), 6),
('BACKGROUND', (0, 1), (-1, -1), colors.white),
('TEXTCOLOR', (0, 1), (-1, -1), colors.black),
('ALIGN', (0, 1), (-1, -1), 'CENTER'),
('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 1), (-1, -1), 9),
('GRID', (0, 0), (-1, -1), 0.5, colors.black)
]))
elements.append(additional_table)
# Build PDF
doc.build(elements)