diff --git a/app/(admin)/delhivery-logistics/page.tsx b/app/(admin)/delhivery-logistics/page.tsx new file mode 100644 index 0000000..28d2a9d --- /dev/null +++ b/app/(admin)/delhivery-logistics/page.tsx @@ -0,0 +1,452 @@ +'use client'; + +import React, { useState, useEffect } from 'react'; +import { + Navigation, + Wallet, + Package, + Clock, + RefreshCw, + MapPin, + Loader2, + Printer, + ExternalLink, + ShieldAlert, + ArrowRight, + Sparkles, +} from 'lucide-react'; +import { apiFetch } from '@/services/api/client'; +import { API_ENDPOINTS } from '@/services/api/config'; +import { toast } from 'sonner'; +import { format } from 'date-fns'; +import { LogisticsHeaderNav } from '@/components/logistics/LogisticsHeaderNav'; + +interface DelhiveryDashboard { + status: string; + is_enabled: boolean; + mode: string; + client_cms: string; + pickup_location: string; + pickup_pin: string; + wallet: { + balance_inr: number; + currency: string; + is_trial_credit: boolean; + status: string; + }; + shipment_counts: { + ready_to_ship: number; + in_transit: number; + out_for_delivery: number; + delivered: number; + rto_returned: number; + }; + couriers: Array<{ + id: number; + name: string; + mode: string; + min_weight_kg: number; + estimated_days: number; + rate_inr: number; + rating: number; + cod_supported: boolean; + }>; + recent_dispatches: Array<{ + order_id: string; + order_no: string; + awb_code: string; + courier: string; + destination_city: string; + destination_pin: string; + status: string; + fulfillment_status: string; + package_weight: number; + dispatch_date: string; + label_url: string; + tracking_url: string; + }>; +} + +function inr(amount: number) { + return `₹${amount.toLocaleString('en-IN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; +} + +function dateLabel(value?: string) { + if (!value) return '—'; + const date = new Date(value); + if (Number.isNaN(date.getTime())) return '—'; + return format(date, 'dd-MMM-yyyy, hh:mm a'); +} + +export default function DelhiveryLogisticsPage() { + const [data, setData] = useState(null); + const [loading, setLoading] = useState(true); + + // Rate calculator state + const [pickupPin, setPickupPin] = useState('560001'); + const [deliveryPin, setDeliveryPin] = useState(''); + const [weightKg, setWeightKg] = useState(0.5); + const [rateLoading, setRateLoading] = useState(false); + const [calculatedRates, setCalculatedRates] = useState(null); + + const fetchData = async () => { + try { + setLoading(true); + const res = await apiFetch(API_ENDPOINTS.DELHIVERY_DASHBOARD); + if (res) { + setData(res); + setCalculatedRates(res.couriers || []); + } + } catch (err: unknown) { + console.error('Failed to load Delhivery dashboard:', err); + toast.error('Failed to load Delhivery One logistics information'); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + fetchData(); + }, []); + + const handleCalculateRate = async (e: React.FormEvent) => { + e.preventDefault(); + if (!deliveryPin || deliveryPin.trim().length !== 6) { + toast.error('Please enter a valid 6-digit delivery pincode'); + return; + } + try { + setRateLoading(true); + const res = await apiFetch<{ + couriers: DelhiveryDashboard['couriers']; + }>(`${API_ENDPOINTS.DELHIVERY_RATES}?delivery_pincode=${deliveryPin}&pickup_pincode=${pickupPin}&weight_kg=${weightKg}`); + if (res?.couriers) { + setCalculatedRates(res.couriers); + toast.success(`Rates calculated for pincode ${deliveryPin}`); + } + } catch (err: unknown) { + toast.error('Failed to calculate shipping rates'); + } finally { + setRateLoading(false); + } + }; + + const dataCardShell = + 'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0'; + const fieldClass = + 'w-full h-9 px-3 crm-radius-control border border-border bg-card text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-primary'; + const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5'; + const primaryButton = + 'inline-flex items-center justify-center gap-2 h-9 px-4 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50'; + const secondaryButton = + 'inline-flex items-center justify-center gap-2 h-9 px-4 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer disabled:opacity-50'; + + const couriers = calculatedRates || data?.couriers || []; + const dispatches = data?.recent_dispatches || []; + const counts = data?.shipment_counts || { + ready_to_ship: 0, + in_transit: 0, + out_for_delivery: 0, + delivered: 0, + rto_returned: 0, + }; + + return ( +
+ + + {/* Page Header */} +
+
+
+ D1 +
+
+
+

Delhivery One Logistics

+ + Trial Mode + + + Auto-Pickup Simulated + +
+

+ Automated Express Fulfillment • CMS Client:{' '} + {data?.client_cms?.slice(-12) || 'e97e8488-2ebd'} +

+
+
+ + +
+ + {/* Trial Mode Alert Banner */} +
+ +
+ Delhivery One is currently running in Trial Mode.{' '} + When orders are marked "Ready for delivery" with Delhivery One selected, real AWBs and printable thermal shipping slips are generated immediately. Live billing charges and physical carrier dispatches are suppressed until live API tokens are activated. +
+
+ + {/* Metric Cards */} +
+ {/* Trial Wallet */} +
+
+
+

Trial Wallet Balance

+

+ {loading ? '—' : inr(data?.wallet.balance_inr ?? 5000)} +

+ + Trial Credits Active + +
+ + + +
+
+ + {/* Ready to Ship */} +
+
+
+

Ready to Dispatch

+

+ {loading ? '—' : counts.ready_to_ship} +

+ + Manifested & Label Ready + +
+ + + +
+
+ + {/* In Transit */} +
+
+
+

In Transit / Out for Delivery

+

+ {loading ? '—' : counts.in_transit + counts.out_for_delivery} +

+ + On road to consignee + +
+ + + +
+
+ + {/* Total Delivered */} +
+
+
+

Delivered Parcels

+

+ {loading ? '—' : counts.delivered} +

+ + Successful handovers + +
+ + + +
+
+
+ + {/* Main Grid: Rate Calculator & Available Services */} +
+ {/* Rate Calculator */} +
+
+ +

Delhivery Rate & TAT Calculator

+
+
+
+ + setPickupPin(e.target.value)} + maxLength={6} + className={fieldClass} + required + /> +
+
+ + setDeliveryPin(e.target.value)} + maxLength={6} + className={fieldClass} + required + /> +
+
+ + setWeightKg(parseFloat(e.target.value) || 0.5)} + className={fieldClass} + required + /> +
+ +
+
+ + {/* Courier Services Table */} +
+
+
+

Available Delhivery Service Lines

+

Estimated transit time and shipping charges for {weightKg} kg

+
+
+
+ + + + + + + + + + + + {couriers.map((c) => ( + + + + + + + + ))} + +
Service TierModeEst. TransitRate (INR)COD
+
+ {c.name} +
{c.mode} + {c.estimated_days} {c.estimated_days === 1 ? 'Day' : 'Days'} + {inr(c.rate_inr)} + {c.cod_supported ? ( + Supported + ) : ( + Prepaid only + )} +
+
+
+
+ + {/* Recent Delhivery Dispatches */} +
+
+
+

Recent Delhivery Shipments

+

Orders dispatched via Delhivery One automated fulfillment

+
+
+ + {dispatches.length === 0 ? ( +
+ +

No Delhivery shipments dispatched yet

+

+ When an order is updated to "Ready for delivery" with Delhivery One selected, it will appear here with live AWB tracking and thermal slips. +

+
+ ) : ( +
+ + + + + + + + + + + + + + {dispatches.map((d) => ( + + + + + + + + + + ))} + +
Order NoAWB CodeCourier ModeDestinationStatusDateActions
{d.order_no} + {d.awb_code} + {d.courier} + {d.destination_city} ({d.destination_pin}) + + + {d.status} + + + {dateLabel(d.dispatch_date)} + + + + Label + + + + Track + +
+
+ )} +
+
+ ); +} diff --git a/app/(admin)/integrations/shipping/page.tsx b/app/(admin)/integrations/shipping/page.tsx index c2ddc42..12b4f66 100644 --- a/app/(admin)/integrations/shipping/page.tsx +++ b/app/(admin)/integrations/shipping/page.tsx @@ -136,6 +136,64 @@ export default function ShippingIntegrationsPage() { + {/* Delhivery One Configuration Card */} +
+
+
+
+ D1 +
+
+
+

Delhivery One Logistics

+ + Trial Mode Active + + + Auto-Pickup Simulated + +
+

+ Configured with client credentials & MCP gateway • Safe simulated fulfillment +

+
+
+ + Open Delhivery Hub → + +
+ +
+
+ Client ID + ucp-service-cli +
+
+ Client Realm + ucp-Q72U870K1D05 +
+
+ Client CMS ID + cms::client::e97e8488-2ebd... +
+
+ Auth Endpoint + https://ucp-auth.delhivery.com/holyknight +
+
+ Origin Pickup PIN + 560001 (Bengaluru Central Hub) +
+
+ MCP Auto-Mint + d1-mcp-mint@latest (Active) +
+
+
+
{/* Credentials Form (Cols 1-6) */}
diff --git a/app/(admin)/logistics/page.tsx b/app/(admin)/logistics/page.tsx index d172fc7..650c399 100644 --- a/app/(admin)/logistics/page.tsx +++ b/app/(admin)/logistics/page.tsx @@ -14,6 +14,7 @@ import { apiFetch } from '@/services/api/client'; import { API_ENDPOINTS } from '@/services/api/config'; import { toast } from 'sonner'; import { format } from 'date-fns'; +import { LogisticsHeaderNav } from '@/components/logistics/LogisticsHeaderNav'; interface LogisticsDashboard { status: string; @@ -114,6 +115,7 @@ export default function LogisticsDashboardPage() { return (
+

Shiprocket

diff --git a/app/(admin)/orders/page.tsx b/app/(admin)/orders/page.tsx index ed6692e..b207819 100644 --- a/app/(admin)/orders/page.tsx +++ b/app/(admin)/orders/page.tsx @@ -18,6 +18,7 @@ import { import { toast } from 'sonner'; import { format } from 'date-fns'; import { apiFetch } from '@/services/api/client'; +import { API_BASE_URL } from '@/services/api/config'; import { SlideOver } from '@/components/ui/SlideOver'; import { CustomSelect } from '@/components/ui/CustomSelect'; import { TablePagination, useClientPagination } from '@/components/ui/TablePagination'; @@ -181,7 +182,7 @@ export default function OrderIntelligencePage() { const [verifiedItems, setVerifiedItems] = useState([]); const [showStatusForm, setShowStatusForm] = useState(false); const [newStatus, setNewStatus] = useState('PROCESSING'); - const [logisticsProvider, setLogisticsProvider] = useState<'SHIPROCKET' | 'LOCAL_DELIVERY'>('SHIPROCKET'); + const [logisticsProvider, setLogisticsProvider] = useState<'DELHIVERY' | 'SHIPROCKET' | 'LOCAL_DELIVERY'>('DELHIVERY'); const [packageWeight, setPackageWeight] = useState('0.5'); const [trackingNo, setTrackingNo] = useState(''); const [courier, setCourier] = useState(''); @@ -1102,7 +1103,7 @@ export default function OrderIntelligencePage() { + {logisticsProvider === 'DELHIVERY' && ( +
+ ⚡ Delhivery One Automated Pickup (Trial Mode): Confirming will immediately assign a Delhivery AWB, generate the printable 4"x6" thermal shipping slip, and register package dispatch. +
+ )} {logisticsProvider === 'SHIPROCKET' && (
⚡ Automated Pickup Trigger: Confirming will immediately create the order in Shiprocket, assign the AWB number, generate the printable shipping label slip, and schedule carrier pickup at the shop location. diff --git a/components/layouts/Sidebar.tsx b/components/layouts/Sidebar.tsx index fa2cdcd..3bb7a8c 100644 --- a/components/layouts/Sidebar.tsx +++ b/components/layouts/Sidebar.tsx @@ -106,6 +106,7 @@ const NAV_GROUPS: NavGroup[] = [ children: [ { name: 'Customer Store Orders', href: '/orders' }, { name: 'Razorpay Dashboard', href: '/razorpay-dashboard' }, + { name: 'Delhivery Logistics', href: '/delhivery-logistics' }, { name: 'Shiprocket Logistics', href: '/logistics' }, { name: 'Warranties', href: '/warranties' }, { name: 'Refunds & Returns', href: '/refunds' }, diff --git a/components/logistics/LogisticsHeaderNav.tsx b/components/logistics/LogisticsHeaderNav.tsx new file mode 100644 index 0000000..9954840 --- /dev/null +++ b/components/logistics/LogisticsHeaderNav.tsx @@ -0,0 +1,67 @@ +'use client'; + +import React from 'react'; +import Link from 'next/link'; +import { usePathname } from 'next/navigation'; +import { Truck, Navigation, Settings } from 'lucide-react'; + +export const LogisticsHeaderNav: React.FC = () => { + const pathname = usePathname(); + + const tabs = [ + { + id: 'delhivery', + name: 'Delhivery One (Trial)', + href: '/delhivery-logistics', + icon: Navigation, + badge: 'Trial Active', + match: pathname === '/delhivery-logistics', + }, + { + id: 'shiprocket', + name: 'Shiprocket', + href: '/logistics', + icon: Truck, + match: pathname === '/logistics', + }, + { + id: 'settings', + name: 'Logistics Credentials', + href: '/integrations/shipping', + icon: Settings, + match: pathname.startsWith('/integrations/shipping'), + }, + ]; + + return ( +
+ {tabs.map((tab) => { + const Icon = tab.icon; + const isActive = tab.match; + return ( + + + {tab.name} + {tab.badge && ( + + {tab.badge} + + )} + + ); + })} +
+ ); +}; diff --git a/services/api/config.ts b/services/api/config.ts index ea23921..41b5c0b 100644 --- a/services/api/config.ts +++ b/services/api/config.ts @@ -132,6 +132,12 @@ export const API_ENDPOINTS = { // Shiprocket Logistics SHIPPING_DASHBOARD: '/api/v1/shipping/dashboard', + // Delhivery One Logistics + DELHIVERY_STATUS: '/api/v1/delhivery/status', + DELHIVERY_DASHBOARD: '/api/v1/delhivery/dashboard', + DELHIVERY_RATES: '/api/v1/delhivery/rates', + DELHIVERY_LABEL_PREVIEW: (orderId: string) => `/api/v1/delhivery/label/preview/${orderId}`, + // Reporting Hub REPORTS_EXPORT: '/api/v1/reports/export', } as const;