migrate to gtea from bistbucket

This commit is contained in:
2026-03-15 17:08:23 +07:00
commit 129ca2260c
3716 changed files with 566316 additions and 0 deletions

View File

@@ -0,0 +1,308 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\RestaurantDelivery\Enums\DeliveryStatus;
use Modules\RestaurantDelivery\Http\Requests\CreateDeliveryRequest;
use Modules\RestaurantDelivery\Http\Requests\UpdateDeliveryStatusRequest;
use Modules\RestaurantDelivery\Http\Resources\DeliveryResource;
use Modules\RestaurantDelivery\Http\Resources\DeliveryTrackingResource;
use Modules\RestaurantDelivery\Models\Delivery;
use Modules\RestaurantDelivery\Models\DeliveryStatusHistory;
use Modules\RestaurantDelivery\Models\Rider;
use Modules\RestaurantDelivery\Services\Tracking\LiveTrackingService;
class DeliveryController extends Controller
{
public function __construct(
protected LiveTrackingService $trackingService
) {}
/**
* List deliveries
*/
public function index(Request $request): JsonResponse
{
$query = Delivery::query()
->with(['rider', 'zone', 'rating']);
// Filter by status
if ($request->has('status')) {
$query->where('status', $request->status);
}
// Filter by restaurant
if ($request->has('restaurant_id')) {
$query->where('restaurant_id', $request->restaurant_id);
}
// Filter by rider
if ($request->has('rider_id')) {
$query->where('rider_id', $request->rider_id);
}
// Filter by date range
if ($request->has('from_date')) {
$query->whereDate('created_at', '>=', $request->from_date);
}
if ($request->has('to_date')) {
$query->whereDate('created_at', '<=', $request->to_date);
}
$deliveries = $query->orderBy('created_at', 'desc')
->paginate($request->get('per_page', 20));
return response()->json([
'success' => true,
'data' => DeliveryResource::collection($deliveries),
'meta' => [
'current_page' => $deliveries->currentPage(),
'last_page' => $deliveries->lastPage(),
'per_page' => $deliveries->perPage(),
'total' => $deliveries->total(),
],
]);
}
/**
* Create a new delivery
*/
public function store(CreateDeliveryRequest $request): JsonResponse
{
$delivery = Delivery::create(array_merge($request->validated(), [
'status' => DeliveryStatus::PENDING->value,
]));
// Initialize tracking
$this->trackingService->initializeDeliveryTracking($delivery);
return response()->json([
'success' => true,
'message' => 'Delivery created successfully',
'data' => new DeliveryResource($delivery->load(['rider', 'zone'])),
], 201);
}
/**
* Get delivery details
*/
public function show(Delivery $delivery): JsonResponse
{
return response()->json([
'success' => true,
'data' => new DeliveryResource(
$delivery->load(['rider', 'zone', 'rating', 'tip', 'statusHistory'])
),
]);
}
/**
* Update delivery status
*/
public function updateStatus(UpdateDeliveryStatusRequest $request, $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$metadata = [
'changed_by_type' => $request->input('changed_by', 'api'),
'notes' => $request->input('notes'),
'latitude' => $request->input('latitude'),
'longitude' => $request->input('longitude'),
];
$success = $delivery->updateStatus($request->status, $metadata);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Invalid status transition',
], 422);
}
// Update Firebase tracking
$this->trackingService->updateDeliveryStatus($delivery, $metadata);
return response()->json([
'success' => true,
'message' => 'Status updated successfully',
'data' => new DeliveryResource($delivery->fresh(['rider', 'zone'])),
]);
}
/**
* Assign rider to delivery
*/
public function assignRider(Request $request, $uuid): JsonResponse
{
$request->validate([
'rider_id' => 'required|exists:restaurant_riders,id',
]);
$delivery = Delivery::where('uuid', $uuid)->first();
$rider = Rider::findOrFail($request->rider_id);
if (! $rider->canAcceptOrders()) {
return response()->json([
'success' => false,
'message' => 'Rider is not available',
], 422);
}
$delivery->assignRider($rider);
// Update Firebase tracking
// Check if Firebase tracking module is enabled
if (config('restaurant-delivery.firebase.enabled')) {
// Run Firebase tracking
$this->trackingService->initializeDeliveryTracking($delivery->fresh());
}
return response()->json([
'success' => true,
'message' => 'Rider assigned successfully',
'data' => new DeliveryResource($delivery->fresh(['rider'])),
]);
}
/**
* Cancel delivery
*/
public function cancel(Request $request, Delivery $delivery): JsonResponse
{
$request->validate([
'reason' => 'required|string|max:255',
'cancelled_by' => 'required|string|in:customer,restaurant,rider,admin',
'notes' => 'nullable|string',
]);
$success = $delivery->cancel(
$request->reason,
$request->cancelled_by,
$request->notes
);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Cannot cancel this delivery',
], 422);
}
// End tracking
$this->trackingService->endDeliveryTracking($delivery);
return response()->json([
'success' => true,
'message' => 'Delivery cancelled successfully',
]);
}
/**
* Mark delivery as delivered
*/
public function markDelivered(Request $request, Delivery $delivery): JsonResponse
{
$request->validate([
'photo' => 'nullable|string',
'signature' => 'nullable|string',
'recipient_name' => 'nullable|string|max:100',
]);
$success = $delivery->markDelivered([
'photo' => $request->photo,
'signature' => $request->signature,
'recipient_name' => $request->recipient_name,
]);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Cannot mark this delivery as delivered',
], 422);
}
// End tracking
$this->trackingService->endDeliveryTracking($delivery);
return response()->json([
'success' => true,
'message' => 'Delivery completed successfully',
'data' => new DeliveryResource($delivery->fresh()),
]);
}
/**
* Get delivery tracking info
*/
public function tracking(string $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
if (! $delivery->isTrackable()) {
return response()->json([
'success' => false,
'message' => 'Tracking not available for this delivery',
], 404);
}
$trackingData = $this->trackingService->getDeliveryTracking($delivery);
return response()->json([
'success' => true,
'data' => new DeliveryTrackingResource($delivery, $trackingData),
]);
}
/**
* Public tracking by tracking code
*/
public function publicTracking(string $trackingCode): JsonResponse
{
$delivery = Delivery::where('tracking_code', $trackingCode)->first();
if (! $delivery) {
return response()->json([
'success' => false,
'message' => 'Delivery not found',
], 404);
}
$trackingData = null;
if ($delivery->isTrackable()) {
$trackingData = $this->trackingService->getDeliveryTracking($delivery);
}
return response()->json([
'success' => true,
'data' => [
'tracking_code' => $delivery->tracking_code,
'status' => $delivery->status->value,
'status_label' => $delivery->status->label(),
'status_description' => $delivery->status->description(),
'status_color' => $delivery->status->color(),
'estimated_delivery' => $delivery->estimated_delivery_time?->format('H:i'),
'tracking' => $trackingData,
'timeline' => DeliveryStatusHistory::getTimeline($delivery->id),
],
]);
}
/**
* Get delivery status history
*/
public function statusHistory($uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
return response()->json([
'success' => true,
'data' => DeliveryStatusHistory::getTimeline($delivery->id),
]);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\JsonResponse;
use Modules\RestaurantDelivery\Http\Requests\DeliveryZone\DeliveryZoneStoreRequest;
use Modules\RestaurantDelivery\Http\Requests\DeliveryZone\DeliveryZoneUpdateRequest;
use Modules\RestaurantDelivery\Repositories\DeliveryZoneRepository;
class DeliveryZoneController extends Controller
{
public function __construct(private DeliveryZoneRepository $repo) {}
public function index(): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getAll(request()->all()), 'DeliveryZone has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function store(DeliveryZoneStoreRequest $request): JsonResponse
{
try {
return $this->responseSuccess($this->repo->create($request->all()), 'DeliveryZone has been created successfully.');
} catch (\Illuminate\Database\QueryException $exception) {
return $this->responseError([], 'Database error: '.$exception->getMessage());
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function show($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getById($id), 'DeliveryZone has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function update(DeliveryZoneUpdateRequest $request, $id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->update($id, $request->all()), 'DeliveryZone has been updated successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function destroy($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->delete($id), 'DeliveryZone has been deleted successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
}

View File

@@ -0,0 +1,271 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\RestaurantDelivery\Models\Delivery;
use Modules\RestaurantDelivery\Models\DeliveryRating;
use Modules\RestaurantDelivery\Services\Rating\RatingService;
class RatingController extends Controller
{
public function __construct(
protected RatingService $ratingService
) {}
/**
* Create a rating for a delivery
*/
public function store(Request $request, $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$validated = $request->validate([
'overall_rating' => 'required|integer|min:1|max:5',
'speed_rating' => 'nullable|integer|min:1|max:5',
'communication_rating' => 'nullable|integer|min:1|max:5',
'food_condition_rating' => 'nullable|integer|min:1|max:5',
'professionalism_rating' => 'nullable|integer|min:1|max:5',
'review' => 'nullable|string|max:500',
'tags' => 'nullable|array',
'tags.*' => 'string',
'is_anonymous' => 'nullable|boolean',
]);
$rating = $this->ratingService->createRating(
delivery: $delivery,
overallRating: $validated['overall_rating'],
categoryRatings: [
'speed' => $validated['speed_rating'] ?? null,
'communication' => $validated['communication_rating'] ?? null,
'food_condition' => $validated['food_condition_rating'] ?? null,
'professionalism' => $validated['professionalism_rating'] ?? null,
],
review: $validated['review'] ?? null,
tags: $validated['tags'] ?? [],
isAnonymous: $validated['is_anonymous'] ?? false,
customerId: auth()->id()
);
if (! $rating) {
return response()->json([
'success' => false,
'message' => 'Unable to submit rating. The delivery may have already been rated or the rating window has expired.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Thank you for your feedback!',
'data' => [
'id' => $rating->uuid,
'overall_rating' => $rating->overall_rating,
'star_display' => $rating->star_display,
],
], 201);
}
/**
* Create a restaurant rating for a delivery
*/
public function storeRestaurantRating(Request $request, $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$validated = $request->validate([
'overall_rating' => 'required|integer|min:1|max:5',
'review' => 'nullable|string|max:500',
'tags' => 'nullable|array',
]);
$rating = $this->ratingService->createRating(
delivery: $delivery,
overallRating: $validated['overall_rating'],
review: $validated['review'] ?? null,
tags: $validated['tags'] ?? [],
isRestaurantRating: true
);
if (! $rating) {
return response()->json([
'success' => false,
'message' => 'Unable to submit rating.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Rating submitted successfully',
], 201);
}
/**
* Check if delivery can be rated
*/
public function canRate($uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
return response()->json([
'success' => true,
'data' => [
'can_rate' => $this->ratingService->canRate($delivery),
'categories' => $this->ratingService->getCategories(),
'tags' => $this->ratingService->getTags(),
],
]);
}
/**
* Add rider response to a rating
*/
public function addRiderResponse(Request $request, $uuid): JsonResponse
{
$request->validate([
'response' => 'required|string|max:500',
]);
$rating = DeliveryRating::where('uuid', $uuid)->first();
// Verify the rider owns this rating
// if ($rating->rider_id !== auth()->user()->rider?->id) {
// return response()->json([
// 'success' => false,
// 'message' => 'Unauthorized',
// ], 403);
// }
$success = $this->ratingService->addRiderResponse($rating, $request->response);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'You have already responded to this rating',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Response added successfully',
]);
}
/**
* Mark rating as helpful
*/
public function markHelpful($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$rating->markHelpful();
return response()->json([
'success' => true,
'data' => [
'helpful_count' => $rating->helpful_count,
],
]);
}
/**
* Mark rating as not helpful
*/
public function markNotHelpful($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$rating->markNotHelpful();
return response()->json([
'success' => true,
'data' => [
'not_helpful_count' => $rating->not_helpful_count,
],
]);
}
/**
* List ratings (for admin)
*/
public function index(Request $request): JsonResponse
{
$query = DeliveryRating::with(['delivery', 'rider', 'customer']);
if ($request->has('rider_id')) {
$query->where('rider_id', $request->rider_id);
}
if ($request->has('moderation_status')) {
$query->where('moderation_status', $request->moderation_status);
}
if ($request->has('min_rating')) {
$query->where('overall_rating', '>=', $request->min_rating);
}
if ($request->has('max_rating')) {
$query->where('overall_rating', '<=', $request->max_rating);
}
$ratings = $query->orderBy('created_at', 'desc')
->paginate($request->get('per_page', 20));
return response()->json([
'success' => true,
'data' => $ratings,
]);
}
/**
* Approve a rating (admin)
*/
public function approve($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$this->ratingService->approveRating($rating);
return response()->json([
'success' => true,
'message' => 'Rating approved',
]);
}
/**
* Reject a rating (admin)
*/
public function reject(Request $request, $uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$request->validate([
'reason' => 'required|string|max:255',
]);
$this->ratingService->rejectRating($rating, $request->reason);
return response()->json([
'success' => true,
'message' => 'Rating rejected',
]);
}
/**
* Feature a rating (admin)
*/
public function feature($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$this->ratingService->featureRating($rating);
return response()->json([
'success' => true,
'message' => 'Rating featured',
]);
}
}

View File

@@ -0,0 +1,413 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Modules\Authentication\Models\User;
use Modules\RestaurantDelivery\Enums\RiderStatus;
use Modules\RestaurantDelivery\Http\Resources\DeliveryResource;
use Modules\RestaurantDelivery\Http\Resources\RiderResource;
use Modules\RestaurantDelivery\Models\Rider;
use Modules\RestaurantDelivery\Services\Earnings\EarningsService;
use Modules\RestaurantDelivery\Services\Firebase\FirebaseService;
use Modules\RestaurantDelivery\Services\Rating\RatingService;
use Modules\RestaurantDelivery\Services\Tracking\LiveTrackingService;
use Spatie\Permission\Models\Role;
class RiderController extends Controller
{
public function __construct(
protected LiveTrackingService $trackingService,
protected FirebaseService $firebase,
protected EarningsService $earningsService,
protected RatingService $ratingService
) {}
/**
* List riders
*/
public function index(Request $request): JsonResponse
{
$query = Rider::query();
// Filter by status
if ($request->has('status')) {
$query->where('status', $request->status);
}
// Filter by type
if ($request->has('type')) {
$query->where('type', $request->type);
}
// Filter by online status
if ($request->has('is_online')) {
$query->where('is_online', $request->boolean('is_online'));
}
// Filter by verified
if ($request->has('is_verified')) {
$query->where('is_verified', $request->boolean('is_verified'));
}
// Filter by vehicle type
if ($request->has('vehicle_type')) {
$query->where('vehicle_type', $request->vehicle_type);
}
$riders = $query->with('user')->orderBy('created_at', 'desc')
->paginate($request->get('per_page', 20));
return response()->json([
'success' => true,
'message' => 'Rider has been fetch!',
'data' => RiderResource::collection($riders),
'meta' => [
'current_page' => $riders->currentPage(),
'last_page' => $riders->lastPage(),
'per_page' => $riders->perPage(),
'total' => $riders->total(),
],
], 201);
}
/**
* Get nearby riders
*/
public function nearby(Request $request): JsonResponse
{
$request->validate([
'latitude' => 'required|numeric',
'longitude' => 'required|numeric',
'radius' => 'nullable|numeric|min:0.1|max:50',
]);
$radius = $request->get('radius', config('restaurant-delivery.assignment.assignment_radius'));
$riders = Rider::available()
->nearby($request->latitude, $request->longitude, $radius)
->limit(20)
->get();
return $this->responseSuccess(RiderResource::collection($riders), 'NearBy Rider has been fetched successfully.');
}
/**
* Create a new rider
*/
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'first_name' => 'required|string|max:100',
'last_name' => 'required|string|max:100',
'phone' => 'required|string|unique:restaurant_riders,phone',
'email' => 'nullable|email|unique:restaurant_riders,email',
'type' => 'required|in:internal,freelance,third_party',
'vehicle_type' => 'required|in:bicycle,motorcycle,car,van',
'vehicle_number' => 'nullable|string|max:20',
'commission_type' => 'nullable|in:fixed,percentage,per_km,hybrid',
'commission_rate' => 'nullable|numeric|min:0',
]);
$rider = Rider::create(array_merge($validated, [
'status' => RiderStatus::PENDING->value,
]));
// Delivery Man -> User Create
$restaurantDeliveryManRole = Role::updateOrCreate(['name' => 'Delivery Man', 'restaurant_id' => null, 'guard_name' => 'web']);
$user = $this->createUser($restaurantDeliveryManRole, $rider->first_name, $rider->email, $rider->phone, '123456', getUserRestaurantId());
$rider->update([
'user_id' => $user->id,
]);
return $this->responseSuccess(new RiderResource($rider), 'Rider registered successfully.');
}
/**
* Get rider details
*/
public function show($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
return $this->responseSuccess(new RiderResource($rider->load(['activeDeliveries'])), 'Rider active deliveries fetch successfully.');
}
/**
* Update rider
*/
public function update(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$validated = $request->validate([
'first_name' => 'sometimes|string|max:100',
'last_name' => 'sometimes|string|max:100',
'email' => 'sometimes|nullable|email',
'vehicle_type' => 'sometimes|in:bicycle,motorcycle,car,van',
'vehicle_number' => 'sometimes|nullable|string|max:20',
'vehicle_model' => 'sometimes|nullable|string|max:100',
'vehicle_color' => 'sometimes|nullable|string|max:50',
]);
$rider->update($validated);
return $this->responseSuccess(new RiderResource($rider->fresh()), 'Rider updated successfully.');
}
/**
* Update rider location
*/
public function updateLocation(Request $request, Rider $rider): JsonResponse
{
$validated = $request->validate([
'latitude' => 'required|numeric|between:-90,90',
'longitude' => 'required|numeric|between:-180,180',
'speed' => 'nullable|numeric|min:0',
'bearing' => 'nullable|numeric|between:0,360',
'accuracy' => 'nullable|numeric|min:0',
]);
$result = $this->trackingService->updateRiderLocation(
$rider,
$validated['latitude'],
$validated['longitude'],
$validated['speed'] ?? null,
$validated['bearing'] ?? null,
$validated['accuracy'] ?? null
);
return $this->responseSuccess($result, 'Update location successfully.');
}
/**
* Toggle rider online status
*/
public function toggleOnline(Request $request, Rider $rider): JsonResponse
{
$request->validate([
'is_online' => 'required|boolean',
]);
if ($request->is_online) {
if (! $rider->canAcceptOrders()) {
return response()->json([
'success' => false,
'message' => 'Cannot go online. Please ensure your account is active and verified.',
], 422);
}
$rider->goOnline();
// Update Firebase status
$this->firebase->updateRiderStatus($rider->id, 'online');
} else {
// Check for active deliveries
if ($rider->activeDeliveries()->exists()) {
return response()->json([
'success' => false,
'message' => 'Cannot go offline while having active deliveries.',
], 422);
}
$rider->goOffline();
// Update Firebase status
$this->firebase->updateRiderStatus($rider->id, 'offline');
}
return $this->responseSuccess([
'is_online' => $rider->is_online,
], $rider->is_online ? 'You are now online' : 'You are now offline');
}
/**
* Get rider's active deliveries
*/
public function activeDeliveries($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$deliveries = $rider->activeDeliveries()
->with(['zone'])
->orderBy('created_at', 'desc')
->get();
return $this->responseSuccess(DeliveryResource::collection($deliveries), 'Active Deliveries Fetch successfully.');
}
/**
* Get rider's delivery history
*/
public function deliveryHistory(Request $request, Rider $rider): JsonResponse
{
$query = $rider->deliveries()
->completed()
->with(['rating']);
if ($request->has('from_date')) {
$query->whereDate('delivered_at', '>=', $request->from_date);
}
if ($request->has('to_date')) {
$query->whereDate('delivered_at', '<=', $request->to_date);
}
$deliveries = $query->orderBy('delivered_at', 'desc')
->paginate($request->get('per_page', 20));
return $this->responseSuccess([
'data' => DeliveryResource::collection($deliveries),
'meta' => [
'current_page' => $deliveries->currentPage(),
'last_page' => $deliveries->lastPage(),
'per_page' => $deliveries->perPage(),
'total' => $deliveries->total(),
],
], 'Delivery History Fetch successfully.');
}
/**
* Get rider earnings
*/
public function earnings(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$period = $request->get('period', 'today');
return $this->responseSuccess($this->earningsService->getRiderEarningsSummary($rider, $period), 'Rider Earning Fetch successfully.');
}
/**
* Get rider earnings history
*/
public function earningsHistory(Request $request, Rider $rider): JsonResponse
{
$type = $request->get('type');
$limit = $request->get('limit', 50);
return $this->responseSuccess($this->earningsService->getEarningsHistory($rider, $limit, $type), 'Rider Earning History Fetch successfully.');
}
/**
* Get rider payout history
*/
public function payouts(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
return $this->responseSuccess($this->earningsService->getPayoutHistory($rider), 'Rider Payout History Fetch successfully.');
}
/**
* Get rider ratings and stats
*/
public function ratings($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
return $this->responseSuccess($this->ratingService->getRiderStats($rider), 'Rider Ratings Fetch successfully.');
}
/**
* Update FCM token
*/
public function updateFcmToken(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$request->validate([
'fcm_token' => 'required|string',
'device_id' => 'nullable|string',
]);
$rider->update([
'fcm_token' => $request->fcm_token,
'device_id' => $request->device_id,
]);
return $this->responseSuccess($rider, 'FCM token updated.');
}
/**
* Verify rider
*/
public function verify(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$request->validate([
'verified_by' => 'required|string',
]);
$rider->update([
'is_verified' => true,
'verified_at' => now(),
'verified_by' => $request->verified_by,
'status' => 'active',
]);
return $this->responseSuccess(new RiderResource($rider->fresh()), 'Rider verified successfully.');
}
/**
* Suspend rider
*/
public function suspend(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$request->validate([
'reason' => 'required|string|max:255',
]);
$rider->update(['status' => 'suspended']);
$rider->goOffline();
return $this->responseSuccess(new RiderResource($rider), 'Rider suspended.');
}
/**
* Reactivate rider
*/
public function reactivate($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$rider->update(['status' => 'active']);
return $this->responseSuccess(new RiderResource($rider->fresh()), 'Rider reactivated.');
}
public function createUser(Role $role, string $name, string $email, string $phone, string $password, ?int $restaurantId = null)
{
$user = User::factory([
'first_name' => $name,
'email' => $email,
'email_verified_at' => now(),
'password' => Hash::make($password),
'phone' => $phone,
'user_type' => $role->name,
'role_id' => $role->id,
'remember_token' => Str::random(10),
'platform' => 'WEB',
'address' => 'Dhaka, Bangladesh',
'status' => 1,
'created_by' => 1,
'restaurant_id' => $restaurantId,
'created_at' => now(),
'updated_at' => now(),
])->create();
/* Get all the permission and assign admin role */
$user->assignRole($role);
return $user;
}
}

View File

@@ -0,0 +1,205 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\RestaurantDelivery\Models\Delivery;
use Modules\RestaurantDelivery\Models\DeliveryTip;
use Modules\RestaurantDelivery\Services\Tip\TipService;
class TipController extends Controller
{
public function __construct(
protected TipService $tipService
) {}
/**
* Get tip options for a delivery
*/
public function options($uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$canTipPre = $this->tipService->canTipPreDelivery($delivery);
$canTipPost = $this->tipService->canTipPostDelivery($delivery);
if (! $canTipPre && ! $canTipPost) {
return response()->json([
'success' => false,
'message' => 'Tipping is not available for this delivery',
], 422);
}
return response()->json([
'success' => true,
'data' => array_merge(
$this->tipService->getTipOptions($delivery),
[
'can_tip_pre_delivery' => $canTipPre,
'can_tip_post_delivery' => $canTipPost,
]
),
]);
}
/**
* Create a pre-delivery tip
*/
public function createPreDeliveryTip(Request $request, Delivery $delivery): JsonResponse
{
$validated = $request->validate([
'amount' => 'required|numeric|min:1',
'calculation_type' => 'required|in:fixed,percentage',
'percentage_value' => 'required_if:calculation_type,percentage|nullable|numeric|min:1|max:50',
'message' => 'nullable|string|max:200',
]);
$tip = $this->tipService->createPreDeliveryTip(
delivery: $delivery,
amount: $validated['amount'],
customerId: auth()->id(),
calculationType: $validated['calculation_type'],
percentageValue: $validated['percentage_value'] ?? null,
message: $validated['message'] ?? null
);
if (! $tip) {
return response()->json([
'success' => false,
'message' => 'Unable to add tip. This delivery may not be eligible for tipping.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Tip added successfully',
'data' => [
'id' => $tip->uuid,
'amount' => $tip->amount,
'rider_amount' => $tip->rider_amount,
'type' => $tip->type,
],
], 201);
}
/**
* Create a post-delivery tip
*/
public function createPostDeliveryTip(Request $request, Delivery $delivery): JsonResponse
{
$validated = $request->validate([
'amount' => 'required|numeric|min:1',
'calculation_type' => 'required|in:fixed,percentage',
'percentage_value' => 'required_if:calculation_type,percentage|nullable|numeric|min:1|max:50',
'message' => 'nullable|string|max:200',
]);
$tip = $this->tipService->createPostDeliveryTip(
delivery: $delivery,
amount: $validated['amount'],
customerId: auth()->id(),
calculationType: $validated['calculation_type'],
percentageValue: $validated['percentage_value'] ?? null,
message: $validated['message'] ?? null
);
if (! $tip) {
return response()->json([
'success' => false,
'message' => 'Unable to add tip. The tipping window may have expired.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Thank you for the tip!',
'data' => [
'id' => $tip->uuid,
'amount' => $tip->amount,
'rider_amount' => $tip->rider_amount,
'type' => $tip->type,
],
], 201);
}
/**
* Process tip payment
*/
public function processPayment(Request $request, DeliveryTip $tip): JsonResponse
{
$validated = $request->validate([
'payment_reference' => 'required|string',
'payment_method' => 'required|string',
]);
$success = $this->tipService->markTipAsPaid(
$tip,
$validated['payment_reference'],
$validated['payment_method']
);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Failed to process tip payment',
], 500);
}
return response()->json([
'success' => true,
'message' => 'Tip payment processed successfully',
]);
}
/**
* Get tip details
*/
public function show($uuid): JsonResponse
{
$tip = DeliveryTip::where('uuid', $uuid)->first();
return response()->json([
'success' => true,
'data' => [
'id' => $tip->uuid,
'amount' => $tip->amount,
'currency' => $tip->currency,
'type' => $tip->type,
'calculation_type' => $tip->calculation_type,
'rider_amount' => $tip->rider_amount,
'payment_status' => $tip->payment_status,
'message' => $tip->message,
'created_at' => $tip->created_at->toIso8601String(),
],
]);
}
/**
* Calculate tip amount from percentage
*/
public function calculate(Request $request): JsonResponse
{
$validated = $request->validate([
'order_value' => 'required|numeric|min:0',
'percentage' => 'required|numeric|min:1|max:50',
]);
$amount = $this->tipService->calculateTipFromPercentage(
$validated['order_value'],
$validated['percentage']
);
return response()->json([
'success' => true,
'data' => [
'amount' => $amount,
'currency' => config('restaurant-delivery.pricing.currency'),
'currency_symbol' => config('restaurant-delivery.pricing.currency_symbol'),
],
]);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\JsonResponse;
use Modules\RestaurantDelivery\Http\Requests\ZonePricingRule\ZonePricingRuleStoreRequest;
use Modules\RestaurantDelivery\Http\Requests\ZonePricingRule\ZonePricingRuleUpdateRequest;
use Modules\RestaurantDelivery\Repositories\ZonePricingRuleRepository;
class ZonePricingRuleController extends Controller
{
public function __construct(private ZonePricingRuleRepository $repo) {}
public function index(): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getAll(request()->all()), 'ZonePricingRule has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function store(ZonePricingRuleStoreRequest $request): JsonResponse
{
try {
return $this->responseSuccess($this->repo->create($request->all()), 'ZonePricingRule has been created successfully.');
} catch (\Illuminate\Database\QueryException $exception) {
return $this->responseError([], 'Database error: '.$exception->getMessage());
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function show($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getById($id), 'ZonePricingRule has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function update(ZonePricingRuleUpdateRequest $request, $id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->update($id, $request->all()), 'ZonePricingRule has been updated successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function destroy($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->delete($id), 'ZonePricingRule has been deleted successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AssignRiderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'rider_id' => ['required', 'integer', 'exists:restaurant_riders,id'],
'force_assign' => ['boolean'],
'notes' => ['nullable', 'string', 'max:255'],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'rider_id' => 'rider',
'force_assign' => 'force assignment',
];
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'force_assign' => $this->boolean('force_assign'),
]);
}
}

View File

@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
/**
* CreateDeliveryRequest
*
* IMPORTANT: restaurant_id is NEVER accepted from request input.
* It is always obtained via getUserRestaurantId() in the controller.
*/
class CreateDeliveryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
// User must be authenticated and have a restaurant_id
return auth()->check() && getUserRestaurantId() !== null;
}
/**
* Get the validation rules that apply to the request.
*
* NOTE: restaurant_id is NOT in this list - it comes from getUserRestaurantId()
*/
public function rules(): array
{
return [
// Order Info
'orderable_type' => ['nullable', 'string', 'max:255'],
'orderable_id' => ['nullable', 'integer'],
'zone_id' => ['nullable', 'integer', 'exists:restaurant_delivery_zones,id'],
// Pickup Details
'restaurant_name' => ['required', 'string', 'max:255'],
'pickup_address' => ['required', 'string', 'max:500'],
'pickup_latitude' => ['required', 'numeric', 'between:-90,90'],
'pickup_longitude' => ['required', 'numeric', 'between:-180,180'],
'pickup_contact_name' => ['nullable', 'string', 'max:100'],
'pickup_contact_phone' => ['nullable', 'string', 'max:20'],
'pickup_instructions' => ['nullable', 'string', 'max:500'],
// Drop-off Details
'customer_name' => ['required', 'string', 'max:100'],
'drop_address' => ['required', 'string', 'max:500'],
'drop_latitude' => ['required', 'numeric', 'between:-90,90'],
'drop_longitude' => ['required', 'numeric', 'between:-180,180'],
'drop_contact_name' => ['nullable', 'string', 'max:100'],
'drop_contact_phone' => ['required', 'string', 'max:20'],
'drop_instructions' => ['nullable', 'string', 'max:500'],
'drop_floor' => ['nullable', 'string', 'max:50'],
'drop_apartment' => ['nullable', 'string', 'max:50'],
// Scheduling
'is_scheduled' => ['boolean'],
'scheduled_for' => ['nullable', 'required_if:is_scheduled,true', 'date', 'after:now'],
'is_priority' => ['boolean'],
// Order Value
'order_value' => ['nullable', 'numeric', 'min:0'],
// Pre-delivery Tip
'tip_amount' => ['nullable', 'numeric', 'min:0'],
'tip_type' => ['nullable', Rule::in(['amount', 'percentage'])],
// Additional Metadata
'meta' => ['nullable', 'array'],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'pickup_latitude' => 'pickup location latitude',
'pickup_longitude' => 'pickup location longitude',
'drop_latitude' => 'delivery location latitude',
'drop_longitude' => 'delivery location longitude',
'drop_contact_phone' => 'customer phone',
'scheduled_for' => 'scheduled delivery time',
];
}
/**
* Get the error messages for the defined validation rules.
*/
public function messages(): array
{
return [
'pickup_latitude.between' => 'The pickup latitude must be between -90 and 90.',
'pickup_longitude.between' => 'The pickup longitude must be between -180 and 180.',
'drop_latitude.between' => 'The delivery latitude must be between -90 and 90.',
'drop_longitude.between' => 'The delivery longitude must be between -180 and 180.',
'scheduled_for.after' => 'The scheduled delivery time must be in the future.',
'scheduled_for.required_if' => 'The scheduled delivery time is required when scheduling a delivery.',
];
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'is_scheduled' => $this->boolean('is_scheduled'),
'is_priority' => $this->boolean('is_priority'),
]);
}
/**
* Get validated data with restaurant_id from getUserRestaurantId().
*/
public function validatedWithRestaurant(): array
{
return array_merge($this->validated(), [
'restaurant_id' => getUserRestaurantId(),
]);
}
}

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateRatingRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$config = config('restaurant-delivery.rating');
$minRating = $config['min_rating'] ?? 1;
$maxRating = $config['max_rating'] ?? 5;
$maxReviewLength = $config['review_max_length'] ?? 500;
return [
'overall_rating' => ['required', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'speed_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'communication_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'food_condition_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'professionalism_rating' => ['nullable', 'integer', "min:{$minRating}", "max:{$maxRating}"],
'review' => ['nullable', 'string', "max:{$maxReviewLength}"],
'tags' => ['nullable', 'array'],
'tags.*' => ['string', 'max:50'],
'is_anonymous' => ['boolean'],
'is_restaurant_rating' => ['boolean'],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'overall_rating' => 'overall rating',
'speed_rating' => 'speed rating',
'communication_rating' => 'communication rating',
'food_condition_rating' => 'food condition rating',
'professionalism_rating' => 'professionalism rating',
];
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'is_anonymous' => $this->boolean('is_anonymous'),
'is_restaurant_rating' => $this->boolean('is_restaurant_rating'),
]);
}
/**
* Get validated category ratings as array.
*/
public function categoryRatings(): array
{
return array_filter([
'speed' => $this->validated('speed_rating'),
'communication' => $this->validated('communication_rating'),
'food_condition' => $this->validated('food_condition_rating'),
'professionalism' => $this->validated('professionalism_rating'),
]);
}
}

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class CreateTipRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$config = config('restaurant-delivery.tip');
$minTip = $config['min_tip'] ?? 10;
$maxTip = $config['max_tip'] ?? 1000;
$maxPercentage = $config['max_percentage'] ?? 50;
return [
'amount' => ['required', 'numeric', "min:{$minTip}", "max:{$maxTip}"],
'calculation_type' => ['nullable', Rule::in(['fixed', 'percentage'])],
'percentage_value' => [
'nullable',
'required_if:calculation_type,percentage',
'numeric',
'min:1',
"max:{$maxPercentage}",
],
'message' => ['nullable', 'string', 'max:255'],
'type' => ['nullable', Rule::in(['pre_delivery', 'post_delivery'])],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'amount' => 'tip amount',
'calculation_type' => 'calculation type',
'percentage_value' => 'percentage',
];
}
/**
* Get the error messages for the defined validation rules.
*/
public function messages(): array
{
return [
'percentage_value.required_if' => 'The percentage value is required when using percentage calculation.',
];
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
if (! $this->has('calculation_type')) {
$this->merge(['calculation_type' => 'fixed']);
}
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Modules\RestaurantDelivery\Http\Requests\DeliveryZone;
use Illuminate\Foundation\Http\FormRequest;
class DeliveryZoneStoreRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'slug' => 'required|string|max:255|unique:restaurant_delivery_zones,slug',
'description' => 'nullable|string',
'color' => ['nullable', 'regex:/^#[A-Fa-f0-9]{6}$/'],
'coordinates' => 'required|array',
'coordinates.type' => 'required|in:Polygon',
'coordinates.coordinates' => 'required|array|min:1',
'coordinates.coordinates.0' => 'required|array|min:4',
'coordinates.coordinates.0.*' => 'required|array|size:2',
'coordinates.coordinates.0.*.0' => 'required|numeric|between:-180,180',
'coordinates.coordinates.0.*.1' => 'required|numeric|between:-90,90',
'priority' => 'nullable|integer|min:0',
'is_active' => 'nullable|boolean',
'is_default' => 'nullable|boolean',
'max_delivery_distance' => 'nullable|numeric|min:0',
'operating_hours' => 'nullable|array',
'operating_hours.*.open' => 'nullable|date_format:H:i',
'operating_hours.*.close' => 'nullable|date_format:H:i',
'operating_hours.*.enabled' => 'required|boolean',
];
}
public function messages(): array
{
return [
'coordinates.required' => 'Zone polygon is required',
'coordinates.coordinates.0.min' => 'Polygon must contain at least 4 points',
'coordinates.coordinates.0.*.size' => 'Each coordinate must have longitude and latitude',
];
}
public function authorize(): bool
{
return true;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Modules\RestaurantDelivery\Http\Requests\DeliveryZone;
use Illuminate\Foundation\Http\FormRequest;
class DeliveryZoneUpdateRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'color' => ['nullable', 'regex:/^#[A-Fa-f0-9]{6}$/'],
'coordinates' => 'required|array',
'coordinates.type' => 'required|in:Polygon',
'coordinates.coordinates' => 'required|array|min:1',
'coordinates.coordinates.0' => 'required|array|min:4',
'coordinates.coordinates.0.*' => 'required|array|size:2',
'coordinates.coordinates.0.*.0' => 'required|numeric|between:-180,180',
'coordinates.coordinates.0.*.1' => 'required|numeric|between:-90,90',
'priority' => 'nullable|integer|min:0',
'is_active' => 'nullable|boolean',
'is_default' => 'nullable|boolean',
'max_delivery_distance' => 'nullable|numeric|min:0',
'operating_hours' => 'nullable|array',
'operating_hours.*.open' => 'nullable|date_format:H:i',
'operating_hours.*.close' => 'nullable|date_format:H:i',
'operating_hours.*.enabled' => 'required|boolean',
];
}
public function messages(): array
{
return [
'coordinates.required' => 'Zone polygon is required',
'coordinates.coordinates.0.min' => 'Polygon must contain at least 4 points',
'coordinates.coordinates.0.*.size' => 'Each coordinate must have longitude and latitude',
];
}
public function authorize(): bool
{
return true;
}
}

View File

@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\RestaurantDelivery\Enums\DeliveryStatus;
class UpdateDeliveryStatusRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'status' => [
'required',
'string',
Rule::in(array_column(DeliveryStatus::cases(), 'value')),
],
'changed_by' => [
'nullable',
'string',
Rule::in(['customer', 'restaurant', 'rider', 'admin', 'system', 'api']),
],
'notes' => ['nullable', 'string', 'max:500'],
'latitude' => ['nullable', 'numeric', 'between:-90,90'],
'longitude' => ['nullable', 'numeric', 'between:-180,180'],
// For delivery proof
'photo' => ['nullable', 'string'],
'signature' => ['nullable', 'string'],
'recipient_name' => ['nullable', 'string', 'max:100'],
// For cancellation
'cancellation_reason' => [
'nullable',
'required_if:status,cancelled',
'string',
'max:255',
],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'status' => 'delivery status',
'changed_by' => 'status changed by',
'cancellation_reason' => 'cancellation reason',
];
}
/**
* Get the error messages for the defined validation rules.
*/
public function messages(): array
{
return [
'status.in' => 'The selected status is invalid.',
'cancellation_reason.required_if' => 'A cancellation reason is required when cancelling a delivery.',
];
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRiderLocationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'latitude' => ['required', 'numeric', 'between:-90,90'],
'longitude' => ['required', 'numeric', 'between:-180,180'],
'speed' => ['nullable', 'numeric', 'min:0', 'max:500'],
'bearing' => ['nullable', 'numeric', 'min:0', 'max:360'],
'accuracy' => ['nullable', 'numeric', 'min:0'],
'altitude' => ['nullable', 'numeric'],
'timestamp' => ['nullable', 'date'],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'latitude' => 'location latitude',
'longitude' => 'location longitude',
'bearing' => 'direction bearing',
];
}
/**
* Get the error messages for the defined validation rules.
*/
public function messages(): array
{
return [
'latitude.between' => 'The latitude must be between -90 and 90 degrees.',
'longitude.between' => 'The longitude must be between -180 and 180 degrees.',
'bearing.max' => 'The bearing must be between 0 and 360 degrees.',
];
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateRiderProfileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$riderId = $this->route('rider')?->id;
return [
'first_name' => ['sometimes', 'string', 'max:100'],
'last_name' => ['sometimes', 'string', 'max:100'],
'email' => [
'sometimes',
'email',
'max:255',
Rule::unique('restaurant_riders', 'email')->ignore($riderId),
],
'phone' => [
'sometimes',
'string',
'max:20',
Rule::unique('restaurant_riders', 'phone')->ignore($riderId),
],
'photo_url' => ['nullable', 'string', 'url', 'max:500'],
'vehicle_type' => ['nullable', Rule::in(['bike', 'motorcycle', 'car', 'bicycle', 'scooter'])],
'vehicle_number' => ['nullable', 'string', 'max:50'],
'license_number' => ['nullable', 'string', 'max:50'],
'license_expiry' => ['nullable', 'date', 'after:today'],
'bank_name' => ['nullable', 'string', 'max:100'],
'bank_account_number' => ['nullable', 'string', 'max:50'],
'bank_branch' => ['nullable', 'string', 'max:100'],
'mobile_banking_provider' => ['nullable', 'string', 'max:50'],
'mobile_banking_number' => ['nullable', 'string', 'max:20'],
'preferred_payment_method' => ['nullable', Rule::in(['bank_transfer', 'bkash', 'nagad', 'rocket'])],
'preferred_zones' => ['nullable', 'array'],
'preferred_zones.*' => ['integer', 'exists:restaurant_delivery_zones,id'],
'emergency_contact_name' => ['nullable', 'string', 'max:100'],
'emergency_contact_phone' => ['nullable', 'string', 'max:20'],
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'first_name' => 'first name',
'last_name' => 'last name',
'photo_url' => 'profile photo',
'vehicle_type' => 'vehicle type',
'vehicle_number' => 'vehicle number',
'license_number' => 'license number',
'license_expiry' => 'license expiry date',
'bank_account_number' => 'bank account number',
'mobile_banking_provider' => 'mobile banking provider',
'mobile_banking_number' => 'mobile banking number',
'preferred_payment_method' => 'preferred payment method',
'preferred_zones' => 'preferred delivery zones',
'emergency_contact_name' => 'emergency contact name',
'emergency_contact_phone' => 'emergency contact phone',
];
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Modules\RestaurantDelivery\Http\Requests\ZonePricingRule;
use Illuminate\Foundation\Http\FormRequest;
class ZonePricingRuleStoreRequest extends FormRequest
{
public function rules(): array
{
return [
// validation rules
];
}
public function authorize(): bool
{
return true;
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Modules\RestaurantDelivery\Http\Requests\ZonePricingRule;
use Illuminate\Foundation\Http\FormRequest;
class ZonePricingRuleUpdateRequest extends FormRequest
{
public function rules(): array
{
return [
// validation rules
];
}
public function authorize(): bool
{
return true;
}
}

View File

@@ -0,0 +1,144 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class DeliveryResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->uuid,
'tracking_code' => $this->tracking_code,
// Status
'status' => $this->status,
'status_label' => $this->status->label(),
'status_description' => $this->status->description(),
'status_color' => $this->status->color(),
'is_active' => $this->status->isActive(),
'is_trackable' => $this->status->isTrackable(),
// Restaurant/Pickup
'pickup' => [
'restaurant_name' => $this->restaurant_name,
'address' => $this->pickup_address,
'latitude' => (float) $this->pickup_latitude,
'longitude' => (float) $this->pickup_longitude,
'contact_name' => $this->pickup_contact_name,
'contact_phone' => $this->pickup_contact_phone,
'instructions' => $this->pickup_instructions,
],
// Customer/Drop
'drop' => [
'customer_name' => $this->customer_name,
'address' => $this->drop_address,
'latitude' => (float) $this->drop_latitude,
'longitude' => (float) $this->drop_longitude,
'contact_name' => $this->drop_contact_name,
'contact_phone' => $this->drop_contact_phone,
'instructions' => $this->drop_instructions,
'floor' => $this->drop_floor,
'apartment' => $this->drop_apartment,
],
// Distance and Time
'distance' => [
'value' => (float) $this->distance,
'unit' => $this->distance_unit,
],
'estimated_duration' => $this->estimated_duration,
'estimated_pickup_time' => $this->estimated_pickup_time?->toIso8601String(),
'estimated_delivery_time' => $this->estimated_delivery_time?->toIso8601String(),
// Pricing
'pricing' => [
'base_fare' => (float) $this->base_fare,
'distance_charge' => (float) $this->distance_charge,
'surge_charge' => (float) $this->surge_charge,
'surge_multiplier' => (float) $this->surge_multiplier,
'peak_hour_charge' => (float) $this->peak_hour_charge,
'late_night_charge' => (float) $this->late_night_charge,
'small_order_fee' => (float) $this->small_order_fee,
'total' => (float) $this->total_delivery_charge,
'breakdown' => $this->charge_breakdown,
'currency' => config('restaurant-delivery.pricing.currency'),
'currency_symbol' => config('restaurant-delivery.pricing.currency_symbol'),
],
// Tip
'tip' => [
'amount' => (float) $this->tip_amount,
'type' => $this->tip_type,
'paid_at' => $this->tip_paid_at?->toIso8601String(),
],
// Rider
'rider' => $this->when($this->rider, fn () => [
'id' => $this->rider->uuid,
'name' => $this->rider->full_name,
'phone' => $this->rider->phone,
'photo' => $this->rider->photo_url,
'rating' => (float) $this->rider->rating,
'rating_count' => $this->rider->rating_count,
'vehicle_type' => $this->rider->vehicle_type,
'vehicle_number' => $this->rider->vehicle_number,
]),
// Zone
'zone' => $this->when($this->zone, fn () => [
'id' => $this->zone->uuid,
'name' => $this->zone->name,
]),
// Rating
'rating' => $this->when($this->rating, fn () => [
'overall' => $this->rating->overall_rating,
'star_display' => $this->rating->star_display,
'review' => $this->rating->review,
'created_at' => $this->rating->created_at->toIso8601String(),
]),
'can_rate' => $this->canBeRated(),
'can_tip' => $this->canReceiveTip(),
// Timestamps
'timestamps' => [
'created_at' => $this->created_at->toIso8601String(),
'food_ready_at' => $this->food_ready_at?->toIso8601String(),
'rider_assigned_at' => $this->rider_assigned_at?->toIso8601String(),
'picked_up_at' => $this->picked_up_at?->toIso8601String(),
'delivered_at' => $this->delivered_at?->toIso8601String() ?? null,
'cancelled_at' => $this->cancelled_at?->toIso8601String(),
],
// Cancellation
'cancellation' => $this->when($this->status->isCancelled(), fn () => [
'reason' => $this->cancellation_reason,
'cancelled_by' => $this->cancelled_by,
'notes' => $this->cancellation_notes,
]),
// Failure
'failure' => $this->when($this->status->isFailed(), fn () => [
'reason' => $this->failure_reason,
'notes' => $this->failure_notes,
]),
// Proof of delivery
'proof' => $this->when($this->status->isCompleted(), fn () => [
'photo' => $this->delivery_photo,
'signature' => $this->signature,
'recipient_name' => $this->recipient_name,
]),
// Priority
'is_priority' => $this->is_priority,
'is_scheduled' => $this->is_scheduled,
'scheduled_for' => $this->scheduled_for?->toIso8601String(),
];
}
}

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\RestaurantDelivery\Models\Delivery;
class DeliveryTrackingResource extends JsonResource
{
protected ?array $trackingData;
public function __construct(Delivery $delivery, ?array $trackingData = null)
{
parent::__construct($delivery);
$this->trackingData = $trackingData;
}
public function toArray($request): array
{
$delivery = $this->resource;
return [
'tracking_code' => $delivery->tracking_code,
// Status
'status' => [
'code' => $delivery->status->value,
'label' => $delivery->status->label(),
'description' => $delivery->status->description(),
'color' => $delivery->status->color(),
'icon' => $delivery->status->icon(),
],
// Restaurant location
'restaurant' => [
'name' => $delivery->restaurant_name,
'address' => $delivery->pickup_address,
'latitude' => (float) $delivery->pickup_latitude,
'longitude' => (float) $delivery->pickup_longitude,
],
// Customer location
'customer' => [
'address' => $delivery->drop_address,
'latitude' => (float) $delivery->drop_latitude,
'longitude' => (float) $delivery->drop_longitude,
],
// Rider info
'rider' => $delivery->rider ? [
'id' => $delivery->rider->uuid,
'name' => $delivery->rider->full_name,
'phone' => $delivery->rider->phone,
'photo' => $delivery->rider->photo_url,
'rating' => (float) $delivery->rider->rating,
'rating_count' => $delivery->rider->rating_count,
'vehicle_type' => $delivery->rider->vehicle_type->value,
'vehicle_number' => $delivery->rider->vehicle_number,
] : null,
// Live tracking data
'tracking' => $this->trackingData ? [
'rider_location' => $this->trackingData['rider_location'] ?? null,
'route' => $this->trackingData['route'] ?? null,
'eta' => $this->trackingData['eta'] ?? null,
'remaining_distance' => $this->trackingData['remaining_distance'] ?? null,
'animation' => $this->trackingData['animation'] ?? null,
] : null,
// Estimated times
'estimated_pickup' => $delivery->estimated_pickup_time?->format('H:i'),
'estimated_delivery' => $delivery->estimated_delivery_time?->format('H:i'),
// Distance
'distance' => [
'total' => (float) $delivery->distance,
'unit' => $delivery->distance_unit,
],
// Map config
'map_config' => [
'default_zoom' => config('restaurant-delivery.tracking.map.default_zoom'),
'tracking_zoom' => config('restaurant-delivery.tracking.map.tracking_zoom'),
'auto_center' => config('restaurant-delivery.tracking.map.auto_center'),
'show_route' => config('restaurant-delivery.tracking.map.show_route_polyline'),
'show_eta' => config('restaurant-delivery.tracking.map.show_eta'),
],
// Markers config
'markers' => config('restaurant-delivery.tracking.markers'),
// Polyline config
'polyline' => config('restaurant-delivery.tracking.polyline'),
// Animation config
'animation' => config('restaurant-delivery.tracking.animation'),
// Firebase config for client
'firebase_config' => [
'path' => "deliveries/{$delivery->id}/tracking",
'enabled' => config('restaurant-delivery.firebase.enabled'),
],
];
}
}

View File

@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class RiderResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->uuid,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'full_name' => $this->full_name,
'phone' => $this->phone,
'email' => $this->email,
'photo' => $this->photo_url,
// Type and Status
'type' => $this->type->value,
'type_label' => $this->type->label(),
'status' => $this->status->value,
'status_label' => $this->status->label(),
'is_verified' => $this->is_verified,
'is_online' => $this->is_online,
'user_info' => [
'restaurant_id' => $this->user?->restaurant_id,
'user_id' => $this->user?->id,
'email' => $this->user?->email,
'phone' => $this->user?->phone,
'user_type' => $this->user?->user_type,
'fcm_token' => $this->user?->fcm_token,
'address' => $this->user?->address,
],
// Vehicle
'vehicle' => [
'type' => $this->vehicle_type->value,
'type_label' => $this->vehicle_type->label(),
'number' => $this->vehicle_number,
'model' => $this->vehicle_model,
'color' => $this->vehicle_color,
],
// Location
'location' => $this->when($this->current_latitude && $this->current_longitude, fn () => [
'latitude' => (float) $this->current_latitude,
'longitude' => (float) $this->current_longitude,
'last_update' => $this->last_location_update?->toIso8601String(),
]),
// Distance (only when queried with nearby scope)
'distance' => $this->when(isset($this->distance), fn () => round($this->distance, 2)),
// Stats
'stats' => [
'rating' => (float) $this->rating,
'rating_count' => $this->rating_count,
'total_deliveries' => $this->total_deliveries,
'successful_deliveries' => $this->successful_deliveries,
'acceptance_rate' => (float) $this->acceptance_rate,
'completion_rate' => (float) $this->completion_rate,
],
// Commission
'commission' => $this->when($request->user()?->isAdmin ?? false, fn () => [
'type' => $this->commission_type->value,
'rate' => (float) $this->commission_rate,
'base' => $this->base_commission ? (float) $this->base_commission : null,
'per_km_rate' => $this->per_km_rate ? (float) $this->per_km_rate : null,
]),
// Active deliveries count
'active_deliveries_count' => $this->whenLoaded('activeDeliveries', fn () => $this->activeDeliveries->count()),
// Online status
'last_online_at' => $this->last_online_at?->toIso8601String(),
// Timestamps
'created_at' => $this->created_at->toIso8601String(),
'verified_at' => $this->verified_at?->toIso8601String(),
'verified_by' => $this->verified_by,
];
}
}