import type { Order } from "@/lib/stores/ordersStore"; import { formatETB } from "@/lib/utils"; /** Shared commerce numbers so cart, checkout and orders stay consistent. */ export const DELIVERY_FEE = 80; export const DEMO_VOUCHER = "GISHEN10"; export const DEMO_VOUCHER_PCT = 0.1; export const DEMO_OTP = "1234"; /** * Short GSH-XXXX reference guests share with a pharmacist. * Pharmacist enters the code on the admin page to create / fulfill the order. */ export function generatePickupCode() { const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; let body = ""; for (let i = 0; i < 4; i += 1) { body += alphabet[Math.floor(Math.random() * alphabet.length)]; } return `GSH-${body}`; } /** Alias used for guest → admin pharmacist order codes (same format as pickup). */ export const generatePharmacistOrderCode = generatePickupCode; /** Plain-text payload for copy / share / WhatsApp (admin-friendly). */ export function buildPharmacistOrderShareText(order: Pick< Order, | "pickupCode" | "customerName" | "customerPhone" | "items" | "total" | "selfPay" | "paymentMethod" | "notes" >) { const code = order.pickupCode ?? "—"; const lines = order.items .map((i) => `• ${i.name} ×${i.quantity} — ${formatETB(i.price * i.quantity)}`) .join("\n"); const due = order.selfPay ?? order.total; return [ "Gishen Pharmacy — Pharmacist order code", "", `Code: ${code}`, "", `Customer: ${order.customerName ?? "—"}`, `Phone: ${order.customerPhone ?? "—"}`, "", "Items:", lines || "• (no items)", "", `Total: ${formatETB(due)}`, `Payment: ${order.paymentMethod === "pay_at_branch" ? "Pay at branch" : order.paymentMethod}`, order.notes ? `Notes: ${order.notes}` : "", "", "Enter this code on the Gishen admin page to create the order.", ] .filter((line) => line !== undefined) .join("\n"); } export function voucherDiscount(subtotal: number, code: string | null | undefined) { if (!code) return 0; if (code.trim().toUpperCase() === DEMO_VOUCHER) { return Math.round(subtotal * DEMO_VOUCHER_PCT); } return 0; } export function orderSubtotalTotal(input: { subtotal: number; deliveryFee: number; discount?: number; }) { return Math.max(0, input.subtotal + input.deliveryFee - (input.discount ?? 0)); }