2020-10-08 00:25:39 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2020-10-08 00:25:39 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-10-08 00:25:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Project;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
2020-10-20 01:55:14 +02:00
|
|
|
use App\Models\Client;
|
2020-10-08 00:25:39 +02:00
|
|
|
use App\Models\Project;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-12-11 21:51:10 +01:00
|
|
|
use Illuminate\Validation\Rule;
|
2020-10-08 00:25:39 +02:00
|
|
|
|
|
|
|
class StoreProjectRequest extends Request
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-10-08 00:25:39 +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
|
2020-10-08 00:25:39 +02:00
|
|
|
{
|
2023-10-27 08:13:23 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
return $user->can('create', Project::class);
|
2020-10-08 00:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
2023-10-27 08:13:23 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
2020-10-08 00:25:39 +02:00
|
|
|
$rules = [];
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
$rules['name'] = 'required';
|
2023-10-27 08:13:23 +02:00
|
|
|
$rules['client_id'] = 'required|exists:clients,id,company_id,'.$user->company()->id;
|
2024-03-10 03:01:30 +01:00
|
|
|
$rules['budgeted_hours'] = 'sometimes|numeric';
|
2024-06-23 23:56:13 +02:00
|
|
|
$rules['task_rate'] = 'required|bail|numeric';
|
|
|
|
|
2020-12-11 21:51:10 +01:00
|
|
|
if (isset($this->number)) {
|
2023-10-27 08:13:23 +02:00
|
|
|
$rules['number'] = Rule::unique('projects')->where('company_id', $user->company()->id);
|
2020-12-11 21:51:10 +01:00
|
|
|
}
|
2020-10-08 00:25:39 +02:00
|
|
|
|
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-06-14 09:09:44 +02:00
|
|
|
} else {
|
2024-02-16 19:57:15 +01:00
|
|
|
$rules['documents'] = 'bail|sometimes|array';
|
2023-03-18 08:24:56 +01: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();
|
2023-02-27 10:12:59 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2020-10-29 01:09:51 +01:00
|
|
|
return $this->globalRules($rules);
|
2020-10-08 00:25:39 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 03:55:41 +02:00
|
|
|
public function prepareForValidation()
|
2020-10-08 00:25:39 +02:00
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
$input = $this->decodePrimaryKeys($this->all());
|
2020-10-08 00:25:39 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('color', $input) && is_null($input['color'])) {
|
2021-07-11 02:16:27 +02:00
|
|
|
$input['color'] = '';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2024-06-14 09:09:44 +02:00
|
|
|
if(array_key_exists('budgeted_hours', $input) && empty($input['budgeted_hours'])) {
|
2024-03-10 03:01:30 +01:00
|
|
|
$input['budgeted_hours'] = 0;
|
2024-06-14 09:09:44 +02:00
|
|
|
}
|
2024-03-10 03:01:30 +01:00
|
|
|
|
2024-07-08 01:20:05 +02:00
|
|
|
$input['task_rate'] = (isset($input['task_rate']) && floatval($input['task_rate']) >= 0) ? $input['task_rate'] : 0;
|
|
|
|
|
2020-10-08 00:25:39 +02:00
|
|
|
$this->replace($input);
|
|
|
|
}
|
2020-10-20 01:55:14 +02:00
|
|
|
|
|
|
|
public function getClient($client_id)
|
|
|
|
{
|
2022-07-01 10:56:19 +02:00
|
|
|
return Client::withTrashed()->find($client_id);
|
2020-10-20 01:55:14 +02:00
|
|
|
}
|
2020-10-08 00:25:39 +02:00
|
|
|
}
|