Skip to content

Commit

Permalink
Merge pull request #34 from dvicklund/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dvicklund authored May 26, 2022
2 parents 53c4795 + 8837676 commit 1020067
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Models/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Inventory extends BaseModel
'name',
'description',
'is_parent',
'is_bundle',
];

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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');
}
}
29 changes: 29 additions & 0 deletions src/Models/LocationContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Stevebauman\Inventory\Models;

/**
* Class LocationContact.
*/
class LocationContact extends BaseModel
{
protected $table = 'location_contacts';

protected $fillable = [
'name',
'email',
'phone',
'fax',
'type'
];

/**
* The belongsTo location relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function location()
{
return $this->belongsTo(Location::class, 'location_id', 'id');
}
}
6 changes: 3 additions & 3 deletions src/Traits/InventoryStockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
27 changes: 27 additions & 0 deletions src/migrations/2014_07_31_123204_create_locations_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,40 @@ 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
* want to store multiple nested sets on the same table
*/
$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');
});
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
]);
}
Expand Down
61 changes: 55 additions & 6 deletions tests/InventoryStockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1020067

Please sign in to comment.