99 lines
3.2 KiB
PHP
99 lines
3.2 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'] . "\\";
|
|
$code = '';
|
|
if (isset($input['complete'])) {
|
|
foreach (glob($tmp_dirname . "*.tmp") as $filename) {
|
|
$code .= file_get_contents($filename);
|
|
}
|
|
$filename = str_replace(' ', '_', date("d.m.Y H.i.s")) . '.csv';
|
|
file_put_contents($dirname . $filename, $code);
|
|
$files = glob($tmp_dirname . "*");
|
|
foreach ($files as $file) {
|
|
if (is_file($file)) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
$files = glob($dirname . "*.*");
|
|
usort($files, function ($a, $b) {
|
|
return filemtime($a) < filemtime($b);
|
|
});
|
|
foreach ($files as $plk2) {
|
|
$file_array[] = str_replace($dirname, '', $plk2);
|
|
}
|
|
if (array_key_exists(0, $file_array)) {
|
|
$file = date("d.m.Y H:i:s", filemtime($dirname . $file_array['0']));
|
|
} else {
|
|
$file = '';
|
|
}
|
|
$baseCSV = file($dirname . $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
|
|
foreach ($baseCSV as $itemBaseCSV) {
|
|
$arrLineCsv = explode(";", $itemBaseCSV);
|
|
$arrUniqFinish[$arrLineCsv[0] . ";" . $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,
|
|
'filedate' => $file,
|
|
];
|
|
}
|
|
$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+');
|
|
fputs($handle, chr(0xEF) . chr(0xBB) . chr(0xBF)); // BOM
|
|
$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);
|
|
$files = glob($tmp_dirname . "*.*");
|
|
usort($files, function ($a, $b) {
|
|
return filemtime($a) < filemtime($b);
|
|
});
|
|
foreach ($files as $plk2) {
|
|
$file_array[] = str_replace($tmp_dirname, '', $plk2);
|
|
}
|
|
if (array_key_exists(0, $file_array)) {
|
|
$file = date("d.m.Y H:i:s", filemtime($tmp_dirname . $file_array['0']));
|
|
} else {
|
|
$file = '';
|
|
}
|
|
return [
|
|
'status' => 'success',
|
|
'filename' => $filename,
|
|
'filedate' => $file,
|
|
];
|
|
}
|
|
} |