Skip to content

Commit 16a23e2

Browse files
committed
Added the ability to add cost through take/remove operations. Also added a reverse cost rollback function
1 parent 5ca751b commit 16a23e2

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

src/Stevebauman/Inventory/Traits/InventoryStockTrait.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait InventoryStockTrait {
3333
*
3434
* @var
3535
*/
36-
public $beforeQuantity = 0;
36+
private $beforeQuantity = 0;
3737

3838
/**
3939
* Stores the reason for updating / creating a stock
@@ -114,6 +114,9 @@ public function postCreate()
114114
}
115115
}
116116

117+
/**
118+
* Generates a stock movement after a stock is updated
119+
*/
117120
public function postUpdate()
118121
{
119122
$this->generateStockMovement($this->beforeQuantity, $this->quantity, $this->reason, $this->cost);
@@ -143,27 +146,29 @@ public function updateQuantity($quantity, $reason= '', $cost = 0)
143146
*
144147
* @param $quantity
145148
* @param string $reason
146-
* @return InventoryStock
149+
* @param int $cost
150+
* @return $this|bool|InventoryStockTrait
147151
*/
148-
public function remove($quantity, $reason= '')
152+
public function remove($quantity, $reason= '', $cost = 0)
149153
{
150-
return $this->take($quantity, $reason);
154+
return $this->take($quantity, $reason, $cost);
151155
}
152156

153157
/**
154158
* Processes a 'take' operation on the current stock
155159
*
156160
* @param $quantity
157161
* @param string $reason
158-
* @return InventoryStock
162+
* @param int $cost
163+
* @return $this|bool|InventoryStockTrait
159164
* @throws InvalidQuantityException
160165
* @throws NotEnoughStockException
161166
*/
162-
public function take($quantity, $reason = '')
167+
public function take($quantity, $reason = '', $cost = 0)
163168
{
164169
if($this->isValidQuantity($quantity) && $this->hasEnoughStock($quantity)) {
165170

166-
return $this->processTakeOperation($quantity, $reason);
171+
return $this->processTakeOperation($quantity, $reason, $cost);
167172

168173
}
169174
}
@@ -366,7 +371,7 @@ private function processUpdateQuantityOperation($quantity, $reason = '', $cost =
366371

367372
$taking = $this->quantity - $quantity;
368373

369-
return $this->take($taking, $reason);
374+
return $this->take($taking, $reason, $cost);
370375

371376
}
372377
}
@@ -376,9 +381,10 @@ private function processUpdateQuantityOperation($quantity, $reason = '', $cost =
376381
*
377382
* @param $taking
378383
* @param string $reason
384+
* @param int $cost
379385
* @return $this|bool
380386
*/
381-
private function processTakeOperation($taking, $reason = '')
387+
private function processTakeOperation($taking, $reason = '', $cost = 0)
382388
{
383389
$left = $this->quantity - $taking;
384390

@@ -395,6 +401,8 @@ private function processTakeOperation($taking, $reason = '')
395401

396402
$this->setReason($reason);
397403

404+
$this->setCost($cost);
405+
398406
$this->dbStartTransaction();
399407

400408
if($this->save()) {
@@ -513,6 +521,14 @@ private function processRollbackOperation($movement, $recursive = false)
513521

514522
$this->setReason($reason);
515523

524+
if(config('inventory::rollback_cost')) {
525+
526+
$this->setCost($movement->cost);
527+
528+
$this->reverseCost();
529+
530+
}
531+
516532
$this->dbStartTransaction();
517533

518534
if($this->save()) {
@@ -589,6 +605,22 @@ private function setCost($cost = 0)
589605
$this->cost = $cost;
590606
}
591607

608+
/**
609+
* Reverses the cost of a movement
610+
*/
611+
private function reverseCost()
612+
{
613+
if($this->isPositive($this->cost)) {
614+
615+
$this->setCost(-abs($this->cost));
616+
617+
} else {
618+
619+
$this->setCost(abs($this->cost));
620+
621+
}
622+
}
623+
592624
/**
593625
* Sets the reason attribute
594626
*

src/config/config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
*/
1616
'allow_duplicate_movements' => true,
1717

18+
/*
19+
* When set to true, this will reverse the cost in the rolled back movement.
20+
*
21+
* For example, if the movement's cost that is being rolled back is 500, the rolled back
22+
* movement will be -500.
23+
*/
24+
'rollback_cost' => true,
25+
1826
/*
1927
* Default reason to give when creating a new inventory stock
2028
*/

src/lang/en/reasons.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
'change' => 'Stock Adjustment',
1313

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

0 commit comments

Comments
 (0)