> ## Documentation Index
> Fetch the complete documentation index at: https://docs.adhdsimple.co.uk/llms.txt
> Use this file to discover all available pages before exploring further.

# Enhanced Shipment Workflow with DHL Integration

> End-to-end tracking and automation for prescription deliveries

## Overview

Prescriptions are shipped from clinics to pharmacies using DHL Express. The system pre-generates tracking numbers and automatically schedules pickups.

## How It Works

### Basic Flow

1. Doctor creates prescription and selects "Send via ADHD Simple"
2. System creates shipment with pre-generated DHL tracking number
3. DHL pickup is automatically scheduled via API
4. DHL collects from clinic and delivers to pharmacy
5. Pharmacy processes and ships to patients

### Bulk Shipments

Clinics can create up to 10 pre-labeled shipments at once for busy periods.

## Code Examples

### Create Single Shipment

```typescript theme={null}
const response = await supabase.functions.invoke("clinical-shipment-operations", {
  body: {
    action: "createWithPrescriptions",
    payload: {
      prescriptionIds: ["uuid-1", "uuid-2"],
      pickup_date: "2025-02-01",
      pickup_time: "14:00",
      pickup_location: "ADHD Simple Clinic, 123 Medical Centre, London, SW1A 1AA",
      notes: "Temperature controlled medications included",
    },
  },
});

// Returns: { data: { shipment: {..., tracking_number: 'DHL123456ABCDEFGB'} } }
```

### List Shipments

```typescript theme={null}
const response = await supabase.functions.invoke("clinical-shipment-operations", {
  body: {
    action: "list",
    payload: {}
  },
});

// Returns: { data: { shipments: [...] } }
```

## DHL Integration

### Pickup Scheduling

* Automatic scheduling via DHL Express API
* Pre-generated tracking numbers (no waiting for collection)
* Van pickup confirmation emails sent automatically

### Tracking

* Real-time tracking updates from DHL
* Delivery notifications to pharmacies
* Patient delivery confirmation

## Pharmacy Processing

### Arrival Verification

1. Package arrives at pharmacy
2. Pharmacist marks as arrived in system
3. Contents verified against manifest
4. Discrepancies logged if found

### Patient Shipping

1. Pharmacist reviews individual prescriptions
2. Creates DHL Express labels for patients
3. Packages medication with labels
4. Hands to DHL courier with scan out

## Edge Functions

The system uses these Supabase Edge Functions:

* **`clinical-shipment-operations`** - Consolidated shipment management for doctors and pharmacies
* **`dhl-tracking-webhook`** - Receive DHL tracking updates

## Database Tables

Key tables involved:

* **`shipments`** - Main shipment records with DHL tracking
* **`shipment_prescriptions`** - Links prescriptions to shipments
* **`shipment_tracking_events`** - Tracking history from DHL
* **`patient_shipments`** - Individual patient deliveries from pharmacy

## Error Handling

Common issues and handling:

* **DHL API failures** - Fallback to manual scheduling
* **Missing tracking updates** - Retry mechanism with exponential backoff
* **Content discrepancies** - Pharmacy notification and clinic contact system

<Note>
  This is a simplified overview. The actual implementation includes additional error handling, validation, and edge cases for production use.
</Note>
