Files
admin-php-module/commands/Clients.php
2021-05-31 16:11:03 +03:00

56 lines
1.6 KiB
PHP

<?php
namespace App\Commands;
use App\Component\Models\Client;
use App\Component\Models\ClientsGroup;
use App\Component\Models\ClientsPhone;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
class Clients extends HRCCommand implements HRCCommandInterface {
protected $signature = 'getclients';
public function command($input, $output = null) {
if (ClientsGroup::where('code', '0')->count() == 0) {
$group = new ClientsGroup([
'code' => '0',
'name' => 'Без группы',
]);
$group->save();
}
$countPerPage = 25;
$offset = ($input['page'] - 1) * $countPerPage;
if ($input['group_id'] == 0) {
$group = ClientsGroup::where('code', '=', $input['group_id'])->first();
} else {
$group = ClientsGroup::where('id', '=', $input['group_id'])->first();
}
$clients = Client::where('group_id', '=', $group['code'])->skip($offset)->take($countPerPage)->get();
$total = Client::where('group_id', '=', $group['code'])->count();
if ($total == NULL) {
return [
'status' => 'success',
'clients' => array(),
'total' => 0,
'size' => $countPerPage,
'pages' => 1,
'currentGroup' => $group['id'],
];
} else {
foreach ($clients as $client) {
$phone = ClientsPhone::where('client_guid', '=', $client['user_code'])->first();
$out[] = array('id' => $client['id'], 'name' => $client['name'], 'phone' => $phone['phone']);
}
return [
'status' => 'success',
'clients' => $out,
'total' => $total,
'size' => $countPerPage,
'pages' => ceil($total / $countPerPage),
'currentGroup' => $group['id'],
];
}
}
}