110 lines
3.9 KiB
PHP
110 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo;
|
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class PrepareOrderRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Order baskter items keyed by id
|
|
*
|
|
* @var \Illuminate\Support\Collection<integer,object>
|
|
*/
|
|
private $basketItems;
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return \auth('backend')->check();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function prepareForValidation(): void
|
|
{
|
|
$this->basketItems = collect($this->order->basket)->keyBy(function (object $basketItem) {
|
|
if (!ToolboxHelper::basketObjectIsValid($basketItem)) {
|
|
throw ValidationException::withMessages([
|
|
'error' => \trans('Cannot validate basket item')
|
|
]);
|
|
}
|
|
return $basketItem->id;
|
|
});
|
|
$this->merge([
|
|
'willExceedOrderAmount' => ($this->order->total_ttc -
|
|
$this->order->total_refund - $this->basketItems->map(function (object $basketItem) {
|
|
return \intval(collect($this->items)->get($basketItem->id)) * $basketItem->options->price_ttc;
|
|
})->reduce(function ($carry, int $lineAmount) {
|
|
/** @var null|integer $carry */
|
|
return ($carry ? $carry : 0) + $lineAmount;
|
|
})
|
|
) < 0
|
|
]);
|
|
$this->merge([
|
|
'willNeedCompleteRefund' => !collect($this->items)->reduce(function (?int $carry, $item) {
|
|
return $carry + (\is_numeric($item) ? \intval($item) : 0);
|
|
}) or $this->willExceedOrderAmount,
|
|
'willNeedRefund' => collect($this->items)->reduce(function (?int $carry, $item) {
|
|
return $carry + $item;
|
|
}) !== $this->basketItems->sum('qty'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'willNeedCompleteRefund' => 'required|boolean',
|
|
'willNeedRefund' => 'required|boolean',
|
|
'willExceedOrderAmount' => 'required|boolean',
|
|
'items' => 'required|array',
|
|
'items.*' => [
|
|
// ! Submited Items must be in the basket.
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
'required', 'numeric', function ($attribute, $value, $fail) {
|
|
$itemId = \intval(\str_replace('items.', '', $attribute));
|
|
$basketItemKey = $this->basketItems->search(function (object $basketItem) use ($itemId) {
|
|
return $basketItem->id === $itemId && $itemId !== 0;
|
|
});
|
|
if (!$basketItemKey) {
|
|
$fail(":attribute id '{$itemId}' ne se trouve pas dans le panier de la commande");
|
|
return;
|
|
}
|
|
/** @var object $basketItem */
|
|
$basketItem = $this->basketItems->get($basketItemKey);
|
|
if ($basketItem->qty < \intval($value)) {
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
$fail(":attribute référence '{$basketItem->options->reference}' ne peut contenir plus d'éléments que ce que le client à commandé");
|
|
return;
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'items' => trans('produits'),
|
|
'items.*' => trans('produit')
|
|
];
|
|
}
|
|
}
|