new invoice design

This commit is contained in:
Parsa Nazer
2026-02-24 16:51:15 +03:30
parent a40b371b27
commit ad80907aa4
5 changed files with 892 additions and 161 deletions
+48 -1
View File
@@ -517,6 +517,53 @@ from django.contrib.auth.decorators import login_required
from .invoice_generator import generate_order_invoice, generate_shop_order_invoice
class UserOrderInvoiceView(APIView):
"""
API endpoint for authenticated users to download their own order invoice PDF.
Users can only download invoices for orders that belong to them.
"""
permission_classes = [IsAuthenticated]
@extend_schema(
tags=["order invoice"],
description="Download PDF invoice for the authenticated user's order.",
responses={200: OpenApiTypes.BINARY},
)
def get(self, request, order_id):
from .models import OrderModel
try:
order = OrderModel.objects.get(pk=order_id)
except OrderModel.DoesNotExist:
return Response(
{'detail': 'سفارش مورد نظر یافت نشد'},
status=status.HTTP_404_NOT_FOUND,
)
if order.user != request.user:
return Response(
{'detail': 'شما اجازه دسترسی به این فاکتور را ندارید'},
status=status.HTTP_403_FORBIDDEN,
)
if not order.is_paid:
return Response(
{'detail': 'فاکتور فقط برای سفارش‌های پرداخت شده قابل دانلود است'},
status=status.HTTP_400_BAD_REQUEST,
)
try:
pdf_file = generate_order_invoice(order_id)
response = HttpResponse(pdf_file.read(), content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="invoice_{order_id}.pdf"'
return response
except Exception as e:
return Response(
{'detail': f'خطا در ایجاد فاکتور: {str(e)}'},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@login_required
def download_order_invoice(request, order_id):
"""
@@ -530,7 +577,7 @@ def download_order_invoice(request, order_id):
pdf_file = generate_order_invoice(order_id)
response = HttpResponse(pdf_file.read(), content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="order_invoice_{order_id + 1000}.pdf"'
response['Content-Disposition'] = f'attachment; filename="order_invoice_{order_id}.pdf"'
return response
except ValueError as e: