210 lines
7.2 KiB
PHP
210 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Actions\Shops\CreateShop;
|
|
use App\Actions\Shops\CreateShopInput;
|
|
use App\Actions\Shops\DeleteShop;
|
|
use App\Actions\Shops\DeleteShopInput;
|
|
use App\Actions\Shops\UpdateShop;
|
|
use App\Actions\Shops\UpdateShopInput;
|
|
use App\Actions\Shops\UpdateShopPickupExceptions;
|
|
use App\Actions\Shops\UpdateShopPickupExceptionsInput;
|
|
use App\Contracts\Executor;
|
|
use App\Dto\ShopDto;
|
|
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\Shop;
|
|
use App\Queries\Shops\GetPaginatedShopsQuery;
|
|
use App\Queries\Shops\GetPaginatedShopsQueryInput;
|
|
use App\Queries\Shops\GetShopPickupExceptionsQuery;
|
|
use App\Queries\Shops\GetShopPickupExceptionsQueryInput;
|
|
use Domain\Models\ValueObjects\Coordinates;
|
|
use Domain\Models\ValueObjects\PhoneNumber;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ShopsController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly Executor $executor,
|
|
) {
|
|
}
|
|
|
|
public function index(Request $request, GetPaginatedShopsQuery $query): View
|
|
{
|
|
$this->authorize('viewAny', Shop::class);
|
|
|
|
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');
|
|
$pickupExceptionId = $pickupException !== null ? (string) $pickupException : null;
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
new GetPaginatedShopsQueryInput(
|
|
pagination: $request->pagination(),
|
|
pickupExceptionId: $pickupExceptionId,
|
|
),
|
|
);
|
|
|
|
return view('bo.shops.index', [
|
|
'shops' => $output->shops,
|
|
'pickupExceptions' => $output->pickupExceptions,
|
|
]);
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
$this->authorize('create', Shop::class);
|
|
|
|
return view('bo.shops.create');
|
|
}
|
|
|
|
public function store(StoreShopRequest $request, CreateShop $action): RedirectResponse
|
|
{
|
|
$this->authorize('create', Shop::class);
|
|
|
|
$coordinates = Coordinates::tryFrom(
|
|
$request->input('latitude') !== null ? (string) $request->input('latitude') : null,
|
|
$request->input('longitude') !== null ? (string) $request->input('longitude') : null,
|
|
);
|
|
|
|
$phone = PhoneNumber::tryFrom($request->input('phone') !== null ? (string) $request->input('phone') : null);
|
|
|
|
$shop = $this->executor->execute(
|
|
$action,
|
|
new CreateShopInput(
|
|
name: $request->string('name')->value(),
|
|
address: $request->string('address')->value(),
|
|
postcode: $request->string('postcode')->value(),
|
|
city: $request->string('city')->value(),
|
|
phone: $phone,
|
|
coordinates: $coordinates,
|
|
default: $request->boolean('default'),
|
|
),
|
|
);
|
|
|
|
return to_route('bo.shops.edit', $shop)
|
|
->with('success', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
|
]));
|
|
}
|
|
|
|
public function edit(Shop $shop): View
|
|
{
|
|
$this->authorize('update', $shop);
|
|
|
|
$shop = ShopDto::fromModel($shop);
|
|
|
|
return view('bo.shops.edit', compact('shop'));
|
|
}
|
|
|
|
public function update(UpdateShopRequest $request, Shop $shop, UpdateShop $action): RedirectResponse
|
|
{
|
|
$this->authorize('update', $shop);
|
|
|
|
$coordinates = Coordinates::tryFrom(
|
|
$request->input('latitude') !== null ? (string) $request->input('latitude') : null,
|
|
$request->input('longitude') !== null ? (string) $request->input('longitude') : null,
|
|
);
|
|
|
|
$phone = PhoneNumber::tryFrom($request->input('phone') !== null ? (string) $request->input('phone') : null);
|
|
|
|
$this->executor->execute(
|
|
$action,
|
|
new UpdateShopInput(
|
|
shop: $shop,
|
|
name: $request->string('name')->value(),
|
|
address: $request->string('address')->value(),
|
|
postcode: $request->string('postcode')->value(),
|
|
city: $request->string('city')->value(),
|
|
phone: $phone,
|
|
coordinates: $coordinates,
|
|
default: $request->boolean('default'),
|
|
),
|
|
);
|
|
|
|
return to_route('bo.shops.edit', $shop)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
|
]));
|
|
}
|
|
|
|
public function destroy(Shop $shop, DeleteShop $action): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $shop);
|
|
|
|
$this->executor->execute(
|
|
$action,
|
|
new DeleteShopInput(shop: $shop),
|
|
);
|
|
|
|
return to_route('bo.shops.index')
|
|
->with('warning', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
|
]));
|
|
}
|
|
|
|
public function pickupExceptionsEdit(Shop $shop, GetShopPickupExceptionsQuery $query): View
|
|
{
|
|
$this->authorize('pickupExceptionUpdate', $shop);
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
new GetShopPickupExceptionsQueryInput(shop: $shop),
|
|
);
|
|
|
|
return view('bo.shops.pickup-exceptions', [
|
|
'shop' => $output->shop,
|
|
'pickupExceptions' => $output->pickupExceptions,
|
|
'pickupDay' => $output->pickupDay,
|
|
'pickupPeriod' => $output->pickupPeriod,
|
|
]);
|
|
}
|
|
|
|
public function pickupExceptionsUpdate(UpdateShopPickupExceptionsRequest $request, Shop $shop, UpdateShopPickupExceptions $action): RedirectResponse
|
|
{
|
|
$this->authorize('pickupExceptionUpdate', $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,
|
|
];
|
|
}
|
|
|
|
$this->executor->execute(
|
|
$action,
|
|
new UpdateShopPickupExceptionsInput(
|
|
shop: $shop,
|
|
syncData: $syncData,
|
|
),
|
|
);
|
|
|
|
return back()->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
|
|
]));
|
|
}
|
|
}
|