Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AuditInventory
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\Stock\InventoryAuditService;
6use Illuminate\Console\Command;
7
8class AuditInventory extends Command
9{
10    protected $signature = 'inventory:audit {--repair : Reconcile warehouse totals to the product quantities}';
11
12    protected $description = 'Check warehouse and product stock for mismatches or invalid balances';
13
14    public function handle(InventoryAuditService $audit): int
15    {
16        $issues = $audit->issues();
17        if ($issues->isEmpty()) {
18            $this->info('Inventory is healthy: no stock mismatch found.');
19
20            return self::SUCCESS;
21        }
22
23        $this->table(['Problem', 'Item', 'Expected', 'Actual'], $issues->map(fn ($issue) => array_values($issue)));
24        if ($this->option('repair')) {
25            $count = $audit->repairAggregateMismatches();
26            $remaining = $audit->issues();
27            $this->info("Reconciled {$count} aggregate mismatch(es).");
28
29            return $remaining->isEmpty() ? self::SUCCESS : self::FAILURE;
30        }
31
32        $this->error('Inventory audit failed. Review the rows above or run inventory:audit --repair for aggregate mismatches.');
33
34        return self::FAILURE;
35    }
36}