PHP code example of nandan108 / slot-flow

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

    

nandan108 / slot-flow example snippets


use Nandan108\SlotFlow\Flow;
use Nandan108\SlotFlow\MovementEngine;
use Nandan108\SlotFlow\Policies\DimensionPriority;
use Nandan108\SlotFlow\QuantityState;
use Nandan108\SlotFlow\SlotSpace;

$space = SlotSpace::define([
    'loc' => ['sup', 'wh1'],
    'stt' => ['fs', 'res', 'sd'],
])
->flow('reserve', static fn (Flow $flow) => $flow
    ->move(['stt' => 'fs'], ['stt' => 'res'])
    ->orderBy(new DimensionPriority(['loc' => ['wh*', 'sup']]))
);

$inventory = new QuantityState($space, [
    ['wh1.fs', 5],
    ['sup.fs', 10],
]);

$result = (new MovementEngine())->execute(
    inventory: $inventory,
    space: $space,
    flow: 'reserve',
    quantity: 6,
    subject: 'SKU-123',
);

$space = SlotSpace::define([
    'loc' => ['sup','wh1', 'wh2'], // sup: supplier, wh*: our warehouses
    'own' => ['S', 'P'],           // S: supplier-owned / P: purchased
    'stt' => ['fs', 'res', 'sd'],  // fs: for-sale, res: reserved, sd: sold
])
->flow('backorder', static fn (Flow $flow) => $flow
    // prioritize stock we already own
    ->move(['stt' => 'fs', 'own' => 'P'], ['stt' => 'res'])

    // prefer warehouse over supplier
    ->orderBy(new DimensionPriority(['loc' => ['wh*', 'sup'],]))

    // fallback: create supplier-owned reservation (backorder)
    // this represents stock that will be ordered from the supplier
    // Note: could also be written ->create('sup.S.res') or ->create(['sup','S','res'])
    ->create(['loc' => 'sup', 'own' => 'S', 'stt' => 'res'])
    // disallow backorders beyond 100
    ->constraint(static fn (MovementEdge $edge, FlowContext $ctx): int|float =>
        max(0, 100 - $ctx->inventory->getSum('sup.S.res|sd')))
);

use Nandan108\SlotFlow\Rules\EdgeRule;
use Nandan108\SlotFlow\Rules\EdgeRuleBase;

$space->edgeRules([
    EdgeRule::allowLabeled('reserve', ['stt' => 'fs'], ['stt' => 'res']),
    EdgeRule::allow(['stt' => 'res'], ['stt' => 'sd']),
], EdgeRuleBase::None);

$result = (new MovementEngine())->execute($before, $space, 'reserve', 6, 'SKU-123');
$after  = $result->applyTo($before);   // $before is untouched

$engine = new MovementEngine(new GreedyFlowSolver(trace: true));
$result = $engine->execute($state, $space, 'reserve', 6);

foreach ($result->trace() as $step) {
    foreach ($step['edges'] as $edge) {
        printf(
            "%s  available=%s movable=%s moved=%s\n",
            $edge['edge'], $edge['available'], $edge['movable'], $edge['moved'],
        );
    }
}