From ce5ccc31f06a856e8c29f4ecc78eac309c7b7c45 Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 28 Oct 2017 01:48:27 -0700 Subject: [PATCH] Added location method, fixed assetLoc --- app/Models/Asset.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 9446df9996..798d4cd4c9 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -255,23 +255,33 @@ class Asset extends Depreciable **/ public function assetLoc() { + static $iterations=0; + static $first_asset; if (!empty($this->assignedType())) { - // dd($this->assignedType()); if ($this->assignedType() == self::ASSET) { - return $this->assignedto->assetloc(); // Recurse until we have a final location + $iterations++; + if(!$first_asset) { + $first_asset=$this; + } + if($iterations>10) { + throw new \Exception("Asset assignment Loop for Asset ID: ".$first_asset->id); + } + $assigned_to=Asset::find($this->assigned_to); //have to do this this way because otherwise it errors + return $assigned_to->assetLoc(); // Recurse until we have a final location } if ($this->assignedType() == self::LOCATION) { - return $this->assignedTo(); + return $this->assignedTo; } if ($this->assignedType() == self::USER) { - if (!$this->assignedTo) { - return $this->defaultLoc(); + if (!$this->assignedTo->userLoc) { + //this makes no sense + return $this->defaultLoc; } - return $this->assignedTo->userLoc(); + return $this->assignedTo->userLoc; } } - return $this->defaultLoc(); + return $this->defaultLoc; } public function assignedType() @@ -407,6 +417,12 @@ class Asset extends Depreciable } + public function location() + { + return $this->belongsTo('\App\Models\Location', 'location_id'); + } + + /** * Get auto-increment