Fixed and added filtration

This commit is contained in:
m.zargarov 2024-01-30 11:21:30 +04:00
parent f3e8f6aa44
commit 57e2b24686
13 changed files with 140 additions and 10 deletions

View File

@ -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,

View File

@ -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');
}
}

View File

@ -64,7 +64,7 @@ class WarehouseController extends Controller
{
$this->authorize('update', $warehouse);
return view('warehouse.edit', [
return view('warehouses.edit', [
'warehouse' => $warehouse,
]);
}

View File

@ -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%"]);
});
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -10,7 +10,7 @@ class CarRepository implements CarRepositoryInterface
public function getAllCars()
{
return Car::paginate(5);
return Car::all();
}
public function createCar(array $data)

View File

@ -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);
}
}

View File

@ -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)

View File

@ -11,6 +11,20 @@
</div>
</div>
@endcan
<div class="card mt-4">
<div class="card-header">{{__('Поиск доставки')}}</div>
<div class="card-body">
<form action="{{ route('deliveries.index') }}" method="GET">
<div class="mb-3">
<label for="address" class="form-label">Адрес доставки:</label>
<input type="text" id="address" name="address" class="form-control" value="{{ request('address') ?? '' }}">
</div>
<div>
<button type="submit" class="btn btn-primary">Фильтр</button>
</div>
</form>
</div>
</div>
@if (count($deliveries))
<div class="card mt-4">
<div class="card-header">{{__('Доставки')}}</div>
@ -41,6 +55,24 @@
<p>{{ $delivery->status->description()}}</p>
</div>
</td>
<td>
@can('startDelivery', $delivery)
<form action="{{ route('deliveries.start', $delivery) }}" method="POST" style="display: inline-block;">
@csrf
@method('PUT')
<button type="submit" class="btn btn-primary">Отправить</button>
</form>
@endcan
</td>
<td>
@can('endDelivery', $delivery)
<form action="{{ route('deliveries.end', $delivery) }}" method="POST" style="display: inline-block;">
@csrf
@method('PUT')
<button type="submit" class="btn btn-primary">Завершить</button>
</form>
@endcan
</td>
<td>
@can('update', $delivery)
<div>

View File

@ -11,6 +11,20 @@
</div>
</div>
@endcan
<div class="card mt-4">
<div class="card-header">{{__('Поиск пользователя')}}</div>
<div class="card-body">
<form action="{{ route('users.index') }}" method="GET">
<div class="mb-3">
<label for="name" class="form-label">ФИО:</label>
<input type="text" id="name" name="name" class="form-control" value="{{ request('name') ?? '' }}">
</div>
<div>
<button type="submit" class="btn btn-primary">Фильтр</button>
</div>
</form>
</div>
</div>
@if (count($users))
<div class="card mt-4">
<div class="card-header">{{__('Пользователи')}}</div>

View File

@ -49,7 +49,6 @@
</tbody>
</table>
</div>
<div class="card-footer">{{ $warehouses->links() }}</div>
</div>
@endif
</div>

View File

@ -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';