190 lines
7.2 KiB
PHP
190 lines
7.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Manual test for EmployeePolicyModel global search.
|
|
*
|
|
* Usage:
|
|
* php spark test:employee-global-search
|
|
* php spark test:employee-global-search --search Self
|
|
* php spark test:employee-global-search --client_id cede2d63a7c04ebd4cb55a2228c7141a --client_policy_id 5769 --client_branch_id 444 --search john
|
|
*/
|
|
|
|
namespace App\Commands;
|
|
|
|
use App\Models\EmployeePolicyModel;
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
use CodeIgniter\CLI\CLI;
|
|
use Config\Database;
|
|
|
|
class TestEmployeeGlobalSearch extends BaseCommand
|
|
{
|
|
protected $group = 'Testing';
|
|
protected $name = 'test:employee-global-search';
|
|
protected $description = 'Test getEmployeePolicy global search (emp_code, name, mobile, email, tpa_id, dob, relationship, status)';
|
|
protected $usage = 'test:employee-global-search [--client_id ...] [--client_policy_id ...] [--client_branch_id ...] [--search ...]';
|
|
|
|
protected $options = [
|
|
'--client_id' => 'Client id (md5 hash or numeric)',
|
|
'--client_policy_id' => 'Client policy id',
|
|
'--client_branch_id' => 'Client branch id',
|
|
'--search' => 'Global search term',
|
|
'--limit' => 'Max rows to print (default 10)',
|
|
];
|
|
|
|
public function run(array $params)
|
|
{
|
|
$clientId = $this->optionValue($params, 'client_id', 'cede2d63a7c04ebd4cb55a2228c7141a');
|
|
$clientPolicyId = $this->optionValue($params, 'client_policy_id', '5769');
|
|
$clientBranchId = $this->optionValue($params, 'client_branch_id', '444');
|
|
$search = $this->optionValue($params, 'search', '');
|
|
$limit = (int) $this->optionValue($params, 'limit', '10');
|
|
|
|
$model = new EmployeePolicyModel();
|
|
|
|
$withoutSearch = $model->getEmployeePolicy(
|
|
client_id: $clientId,
|
|
policy_id: $clientPolicyId,
|
|
status: 0,
|
|
branch_id: $clientBranchId,
|
|
status_type: 'hr'
|
|
);
|
|
|
|
// Fallback to a real active employee_policy row when defaults have no data
|
|
if (count($withoutSearch) === 0 && !$this->hasExplicitIds($params)) {
|
|
$fixture = $this->findSampleIds();
|
|
if ($fixture === null) {
|
|
CLI::error('No active employee_policy rows found to test against.');
|
|
|
|
return;
|
|
}
|
|
|
|
$clientId = (string) $fixture['client_id'];
|
|
$clientPolicyId = (string) $fixture['client_policy_id'];
|
|
$clientBranchId = (string) $fixture['client_branch_id'];
|
|
|
|
CLI::write('Default IDs had no rows. Using sample from DB:', 'yellow');
|
|
CLI::write(" client_id : {$clientId}");
|
|
CLI::write(" client_policy_id : {$clientPolicyId}");
|
|
CLI::write(" client_branch_id : {$clientBranchId}");
|
|
CLI::newLine();
|
|
|
|
$withoutSearch = $model->getEmployeePolicy(
|
|
client_id: $clientId,
|
|
policy_id: $clientPolicyId,
|
|
status: 0,
|
|
branch_id: $clientBranchId,
|
|
status_type: 'hr'
|
|
);
|
|
}
|
|
|
|
if ($search === '' && count($withoutSearch) > 0) {
|
|
$search = (string) ($withoutSearch[0]['emp_code'] ?? $withoutSearch[0]['name'] ?? 'Self');
|
|
CLI::write("No --search given. Using sample term from first row: {$search}", 'yellow');
|
|
CLI::newLine();
|
|
}
|
|
|
|
CLI::write('Params:', 'yellow');
|
|
CLI::write(" client_id : {$clientId}");
|
|
CLI::write(" client_policy_id : {$clientPolicyId}");
|
|
CLI::write(" client_branch_id : {$clientBranchId}");
|
|
CLI::write(' search : ' . ($search !== '' ? $search : '(none)'));
|
|
CLI::newLine();
|
|
|
|
$withSearch = $model->getEmployeePolicy(
|
|
client_id: $clientId,
|
|
policy_id: $clientPolicyId,
|
|
status: 0,
|
|
branch_id: $clientBranchId,
|
|
status_type: 'hr',
|
|
search: $search
|
|
);
|
|
|
|
CLI::write('Result counts:', 'green');
|
|
CLI::write(' without search : ' . count($withoutSearch));
|
|
CLI::write(' with search : ' . count($withSearch));
|
|
CLI::newLine();
|
|
|
|
$rows = $search !== '' ? $withSearch : $withoutSearch;
|
|
$preview = array_slice($rows, 0, max(1, $limit));
|
|
|
|
CLI::write('Preview fields (emp_code | name | mobile | email | tpa_id | dob | relationship | status):', 'yellow');
|
|
foreach ($preview as $i => $row) {
|
|
CLI::write(sprintf(
|
|
'%d) %s | %s | %s | %s | %s | %s | %s | %s',
|
|
$i + 1,
|
|
$row['emp_code'] ?? '',
|
|
$row['name'] ?? '',
|
|
$row['mobile'] ?? '',
|
|
$row['email_corporate'] ?? '',
|
|
$row['tpa_id'] ?? '',
|
|
$row['formatted_dob'] ?? ($row['dob'] ?? ''),
|
|
$row['relationship'] ?? '',
|
|
$row['status'] ?? ($row['emp_status'] ?? '')
|
|
));
|
|
}
|
|
|
|
if (count($rows) > $limit) {
|
|
CLI::write('... ' . (count($rows) - $limit) . ' more row(s)');
|
|
}
|
|
|
|
if ($search !== '' && count($withSearch) > 0) {
|
|
CLI::newLine();
|
|
CLI::write('Matched sample JSON:', 'green');
|
|
CLI::write(json_encode(array_map(static function ($row) {
|
|
return [
|
|
'emp_code' => $row['emp_code'] ?? null,
|
|
'name' => $row['name'] ?? null,
|
|
'mobile' => $row['mobile'] ?? null,
|
|
'email_corporate' => $row['email_corporate'] ?? null,
|
|
'tpa_id' => $row['tpa_id'] ?? null,
|
|
'dob' => $row['dob'] ?? null,
|
|
'formatted_dob' => $row['formatted_dob'] ?? null,
|
|
'relationship' => $row['relationship'] ?? null,
|
|
'status' => $row['status'] ?? null,
|
|
'emp_status' => $row['emp_status'] ?? null,
|
|
];
|
|
}, array_slice($withSearch, 0, 3)), JSON_PRETTY_PRINT));
|
|
} elseif ($search !== '') {
|
|
CLI::write('No rows matched the search term.', 'red');
|
|
}
|
|
}
|
|
|
|
private function optionValue(array $params, string $name, string $default = ''): string
|
|
{
|
|
$value = $params[$name] ?? CLI::getOption($name);
|
|
if ($value === null || $value === false || $value === '') {
|
|
return $default;
|
|
}
|
|
|
|
return (string) $value;
|
|
}
|
|
|
|
private function hasExplicitIds(array $params): bool
|
|
{
|
|
foreach (['client_id', 'client_policy_id', 'client_branch_id'] as $key) {
|
|
$value = $params[$key] ?? CLI::getOption($key);
|
|
if ($value !== null && $value !== false && $value !== '') {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function findSampleIds(): ?array
|
|
{
|
|
$db = Database::connect('default');
|
|
$row = $db->query(
|
|
"SELECT emp.client_id, ep.client_policy_id, emp.client_branch_id
|
|
FROM employee_polices ep
|
|
JOIN employees emp ON emp.id = ep.employee_id
|
|
WHERE ep.is_active = 1
|
|
AND emp.is_active = 1
|
|
ORDER BY ep.id DESC
|
|
LIMIT 1"
|
|
)->getRowArray();
|
|
|
|
return $row ?: null;
|
|
}
|
|
}
|