diff --git a/app/Http/Controllers/CarController.php b/app/Http/Controllers/CarController.php index c222112..5ba78e3 100644 --- a/app/Http/Controllers/CarController.php +++ b/app/Http/Controllers/CarController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Http\Requests\CarPostRequest; use App\Models\Car; use App\Models\User; +use App\Policies\CarPolicy; use App\Repositories\Interfaces\CarRepositoryInterface; use Illuminate\Http\RedirectResponse; use Illuminate\View\View; @@ -31,7 +32,7 @@ class CarController extends Controller */ public function create(User $user): View { - $this->authorize('create', $user); + $this->authorize('create', [Car::class, $user]); return view('cars.create', [ 'user' => $user, diff --git a/app/Http/Controllers/DeliveryController.php b/app/Http/Controllers/DeliveryController.php index c94fb34..b011446 100644 --- a/app/Http/Controllers/DeliveryController.php +++ b/app/Http/Controllers/DeliveryController.php @@ -13,7 +13,7 @@ use Illuminate\View\View; class DeliveryController extends Controller { public function __construct( - private readonly DeliveryRepositoryInterface $deliveryRepository + private readonly DeliveryRepositoryInterface $deliveryRepository, ){ } @@ -93,4 +93,20 @@ class DeliveryController extends Controller return redirect()->route('deliveries.index'); } + + public function startDelivery(Delivery $delivery): RedirectResponse + { + $this->authorize('startDelivery', $delivery); + $this->deliveryRepository->startDelivery($delivery); + + return redirect()->route('deliveries.index'); + } + + public function endDelivery(Delivery $delivery): RedirectResponse + { + $this->authorize('endDelivery', $delivery); + $this->deliveryRepository->endDelivery($delivery); + + return redirect()->route('deliveries.index'); + } } diff --git a/app/Http/Controllers/WarehouseController.php b/app/Http/Controllers/WarehouseController.php index a0a2e41..5ac1caa 100644 --- a/app/Http/Controllers/WarehouseController.php +++ b/app/Http/Controllers/WarehouseController.php @@ -64,7 +64,7 @@ class WarehouseController extends Controller { $this->authorize('update', $warehouse); - return view('warehouse.edit', [ + return view('warehouses.edit', [ 'warehouse' => $warehouse, ]); } diff --git a/app/Models/Delivery.php b/app/Models/Delivery.php index cfb3e40..c8e6e20 100644 --- a/app/Models/Delivery.php +++ b/app/Models/Delivery.php @@ -3,10 +3,10 @@ namespace App\Models; use App\Enums\StatusEnum; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Delivery extends Model { @@ -38,4 +38,12 @@ class Delivery extends Model { return $this->belongsTo(Warehouse::class); } + + public function scopeFilter(Builder $query): void + { + $address = request('address'); + $query->when($address, function (Builder $query, $address){ + $query->whereRaw('CONCAT (address) ilike ?', ["$address%"]); + }); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 34226fe..d9dc021 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Enums\RoleEnum; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -75,4 +76,16 @@ class User extends Authenticatable ); } + public function scopeFilter(Builder $query): void + { + $name = request('name'); + $query->when($name, function (Builder $query, $name) { + $query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (surname, \' \', patronymic, \' \', name) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (patronymic, \' \', name, \' \', surname) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (patronymic, \' \', surname, \' \', name) ilike ?', ["$name%"]); + })->paginate(10)->withQueryString(); + } } diff --git a/app/Policies/DeliveryPolicy.php b/app/Policies/DeliveryPolicy.php index d3a4931..10b975c 100644 --- a/app/Policies/DeliveryPolicy.php +++ b/app/Policies/DeliveryPolicy.php @@ -3,6 +3,7 @@ namespace App\Policies; use App\Enums\RoleEnum; +use App\Enums\StatusEnum; use App\Models\Delivery; use App\Models\User; @@ -11,7 +12,7 @@ class DeliveryPolicy public function view(User $user, Delivery $delivery) { if($user->role == RoleEnum::DRIVER) { - return $delivery->user_id == $user->id && $delivery->warehouse_id == $user->warehouse_id; + return $delivery->user_id == $user->id; } return $user->warehouse_id == $delivery->warehouse_id; @@ -32,4 +33,18 @@ class DeliveryPolicy { return $user->role == RoleEnum::ADMIN && $delivery->warehouse_id == $user->warehouse_id; } + + public function startDelivery(User $user, Delivery $delivery) + { + return $user->role == RoleEnum::DISPATCHER && + $delivery->warehouse_id == $user->warehouse_id && + $delivery->status == StatusEnum::CREATED; + } + + public function endDelivery(User $user, Delivery $delivery) + { + return $user->role == RoleEnum::DRIVER && + $delivery->warehouse_id == $user->warehouse_id && + $delivery->status == StatusEnum::SENT; + } } diff --git a/app/Repositories/SQL/CarRepository.php b/app/Repositories/SQL/CarRepository.php index e186dd5..c1809ba 100644 --- a/app/Repositories/SQL/CarRepository.php +++ b/app/Repositories/SQL/CarRepository.php @@ -10,7 +10,7 @@ class CarRepository implements CarRepositoryInterface public function getAllCars() { - return Car::paginate(5); + return Car::all(); } public function createCar(array $data) diff --git a/app/Repositories/SQL/DeliveryRepository.php b/app/Repositories/SQL/DeliveryRepository.php index f2f59f3..2567704 100644 --- a/app/Repositories/SQL/DeliveryRepository.php +++ b/app/Repositories/SQL/DeliveryRepository.php @@ -2,15 +2,29 @@ namespace App\Repositories\SQL; +use App\Enums\RoleEnum; +use App\Enums\StatusEnum; use App\Models\Delivery; use App\Repositories\Interfaces\DeliveryRepositoryInterface; +use Illuminate\Support\Facades\Date; class DeliveryRepository implements DeliveryRepositoryInterface { - public function getAllDeliveries() { - return Delivery::orderBy('status', 'asc')->paginate(1000); + if(auth()->user()->role == RoleEnum::DRIVER) { + return Delivery::where('user_id', auth()->user()->id) + ->orderBy('status', 'asc') + ->filter() + ->paginate(10) + ->withQueryString(); + } + + return Delivery::where('deliveries.warehouse_id', auth()->user()->warehouse_id) + ->orderBy('status', 'asc') + ->filter() + ->paginate(10) + ->withQueryString(); } public function createDelivery(array $data) @@ -27,4 +41,18 @@ class DeliveryRepository implements DeliveryRepositoryInterface { $delivery->delete(); } + + public function startDelivery(Delivery $delivery) + { + $delivery->status = StatusEnum::SENT; + $delivery->sending_date = Date::now(); + $delivery->update((array)$delivery); + } + + public function endDelivery(Delivery $delivery) + { + $delivery->status = StatusEnum::DELIVERED; + $delivery->delivery_date = Date::now(); + $delivery->update((array)$delivery); + } } diff --git a/app/Repositories/SQL/UserRepository.php b/app/Repositories/SQL/UserRepository.php index 0b8070c..6469b3a 100644 --- a/app/Repositories/SQL/UserRepository.php +++ b/app/Repositories/SQL/UserRepository.php @@ -9,7 +9,7 @@ class UserRepository implements UserRepositoryInterface { public function getAllUsers() { - return User::paginate(5); + return User::filter()->paginate(10)->withQueryString(); } public function createUser(array $data) diff --git a/resources/views/deliveries/index.blade.php b/resources/views/deliveries/index.blade.php index 6e8955a..78201c6 100644 --- a/resources/views/deliveries/index.blade.php +++ b/resources/views/deliveries/index.blade.php @@ -11,6 +11,20 @@ @endcan +
+
{{__('Поиск доставки')}}
+
+
+
+ + +
+
+ +
+
+
+
@if (count($deliveries))
{{__('Доставки')}}
@@ -41,6 +55,24 @@

{{ $delivery->status->description()}}

+ + @can('startDelivery', $delivery) +
+ @csrf + @method('PUT') + +
+ @endcan + + + @can('endDelivery', $delivery) +
+ @csrf + @method('PUT') + +
+ @endcan + @can('update', $delivery)
diff --git a/resources/views/users/index.blade.php b/resources/views/users/index.blade.php index d186741..4756c0e 100644 --- a/resources/views/users/index.blade.php +++ b/resources/views/users/index.blade.php @@ -11,6 +11,20 @@
@endcan +
+
{{__('Поиск пользователя')}}
+
+
+
+ + +
+
+ +
+
+
+
@if (count($users))
{{__('Пользователи')}}
diff --git a/resources/views/warehouses/index.blade.php b/resources/views/warehouses/index.blade.php index f064d4e..a1b6d61 100644 --- a/resources/views/warehouses/index.blade.php +++ b/resources/views/warehouses/index.blade.php @@ -49,7 +49,6 @@
- @endif diff --git a/routes/web.php b/routes/web.php index f9e3350..c8b252f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -32,6 +32,10 @@ Route::middleware('auth')->group(function () { 'warehouses' => WarehouseController::class, 'users.cars' => CarController::class, ]); + Route::put('/deliveries/{delivery}/start', [DeliveryController::class, 'startDelivery']) + ->name('deliveries.start'); + Route::put('/deliveries/{delivery}/end', [DeliveryController::class, 'endDelivery']) + ->name('deliveries.end'); }); require __DIR__.'/auth.php';