Skip to content

A free open source IT asset/license management system

Added attribute to normalize boolean requestbale

snipec6b480d153a4
diff --git a/app/Models/Component.php b/app/Models/Component.php
index e6d0eaae99..04f9bc7d39 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -7,6 +7,7 @@ use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\HasOrders;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Loggable;
+use App\Models\Traits\Requestable;
use App\Models\Traits\Searchable;
use App\Presenters\ComponentPresenter;
use App\Presenters\Presentable;
@@ -36,10 +37,12 @@ class Component extends SnipeModel
use HasOrders;
use HasUploads;
use Loggable, Presentable;
+ use Requestable;
use SoftDeletes;
protected $casts = [
'purchase_date' => 'datetime',
+ 'requestable' => 'boolean',
];
protected $table = 'components';
@@ -60,6 +63,7 @@ class Component extends SnipeModel
'manufacturer_id' => 'integer|exists:manufacturers,id|nullable',
'default_supplier_id' => 'nullable|integer|exists:suppliers,id',
'default_purchase_cost' => 'numeric|nullable|gte:0|max:99999999999999999.99',
+ 'requestable' => 'nullable|boolean',
];
/**
@@ -95,6 +99,7 @@ class Component extends SnipeModel
'notes',
'default_supplier_id',
'default_purchase_cost',
+ 'requestable',
];
use Searchable;
@@ -150,6 +155,31 @@ class Component extends SnipeModel
&& ($this->deleted_at == '');
}
+ /**
+ * Normalize the requestable form input so an empty string from an
+ * unchecked checkbox lands as false rather than a truthy "0" cast
+ * (matches Accessory / Consumable setRequestableAttribute).
+ */
+ public function setRequestableAttribute($value)
+ {
+ if ($value == '') {
+ $value = null;
+ }
+ $this->attributes['requestable'] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
+ }
+
+ /**
+ * Scope query to only requestable components. FMCS + location
+ * scoping falls out of the CompanyableTrait global scope, so the
+ * usual "user only sees rows in their reachable companies" rule
+ * applies without any additional wrapping here (matches the
+ * Accessory scope's shape and rationale).
+ */
+ public function scopeRequestableComponents($query)
+ {
+ return $query->where('components.requestable', '1');
+ }
+
/**
* Establishes the component -> location relationship
*
@@ -547,6 +577,6 @@ class Component extends SnipeModel
{
$direction = strtolower($order) === 'asc' ? 'asc' : 'desc';
- return $query->orderByRaw('(components.qty - COALESCE(sum_unconstrained_assets, 0)) ' . $direction);
+ return $query->orderByRaw('(components.qty - COALESCE(sum_unconstrained_assets, 0)) '.$direction);
}
}
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 95cb09a4e4..08c8d2de19 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -8,10 +8,10 @@ use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\HasOrders;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Loggable;
+use App\Models\Traits\Requestable;
use App\Models\Traits\Searchable;
use App\Presenters\ConsumablePresenter;
use App\Presenters\Presentable;
-use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Relation;
@@ -33,6 +33,7 @@ class Consumable extends SnipeModel
use HasOrders;
use HasUploads;
use Loggable, Presentable;
+ use Requestable;
use SoftDeletes;
protected $table = 'consumables';
@@ -59,6 +60,7 @@ class Consumable extends SnipeModel
'purchase_date' => 'date_format:Y-m-d|nullable',
'default_supplier_id' => 'nullable|integer|exists:suppliers,id',
'default_purchase_cost' => 'numeric|nullable|gte:0|max:99999999999999999.99',
+ 'requestable' => 'nullable|boolean',
];
/**
@@ -132,19 +134,9 @@ class Consumable extends SnipeModel
];
/**
- * Sets the attribute of whether or not the consumable is requestable
- *
- * This isn't really implemented yet, as you can't currently request a consumable
- * however it will be implemented in the future, and we needed to include
- * this method here so all of our polymorphic methods don't break.
- *
- * @todo Update this comment once it's been implemented
- *
- * @author [A. Gianotto] [<[email protected]>]
- *
- * @since [v3.0]
- *
- * @return Relation
+ * Normalize the requestable form input so an empty string from an
+ * unchecked checkbox lands as false rather than a truthy "0" cast
+ * (matches Accessory::setRequestableAttribute).
*/
public function setRequestableAttribute($value)
{
@@ -154,6 +146,18 @@ class Consumable extends SnipeModel
$this->attributes['requestable'] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
+ /**
+ * Scope query to only requestable consumables. FMCS + location
+ * scoping falls out of the CompanyableTrait global scope, so the
+ * usual "user only sees rows in their reachable companies" rule
+ * applies without any additional wrapping here (matches the
+ * Accessory scope's shape and rationale).
+ */
+ public function scopeRequestableConsumables($query)
+ {
+ return $query->where('consumables.requestable', '1');
+ }
+
public function isDeletable()
{
return Gate::allows('delete', $this)
diff --git a/app/Models/License.php b/app/Models/License.php
index a42313a792..7a639d2a02 100755
--- a/app/Models/License.php
+++ b/app/Models/License.php
@@ -7,6 +7,7 @@ use App\Helpers\Helper;
use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Loggable;
+use App\Models\Traits\Requestable;
use App\Models\Traits\Searchable;
use App\Presenters\LicensePresenter;
use App\Presenters\Presentable;
@@ -30,6 +31,7 @@ class License extends Depreciable
use CompanyableTrait;
use HasUploads;
use Loggable, Presentable;
+ use Requestable;
use SoftDeletes;
protected $injectUniqueIdentifier = true;
@@ -57,6 +59,7 @@ class License extends Depreciable
'termination_date' => 'date',
'category_id' => 'integer',
'company_id' => 'integer',
+ 'requestable' => 'boolean',
];
protected $rules = [
@@ -72,6 +75,7 @@ class License extends Depreciable
'expiration_date' => 'date_format:Y-m-d|nullable|max:10',
'termination_date' => 'date_format:Y-m-d|nullable|max:10',
'min_amt' => 'numeric|nullable|gte:0',
+ 'requestable' => 'nullable|boolean',
];
/**
@@ -100,6 +104,7 @@ class License extends Depreciable
'supplier_id',
'termination_date',
'min_amt',
+ 'requestable',
];
use Searchable;
@@ -182,6 +187,31 @@ class License extends Depreciable
&& ($this->deleted_at == '');
}
+ /**
+ * Normalize the requestable form input so an empty string from an
+ * unchecked checkbox lands as false rather than a truthy "0" cast
+ * (matches Accessory / Consumable / Component setRequestableAttribute).
+ */
+ public function setRequestableAttribute($value)
+ {
+ if ($value == '') {
+ $value = null;
+ }
+ $this->attributes['requestable'] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
+ }
+
+ /**
+ * Scope query to only requestable licenses. FMCS + location
+ * scoping falls out of the CompanyableTrait global scope, so the
+ * usual "user only sees rows in their reachable companies" rule
+ * applies without any additional wrapping here (matches the
+ * Accessory / Consumable / Component scope shape).
+ */
+ public function scopeRequestableLicenses($query)
+ {
+ return $query->where('licenses.requestable', '1');
+ }
+
/**
* @return Attribute<string|null, never>
*/