From bd944a5d38dbf6ef6136e06f3fc6b387d8d35c9b Mon Sep 17 00:00:00 2001 From: David Vicklund Date: Fri, 10 Jun 2022 11:34:17 -0700 Subject: [PATCH] add ability to get location by id --- src/Traits/LocationTrait.php | 13 +++++++++++++ tests/InventoryStockTest.php | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Traits/LocationTrait.php b/src/Traits/LocationTrait.php index ba30d713..fd7164d3 100644 --- a/src/Traits/LocationTrait.php +++ b/src/Traits/LocationTrait.php @@ -4,6 +4,7 @@ use Stevebauman\Inventory\Exceptions\InvalidLocationException; use Illuminate\Support\Facades\Lang; +use Stevebauman\Inventory\Models\Location; /** * Trait LocationTrait. @@ -24,6 +25,18 @@ public function getLocation($location) { if ($this->isLocation($location)) { return $location; + } else if (is_numeric($location)) { + try { + $result = Location::where('id', '=', $location)->first(); + + return $result; + } catch (\Exception $e) { + $message = Lang::get('inventory::exceptions.InvalidLocationException', [ + 'location' => $location, + ]); + + throw new InvalidLocationException($message); + } } else { $message = Lang::get('inventory::exceptions.InvalidLocationException', [ 'location' => $location, diff --git a/tests/InventoryStockTest.php b/tests/InventoryStockTest.php index 86480120..a396fe2f 100644 --- a/tests/InventoryStockTest.php +++ b/tests/InventoryStockTest.php @@ -285,4 +285,16 @@ public function testRollbackStockMovement() { $this->assertEquals($initialQuantity, $stock->quantity); } + + public function testNumericLocation() { + $location = $this->newLocation(); + + $item = $this->newInventory(); + + $item->createStockOnLocation(42, $location->id, "New stuff"); + + $newQuantity = $item->getTotalStock(); + + $this->assertEquals(42, $newQuantity); + } }