fix inventory
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\Inventory\InventoryMovementService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class InventoryReconcile extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'inventory:reconcile
|
||||
{--school-year= : The school year to reconcile (e.g., 2025-2026). Defaults to current.}
|
||||
{--fix : Automatically fix discrepancies by creating adjustment movements.}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Reconcile inventory item quantities against movement ledger totals';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(InventoryMovementService $movementService)
|
||||
{
|
||||
$schoolYear = $this->option('school-year');
|
||||
$autoFix = $this->option('fix');
|
||||
|
||||
$result = $movementService->reconcile($schoolYear);
|
||||
|
||||
$this->info("School Year: {$result['school_year']}");
|
||||
$this->info("Total Items: {$result['total_items']}");
|
||||
$this->info("Discrepancies: {$result['discrepancies_count']}");
|
||||
|
||||
if (empty($result['discrepancies'])) {
|
||||
$this->info('✅ All item quantities match movement ledger.');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$this->table(
|
||||
['ID', 'Name', 'Stored Qty', 'Movement Sum', 'Variance'],
|
||||
collect($result['discrepancies'])->map(fn ($d) => [
|
||||
$d['id'],
|
||||
$d['name'],
|
||||
$d['stored_quantity'],
|
||||
$d['movement_sum'],
|
||||
$d['variance'],
|
||||
])->toArray()
|
||||
);
|
||||
|
||||
if ($autoFix) {
|
||||
$this->info('Fixing discrepancies...');
|
||||
foreach ($result['discrepancies'] as $d) {
|
||||
if ($d['variance'] !== 0) {
|
||||
$movementService->recordMovement(
|
||||
itemId: $d['id'],
|
||||
qtyChange: -$d['variance'],
|
||||
type: 'adjust',
|
||||
reason: 'Auto-reconciliation: stored qty differed from movement sum',
|
||||
note: 'Reconciled from ' . $d['stored_quantity'] . ' to ' . $d['movement_sum'],
|
||||
performedBy: null
|
||||
);
|
||||
$this->line(" Fixed item #{$d['id']} ({$d['name']}): variance was {$d['variance']}");
|
||||
}
|
||||
}
|
||||
$this->info('✅ Fix applied.');
|
||||
} else {
|
||||
$this->warn('Run with --fix to automatically correct discrepancies.');
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user