2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Bo\Discounts;
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Actions\Discounts\CreateDiscountInput;
|
2026-11-16 01:58:43 +01:00
|
|
|
use App\Traits\Requests\ConvertsClientDatesToUtc;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Domain\Enums\DiscountType;
|
2026-09-30 12:43:55 +02:00
|
|
|
use Domain\Models\ValueObjects\DatePeriod;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Domain\Models\ValueObjects\DiscountValue;
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
use Illuminate\Validation\Rules\Enum;
|
|
|
|
|
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
|
|
|
|
use Money\Currency;
|
|
|
|
|
use Money\Money;
|
|
|
|
|
|
|
|
|
|
class StoreDiscountRequest extends FormRequest
|
|
|
|
|
{
|
2026-11-16 01:58:43 +01:00
|
|
|
use ConvertsClientDatesToUtc;
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
public function __construct(
|
2026-11-15 19:43:25 +01:00
|
|
|
private readonly AppAdminAuthService $authService,
|
|
|
|
|
private readonly Currency $currency,
|
2026-06-14 21:32:07 +02:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:25:05 +02:00
|
|
|
public function toCreateDiscountInput(): CreateDiscountInput
|
|
|
|
|
{
|
|
|
|
|
return new CreateDiscountInput(
|
|
|
|
|
type: $this->toDiscountType(),
|
|
|
|
|
value: $this->toDiscountValue(),
|
|
|
|
|
is_old_price_displayed: $this->boolean('is_old_price_displayed'),
|
|
|
|
|
period: $this->toDatePeriod(),
|
|
|
|
|
associatedProductsIds: $this->toAssociatedProductsIds(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
public function authorize(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->authService->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
protected function prepareForValidation(): void
|
|
|
|
|
{
|
|
|
|
|
$this->merge([
|
|
|
|
|
'is_old_price_displayed' => $this->boolean('is_old_price_displayed'),
|
|
|
|
|
'associated_products' => $this->array('associated_products'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array<string> */
|
|
|
|
|
public function getAssociatedProductsIds(): array
|
|
|
|
|
{
|
2026-11-14 16:06:42 +01:00
|
|
|
$filtered = array_filter(
|
|
|
|
|
$this->array('associated_products'),
|
|
|
|
|
fn ($id) => is_string($id) || is_int($id)
|
|
|
|
|
);
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
return array_map(
|
|
|
|
|
fn ($id) => (string) $id,
|
2026-11-14 16:06:42 +01:00
|
|
|
$filtered
|
2026-06-14 21:32:07 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
/** @return array<string> */
|
|
|
|
|
public function toAssociatedProductsIds(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->getAssociatedProductsIds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toDiscountType(): DiscountType
|
|
|
|
|
{
|
|
|
|
|
/** @var DiscountType */
|
|
|
|
|
return $this->enum('type', DiscountType::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
public function getDiscountValue(): DiscountValue
|
|
|
|
|
{
|
|
|
|
|
$type = $this->enum('type', DiscountType::class);
|
|
|
|
|
|
|
|
|
|
if ($type === DiscountType::PERCENT) {
|
|
|
|
|
return DiscountValue::fromPercentage(
|
|
|
|
|
$this->integer('value')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DiscountValue::fromAmount(
|
|
|
|
|
new Money(
|
|
|
|
|
intval($this->integer('value')),
|
2026-11-15 19:43:25 +01:00
|
|
|
$this->currency,
|
2026-06-14 21:32:07 +02:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
public function toDiscountValue(): DiscountValue
|
|
|
|
|
{
|
|
|
|
|
return $this->getDiscountValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toDatePeriod(): DatePeriod
|
|
|
|
|
{
|
|
|
|
|
$start = $this->filled('date_start')
|
|
|
|
|
? new \DateTimeImmutable($this->string('date_start')->value(), new \DateTimeZone('UTC'))
|
|
|
|
|
: null;
|
|
|
|
|
$end = $this->filled('date_end')
|
|
|
|
|
? new \DateTimeImmutable($this->string('date_end')->value(), new \DateTimeZone('UTC'))
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
return new DatePeriod($start, $end);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return array<string,mixed> */
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'type' => ['required', new Enum(DiscountType::class)],
|
|
|
|
|
'value' => [
|
|
|
|
|
'required',
|
2026-10-03 11:30:52 +02:00
|
|
|
'integer',
|
2026-06-14 21:32:07 +02:00
|
|
|
'min:0',
|
|
|
|
|
Rule::when(
|
|
|
|
|
$this->input('type') == DiscountType::PERCENT->value(),
|
|
|
|
|
['max:100']
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
'is_old_price_displayed' => ['required', 'boolean'],
|
|
|
|
|
'date_start' => ['nullable', 'date_format:Y-m-d\TH:i'],
|
|
|
|
|
'date_end' => ['nullable', 'date_format:Y-m-d\TH:i', 'after:date_start'],
|
|
|
|
|
'client_timezone' => ['required', 'timezone'],
|
|
|
|
|
|
|
|
|
|
'associated_products' => ['present', 'array'],
|
|
|
|
|
'associated_products.*' => ['string', 'exists:products,id'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
protected function passedValidation()
|
|
|
|
|
{
|
2026-11-16 01:58:43 +01:00
|
|
|
$this->mergeLocalDatetimeAsUtc('date_start');
|
|
|
|
|
$this->mergeLocalDatetimeAsUtc('date_end');
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function attributes(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'type' => trans('validation.custom.attributes.discount_type'),
|
|
|
|
|
'value' => trans('validation.custom.attributes.discount_value'),
|
|
|
|
|
'is_old_price_displayed' => trans('validation.custom.attributes.is_old_price_displayed'),
|
|
|
|
|
'date_start' => trans('validation.custom.attributes.date_start_show'),
|
|
|
|
|
'date_end' => trans('validation.custom.attributes.date_end_show'),
|
|
|
|
|
'associated_products' => trans('validation.custom.attributes.associated_products'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|