feat: Add Telegram chat ID to ShopModel for automatic invoice sending

chore: Update Dockerfile to install WeasyPrint dependencies

feat: Enhance ShopOrderModelAdmin with invoice download buttons

feat: Implement invoice generation for OrderModel and ShopOrderModel

feat: Send invoice to shop's Telegram chat upon ShopOrderModel creation

feat: Create Celery task to send shop order invoice via Telegram

feat: Add invoice download endpoints for OrderModel and ShopOrderModel

feat: Implement views for downloading order and shop order invoices

chore: Update requirements.txt to include necessary packages for PDF generation

feat: Create HTML templates for order and shop order invoices
This commit is contained in:
Parsa Nazer
2025-12-28 11:43:33 +03:30
parent 6a7e526f23
commit 34715994ce
12 changed files with 1168 additions and 5 deletions
+60 -1
View File
@@ -119,4 +119,63 @@ def generate_daily_shop_reports():
result = f'Generated reports for {target_date}: {reports_created} created, {reports_updated} updated'
logging.info(result)
return result
return result
@shared_task
def send_shop_order_invoice_telegram_task(shop_order_id, chat_id, bot_token):
"""Send shop order invoice PDF to Telegram chat.
Args:
shop_order_id: ID of the ShopOrderModel
chat_id: Telegram chat ID to send invoice to
bot_token: Telegram bot token for authentication
Returns:
Success or error message
"""
import asyncio
import io
from telegram import Bot
from telegram.error import TelegramError
from .invoice_generator import generate_shop_order_invoice
from .models import ShopOrderModel
try:
# Get the shop order
shop_order = ShopOrderModel.objects.get(pk=shop_order_id)
# Generate invoice PDF
pdf_buffer = generate_shop_order_invoice(shop_order_id)
# Reset buffer position
pdf_buffer.seek(0)
# Send via Telegram
async def send_invoice():
bot = Bot(token=bot_token)
await bot.send_document(
chat_id=chat_id,
document=pdf_buffer,
filename=f'invoice_shop_order_{shop_order_id}.pdf',
caption=f'فاکتور سفارش #{shop_order_id}\n{shop_order.shop.shop_name}'
)
# Run async function
asyncio.run(send_invoice())
logging.info(f'Successfully sent shop order invoice {shop_order_id} to Telegram chat {chat_id}')
return f'Invoice sent successfully to chat {chat_id}'
except ShopOrderModel.DoesNotExist:
error_msg = f'ShopOrderModel with id {shop_order_id} does not exist'
logging.error(error_msg)
return error_msg
except TelegramError as e:
error_msg = f'Telegram error sending invoice {shop_order_id}: {str(e)}'
logging.error(error_msg)
return error_msg
except Exception as e:
error_msg = f'Error sending invoice {shop_order_id} to Telegram: {str(e)}'
logging.error(error_msg)
return error_msg