Skip to content

Commit

Permalink
added new DB field
Browse files Browse the repository at this point in the history
added return stock logic
  • Loading branch information
Trexology committed Jan 29, 2018
1 parent 25e27f7 commit dc41180
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function up()
$table->decimal('cost', 8, 2)->default(0)->nullable();
$table->nullableMorphs('receiver');
$table->string('reason')->nullable();
$table->boolean('returned')->default(0);

$table->foreign('stock_id')->references('id')->on('inventory_stocks')
->onUpdate('restrict')
Expand Down
12 changes: 12 additions & 0 deletions src/Traits/InventoryStockMovementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ public function rollback($recursive = false)
{
return $this->stock->rollback($this, $recursive);
}

/**
* Rolls back the current movement.
*
* @param bool $recursive
*
* @return mixed
*/
public function returnStock($collect_amt, $collect_reason, $dispose_amt, $dispose_reason)
{
return $this->stock->returnStock($this, $collect_amt, $collect_reason, $dispose_amt, $dispose_reason);
}
}
36 changes: 36 additions & 0 deletions src/Traits/InventoryStockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,42 @@ public function rollback($movement = null, $recursive = false)
return false;
}

/**
* Return stock assigned to an entity. Stock may be disposed of during the process of returning
*
* @param mixed $movement
* @param bool $recursive
*
* @return $this|bool
*/
public function returnStock($movement, $collect_amt, $collect_reason, $dispose_amt, $dispose_reason)
{
if (!$movement) {
$movement = $this->getLastMovement();
}

$amt = $movement->getAttribute('before') - $movement->getAttribute('after');
$balance = $amt - $dispose_amt - $collect_amt;
if ($amt > 0 && $balance >= 0) {
$movement->returned = true; // Prevent duplicate return for this movements
$movement->save();

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

}

return false;
}

/**
* Rolls back a specific movement.
*
Expand Down

0 comments on commit dc41180

Please sign in to comment.