This repository has been archived on 2026-08-11. You can view files and clone it, but cannot push or open issues or pull requests.
Gishen-Mob/lib/commerce.ts
kirukib 858972b5ca Add multi-provider login and guest pharmacist order codes.
Guests can generate a shareable GSH code for admin fulfillment; sign-in now supports Google, email, phone, and Telegram demos, with the backend spec updated.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 16:11:23 +03:00

79 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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));
}