2023-10-31 19:06:44 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
2023-11-28 13:17:46 -08:00
|
|
|
use App\Models\Company;
|
2024-03-25 12:38:12 -07:00
|
|
|
use Carbon\Carbon;
|
2024-03-25 13:47:24 -07:00
|
|
|
use Carbon\Exceptions\InvalidFormatException;
|
2023-10-31 19:06:44 -07:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
|
|
|
|
class StoreAssetRequest extends ImageUploadRequest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
2023-11-01 09:33:29 -07:00
|
|
|
return Gate::allows('create', new Asset);
|
2023-10-31 19:06:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function prepareForValidation(): void
|
|
|
|
{
|
2024-01-09 10:57:43 -08:00
|
|
|
// Guard against users passing in an array for company_id instead of an integer.
|
|
|
|
// If the company_id is not an integer then we simply use what was
|
|
|
|
// provided to be caught by model level validation later.
|
|
|
|
$idForCurrentUser = is_int($this->company_id)
|
|
|
|
? Company::getIdForCurrentUser($this->company_id)
|
|
|
|
: $this->company_id;
|
|
|
|
|
2024-03-25 12:45:11 -07:00
|
|
|
if ($this->input('last_audit_date')) {
|
2024-03-25 13:47:24 -07:00
|
|
|
try {
|
|
|
|
$lastAuditDate = Carbon::parse($this->input('last_audit_date'));
|
|
|
|
|
|
|
|
$this->merge([
|
|
|
|
'last_audit_date' => $lastAuditDate->startOfDay()->format('Y-m-d H:i:s'),
|
|
|
|
]);
|
|
|
|
} catch (InvalidFormatException $e) {
|
|
|
|
// we don't need to do anything here...
|
2024-03-25 13:48:32 -07:00
|
|
|
// we'll keep the provided date in an
|
|
|
|
// invalid format so validation picks it up later
|
2024-03-25 13:47:24 -07:00
|
|
|
}
|
2024-03-25 12:45:11 -07:00
|
|
|
}
|
|
|
|
|
2023-11-28 13:17:46 -08:00
|
|
|
$this->merge([
|
|
|
|
'asset_tag' => $this->asset_tag ?? Asset::autoincrement_asset(),
|
2024-01-09 10:57:43 -08:00
|
|
|
'company_id' => $idForCurrentUser,
|
2023-11-28 20:11:20 -08:00
|
|
|
'assigned_to' => $assigned_to ?? null,
|
2023-11-28 13:17:46 -08:00
|
|
|
]);
|
2023-10-31 19:06:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
$rules = array_merge(
|
|
|
|
(new Asset)->getRules(),
|
|
|
|
parent::rules(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
}
|