2019-04-03 05:22:13 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2019-04-03 05:22:13 +02:00
|
|
|
|
|
|
|
namespace App\Http\Requests\Product;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
2019-12-30 22:59:12 +01:00
|
|
|
use App\Utils\Traits\ChecksEntityStatus;
|
2019-04-03 05:22:13 +02:00
|
|
|
|
|
|
|
class UpdateProductRequest extends Request
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
use ChecksEntityStatus;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-04-03 05:22:13 +02:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function authorize(): bool
|
2019-04-03 05:22:13 +02:00
|
|
|
{
|
2024-02-16 19:57:15 +01:00
|
|
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
return $user->can('edit', $this->product);
|
2019-04-03 05:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
2023-03-18 08:24:56 +01:00
|
|
|
if ($this->file('documents') && is_array($this->file('documents'))) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['documents.*'] = $this->fileValidation();
|
2023-03-18 08:24:56 +01:00
|
|
|
} elseif ($this->file('documents')) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['documents'] = $this->fileValidation();
|
2024-02-16 19:57:15 +01:00
|
|
|
}else {
|
|
|
|
$rules['documents'] = 'bail|sometimes|array';
|
2023-03-18 08:24:56 +01:00
|
|
|
}
|
2020-06-22 13:41:04 +02:00
|
|
|
|
2023-02-27 10:12:59 +01:00
|
|
|
if ($this->file('file') && is_array($this->file('file'))) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['file.*'] = $this->fileValidation();
|
2023-02-27 10:12:59 +01:00
|
|
|
} elseif ($this->file('file')) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['file'] = $this->fileValidation();
|
2020-06-22 13:41:04 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2020-06-22 13:41:04 +02:00
|
|
|
$rules['cost'] = 'numeric';
|
|
|
|
$rules['price'] = 'numeric';
|
|
|
|
$rules['quantity'] = 'numeric';
|
2022-06-09 02:20:18 +02:00
|
|
|
$rules['in_stock_quantity'] = 'sometimes|numeric';
|
|
|
|
$rules['stock_notification_threshold'] = 'sometimes|numeric';
|
|
|
|
$rules['stock_notification'] = 'sometimes|bool';
|
2020-06-22 13:41:04 +02:00
|
|
|
|
|
|
|
return $rules;
|
2019-04-03 05:22:13 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 03:55:41 +02:00
|
|
|
public function prepareForValidation()
|
2019-10-04 13:57:33 +02:00
|
|
|
{
|
|
|
|
$input = $this->all();
|
|
|
|
|
2023-12-12 04:46:08 +01:00
|
|
|
if (! isset($input['quantity'])) {
|
2019-10-04 13:57:33 +02:00
|
|
|
$input['quantity'] = 1;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-10-04 13:57:33 +02:00
|
|
|
|
2020-06-26 00:29:24 +02:00
|
|
|
if (array_key_exists('assigned_user_id', $input) && is_string($input['assigned_user_id'])) {
|
|
|
|
$input['assigned_user_id'] = $this->decodePrimaryKey($input['assigned_user_id']);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('in_stock_quantity', $input) && request()->has('update_in_stock_quantity') && request()->input('update_in_stock_quantity') == 'true') {
|
|
|
|
} elseif (array_key_exists('in_stock_quantity', $input)) {
|
2022-06-09 02:20:18 +02:00
|
|
|
unset($input['in_stock_quantity']);
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:57:33 +02:00
|
|
|
$this->replace($input);
|
|
|
|
}
|
2019-04-03 05:22:13 +02:00
|
|
|
}
|