PHP code example of aldeebhasan / inventorix

1. Go to this page and download the library: Download aldeebhasan/inventorix library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aldeebhasan / inventorix example snippets


use Aldeebhasan\Inventorix\Traits\HasInventory;

class Product extends Model
{
    use HasInventory;
}

use Aldeebhasan\Inventorix\DTOs\StockOperationDto;

// Add stock
$product->addStock(quantity: 100, location: $location);

// Deduct stock
$product->deductStock(quantity: 10, location: $location);

// Set stock to an absolute quantity (reconciliation)
$product->adjustStock(newQuantity: 50, location: $location);

// Transfer between locations
$product->transfer(quantity: 20, from: $warehouseA, to: $warehouseB);

$options = new StockOperationDto(
    transaction: $existingTransaction,   // attach to an open bulk transaction
    causable: $order,                    // the model that caused this operation
    cost: 9.99,                          // explicit cost per unit (null = no cost, false = use model's cost_price)
    note: 'Purchase order #123',
    createdBy: auth()->id(),
    allowNegative: true,                 // allow stock to go below zero for this call
    expiresAt: now()->addHours(2),       // reservation TTL
    serials: ['SN-001', 'SN-002'],       // explicit serial numbers
    lotReference: 'LOT-2024-01',
    externalReference: 'PO-9876',
    reasonCode: 'purchase',
);

$product->addStock(100, $location, $options);

use Aldeebhasan\Inventorix\Facades\Inventorix;

$transaction = Inventorix::bulk(function ($transaction) use ($product, $location) {
    $options = new StockOperationDto(transaction: $transaction);

    $product->addStock(50, $location, $options);
    $anotherProduct->deductStock(5, $location, $options);
});

$reversalTransaction = Inventorix::rollback($transaction);

// Reserve stock
$reservation = $product->reserve(quantity: 5, location: $location);

// Release the reservation (stock returns to available)
$product->releaseReservation($reservation);

// Fulfill the reservation (converts reserved stock to a real deduction)
$product->fulfillReservation($reservation);

// Stock record at a specific location
$stock = $product->stockAt($location);

// Totals (optionally scoped to a location, with or without child locations)
$product->totalStock();
$product->totalStock($location, uct->stockSummary($location);
// Returns: total_quantity, reserved_quantity, available_quantity, locations[], is_low_stock, last_movement_at

// Value of on-hand stock for this product (uses configured costing strategy)
$product->stockValuation($location);

// Total valuation across all stockables or scoped to a location
Inventorix::totalValuation($location);

// Valuation of movements caused by a specific model
Inventorix::valuationByCausable($order);

// Average units deducted per day over the last N days
$product->stockVelocity($location, days: 30);

// How many days until stock runs out at current velocity
$product->daysOfStock($location, velocityDays: 30);

// The calendar day with the highest deductions in the last N days
$product->peakDemandDay($location, days: 90);

// Set a low-stock threshold for a product at a location
$product->setStockThreshold(location: $location, minQuantity: 10, maxQuantity: 500);

// Manually trigger threshold evaluation
$product->checkThresholds($location);

Inventorix::lowStockItems($location);          // scoped to a location
Inventorix::lowStockItems(stockableType: Product::class); // all products

// config/inventorix.php
'serial_tracking' => [
    'enabled' => true,
],

$product->addStock(2, $location, new StockOperationDto(serials: ['SN-A1', 'SN-A2']));
$product->deductStock(1, $location, new StockOperationDto(serials: ['SN-A1']));

$reservation = $product->reserve(1, $location, new StockOperationDto(serials: ['SN-A2']));

use Aldeebhasan\Inventorix\Facades\Inventorix;

Inventorix::beforeAdd(function ($stockable, $quantity, $location, $dto) {
    // called before every addStock
});

Inventorix::afterAdd(function ($stock, $movement) {
    // called after every addStock
});

Inventorix::beforeDeduct(function ($stockable, $quantity, $location, $dto) { });
Inventorix::afterDeduct(function ($stock, $movement) { });

use Aldeebhasan\Inventorix\Enums\CostingStrategy;

class Product extends Model
{
    use HasInventory;

    public function inventorixCostingStrategy(): CostingStrategy
    {
        return CostingStrategy::Average;
    }
}

'events' => [
    'enabled' => true,
    'disable' => ['StockAdded', 'StockDeducted'],
],

// routes/console.php (Laravel 11+)
Schedule::command('inventorix:expire-reservations')->hourly();
Schedule::command('inventorix:prune-movements')->daily();

// config/inventorix.php
return [
    'default_location_id'       => env('INVENTORIX_DEFAULT_LOCATION', null),
    'allow_negative_stock'       => env('INVENTORIX_ALLOW_NEGATIVE', false),
    'reservation_ttl_minutes'    => env('INVENTORIX_RESERVATION_TTL', null),
    'movement_prune_after_days'  => env('INVENTORIX_PRUNE_DAYS', null),
    'costing_strategy'           => env('INVENTORIX_COSTING', 'fifo'), // fifo | lifo | average
    'queue_alerts'               => env('INVENTORIX_QUEUE_ALERTS', false),
    'alert_queue'                => env('INVENTORIX_ALERT_QUEUE', 'default'),
    'events' => [
        'enabled' => true,
        'disable' => [], // short class names, e.g. ['StockAdded']
    ],
    'threshold_cache' => [
        'enabled' => env('INVENTORIX_THRESHOLD_CACHE', true),
        'ttl'     => env('INVENTORIX_THRESHOLD_TTL', 300),
        'store'   => env('INVENTORIX_THRESHOLD_CACHE_STORE', null),
    ],
    'serial_tracking' => [
        'enabled' => env('INVENTORIX_SERIAL_TRACKING', false),
    ],
    // All table names and model classes are swappable via 'tables' and 'models' keys.
];
bash
php artisan vendor:publish --tag="inventorix-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="inventorix-config"
bash
php artisan inventorix:expire-reservations