Add jalali date formatting utility and update invoice generation

This commit is contained in:
marzban-dev
2026-05-10 22:40:38 +03:30
parent e0714c65a6
commit a40162fb37
3 changed files with 19 additions and 2 deletions
+2 -1
View File
@@ -6,6 +6,7 @@ These functions generate PDF invoices from HTML templates.
from io import BytesIO from io import BytesIO
from datetime import datetime from datetime import datetime
from django.template.loader import render_to_string from django.template.loader import render_to_string
from backend.utils.jalali_formatter import to_jalali
from weasyprint import HTML from weasyprint import HTML
from django.conf import settings from django.conf import settings
import jdatetime import jdatetime
@@ -62,7 +63,7 @@ def generate_order_invoice(order_id):
'user': order.user, 'user': order.user,
'address': order.address, 'address': order.address,
'discount_code': order.discount_code, 'discount_code': order.discount_code,
'created_at_jalali': jdatetime.datetime.fromgregorian(datetime=order.created_at) if order.created_at else None, 'created_at_jalali': to_jalali(order.created_at),
'total_items': sum(item.quantity for item in items), 'total_items': sum(item.quantity for item in items),
'subtotal': order.cart_total or 0, # Stored field: total before any discounts 'subtotal': order.cart_total or 0, # Stored field: total before any discounts
'items_discount_amount': total_items_discount, 'items_discount_amount': total_items_discount,
@@ -1,4 +1,3 @@
{% load price_filters %} {% load price_filters %}
<!doctype html> <!doctype html>
<html lang="fa" dir="rtl"> <html lang="fa" dir="rtl">
+17
View File
@@ -0,0 +1,17 @@
import jdatetime
from django.utils import timezone
def to_jalali(dt):
if not dt:
return None
dt = timezone.localtime(dt)
return jdatetime.datetime.fromgregorian(
year=dt.year,
month=dt.month,
day=dt.day,
hour=dt.hour,
minute=dt.minute,
second=dt.second,
)