Spaces:
Running
Running
Update main.py
Browse files1780 unmodified lines17811782178317841785178610 unmodified lines17971798179918001801180216 unmodified lines18191820182118221823182418251826182718281829183016 unmodified lines184718481849185018511852185318541855185618571858185918602 unmodified lines1863186418651866186718681 unmodified line1870187118721873187418752 unmodified lines18781879188018811882188318841885188618871888188918901891189218931780 unmodified linesreceipt = sale_data.get("receipt_number", "N/A")total = float(sale_data.get("total_amount", 0))# If items not provided, fetch themif items is None:10 unmodified lineslogger.error(f"Failed to fetch sale items for confirmation: {e}")items = []# Build items tableitems_html = ""if items:16 unmodified linestracking_link = f"{PRODUCTION_HOST}/track-order?receipt_number={receipt}&email={customer_email}"subject = f"Order Confirmation β {receipt}"html_content = f"""<h2>Thank you for your order!</h2><p>Your order <strong>{receipt}</strong> has been confirmed.</p>{items_html}<p><strong>Total: β¦{total:,.2f}</strong></p><p>You can track your order anytime using this link:<br><a href="{tracking_link}">{tracking_link}</a></p><p>We'll notify you when it's shipped.</p>16 unmodified linesorg = await run_sync_db_call(client.table("organisations").select("org_name, logistics_email").eq("id", sale_data["org_id"]).maybe_single().execute)org_name = org.data["org_name"] if org.data else "Our Store"logistics_email = org.data.get("logistics_email") if org.data else LOGISTICS_EMAILcustomer_email = sale_data.get("customer_email")if not customer_email:logger.warning(f"No customer email for order {sale_id}")returntracking_link = f"{PRODUCTION_HOST}/track-order?receipt_number={sale_data['receipt_number']}&email={customer_email}"if new_status == "shipped":subject = f"Your order #{sale_data['receipt_number']} is on the way!"customer_html = f"""2 unmodified lines<p><strong>Carrier:</strong> {tracking_info.get('carrier', sale_data.get('shipping_carrier', 'Not specified')) if tracking_info else sale_data.get('shipping_carrier', 'Not specified')}</p><p><strong>Tracking number:</strong> {tracking_info.get('tracking_number', sale_data.get('tracking_number', 'N/A')) if tracking_info else sale_data.get('tracking_number', 'N/A')}</p><p><strong>Estimated delivery:</strong> {tracking_info.get('estimated_delivery', sale_data.get('estimated_delivery', '2-5 business days')) if tracking_info else sale_data.get('estimated_delivery', '2-5 business days')}</p><p>Track your order: <a href="{tracking_link}">Click here</a></p>"""internal_html = f"""1 unmodified line<p>Order <strong>{sale_data['receipt_number']}</strong> has been marked as shipped.</p><p>Carrier: {tracking_info.get('carrier', sale_data.get('shipping_carrier', 'N/A')) if tracking_info else sale_data.get('shipping_carrier', 'N/A')}</p><p>Tracking: {tracking_info.get('tracking_number', sale_data.get('tracking_number', 'N/A')) if tracking_info else sale_data.get('tracking_number', 'N/A')}</p><p>Ensure delivery confirmation is received.</p>"""elif new_status == "delivered":2 unmodified lines<h2>Order Delivered</h2><p>Your order <strong>{sale_data['receipt_number']}</strong> has been delivered.</p><p>Thank you for shopping with {org_name}!</p><p>If you have any issues, please contact support.</p>"""internal_html = f"""<h2>Order Delivered β Complete</h2><p>Order <strong>{sale_data['receipt_number']}</strong> has been marked as delivered.</p><p>Fulfillment cycle closed.</p>"""else:returnawait send_sendgrid_email(customer_email, subject, customer_html)if logistics_email:await send_sendgrid_email(logistics_email, f"Order Update: {sale_data['receipt_number']} - {new_status}", internal_html)1780 unmodified lines17811782178317841785178617871788178910 unmodified lines180018011802180318041805180618071808180916 unmodified lines1826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185416 unmodified lines187118721873187418751876187718781879188018811882188318841885188618872 unmodified lines18901891189218931894189518961 unmodified line18981899190019011902190319042 unmodified lines1907190819091910191119121913191419151916191719181919192019211922192319241780 unmodified linesreceipt = sale_data.get("receipt_number", "N/A")total = float(sale_data.get("total_amount", 0))subtotal = float(sale_data.get("subtotal", total)) # if not stored, we can compute from itemsdelivery_fee = float(sale_data.get("delivery_fee", 0))handling_fee = float(sale_data.get("handling_fee", 0))# If items not provided, fetch themif items is None:10 unmodified lineslogger.error(f"Failed to fetch sale items for confirmation: {e}")items = []# Compute subtotal from items if not storedif not subtotal and items:subtotal = sum(float(i.get("subtotal", 0)) for i in items)# Build items tableitems_html = ""if items:16 unmodified linestracking_link = f"{PRODUCTION_HOST}/track-order?receipt_number={receipt}&email={customer_email}"# --- Build the order summary (with fees broken out) ---summary_rows = []summary_rows.append(f"<tr><td><strong>Subtotal</strong></td><td style='text-align:right;'>β¦{subtotal:,.2f}</td></tr>")if delivery_fee > 0:summary_rows.append(f"<tr><td><strong>Delivery Fee</strong></td><td style='text-align:right;'>β¦{delivery_fee:,.2f}</td></tr>")if handling_fee > 0:summary_rows.append(f"<tr><td><strong>Handling Fee</strong></td><td style='text-align:right;'>β¦{handling_fee:,.2f}</td></tr>")summary_rows.append(f"<tr><td><strong>Total</strong></td><td style='text-align:right;font-weight:bold;'>β¦{total:,.2f}</td></tr>")summary_html = f"""<table style="width: 100%; max-width: 300px; margin-top: 10px;">{''.join(summary_rows)}</table>"""subject = f"Order Confirmation β {receipt}"html_content = f"""<h2>Thank you for your order!</h2><p>Your order <strong>{receipt}</strong> has been confirmed.</p>{items_html}<hr><h3>Order Summary</h3>{summary_html}<hr><p>You can track your order anytime using this link:<br><a href="{tracking_link}">{tracking_link}</a></p><p>We'll notify you when it's shipped.</p>16 unmodified linesorg = await run_sync_db_call(client.table("organisations").select("org_name, logistics_email").eq("id", sale_data["org_id"]).maybe_single().execute)org_name = org.data["org_name"] if org.data else "Our Store"logistics_email = org.data.get("logistics_email") if org.data else LOGISTICS_EMAILcustomer_email = sale_data.get("customer_email")if not customer_email:logger.warning(f"No customer email for order {sale_id}")return# --- Get customer phone (with fallback) ---customer_phone = sale_data.get("customer_phone", "Not provided")tracking_link = f"{PRODUCTION_HOST}/track-order?receipt_number={sale_data['receipt_number']}&email={customer_email}"if new_status == "shipped":subject = f"Your order #{sale_data['receipt_number']} is on the way!"customer_html = f"""2 unmodified lines<p><strong>Carrier:</strong> {tracking_info.get('carrier', sale_data.get('shipping_carrier', 'Not specified')) if tracking_info else sale_data.get('shipping_carrier', 'Not specified')}</p><p><strong>Tracking number:</strong> {tracking_info.get('tracking_number', sale_data.get('tracking_number', 'N/A')) if tracking_info else sale_data.get('tracking_number', 'N/A')}</p><p><strong>Estimated delivery:</strong> {tracking_info.get('estimated_delivery', sale_data.get('estimated_delivery', '2-5 business days')) if tracking_info else sale_data.get('estimated_delivery', '2-5 business days')}</p><p><strong>Customer phone:</strong> {customer_phone}</p> <!-- π Added --><p>Track your order: <a href="{tracking_link}">Click here</a></p>"""internal_html = f"""1 unmodified line<p>Order <strong>{sale_data['receipt_number']}</strong> has been marked as shipped.</p><p>Carrier: {tracking_info.get('carrier', sale_data.get('shipping_carrier', 'N/A')) if tracking_info else sale_data.get('shipping_carrier', 'N/A')}</p><p>Tracking: {tracking_info.get('tracking_number', sale_data.get('tracking_number', 'N/A')) if tracking_info else sale_data.get('tracking_number', 'N/A')}</p><p><strong>Customer phone:</strong> {customer_phone}</p> <!-- π Added --><p>Ensure delivery confirmation is received.</p>"""elif new_status == "delivered":2 unmodified lines<h2>Order Delivered</h2><p>Your order <strong>{sale_data['receipt_number']}</strong> has been delivered.</p><p>Thank you for shopping with {org_name}!</p><p><strong>Customer phone:</strong> {customer_phone}</p> <!-- π Added (optional) --><p>If you have any issues, please contact support.</p>"""internal_html = f"""<h2>Order Delivered β Complete</h2><p>Order <strong>{sale_data['receipt_number']}</strong> has been marked as delivered.</p><p><strong>Customer phone:</strong> {customer_phone}</p> <!-- π Added --><p>Fulfillment cycle closed.</p>"""else:returnawait send_sendgrid_email(customer_email, subject, customer_html)if logistics_email:await send_sendgrid_email(logistics_email, f"Order Update: {sale_data['receipt_number']} - {new_status}", internal_html)
main.py+36-5