Edit File: AssignOrderToDelegateController.php
<?php namespace App\Http\Controllers\Admin; use App\Enums\NotificationTypeEnum; use App\Enums\OrderStatusEnum; use App\Enums\ProductDeliveryTypesEnum; use App\Models\Delegate; use App\Models\Order; use App\Notifications\OrderNotification; use App\Services\Api\OrderService; use App\Traits\OrderTrait; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Notification; class AssignOrderToDelegateController extends AdminBasicController { use OrderTrait; public function __construct(private OrderService $orderService) { } public function index($id = null) { if (request()->ajax()) { $rows = $this->orderService->getOrderAccordingToStatusInDashboard( [OrderStatusEnum::Prepared], ['provider', 'user'], [ 'delegate_id' => null, 'receiving_method' => ProductDeliveryTypesEnum::Home->value, ] ); $html = view('admin.assign_delegate_orders.table', compact('rows'))->render(); return response()->json(['html' => $html]); } return view('admin.assign_delegate_orders.index'); } public function details($id) { $order = $this->orderService->showUnAssignedDelegatesOrdersInDashboard($id); $delegates = Delegate::with([ 'orders' => function ($q) { $q->whereIn('status', $this->orderStatusesForDelegate('current'))->first(); } ]) ->where('is_blocked', 0) ->whereRelation('neighborhoods', 'neighborhood_id', $order->neighborhood_id)->get(['id', 'name', 'lat', 'lng', 'image']); return view('admin.assign_delegate_orders.show', compact('order', 'delegates')); } public function assign($id, $delegate_id) { try { $order = Order::where([ 'id' => $id, 'delegate_id' => null, 'receiving_method' => ProductDeliveryTypesEnum::Home->value ]) ?->firstOrFail(); $order->update(['delegate_id' => $delegate_id]); DB::table('ignored_orders')->where('order_id', $id)->where('delegate_id', $delegate_id)->delete(); Notification::send($order?->delegate, new OrderNotification(order: $order, type: NotificationTypeEnum::Order_From_Admin->value)); return response()->json(['key' => 'success', 'msg' => __('admin.assign_success'), 'url' => route('admin.assign_delegate_orders.index')]); } catch (\Exception $exception) { return response()->json(['key' => 'error', 'msg' => __('admin.order_assigned_or_not_found'), 'url' => route('admin.assign_delegate_orders.index')]); } } }
Back to File Manager