Skip to content

Commit

Permalink
added serial number tracking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Trexology committed Feb 8, 2018
1 parent 97b75c8 commit 7a9d0ec
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 23 deletions.
35 changes: 35 additions & 0 deletions src/Migrations/2017_02_08_115523_add_serial_number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddSerialNumber extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('inventory_stocks', function (Blueprint $table) {
$table->text('serial')->nullable()->after('bin');
});

Schema::table('inventory_stock_movements', function (Blueprint $table) {
$table->text('serial')->nullable()->after('returned');
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('inventory_stocks', function (Blueprint $table) {
$table->dropColumn('serial');
});

Schema::table('inventory_stock_movements', function (Blueprint $table) {
$table->dropColumn('serial');
});
}
}
4 changes: 4 additions & 0 deletions src/Models/InventoryStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class InventoryStock extends Model
{
use InventoryStockTrait;

protected $casts = [
'serial' => 'array',
];

/**
* The belongsTo inventory item relationship.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Models/InventoryStockMovement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class InventoryStockMovement extends Model
{
use InventoryStockMovementTrait;

protected $casts = [
'serial' => 'array',
];

/**
* The belongsTo stock relationship.
*
Expand Down
69 changes: 46 additions & 23 deletions src/Traits/InventoryStockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ trait InventoryStockTrait
*/
protected $beforeQuantity = 0;

/**
* Serial Number for stock movment
*
* @var Array
*/
public $movementSerial = [];

/**
* The hasOne location relationship.
*
Expand Down Expand Up @@ -115,7 +122,7 @@ public static function bootInventoryStockTrait()
*/
public function postCreate()
{
$this->generateStockMovement(0, $this->getAttribute('quantity'), $this->reason, $this->cost, $this->receiver_id, $this->receiver_type);
$this->generateStockMovement(0, $this->getAttribute('quantity'), $this->reason, $this->cost, $this->receiver_id, $this->receiver_type, $this->movementSerial);
}

/**
Expand All @@ -125,7 +132,7 @@ public function postCreate()
*/
public function postUpdate()
{
$this->generateStockMovement($this->beforeQuantity, $this->getAttribute('quantity'), $this->reason, $this->cost, $this->receiver_id, $this->receiver_type);
$this->generateStockMovement($this->beforeQuantity, $this->getAttribute('quantity'), $this->reason, $this->cost, $this->receiver_id, $this->receiver_type, $this->movementSerial);
}

/**
Expand Down Expand Up @@ -157,9 +164,9 @@ public function updateQuantity($quantity, $reason = '', $cost = 0)
*
* @return $this|bool
*/
public function remove($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null)
public function remove($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null, $serial = null)
{
return $this->take($quantity, $reason, $cost, $receiver_id, $receiver_type);
return $this->take($quantity, $reason, $cost, $receiver_id, $receiver_type, $serial);
}

/**
Expand All @@ -174,9 +181,9 @@ public function remove($quantity, $reason = '', $cost = 0, $receiver_id = null,
*
* @return $this|bool
*/
public function take($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null)
public function take($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null, $serial = null)
{
return $this->processTakeOperation($quantity, $reason, $cost, $receiver_id, $receiver_type);
return $this->processTakeOperation($quantity, $reason, $cost, $receiver_id, $receiver_type, $serial);
}

/**
Expand All @@ -188,9 +195,9 @@ public function take($quantity, $reason = '', $cost = 0, $receiver_id = null, $r
*
* @return $this
*/
public function add($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null)
public function add($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null, $serial = null)
{
return $this->put($quantity, $reason, $cost, $receiver_id, $receiver_type);
return $this->put($quantity, $reason, $cost, $receiver_id, $receiver_type, $serial);
}

/**
Expand All @@ -204,9 +211,9 @@ public function add($quantity, $reason = '', $cost = 0, $receiver_id = null, $re
*
* @return $this
*/
public function put($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null)
public function put($quantity, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null, $serial = null)
{
return $this->processPutOperation($quantity, $reason, $cost, $receiver_id, $receiver_type);
return $this->processPutOperation($quantity, $reason, $cost, $receiver_id, $receiver_type,$serial);
}

/**
Expand Down Expand Up @@ -253,7 +260,7 @@ public function rollback($movement = null, $recursive = false)
*
* @return $this|bool
*/
public function returnStock($movement, $collect_amt, $collect_reason, $dispose_amt, $dispose_reason)
public function returnStock($movement, $collect_amt, $collect_reason, $collect_serial, $dispose_amt, $dispose_reason,$dispose_serial)
{
if (!$movement) {
$movement = $this->getLastMovement();
Expand All @@ -265,15 +272,17 @@ public function returnStock($movement, $collect_amt, $collect_reason, $dispose_a
$movement->returned = true; // Prevent duplicate return for this movements
$movement->save();

// $reason = Lang::get('inventory::reasons.rollback', [
// $bal_reason = Lang::get('inventory::reasons.rollback', [
// 'id' => $movement->getOriginal('id'),
// 'date' => $movement->getOriginal('created_at'),
// ]);
$bal_reason = "Balance leftover from stock redistribution. (id: $movement->id)";
$this->put($amt,$collect_reason,0,$movement->receiver_id,$movement->receiver_type);
$this->take($dispose_amt,$dispose_reason);

$bal_serial = array_diff($movement->serial, $collect_serial,$dispose_serial);
$this->put($amt,$collect_reason,0,$movement->receiver_id,$movement->receiver_type,$collect_serial);
$this->take($dispose_amt,$dispose_reason,$dispose_serial);
if ($balance > 0) {
$this->take($balance,$bal_reason,0,$movement->receiver_id,$movement->receiver_type);
$this->take($balance,$bal_reason,0,$movement->receiver_id,$movement->receiver_type,$bal_serial);
}

}
Expand Down Expand Up @@ -429,7 +438,7 @@ protected function processUpdateQuantityOperation($quantity, $reason = '', $cost
*
* @return $this|bool
*/
protected function processTakeOperation($taking, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null)
protected function processTakeOperation($taking, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null, $serial = null)
{
if($this->isValidQuantity($taking) && $this->hasEnoughStock($taking)) {
$available = $this->getAttribute('quantity');
Expand All @@ -444,13 +453,18 @@ protected function processTakeOperation($taking, $reason = '', $cost = 0, $recei
if ((float) $left === (float) $available && !$this->allowDuplicateMovementsEnabled()) {
return $this;
}

$this->setAttribute('quantity', $left);

$this->setReason($reason);
if (is_string($serial)) {
$serial = explode(',', $serial);
}
if ($this->serial) {
$this->serial = array_diff($this->serial,$serial);
}

$this->movementSerial = $serial;
$this->setReason($reason);
$this->setCost($cost);

$this->receiver_id = $receiver_id;
$this->receiver_type = $receiver_type;

Expand Down Expand Up @@ -482,7 +496,7 @@ protected function processTakeOperation($taking, $reason = '', $cost = 0, $recei
*
* @return $this|bool
*/
protected function processPutOperation($putting, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null)
protected function processPutOperation($putting, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null, $serial = null)
{
if($this->isValidQuantity($putting)) {
$current = $this->getAttribute('quantity');
Expand All @@ -495,12 +509,20 @@ protected function processPutOperation($putting, $reason = '', $cost = 0, $recei
return $this;
}

if (is_string($serial)) {
$serial = explode(',', $serial);
}
if ($this->serial) {
$this->serial = array_merge($this->serial,$serial);
}
else{
$this->serial = $serial;
}
$this->quantity = $total;

$this->movementSerial = $serial;
$this->setReason($reason);

$this->setCost($cost);

$this->receiver_id = $receiver_id;
$this->receiver_type = $receiver_type;

Expand Down Expand Up @@ -647,7 +669,7 @@ protected function processRecursiveRollbackOperation(Model $movement)
*
* @return bool|Model
*/
protected function generateStockMovement($before, $after, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null)
protected function generateStockMovement($before, $after, $reason = '', $cost = 0, $receiver_id = null, $receiver_type = null, $serial = null)
{
$movement = $this->movements()->getRelated()->newInstance();

Expand All @@ -660,6 +682,7 @@ protected function generateStockMovement($before, $after, $reason = '', $cost =
}
$movement->setAttribute('reason', $reason);
$movement->setAttribute('cost', $cost);
$movement->setAttribute('serial', $serial);

if($movement->save()) {
return $movement;
Expand Down

0 comments on commit 7a9d0ec

Please sign in to comment.