A adjustStock method
This commit is contained in:
parent
f13f225cec
commit
8dbf70168c
|
|
@ -79,6 +79,30 @@ trait HasStocks
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function adjustStock(
|
||||||
|
StockType $type,
|
||||||
|
int $quantity,
|
||||||
|
\DateTimeInterface|null $until = null,
|
||||||
|
?StockStatus $status = null,
|
||||||
|
) {
|
||||||
|
if (!$this->manage_stock) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->stocks()->create([
|
||||||
|
'quantity' => $type === StockType::INCREASE ? $quantity : -$quantity,
|
||||||
|
'type' => $type,
|
||||||
|
'status' => $status ?? StockStatus::COMPLETED,
|
||||||
|
'expires_at' => $until,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->logStockChange($type === StockType::INCREASE ? $quantity : -$quantity, 'adjust');
|
||||||
|
|
||||||
|
$this->save();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public function reserveStock(
|
public function reserveStock(
|
||||||
int $quantity,
|
int $quantity,
|
||||||
$reference = null,
|
$reference = null,
|
||||||
|
|
|
||||||
|
|
@ -193,7 +193,7 @@ class ProductStockTest extends TestCase
|
||||||
|
|
||||||
// Expired reservations should be counted in available stock
|
// Expired reservations should be counted in available stock
|
||||||
$available = $product->reservations()->get();
|
$available = $product->reservations()->get();
|
||||||
|
|
||||||
$this->assertEquals(0, $available->count());
|
$this->assertEquals(0, $available->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -275,4 +275,41 @@ class ProductStockTest extends TestCase
|
||||||
$this->assertCount(1, $reservations);
|
$this->assertCount(1, $reservations);
|
||||||
$this->assertEquals($active->id, $reservations->first()->id);
|
$this->assertEquals($active->id, $reservations->first()->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function can_adjust_stock()
|
||||||
|
{
|
||||||
|
$product = Product::factory()->create(['manage_stock' => true]);
|
||||||
|
|
||||||
|
$product->increaseStock(20);
|
||||||
|
$this->assertEquals(20, $product->getAvailableStock());
|
||||||
|
|
||||||
|
$product->adjustStock(
|
||||||
|
type: \Blax\Shop\Enums\StockType::DECREASE,
|
||||||
|
quantity: 5
|
||||||
|
);
|
||||||
|
$this->assertEquals(15, $product->getAvailableStock());
|
||||||
|
|
||||||
|
$product->adjustStock(
|
||||||
|
type: \Blax\Shop\Enums\StockType::INCREASE,
|
||||||
|
quantity: 10
|
||||||
|
);
|
||||||
|
$this->assertEquals(25, $product->getAvailableStock());
|
||||||
|
|
||||||
|
// Also with until
|
||||||
|
$product->adjustStock(
|
||||||
|
type: \Blax\Shop\Enums\StockType::DECREASE,
|
||||||
|
quantity: 5,
|
||||||
|
until: now()->addDay()
|
||||||
|
);
|
||||||
|
$this->assertEquals(20, $product->getAvailableStock());
|
||||||
|
|
||||||
|
$this->travel(23)->hours();
|
||||||
|
|
||||||
|
$this->assertEquals(20, $product->getAvailableStock());
|
||||||
|
|
||||||
|
$this->travel(2)->days();
|
||||||
|
|
||||||
|
$this->assertEquals(25, $product->getAvailableStock());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue