nhance/app/Commands/DigitMotorImportMasters.php

104 lines
4.0 KiB
PHP

<?php
namespace App\Commands;
use App\Libraries\DigitMotor\DigitMasterImportService;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
/**
* Create Digit motor master tables and import kit Excel files.
*
* Usage:
* php spark digit-motor:import-masters --create-tables
* php spark digit-motor:import-masters --path="/path/to/Masters"
* php spark digit-motor:import-masters --only=products,previous_insurers,ncb
* php spark digit-motor:import-masters --skip-vehicles
*/
class DigitMotorImportMasters extends BaseCommand
{
protected $group = 'DigitMotor';
protected $name = 'digit-motor:import-masters';
protected $description = 'Create motor_master_* tables and import Digit Masters Excel files.';
protected $usage = 'digit-motor:import-masters [--path] [--create-tables] [--only] [--skip-vehicles]';
protected $options = [
'--path' => 'Path to Digit Masters folder (xlsx files).',
'--create-tables' => 'Run digit_motor_master_tables.sql first.',
'--only' => 'Comma-separated master keys to import.',
'--skip-vehicles' => 'Skip large vehicle master import.',
];
public function run(array $params)
{
$defaultPath = '/home/smart/Downloads/Digit API Integration_API kit (1)/Masters';
$path = CLI::getOption('path') ?: env('DIGIT_MOTOR_MASTERS_PATH', $defaultPath);
$path = rtrim((string) $path, '/');
if (!is_dir($path)) {
CLI::error('Masters path not found: ' . $path);
return EXIT_ERROR;
}
$service = new DigitMasterImportService($path);
$service->setProgressCallback(static function (string $msg) {
CLI::write($msg, 'yellow');
});
if (CLI::getOption('create-tables') !== null) {
$sql = ROOTPATH . 'app/Database/digit_motor_master_tables.sql';
CLI::write('Creating tables from ' . $sql, 'green');
try {
$service->createTables($sql);
CLI::write('Tables created / verified.', 'green');
} catch (\Throwable $e) {
CLI::error('Create tables failed: ' . $e->getMessage());
return EXIT_ERROR;
}
}
$onlyOpt = CLI::getOption('only');
// CI may return true for `--only=vehicles` when value parsing fails — also check argv
if ($onlyOpt === true || $onlyOpt === null || $onlyOpt === '') {
foreach ($_SERVER['argv'] ?? [] as $arg) {
if (str_starts_with((string) $arg, '--only=')) {
$onlyOpt = substr((string) $arg, 7);
break;
}
}
}
$only = [];
if (is_string($onlyOpt) && $onlyOpt !== '' && $onlyOpt !== '1') {
$only = array_filter(array_map('trim', explode(',', $onlyOpt)));
}
if (CLI::getOption('skip-vehicles') !== null) {
if (!$only) {
$only = [
'products', 'previous_insurers', 'ncb', 'voluntary_deductible',
'previous_policy_type', 'doc_types', 'nominee_relations', 'states',
'sub_products', 'addon_age_limits', 'pincodes', 'rtos',
];
} else {
$only = array_values(array_diff($only, ['vehicles']));
}
}
CLI::write('Importing from: ' . $path, 'green');
$results = $service->importAll($only);
CLI::newLine();
CLI::write(str_pad('Master', 28) . str_pad('Rows', 10) . 'Status', 'white');
CLI::write(str_repeat('-', 55));
foreach ($results as $key => $res) {
$line = str_pad($key, 28) . str_pad((string) $res['rows'], 10) . $res['status'];
if (!empty($res['message'])) {
$line .= ' — ' . $res['message'];
}
CLI::write($line, $res['status'] === 'OK' ? 'green' : 'red');
}
$failed = array_filter($results, static fn ($r) => $r['status'] !== 'OK');
return $failed ? EXIT_ERROR : EXIT_SUCCESS;
}
}