<?php
require __DIR__ . '/vendor/autoload.php';
$app = require __DIR__ . '/bootstrap/app.php';

use App\Models\Stock;

$stocks = Stock::with('product')->get();
echo "Total Stock Details:\n";
$total = 0;
$byBranch = [];

foreach ($stocks as $s) {
    $v = ($s->quantity ?? 0) * ($s->product->selling_price ?? 0);
    echo sprintf("  - %s (%s): %d × %d = %s\n", 
        $s->product->name, 
        $s->branch, 
        $s->quantity, 
        $s->product->selling_price, 
        number_format($v, 0, ',', '.')
    );
    $total += $v;
    
    if (!isset($byBranch[$s->branch])) {
        $byBranch[$s->branch] = 0;
    }
    $byBranch[$s->branch] += $v;
}

echo "\nBy Branch:\n";
foreach ($byBranch as $branch => $value) {
    echo "  $branch: Rp" . number_format($value, 0, ',', '.') . "\n";
}

echo "\nGrand Total Modal: Rp" . number_format($total, 0, ',', '.') . "\n";
?>
