From de777445570ee335a646455dfd608dda8c0767cd Mon Sep 17 00:00:00 2001 From: Parsa Nazer Date: Tue, 12 May 2026 09:48:34 +0330 Subject: [PATCH] fix order time --- backend/order/invoice_generator.py | 12 +++++++++-- .../0044_alter_ordermodel_created_at.py | 21 +++++++++++++++++++ .../0045_alter_ordermodel_created_at.py | 18 ++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 backend/order/migrations/0044_alter_ordermodel_created_at.py create mode 100644 backend/order/migrations/0045_alter_ordermodel_created_at.py diff --git a/backend/order/invoice_generator.py b/backend/order/invoice_generator.py index 6934262..6128df0 100644 --- a/backend/order/invoice_generator.py +++ b/backend/order/invoice_generator.py @@ -55,6 +55,10 @@ def generate_order_invoice(order_id): qr_code_path = os.path.join(template_dir, 'qr-code.png') # Use stored model fields for accuracy and consistency + # Format the Jalali date for display + jalali_date = to_jalali(order.created_at) + created_at_jalali_str = jalali_date.strftime('%Y-%m-%d %H:%M') if jalali_date else '---' + context = { 'order': order, 'order_number': order.pk, @@ -63,7 +67,7 @@ def generate_order_invoice(order_id): 'user': order.user, 'address': order.address, 'discount_code': order.discount_code, - 'created_at_jalali': to_jalali(order.created_at), + 'created_at_jalali': created_at_jalali_str, 'total_items': sum(item.quantity for item in items), 'subtotal': order.cart_total or 0, # Stored field: total before any discounts 'items_discount_amount': total_items_discount, @@ -157,6 +161,10 @@ def generate_shop_order_invoice(shop_order_id): # Calculate subtotal (cart total before discounts) subtotal = shop_order.subtotal or 0 + # Format the Jalali date for display + jalali_date = jdatetime.datetime.fromgregorian(datetime=shop_order.order_created_at) if shop_order.order_created_at else None + created_at_jalali_str = jalali_date.strftime('%Y-%m-%d %H:%M') if jalali_date else '---' + # Resolve image paths for the template (absolute file paths for WeasyPrint) import os template_dir = os.path.join(settings.BASE_DIR, 'templates', 'order') @@ -181,7 +189,7 @@ def generate_shop_order_invoice(shop_order_id): 'address_recipient_name': shop_order.address_recipient_name, 'items': items, 'items_with_discount': items_with_discount, - 'created_at_jalali': jdatetime.datetime.fromgregorian(datetime=shop_order.order_created_at) if shop_order.order_created_at else None, + 'created_at_jalali': created_at_jalali_str, 'total_items': shop_order.items_count, 'subtotal': subtotal, # Total after product discount 'items_discount_amount': total_items_discount, # Product discount amount diff --git a/backend/order/migrations/0044_alter_ordermodel_created_at.py b/backend/order/migrations/0044_alter_ordermodel_created_at.py new file mode 100644 index 0000000..3d68d97 --- /dev/null +++ b/backend/order/migrations/0044_alter_ordermodel_created_at.py @@ -0,0 +1,21 @@ +# Generated by Django 5.2 on 2026-05-12 05:46 + +import django.utils.timezone +import django_jalali.db.models +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('order', '0043_shopdailyreport_is_settled_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='ordermodel', + name='created_at', + field=django_jalali.db.models.jDateTimeField(auto_now_add=True, default=django.utils.timezone.now, verbose_name='تاریخ ثبت سفارش'), + preserve_default=False, + ), + ] diff --git a/backend/order/migrations/0045_alter_ordermodel_created_at.py b/backend/order/migrations/0045_alter_ordermodel_created_at.py new file mode 100644 index 0000000..eefcb1d --- /dev/null +++ b/backend/order/migrations/0045_alter_ordermodel_created_at.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2 on 2026-05-12 05:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('order', '0044_alter_ordermodel_created_at'), + ] + + operations = [ + migrations.AlterField( + model_name='ordermodel', + name='created_at', + field=models.DateTimeField(auto_now_add=True, verbose_name='تاریخ ثبت سفارش'), + ), + ]