Skip to content

Commit

Permalink
Updated the way stock movements are generated. Movements are now gene…
Browse files Browse the repository at this point in the history
…rated upon model events
  • Loading branch information
stevebauman committed Jan 21, 2015
1 parent 7916cb8 commit 5ca751b
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 51 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ InventoryStock:
{
protected $table = 'inventory_stocks';
protected $fillable = array(
'inventory_id',
'location_id',
'quantity',
'aisle',
'row',
'bin',
);
use InventoryStockTrait;

public function item()
Expand Down Expand Up @@ -153,6 +162,15 @@ InventoryStockMovement:

protected $table = 'inventory_stock_movements';
protected $fillable = array(
'stock_id',
'user_id',
'before',
'after',
'cost',
'reason',
);
use InventoryStockMovementTrait;
public function stock()
Expand Down Expand Up @@ -202,10 +220,16 @@ Now we can add stock to our inventory by supplying a number (int or string), and
$item->createStockOnLocation(20, $location);

/*
* If we know the location ID we want to add the stock to, we can also use ID's
* Or we can create a stock manually. This will automatically create a stock movement.
* If you want to set the cost and reason for the creation of the stock, be sure to do so
*/
$item->createStockOnLocation(20, 1);
$item->createStockOnLocation(20, '1');
$stock = new InventoryStock;
$stock->inventory_id = $item->id;
$stock->location_id = $location->id;
$stock->quantity = 20;
$stock->cost = '5.20';
$stock->reason = 'I bought some';
$stock->save();

So, we've successfully added stock to our inventory item, now let's add some more quantity to it:

Expand Down
21 changes: 21 additions & 0 deletions src/Stevebauman/Inventory/Traits/InventoryStockMovementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ trait InventoryStockMovementTrait {

use DatabaseTransactionTrait;

/**
* Overrides the models boot function to set the user ID automatically
* to every new record
*/
public static function boot()
{
parent::boot();

parent::creating(function($record) {

$record->user_id = parent::getCurrentUserId();

});
}

/**
* Rolls back the current movement
*
* @param bool $recursive
* @return mixed
*/
public function rollback($recursive = false)
{
$stock = $this->stock;
Expand Down
172 changes: 140 additions & 32 deletions src/Stevebauman/Inventory/Traits/InventoryStockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,97 @@ trait InventoryStockTrait {
*/
use DatabaseTransactionTrait;

/**
* Stores the quantity before an update
*
* @var
*/
public $beforeQuantity = 0;

/**
* Stores the reason for updating / creating a stock
*
* @var string
*/
public $reason = '';

/**
* Stores the cost for updating a stock
*
* @var int
*/
public $cost = 0;

/**
* Overrides the models boot function to set the user ID automatically
* to every new record
*/
public static function boot()
{
parent::boot();

parent::creating(function($model) {

$model->user_id = $model->getCurrentUserId();

/*
* Check if a reason has been set, if not let's retrieve the default first entry reason
*/
if(!$model->reason) $model->reason = trans('inventory::reasons.first_record');

});

parent::created(function($model) {

$model->postCreate();

});

parent::updating(function($model) {

/*
* Retrieve the original quantity before it was updated,
* so we can create generate an update with it
*/
$model->beforeQuantity = $model->getOriginal('quantity');

/*
* Check if a reason has been set, if not let's retrieve the default change reason
*/
if(!$model->reason) $model->reason = trans('inventory::reasons.change');

});

parent::updated(function($model) {

$model->postUpdate();

});
}

/**
* Generates a stock movement on the creation of a stock
*/
public function postCreate()
{
/*
* Only create a first record movement if one isn't created already
*/
if(!$this->getLastMovement()) {

/*
* Generate the movement
*/
$this->generateStockMovement(0, $this->quantity, $this->reason, $this->cost);

}
}

public function postUpdate()
{
$this->generateStockMovement($this->beforeQuantity, $this->quantity, $this->reason, $this->cost);
}

/**
* Performs a quantity update. Automatically determining depending on the quantity entered if stock is being taken
* or added
Expand Down Expand Up @@ -289,8 +380,6 @@ private function processUpdateQuantityOperation($quantity, $reason = '', $cost =
*/
private function processTakeOperation($taking, $reason = '')
{
$before = $this->quantity;

$left = $this->quantity - $taking;

/*
Expand All @@ -304,21 +393,19 @@ private function processTakeOperation($taking, $reason = '')

$this->quantity = $left;

$this->setReason($reason);

$this->dbStartTransaction();

if($this->save()) {

if($this->generateStockMovement($before, $this->quantity, $reason)) {

$this->dbCommitTransaction();

$this->fireEvent('inventory.stock.taken', array(
'stock' => $this,
));
$this->dbCommitTransaction();

return $this;
$this->fireEvent('inventory.stock.taken', array(
'stock' => $this,
));

}
return $this;

}

Expand All @@ -342,7 +429,7 @@ private function processPutOperation($putting, $reason = '', $cost = 0)

$total = $putting + $before;

if($total == $this->quatity) {
if($total == $this->quantity) {

if(!config('inventory::allow_duplicate_movements')) {

Expand All @@ -354,21 +441,21 @@ private function processPutOperation($putting, $reason = '', $cost = 0)

$this->quantity = $total;

$this->dbStartTransaction();
$this->setReason($reason);

if($this->save()) {
$this->setCost($cost);

if($this->generateStockMovement($before, $this->quantity, $reason, $cost)) {
$this->dbStartTransaction();

$this->dbCommitTransaction();
if($this->save()) {

$this->fireEvent('inventory.stock.added', array(
'stock' => $this,
));
$this->dbCommitTransaction();

return $this;
$this->fireEvent('inventory.stock.added', array(
'stock' => $this,
));

}
return $this;

}

Expand Down Expand Up @@ -419,23 +506,24 @@ private function processRollbackOperation($movement, $recursive = false)

$this->quantity = $movement->before;

$this->dbStartTransaction();

if($this->save()) {
$reason = trans('inventory::reasons.rollback', array(
'id' => $movement->id,
'date' => $movement->created_at,
));

$reason = sprintf('Rolled back to movement ID: %s on %s', $movement->id, $movement->created_at);
$this->setReason($reason);

if($this->generateStockMovement($movement->after, $this->quantity, $reason)) {
$this->dbStartTransaction();

$this->dbCommitTransaction();
if($this->save()) {

$this->fireEvent('inventory.stock.rollback', array(
'stock' => $this,
));
$this->dbCommitTransaction();

return $this;
$this->fireEvent('inventory.stock.rollback', array(
'stock' => $this,
));

}
return $this;

}

Expand Down Expand Up @@ -491,6 +579,26 @@ private function generateStockMovement($before, $after, $reason = '', $cost = 0)
return $this->movements()->create($insert);
}

/**
* Sets the cost attribute
*
* @param int $cost
*/
private function setCost($cost = 0)
{
$this->cost = $cost;
}

/**
* Sets the reason attribute
*
* @param string $reason
*/
private function setReason($reason = '')
{
$this->reason = $reason;
}

/**
* Returns true or false if the number inserted is positive
*
Expand Down
16 changes: 15 additions & 1 deletion src/Stevebauman/Inventory/Traits/InventoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ trait InventoryTrait {
*/
use DatabaseTransactionTrait;

/**
* Overrides the models boot function to set the user ID automatically
* to every new record
*/
public static function boot()
{
parent::boot();

parent::creating(function($record) {

$record->user_id = parent::getCurrentUserId();

});
}

/**
* Returns the total sum of the current stock
*
Expand Down Expand Up @@ -119,7 +134,6 @@ public function createStockOnLocation($quantity, $location, $reason = '', $cost
$stock = $this->stocks()->create($insert);

return $stock->put($quantity, $reason, $cost);

}
}

Expand Down
15 changes: 0 additions & 15 deletions src/Stevebauman/Inventory/Traits/UserIdentificationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@

trait UserIdentificationTrait {

/**
* Overrides the models boot function to set the user ID automatically
* to every new record
*/
public static function boot()
{
parent::boot();

parent::creating(function($record) {

$record->user_id = parent::getCurrentUserId();

});
}

/**
* Attempt to find the user id of the currently logged in user
* Supports Cartalyst Sentry/Sentinel based authentication, as well as stock Auth
Expand Down
1 change: 1 addition & 0 deletions src/lang/en/reasons.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

'change' => 'Stock Adjustment',

'rollback' => 'Rolled back to movement ID: :id on :date',
);

0 comments on commit 5ca751b

Please sign in to comment.