PHP code example of aiarmada / inventory

1. Go to this page and download the library: Download aiarmada/inventory 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/ */

    

aiarmada / inventory example snippets


use AIArmada\Inventory\Contracts\InventoryableInterface;
use AIArmada\Inventory\Traits\HasInventory;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;

class Product extends Model implements InventoryableInterface
{
    use HasUuids, HasInventory;
}

use AIArmada\Inventory\Facades\Inventory;

$product = Product::find($id);
$location = InventoryLocation::where('code', 'WAREHOUSE-A')->first();

// Receive inventory
$product->receive($location->id, 100, 'Initial stock');

// Ship inventory
$product->ship($location->id, 5, 'sale', 'ORDER-123');

// Transfer between locations
$product->transfer($fromLocationId, $toLocationId, 20);

// Check availability
$total = $product->getTotalAvailable();          // All locations
$atLocation = $product->getInventoryAtLocation($locationId);

// Check if sufficient inventory exists
$product->hasInventory(10);  // true if >= 10 available across all locations

use AIArmada\Inventory\Facades\Inventory;
use AIArmada\Inventory\Facades\InventoryAllocation;

// Inventory operations
Inventory::receive($product, $locationId, 100, 'Supplier delivery');
Inventory::ship($product, $locationId, 5, 'sale', 'ORDER-123');
Inventory::transfer($product, $fromId, $toId, 20);
Inventory::getAvailability($product);  // [locationId => available, ...]

// Allocations
InventoryAllocation::allocate($product, 5, 'cart-123', 30);  // Returns Collection<InventoryAllocation>
InventoryAllocation::release($product, 'cart-123');
InventoryAllocation::commit('cart-123', 'ORDER-456');

// config/inventory.php
'allocation_strategy' => 'priority',

// Or via .env
INVENTORY_ALLOCATION_STRATEGY=priority

// In your Product model
public function getAllocationStrategy(): ?AllocationStrategy
{
    return $this->allocation_strategy 
        ? AllocationStrategy::from($this->allocation_strategy)
        : null;  // null = use global config
}

$level = $product->inventoryLevels()->where('location_id', $locationId)->first();
$level->update(['allocation_strategy' => 'single_location']);

// Product needs 100 units, Warehouse A has 60, Warehouse B has 50
$allocations = InventoryAllocation::allocate($product, 100, 'cart-123');

// Returns 2 allocations:
// - 60 from Warehouse A
// - 40 from Warehouse B

// config/inventory.php
'allow_split_allocation' => false,

use AIArmada\Cart\Facades\Cart;

// Allocate inventory for checkout
$allocations = Cart::allocateAllInventory(30);  // 30 min TTL

// Validate availability
$validation = Cart::validateInventory();
if (!$validation['available']) {
    foreach ($validation['issues'] as $issue) {
        // $issue['itemId'], $issue['requested'], $issue['available']
    }
}

// Commit after payment
Cart::commitInventory('ORDER-123');

// Release on abandon
Cart::releaseAllInventory();

$schedule->command('inventory:cleanup-allocations')->everyFiveMinutes();

return [
    'allocation_strategy' => 'priority',
    'allocation_ttl_minutes' => 30,
    'allow_split_allocation' => true,
    'default_reorder_point' => 10,
    
    'cart' => [
        'enabled' => true,
    ],
    
    'payment' => [
        'auto_commit' => true,
    ],
];
bash
php artisan migrate
bash
php artisan vendor:publish --tag=inventory-config
bash
# Clean up expired allocations
php artisan inventory:cleanup-allocations