Files
admin-php-module/commands/Clients.php
2021-06-04 13:46:11 +03:00

71 lines
2.2 KiB
PHP

<?php
namespace App\Commands;
use App\Component\Models\Client;
use App\Component\Models\ClientsAddress;
use App\Component\Models\ClientsBarcode;
use App\Component\Models\ClientsEmail;
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();
$email = ClientsEmail::where('client_guid', '=', $client['user_code'])->first();
$address = ClientsAddress::where('client_guid', '=', $client['user_code'])->first();
$barcode = ClientsBarcode::where('client_guid', '=', $client['user_code'])->first();
$group = ClientsGroup::where('code', '=', $client['group_id'])->first();
$out[] = array(
'id' => $client['id'],
'client_group' => $group['id'],
'name' => $client['name'],
'phone' => $phone['phone'],
'email' => $email['email'],
'address' => $address['address'],
'barcode' => $barcode['code_id'],
);
}
return [
'status' => 'success',
'clients' => $out,
'total' => $total,
'size' => $countPerPage,
'pages' => ceil($total / $countPerPage),
'currentGroup' => $group['id'],
];
}
}
}