baks-dev/products-stocks
Модуль складского учета продукции для PHP 8.4+. Установка через Composer, установка конфигурации и ресурсов командой baks:assets:install, миграции Doctrine для обновления схемы БД, тесты PHPUnit (group=products-stocks).
Installation
composer require baks-dev/products-stocks
php bin/console baks:assets:install
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
config/app.php under providers.First Use Case: Check Stock Levels
Inject the StockService into a controller or service:
use BaksDev\ProductsStocks\Services\StockService;
public function __construct(private StockService $stockService) {}
public function checkStock(Request $request, Product $product) {
$stock = $this->stockService->getStock($product->id);
return response()->json(['stock' => $stock]);
}
Key Classes to Explore
StockService: Core service for stock operations.StockRepository: Eloquent repository for stock records.StockEvent: Event listeners for stock changes (e.g., StockUpdated).StockMiddleware: Middleware for stock validation (if provided).Stock Management
$this->stockService->updateStock($productId, $quantityChange, $userId);
$this->stockService->reserveStock($productId, $quantity, $orderId);
$this->stockService->releaseReservedStock($orderId);
Integration with Product Module
ProductCreated/ProductUpdated events to initialize stock records:
public function handle(ProductEvent $event) {
$this->stockService->initializeStock($event->product);
}
API Endpoints
/api/stock/{productId} (GET/PUT/PATCH for stock adjustments).return new StockResource($this->stockService->getStock($productId));
Batch Operations
$this->stockService->bulkUpdateStock($productIds, $quantities);
DB::transaction(function () use ($productId, $quantity) {
$this->stockService->updateStock($productId, $quantity);
// Other related operations (e.g., order creation)
});
Cache::remember("stock_{$productId}", now()->addHours(1), function () use ($productId) {
return $this->stockService->getStock($productId);
});
StockLow, StockOutOfStock):
event(new StockLow($productId, $threshold));
Race Conditions
DB::transaction() or optimistic locking:
$stock = Stock::where('product_id', $productId)->lockForUpdate()->first();
Negative Stock
if ($newStock < 0) {
throw new \InvalidArgumentException("Stock cannot be negative");
}
Migration Conflicts
--force cautiously:
php bin/console doctrine:migrations:migrate --force
Locale/Translation Issues
resources/lang/en/products-stocks.php:
return [
'stock' => 'Inventory',
'reserved' => 'Reserved',
];
StockService:
\Log::info('Stock updated', [
'product_id' => $productId,
'change' => $quantityChange,
'user_id' => $userId,
]);
.env:
DOCTRINE_ORM_LOGGING=true
php bin/phpunit --group=products-stocks --filter=StockServiceTest
Custom Stock Rules
StockService to add business logic (e.g., minimum stock thresholds):
class CustomStockService extends StockService {
public function checkMinimumStock(Product $product, int $threshold) {
$stock = $this->getStock($product->id);
if ($stock < $threshold) {
event(new StockLow($product->id, $threshold));
}
}
}
Additional Stock Types
damaged_stock) via a migration and extend the Stock model:
public function getDamagedStockAttribute() {
return $this->attributes['damaged_stock'] ?? 0;
}
Webhooks/Notifications
StockUpdated events to trigger webhooks or notifications:
public function handle(StockUpdated $event) {
Notification::route('mail', $event->user->email)
->notify(new StockAlert($event->product, $event->newStock));
}
Multi-Warehouse Support
Stock model to include warehouse_id and update queries:
$stock = $this->stockService->getStock($productId, $warehouseId);
How can I help you explore Laravel packages today?