62 lines
2.6 KiB
PHP
62 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Accounting\Console;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Accounting\Services\AccountingHealthService;
|
|
use Modules\Accounting\Services\ExchangeRateService;
|
|
use Modules\Accounting\Services\InstallmentService;
|
|
|
|
class AccountingMaintenanceCommand extends Command
|
|
{
|
|
protected $signature = 'accounting:maintenance {--business_id= : Limit to one business}';
|
|
|
|
protected $description = 'Run accounting health checks and mark overdue installments';
|
|
|
|
public function handle(AccountingHealthService $healthService, InstallmentService $installmentService, ExchangeRateService $exchangeRateService): int
|
|
{
|
|
$businessId = $this->option('business_id');
|
|
|
|
$overdue = $installmentService->markOverdue($businessId ? (int) $businessId : null);
|
|
$this->info("Marked {$overdue} installment(s) as overdue (penalties applied where configured).");
|
|
|
|
$query = DB::table('business');
|
|
if ($businessId) {
|
|
$query->where('id', $businessId);
|
|
}
|
|
|
|
foreach ($query->pluck('id') as $id) {
|
|
$fx = $exchangeRateService->syncForBusiness((int) $id, now()->toDateString(), true);
|
|
$this->line("Business #{$id} FX sync: upserted={$fx['upserted']} api={$fx['api_fetched']}");
|
|
|
|
$health = $healthService->assess((int) $id);
|
|
$this->line("Business #{$id}: score={$health['score']} status={$health['status']}");
|
|
|
|
foreach ($health['checks'] as $check) {
|
|
if (! $check['ok']) {
|
|
$this->warn(" - {$check['key']}: {$check['message']}");
|
|
}
|
|
}
|
|
|
|
if (config('accounting.rollout.location_default_mapping_sync', false)) {
|
|
$mappingReport = app(\Modules\Accounting\Services\LocationDefaultMappingService::class)->assess((int) $id);
|
|
$incomplete = ($mappingReport['total_locations'] ?? 0) - ($mappingReport['complete_locations'] ?? 0);
|
|
if ($incomplete > 0) {
|
|
$this->warn(" - location_mappings: {$incomplete} location(s) have incomplete default mappings");
|
|
}
|
|
}
|
|
|
|
if (config('accounting.features.consolidation', false)) {
|
|
$readiness = app(\Modules\Accounting\Services\ConsolidationService::class)->readiness((int) $id);
|
|
if (! $readiness['ready']) {
|
|
$this->warn(' - consolidation: not ready ('.implode(', ', $readiness['issues']).')');
|
|
}
|
|
}
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|