PHP code example of nomandev / noman-inventory

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

    

nomandev / noman-inventory example snippets


// config/inventory.php

return [
    'tenant_mode'            => null,           // null = single-tenant
    'allow_negative_stock'   => false,
    'allocation_strategy'    => 'fefo',         // fefo | fifo | manual
    'valuation_method'       => 'weighted_average', // weighted_average | fifo | standard_cost
    'reservations_enabled'   => true,
    'reservation_expiry_minutes' => null,
    'batching_enabled'       => true,
    'expiry_alert_days'      => 30,
    'multi_warehouse'        => true,
    'bin_tracking'           => false,
    'routes_enabled'         => true,
    'api_middleware'         => ['api'],
    'route_prefix'           => 'inventory',
    'approval_

use Noman\Inventory\Infrastructure\Persistence\Eloquent\InventoryWarehouse;
use Noman\Inventory\Infrastructure\Persistence\Eloquent\InventoryItem;

$warehouse = InventoryWarehouse::create([
    'id'        => (string) \Illuminate\Support\Str::ulid(),
    'name'      => 'Main Warehouse',
    'code'      => 'WH-MAIN',
    'is_active' => true,
]);

$item = InventoryItem::create([
    'id'          => (string) \Illuminate\Support\Str::ulid(),
    'name'        => 'Paracetamol 500mg',
    'code'        => 'PARA-500',
    'sku'         => 'SKU-PARA-500',
    'is_active'   => true,
    'is_stockable'=> true,
    'industry_profile' => 'pharma_goods',
]);

use Noman\Inventory\Support\Facades\NomanInventoryFacade as NomanInventory;
use Noman\Inventory\Application\DTOs\ReceiveStockDTO;
use Noman\Inventory\Domain\Shared\ValueObjects\{Quantity, Money};

$result = NomanInventory::receive(new ReceiveStockDTO(
    itemId:      $item->id,
    quantity:    Quantity::of(500),
    warehouseId: $warehouse->id,
    unitCost:    Money::of(0.25, 'USD'),
    batchCode:   'BATCH-2024-001',
    expiryDate:  '2026-06-30',
    referenceDocNumber: 'PO-2024-00123',
));

echo $result->documentNumber;  // GRN-20241201-A3F2B1
echo $result->status->label(); // Posted

use Noman\Inventory\Application\DTOs\IssueStockDTO;
use Noman\Inventory\Domain\Shared\Enums\MovementType;

$result = NomanInventory::issue(new IssueStockDTO(
    itemId:        $item->id,
    quantity:      Quantity::of(50),
    warehouseId:   $warehouse->id,
    movementType:  MovementType::SaleOut,
    referenceDocNumber: 'SO-2024-00456',
));

use Noman\Inventory\Application\DTOs\TransferStockDTO;

$result = NomanInventory::transfer(new TransferStockDTO(
    itemId:          $item->id,
    quantity:        Quantity::of(100),
    fromWarehouseId: $warehouseA->id,
    toWarehouseId:   $warehouseB->id,
));

use Noman\Inventory\Application\DTOs\ReserveStockDTO;

$reservationId = NomanInventory::reserve(new ReserveStockDTO(
    itemId:        $item->id,
    quantity:      Quantity::of(20),
    warehouseId:   $warehouse->id,
    referenceType: 'sales_order',
    referenceId:   'SO-2024-00789',
    expiryMinutes: 60,
));

// Later, release it:
NomanInventory::releaseReservation($reservationId);

use Noman\Inventory\Application\DTOs\AdjustStockDTO;

// Positive = stock found; negative = stock lost
$result = NomanInventory::adjust(new AdjustStockDTO(
    itemId:      $item->id,
    quantity:    Quantity::of(-5),   // 5 units short
    warehouseId: $warehouse->id,
    reason:      'Damage found during count',
));

use Noman\Inventory\Application\DTOs\ReverseDocumentDTO;

$reversal = NomanInventory::reverseDocument(new ReverseDocumentDTO(
    documentId: $result->documentId,
    reason:     'Incorrect item received — returning to vendor',
));

$available = NomanInventory::getBalance($item->id, $warehouse->id);
echo $available; // 445

use Noman\Inventory\Application\Actions\StartStockCountAction;
use Noman\Inventory\Application\Actions\CompleteStockCountAction;

// 1. Start the count
$session = app(StartStockCountAction::class)->execute(
    warehouseId: $warehouse->id,
    countDate:   '2024-12-01',
);

// 2. Complete with actual counted quantities
app(CompleteStockCountAction::class)->execute(
    sessionId:  $session->id,
    counts: [
        ['entry_id' => $entryId, 'counted_quantity' => 98],
    ],
    autoAdjust: true,   // posts adjustment docs for all variances
);

// In your AppServiceProvider:
use Noman\Inventory\Contracts\TenantResolverContract;
use Noman\Inventory\Domain\Shared\ValueObjects\TenantId;

$this->app->bind(TenantResolverContract::class, function () {
    return new class implements TenantResolverContract {
        public function getCurrentTenantId(): ?TenantId
        {
            $tenantId = app('current_tenant')?->id; // your tenancy hook
            return $tenantId ? TenantId::of($tenantId) : null;
        }

        public function hasTenant(): bool
        {
            return app('current_tenant') !== null;
        }
    };
});

// config/inventory.php
'bindings' => [
    'tenant_resolver'           => \App\Services\CurrentTenantResolver::class,
    'policy_resolver'           => \App\Services\DatabasePolicyResolver::class,
    'document_number_generator' => \App\Services\SequentialDocumentNumberGenerator::class,
    'stock_allocator'           => \App\Services\ZoneRestrictedAllocator::class,
    'stock_valuator'            => \App\Services\FifoValuator::class,
],

// In EventServiceProvider:
protected $listen = [
    \Noman\Inventory\Domain\Inventory\Events\StockReceived::class => [
        \App\Listeners\NotifyPurchasingOnReceipt::class,
    ],
    \Noman\Inventory\Domain\Inventory\Events\BatchExpired::class => [
        \App\Listeners\AlertQualityControlTeam::class,
    ],
    \Noman\Inventory\Domain\Inventory\Events\StockReserved::class => [
        \App\Listeners\UpdateSalesOrderStatus::class,
    ],
];

$item->update([
    'metadata' => [
        'cow_breed'    => 'Holstein',
        'farm_id'      => 42,
        'vaccination_status' => 'up_to_date',
    ],
]);
bash
# Publish the configuration file
php artisan vendor:publish --tag="noman-inventory-config"

# Publish migrations (then review before running)
php artisan vendor:publish --tag="noman-inventory-migrations"

# Run migrations
php artisan migrate
bash
php artisan vendor:publish --tag="noman-inventory-views"
bash
   php artisan view:clear
   php artisan cache:clear
   
bash
     php artisan vendor:publish --tag="noman-inventory-views" --force