3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-02-06 19:35:44 +00:00
Files
snipe-it/app/Models/SnipeModel.php
snipe 0c3103e3d2 Modify the getter
Signed-off-by: snipe <snipe@snipe.net>
2025-08-19 12:56:30 +01:00

175 lines
4.2 KiB
PHP

<?php
namespace App\Models;
use App\Helpers\Helper;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
class SnipeModel extends Model
{
// Setters that are appropriate across multiple models.
public function setPurchaseDateAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['purchase_date'] = $value;
}
/**
* @param $value
*/
public function setPurchaseCostAttribute($value)
{
if (is_float($value)) {
//value is *already* a floating-point number. Just assign it directly
$this->attributes['purchase_cost'] = $value;
return;
}
$value = Helper::ParseCurrency($value);
if ($value == 0) {
$value = null;
}
$this->attributes['purchase_cost'] = $value;
}
public function setLocationIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['location_id'] = $value;
}
public function setCategoryIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['category_id'] = $value;
// dd($this->attributes);
}
public function setSupplierIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['supplier_id'] = $value;
}
public function setDepreciationIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['depreciation_id'] = $value;
}
public function setManufacturerIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['manufacturer_id'] = $value;
}
public function setMinAmtAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['min_amt'] = $value;
}
public function setParentIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['parent_id'] = $value;
}
public function setFieldSetIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['fieldset_id'] = $value;
}
public function setCompanyIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['company_id'] = $value;
}
public function setWarrantyMonthsAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['warranty_months'] = $value;
}
public function setRtdLocationIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['rtd_location_id'] = $value;
}
public function setDepartmentIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['department_id'] = $value;
}
public function setManagerIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['manager_id'] = $value;
}
public function setModelIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['model_id'] = $value;
}
public function setStatusIdAttribute($value)
{
if ($value == '') {
$value = null;
}
$this->attributes['status_id'] = $value;
}
// This gets a little twitchy since *most* things have a property in the table called "name" (but users don't)
// AND we want to be able to use the actual display_name value from the database if it's set (usually via SCIM)
protected function displayNameAttribute(): Attribute
{
// This override should only kick in if the model has a display_name property (users)
if (isset($this->display_name)) {
return Attribute::make(
get: fn (string $value) => $this->display_name,
);
}
return Attribute::make(
get: fn (string $value) => $this->name,
);
}
}