Skip to content

Commit

Permalink
further tweak to rollbacks of inventory movements to calculate order …
Browse files Browse the repository at this point in the history
…by id rather than 'created_at' timestamp
  • Loading branch information
dvicklund committed May 26, 2022
1 parent 8bb2574 commit 8837676
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Traits/InventoryStockTrait.php
Original file line number Diff line number Diff line change
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
40 changes: 39 additions & 1 deletion tests/InventoryStockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,50 @@ public function testRollbackStockMovement() {

$txn = $stock->newTransaction();

$movement = $stock->add(20, 'fresh inventory', '50');
$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 8837676

Please sign in to comment.