189 lines
6.4 KiB
PHP
189 lines
6.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Bo;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Http\Requests\Bo\Shops\StoreShopRequest;
|
||
|
|
use App\Http\Requests\Bo\Shops\UpdateShopPickupExceptionsRequest;
|
||
|
|
use App\Http\Requests\Bo\Shops\UpdateShopRequest;
|
||
|
|
use App\Models\PickupException;
|
||
|
|
use App\Models\Shop;
|
||
|
|
use App\Services\DataTableService;
|
||
|
|
use Domain\Ports\Repositories\ShopRepository;
|
||
|
|
use Domain\Services\PickupSchedulingService;
|
||
|
|
use Illuminate\Contracts\Database\Query\Builder;
|
||
|
|
use Illuminate\Contracts\View\View;
|
||
|
|
use Illuminate\Http\RedirectResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
class ShopsController extends Controller
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private readonly PickupSchedulingService $pickupService,
|
||
|
|
private readonly ShopRepository $shopRepository,
|
||
|
|
) {
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index(Request $request, DataTableService $dataTable): View
|
||
|
|
{
|
||
|
|
$this->authorize('viewAny', Shop::class);
|
||
|
|
|
||
|
|
$query = Shop::query();
|
||
|
|
|
||
|
|
if ($request->has('pickup-exception')) {
|
||
|
|
request()->session()->forget('shops.filter.pickup-exception');
|
||
|
|
request()->session()->put(
|
||
|
|
'shops.filter.pickup-exception',
|
||
|
|
$request->input('pickup-exception')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
$pickupException = request()->session()->get('shops.filter.pickup-exception');
|
||
|
|
if (! is_null($pickupException)) {
|
||
|
|
$query->whereHas(
|
||
|
|
'pickupExceptions',
|
||
|
|
fn (Builder $q) => $q->where('id', $pickupException)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
$dataTable->sortQuery(Shop::class, $query);
|
||
|
|
|
||
|
|
$shops = $dataTable->paginateQuery($query);
|
||
|
|
$pickupExceptions = PickupException::query()->get();
|
||
|
|
|
||
|
|
return view('bo.shops.index', compact('shops', 'pickupExceptions'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create(): View
|
||
|
|
{
|
||
|
|
$this->authorize('create', Shop::class);
|
||
|
|
|
||
|
|
return view('bo.shops.create');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(StoreShopRequest $request): RedirectResponse
|
||
|
|
{
|
||
|
|
$this->authorize('create', Shop::class);
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($request) {
|
||
|
|
$shop = new Shop();
|
||
|
|
$shop->fill($request->only(
|
||
|
|
$shop->getFillable()
|
||
|
|
));
|
||
|
|
if ($shop->saveOrFail()) {
|
||
|
|
return to_route('bo.shops.edit', $shop)
|
||
|
|
->with('success', trans('crud.messages.has_been_created', [
|
||
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
|
||
|
|
return to_route('bo.shops.create')
|
||
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
||
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
||
|
|
]));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit(Shop $shop): View
|
||
|
|
{
|
||
|
|
$this->authorize('update', $shop);
|
||
|
|
|
||
|
|
return view('bo.shops.edit', compact('shop'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(UpdateShopRequest $request, Shop $shop): RedirectResponse
|
||
|
|
{
|
||
|
|
$this->authorize('update', $shop);
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($request, $shop) {
|
||
|
|
$shop->fill($request->only(
|
||
|
|
$shop->getFillable()
|
||
|
|
));
|
||
|
|
|
||
|
|
if ($shop->saveOrFail()) {
|
||
|
|
return to_route('bo.shops.edit', $shop)
|
||
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
||
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
|
||
|
|
return to_route('bo.shops.edit', $shop)
|
||
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
||
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
||
|
|
]));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(Shop $shop): RedirectResponse
|
||
|
|
{
|
||
|
|
$this->authorize('delete', $shop);
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($shop) {
|
||
|
|
if ($shop->deleteOrFail()) {
|
||
|
|
return to_route('bo.shops.index')
|
||
|
|
->with('warning', trans('crud.messages.has_been_deleted', ['model' => Str::ucfirst(trans_choice('models.classes.shops', 1))]));
|
||
|
|
}
|
||
|
|
|
||
|
|
return to_route('bo.shops.edit', $shop)
|
||
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
||
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
||
|
|
]));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function pickupExceptionsEdit(Shop $shop): View
|
||
|
|
{
|
||
|
|
$this->authorize('pickupExceptionUpdate', $shop);
|
||
|
|
|
||
|
|
$shop->load('pickupExceptions');
|
||
|
|
$domainShop = $this->shopRepository->find($shop->getId());
|
||
|
|
|
||
|
|
if ($domainShop === null) {
|
||
|
|
abort(404, 'Shop not found in domain');
|
||
|
|
}
|
||
|
|
|
||
|
|
$pickupExceptions = $this->pickupService->getEditablePickupExceptionsFor($domainShop);
|
||
|
|
$pickupDay = $this->pickupService->getPickupDay();
|
||
|
|
$pickupPeriod = $this->pickupService->getPeriodFromPickUpDay();
|
||
|
|
|
||
|
|
return view('bo.shops.pickup-exceptions', compact(
|
||
|
|
'shop',
|
||
|
|
'pickupExceptions',
|
||
|
|
'pickupDay',
|
||
|
|
'pickupPeriod',
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function pickupExceptionsUpdate(UpdateShopPickupExceptionsRequest $request, Shop $shop): RedirectResponse
|
||
|
|
{
|
||
|
|
$this->authorize('pickupExceptionUpdate', $shop);
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($request, $shop) {
|
||
|
|
$pickupExceptions = $request->array('pickup-exceptions');
|
||
|
|
$replacementDates = $request->array('replacement_date');
|
||
|
|
$orderDeadlines = $request->array('order_deadline_at');
|
||
|
|
|
||
|
|
$syncData = [];
|
||
|
|
|
||
|
|
foreach ($pickupExceptions as $exceptionId) {
|
||
|
|
$replacementDate = ! empty($replacementDates[$exceptionId]) ? $replacementDates[$exceptionId] : null;
|
||
|
|
$orderDeadline = ! empty($orderDeadlines[$exceptionId]) ? $orderDeadlines[$exceptionId] : null;
|
||
|
|
|
||
|
|
$syncData[$exceptionId] = [
|
||
|
|
'replacement_date' => $replacementDate,
|
||
|
|
'order_deadline_at' => $orderDeadline,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
$shop->pickupExceptions()->sync($syncData);
|
||
|
|
|
||
|
|
return back()->with('success', trans('crud.messages.has_been_updated', [
|
||
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
||
|
|
]));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|