updates
This commit is contained in:
@@ -166,4 +166,38 @@ async def get_invoices_by_booking(booking_id: int, current_user: User=Depends(ge
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f'Error fetching invoices by booking: {str(e)}', exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.post('/{id}/send-email')
|
||||
async def send_invoice_email(request: Request, id: int, current_user: User=Depends(authorize_roles('admin', 'staff', 'accountant')), db: Session=Depends(get_db)):
|
||||
try:
|
||||
invoice = db.query(Invoice).filter(Invoice.id == id).first()
|
||||
if not invoice:
|
||||
raise HTTPException(status_code=404, detail='Invoice not found')
|
||||
|
||||
from ..routes.booking_routes import _generate_invoice_email_html
|
||||
from ..models.user import User as UserModel
|
||||
from ..utils.mailer import send_email
|
||||
|
||||
invoice_dict = InvoiceService.invoice_to_dict(invoice)
|
||||
invoice_html = _generate_invoice_email_html(invoice_dict, is_proforma=invoice.is_proforma)
|
||||
invoice_type = 'Proforma Invoice' if invoice.is_proforma else 'Invoice'
|
||||
|
||||
user = db.query(UserModel).filter(UserModel.id == invoice.user_id).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail='User not found')
|
||||
|
||||
await send_email(
|
||||
to=user.email,
|
||||
subject=f'{invoice_type} {invoice.invoice_number}',
|
||||
html=invoice_html
|
||||
)
|
||||
|
||||
logger.info(f'{invoice_type} {invoice.invoice_number} sent to {user.email}')
|
||||
return success_response(message=f'{invoice_type} sent successfully to {user.email}')
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f'Error sending invoice email: {str(e)}', exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
Reference in New Issue
Block a user