Files
admin-php-module/commands/ClientFile.php
2021-06-03 16:59:31 +03:00

74 lines
2.4 KiB
PHP

<?php
namespace App\Commands;
use App\Component\Models\Client;
use App\Component\Models\Terminal;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
class ClientFile extends HRCCommand implements HRCCommandInterface {
protected $signature = 'getclientfile';
public function command($input, $output = null) {
$terminal = Terminal::where('soft', '=', 1)->where('is_active', '=', 1)->where('work_code', '=', 1)->first();
$tmp_dirname = __DIR__ . "\\..\\..\\..\\Exchange\\" . $terminal['key'] . "\\tmp\\" . $terminal['key'] . "\\";
$dirname = __DIR__ . "\\..\\..\\..\\Exchange\\" . $terminal['key'] . "\\";
$filename = str_replace(' ', '_', date("d.m.Y H.i.s")) . '.csv';
$code = '';
if (isset($input['complete'])) {
foreach (glob($tmp_dirname . "*.tmp") as $tmp_filename) {
$code .= file_get_contents($tmp_filename);
file_put_contents($dirname . $filename, $code);
unlink($tmp_filename);
}
$baseCSV = file($dirname . $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($baseCSV as $itemBaseCSV) {
$arrLineCsv = explode(";", $itemBaseCSV);
$arrUniqFinish[mb_convert_encoding($arrLineCsv[0], 'CP1251') . ";" . $arrLineCsv[1]] = $arrLineCsv[1];
}
$arrUniqFinish = array_unique($arrUniqFinish);
foreach ($arrUniqFinish as $keyArr => $valueArr) {
$finishSavedCsv[] = $keyArr;
}
file_put_contents($dirname . $filename, implode("\n", $finishSavedCsv));
return [
'status' => 'success',
'filename' => $filename,
'terminalKey' => $terminal['key'],
];
} else {
$clientsCount = Client::count();
$urlThread = $input['th'];
$mainThreads = 4;
$countPerThread = ceil($clientsCount / $mainThreads);
$offset = ($urlThread - 1) * $countPerThread;
$clients = Client::with('clientPhone:client_guid,phone')->skip($offset)->take($countPerThread)->get();
if (!is_dir($tmp_dirname)) {
mkdir($tmp_dirname, 0755, 'w+');
}
$filename = str_replace(' ', '_', date("d.m.Y H.i.s")) . '_' . $urlThread . '.tmp';
$handle = fopen($tmp_dirname . $filename, 'w+');
$filename = str_replace('_', ' ', $filename);
foreach ($clients as $row) {
if (($row['clientPhone']->phone !== '') && ($row['clientPhone']->phone !== '+375 ( ) - -')) {
$row = array($row['name'], $row['clientPhone']->phone);
fputcsv($handle, $row, ';');
}
}
fclose($handle);
return [
'status' => 'success',
'filename' => $filename,
];
}
}
}