2023-10-31 19:06:44 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
2024-04-04 12:20:03 -07:00
|
|
|
use App\Http\Requests\Traits\MayContainCustomFields;
|
2023-10-31 19:06:44 -07:00
|
|
|
use App\Models\Asset;
|
2023-11-28 13:17:46 -08:00
|
|
|
use App\Models\Company;
|
2024-04-25 17:04:07 -07:00
|
|
|
use App\Models\Setting;
|
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;
|
2024-09-24 07:35:00 -07:00
|
|
|
use App\Rules\AssetCannotBeCheckedOutToNondeployableStatus;
|
2023-10-31 19:06:44 -07:00
|
|
|
|
|
|
|
class StoreAssetRequest extends ImageUploadRequest
|
|
|
|
{
|
2024-04-04 12:20:03 -07:00
|
|
|
use MayContainCustomFields;
|
2023-10-31 19:06:44 -07:00
|
|
|
/**
|
|
|
|
* 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-03-25 13:53:30 -07:00
|
|
|
$this->parseLastAuditDate();
|
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-10-14 06:14:41 -07:00
|
|
|
'company_id' => Company::getIdForCurrentUser($this->company_id),
|
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
|
|
|
|
{
|
2024-04-24 17:18:29 -07:00
|
|
|
$modelRules = (new Asset)->getRules();
|
|
|
|
|
2024-04-25 17:04:07 -07:00
|
|
|
if (Setting::getSettings()->digit_separator === '1.234,56' && is_string($this->input('purchase_cost'))) {
|
2024-04-25 16:24:12 -07:00
|
|
|
// If purchase_cost was submitted as a string with a comma separator
|
|
|
|
// then we need to ignore the normal numeric rules.
|
|
|
|
// Since the original rules still live on the model they will be run
|
|
|
|
// right before saving (and after purchase_cost has been
|
|
|
|
// converted to a float via setPurchaseCostAttribute).
|
|
|
|
$modelRules = $this->removeNumericRulesFromPurchaseCost($modelRules);
|
|
|
|
}
|
2024-04-24 17:18:29 -07:00
|
|
|
|
2024-04-25 16:22:03 -07:00
|
|
|
return array_merge(
|
2024-04-24 17:18:29 -07:00
|
|
|
$modelRules,
|
2024-09-24 07:35:00 -07:00
|
|
|
['status_id' => [new AssetCannotBeCheckedOutToNondeployableStatus()]],
|
2023-10-31 19:06:44 -07:00
|
|
|
parent::rules(),
|
|
|
|
);
|
|
|
|
}
|
2024-03-25 13:49:03 -07:00
|
|
|
|
2024-03-25 13:53:30 -07:00
|
|
|
private function parseLastAuditDate(): void
|
2024-03-25 13:49:03 -07:00
|
|
|
{
|
|
|
|
if ($this->input('last_audit_date')) {
|
|
|
|
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...
|
|
|
|
// we'll keep the provided date in an
|
|
|
|
// invalid format so validation picks it up later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 17:40:40 -07:00
|
|
|
|
2024-04-25 16:22:03 -07:00
|
|
|
private function removeNumericRulesFromPurchaseCost(array $rules): array
|
2024-04-24 17:40:40 -07:00
|
|
|
{
|
2024-04-25 16:22:03 -07:00
|
|
|
$purchaseCost = $rules['purchase_cost'];
|
|
|
|
|
2024-04-24 17:40:40 -07:00
|
|
|
// If rule is in "|" format then turn it into an array
|
|
|
|
if (is_string($purchaseCost)) {
|
|
|
|
$purchaseCost = explode('|', $purchaseCost);
|
|
|
|
}
|
|
|
|
|
2024-04-25 16:22:03 -07:00
|
|
|
$rules['purchase_cost'] = array_filter($purchaseCost, function ($rule) {
|
2024-04-24 17:40:40 -07:00
|
|
|
return $rule !== 'numeric' && $rule !== 'gte:0';
|
|
|
|
});
|
2024-04-25 16:22:03 -07:00
|
|
|
|
|
|
|
return $rules;
|
2024-04-24 17:40:40 -07:00
|
|
|
}
|
2023-10-31 19:06:44 -07:00
|
|
|
}
|