diff --git a/src/Models/Inventory.php b/src/Models/Inventory.php index f2b12d2..ef1308b 100644 --- a/src/Models/Inventory.php +++ b/src/Models/Inventory.php @@ -28,6 +28,7 @@ class Inventory extends BaseModel 'name', 'description', 'is_parent', + 'is_bundle', ]; /** diff --git a/src/Models/Location.php b/src/Models/Location.php index 6851277..5a41c6e 100644 --- a/src/Models/Location.php +++ b/src/Models/Location.php @@ -13,6 +13,15 @@ class Location extends Node protected $fillable = [ 'name', + 'code', + 'address_1', + 'address_2', + 'city', + 'state_province', + 'postal_code', + 'county', + 'district', + 'country' ]; protected $scoped = ['belongs_to']; @@ -26,4 +35,14 @@ public function stocks() { return $this->hasMany(InventoryStock::class, 'location_id', 'id'); } + + /** + * The hasMany locationContacts relationship. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function contacts() + { + return $this->hasMany(LocationContact::class, 'location_id', 'id'); + } } diff --git a/src/Models/LocationContact.php b/src/Models/LocationContact.php new file mode 100644 index 0000000..22c3d63 --- /dev/null +++ b/src/Models/LocationContact.php @@ -0,0 +1,29 @@ +belongsTo(Location::class, 'location_id', 'id'); + } +} diff --git a/src/Traits/InventoryStockTrait.php b/src/Traits/InventoryStockTrait.php index db47540..2b0c401 100644 --- a/src/Traits/InventoryStockTrait.php +++ b/src/Traits/InventoryStockTrait.php @@ -328,7 +328,7 @@ public function hasEnoughStock($quantity = 0) */ public function getLastMovement() { - $movement = $this->movements()->orderBy('created_at', 'DESC')->first(); + $movement = $this->movements()->orderBy('id', 'DESC')->first(); if ($movement) { return $movement; @@ -611,8 +611,8 @@ private function processRecursiveRollbackOperation(Model $movement) */ $movements = $this ->movements() - ->where('created_at', '>=', $movement->getOriginal('created_at')) - ->orderBy('created_at', 'DESC') + ->where('id', '>=', $movement->getOriginal('id')) + ->orderBy('id', 'DESC') ->get(); $rollbacks = []; diff --git a/src/migrations/2014_07_31_123204_create_locations_table.php b/src/migrations/2014_07_31_123204_create_locations_table.php index beccf76..3d3132d 100644 --- a/src/migrations/2014_07_31_123204_create_locations_table.php +++ b/src/migrations/2014_07_31_123204_create_locations_table.php @@ -26,6 +26,18 @@ public function up() $table->integer('rgt')->nullable()->index(); $table->integer('depth')->nullable(); $table->string('name'); + $table->string('code')->nullable(); + $table->string('address_1'); + $table->string('address_2')->nullable(); + $table->string('city'); + $table->string('state_province'); + $table->string('postal_code'); + $table->string('county')->nullable(); + $table->string('district')->nullable(); + $table->string('country'); + // TODO: implement time zones + // $table->foreign('timezone_id')->references('id')->on('timezones') + // ->onUpdate('restrict'); /* * This field is for scoping categories, use it if you @@ -33,6 +45,21 @@ public function up() */ $table->string('belongs_to')->nullable(); }); + + Schema::create('location_contacts', function(Blueprint $table) { + $table->id(); + $table->timestamp('created_at')->useCurrent(); + $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate(); + $table->foreignId('location_id')->unsigned(); + $table->string('name'); + $table->string('email')->nullable(); + $table->string('fax')->nullable(); + $table->string('phone')->nullable(); + $table->string('type'); + + $table->foreign('location_id')->references('id')->on('locations') + ->onUpdate('restrict'); + }); } /** diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 0c0b038..dae7349 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -500,6 +500,12 @@ protected function newLocation() { return Location::create([ 'name' => 'Warehouse', + 'code' => 'WH', + 'address_1' => '111 Fake St.', + 'city' => 'Bellingham', + 'state_province' => 'WA', + 'postal_code' => '98226', + 'country' => 'USA', 'belongs_to' => '', ]); } diff --git a/tests/InventoryStockTest.php b/tests/InventoryStockTest.php index 71c98f4..8648012 100644 --- a/tests/InventoryStockTest.php +++ b/tests/InventoryStockTest.php @@ -53,9 +53,7 @@ public function testStockMove() { $stock = $this->newInventoryStock(); - $newLocation = Location::create([ - 'name' => 'New Location', - ]); + $newLocation = $this->newLocation(); DB::shouldReceive('beginTransaction')->once()->shouldReceive('commit')->once(); Event::shouldReceive('dispatch')->once(); @@ -193,9 +191,7 @@ public function testInventoryMoveItemStock() $locationFrom = Location::find($newStock->location_id); - $locationTo = new Location(); - $locationTo->name = 'New Location'; - $locationTo->save(); + $locationTo = $this->newLocation(); $item = Inventory::find($newStock->inventory_id); @@ -236,4 +232,57 @@ public function testInventoryStockNewTransaction() $this->assertInstanceOf('Stevebauman\Inventory\Interfaces\StateableInterface', $transaction); } + + public function testRollbackStockMovement() { + $stock = $this->newInventoryStock(); + $initialQuantity = $stock->quantity; + + $txn = $stock->newTransaction(); + + $stock->add(20, 'fresh inventory', 50); + + $this->assertEquals($initialQuantity + 20, $stock->quantity); + + $stock->rollback(); + + $this->assertEquals($initialQuantity, $stock->quantity); + + $stock->remove(10, 'removing inventory cheaper', 40); + + $this->assertEquals($initialQuantity - 10, $stock->quantity); + + $stock->rollback($stock->getLastMovement()); + + $this->assertEquals($initialQuantity, $stock->quantity); + + $stock->add(10, 'adding inventory cheaper', 40); + + $this->assertEquals($initialQuantity + 10, $stock->quantity); + + $stock->rollbackMovement($stock->getLastMovement()); + + $this->assertEquals($initialQuantity, $stock->quantity); + + $stock->add(10, 'adding inventory even cheaper', 30); + + $this->assertEquals($initialQuantity + 10, $stock->quantity); + + $lastMovement = $stock->getLastMovement(); + + $stock->rollbackMovement($lastMovement->id); + + $this->assertEquals($initialQuantity, $stock->quantity); + + $stock->add(10, 'adding inventory even cheaperer', 25); + + $this->assertEquals($initialQuantity + 10, $stock->quantity); + + $lastMovement = $stock->getLastMovement(); + $stock->add(10, 'adding inventory even cheaper again', 20); + $stock->add(10, 'adding inventory even cheaper for a third time', 15); + + $rollbacks = $stock->rollback($lastMovement->id, true); + + $this->assertEquals($initialQuantity, $stock->quantity); + } }