|
6 | 6 | use App\Presenters\Presentable; |
7 | 7 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
8 | 8 | use Illuminate\Database\Eloquent\SoftDeletes; |
| 9 | +use Illuminate\Support\Facades\Gate; |
9 | 10 | use Watson\Validating\ValidatingTrait; |
10 | 11 |
|
11 | 12 | /** |
@@ -104,6 +105,13 @@ class Component extends SnipeModel |
104 | 105 | ]; |
105 | 106 |
|
106 | 107 |
|
| 108 | + public function isDeletable() |
| 109 | + { |
| 110 | + return Gate::allows('delete', $this) |
| 111 | + && ($this->numCheckedOut() === 0) |
| 112 | + && ($this->deleted_at == ''); |
| 113 | + } |
| 114 | + |
107 | 115 | /** |
108 | 116 | * Establishes the components -> action logs -> uploads relationship |
109 | 117 | * |
@@ -234,13 +242,24 @@ public function numCheckedOut() |
234 | 242 | // In case there are elements checked out to assets that belong to a different company |
235 | 243 | // than this asset and full multiple company support is on we'll remove the global scope, |
236 | 244 | // so they are included in the count. |
237 | | - foreach ($this->assets()->withoutGlobalScope(new CompanyableScope)->get() as $checkout) { |
238 | | - $checkedout += $checkout->pivot->assigned_qty; |
239 | | - } |
| 245 | + return $this->uncontrainedAssets->sum('pivot.assigned_qty'); |
| 246 | + } |
| 247 | + |
| 248 | + |
| 249 | + /** |
| 250 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
| 251 | + * |
| 252 | + * This allows us to get the assets with assigned components without the company restriction |
| 253 | + */ |
| 254 | + public function uncontrainedAssets() { |
| 255 | + |
| 256 | + return $this->belongsToMany(\App\Models\Asset::class, 'components_assets') |
| 257 | + ->withPivot('id', 'assigned_qty', 'created_at', 'created_by', 'note') |
| 258 | + ->withoutGlobalScope(new CompanyableScope); |
240 | 259 |
|
241 | | - return $checkedout; |
242 | 260 | } |
243 | 261 |
|
| 262 | + |
244 | 263 | /** |
245 | 264 | * Check how many items within a component are remaining |
246 | 265 | * |
|
0 commit comments