166 lines
6.4 KiB
PHP
166 lines
6.4 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\ClientsBonus;
|
|
use App\Component\Models\ClientsEmail;
|
|
use App\Component\Models\ClientsGroup;
|
|
use App\Component\Models\ClientsPhone;
|
|
use App\Component\Models\ClientsPresale;
|
|
use App\Console\Commands\HRCCommand;
|
|
use App\Console\Commands\HRCCommandInterface;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class GETClients extends HRCCommand implements HRCCommandInterface
|
|
{
|
|
protected $signature = 'getclients';
|
|
|
|
public function command($input, $output = null)
|
|
{
|
|
if (isset($input['type']) && $input['type'] == 'loyalty') {
|
|
$last_update = Carbon::createFromTimestampUTC($input['last_update'])->timezone('Europe/Minsk');
|
|
$phones = ClientsPhone::where('updated_at', '>=', $last_update)->get();
|
|
$addresses = ClientsAddress::where('updated_at', '>=', $last_update)->get();
|
|
$barcodes = ClientsBarcode::where('updated_at', '>=', $last_update)->get();
|
|
$emails = ClientsEmail::where('updated_at', '>=', $last_update)->get();
|
|
$presales = ClientsPresale::where('updated_at', '>=', $last_update)->get();
|
|
$bonuses = ClientsBonus::where('updated_at', '>=', $last_update)->get();
|
|
$clients = Client::where('updated_at', '>=', $last_update)->get();
|
|
$info = [];
|
|
foreach ($phones as $phone) {
|
|
$info[] = $phone['client_guid'];
|
|
}
|
|
foreach ($addresses as $address) {
|
|
$info[] = $address['client_guid'];
|
|
}
|
|
foreach ($barcodes as $barcode) {
|
|
$info[] = $barcode['client_guid'];
|
|
}
|
|
foreach ($emails as $email) {
|
|
$info[] = $email['client_guid'];
|
|
}
|
|
foreach ($presales as $presale) {
|
|
$info[] = $presale['client_guid'];
|
|
}
|
|
foreach ($bonuses as $bonus) {
|
|
$info[] = $bonus['client_guid'];
|
|
}
|
|
foreach ($clients as $client) {
|
|
$info[] = $client['user_code'];
|
|
}
|
|
$guides = array_values(array_unique($info));
|
|
$email = $phone = $client = [];
|
|
foreach ($guides as $guid) {
|
|
$email_address = Client::getEmail($guid);
|
|
$phone_number = Client::getPhone($guid);
|
|
$client_info = Client::where('user_code', $guid)->first();
|
|
$email[] = array(
|
|
'email' => $email_address
|
|
);
|
|
$phone[] = array(
|
|
'phone' => $phone_number
|
|
);
|
|
$badge[] = array(
|
|
'name' => 'Test',
|
|
'code' => 1
|
|
);
|
|
$bonus_dishes[] = array(
|
|
'code' => ''
|
|
);
|
|
if (ClientsBonus::getBonus($guid) > 0) {
|
|
$bonus_info[] = array(
|
|
'amount' => ClientsBonus::getBonus($guid),
|
|
'dishes' => $bonus_dishes
|
|
);
|
|
} else {
|
|
$bonus_info[] = array(
|
|
'amount' => ClientsBonus::getBonus($guid)
|
|
);
|
|
}
|
|
|
|
$group_info = ClientsGroup::where('code', $client_info['group_id'])->first();
|
|
$client[] = array(
|
|
'id' => $client_info['id'],
|
|
'name' => $client_info['name'],
|
|
'guid' => $guid,
|
|
'presale' => ClientsPresale::getPresale($guid),
|
|
'bonuses' => $bonus_info,
|
|
'barcode' => Client::getBarcode($guid),
|
|
'group_id' => $client_info['group_id'],
|
|
'emails' => $email,
|
|
'phones' => $phone,
|
|
'badges' => $badge
|
|
);
|
|
$groups[] = $group_info['code'];
|
|
$email = $phone = $badge = $bonus_dishes = $bonus_info = [];
|
|
}
|
|
$groups_guides = array_values(array_unique($groups));
|
|
foreach ($groups_guides as $group) {
|
|
$group_info = ClientsGroup::where('code', $group)->first();
|
|
$groups_info[] = array(
|
|
'id' => $group_info['id'],
|
|
'name' => $group_info['name'],
|
|
'guid' => $group_info['code'],
|
|
);
|
|
}
|
|
|
|
return [
|
|
'status' => 'success',
|
|
'clients' => $client,
|
|
'groups' => $groups_info,
|
|
];
|
|
}
|
|
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 = Client::getPhone($client['user_code']);
|
|
$email = Client::getEmail($client['user_code']);
|
|
$address = Client::getAddress($client['user_code']);
|
|
$out[] = array(
|
|
'id' => $client['id'],
|
|
'name' => $client['name'],
|
|
'phone' => $phone,
|
|
'email' => $email,
|
|
'address' => $address,
|
|
);
|
|
}
|
|
return [
|
|
'status' => 'success',
|
|
'clients' => $out,
|
|
'total' => $total,
|
|
'size' => $countPerPage,
|
|
'pages' => ceil($total / $countPerPage),
|
|
'currentGroup' => $group['id'],
|
|
];
|
|
}
|
|
}
|
|
} |