Joywork/ajax/export_excel_calls.php

329 lines
11 KiB
PHP
Raw Permalink Normal View History

2026-05-22 20:21:54 +02:00
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
function get_users($agency_id, $pdo){
$users = array();
$sql = "SELECT * FROM `mango_call_users` WHERE agency_id = ".$agency_id;
$q = $pdo->query($sql);
while($r = $pdo->fetch_assoc($q)){
$temp_user = array();
$temp_user['fio'] = $r['fio'];
$temp_user['groups'] = '';
if(!empty($r['groups'])){
$groups = json_decode($r['groups']);
// var_dump($groups);
if(is_array($groups)){
foreach ($groups as $group) {
$sql_g = "SELECT * FROM mango_call_groups WHERE group_id = ".$group;
$q_g = $pdo->query($sql_g);
if($pdo->num_rows($q_g) > 0){
while($r_g = $pdo->fetch_assoc($q_g)){
if($temp_user['groups'] != '') $temp_user['groups'] .= ', ';
$temp_user['groups'] .= $r_g['name'];
}
}
}
}
}
$users[$r['number_sip']] = $temp_user;
}
return $users;
}
function int_to_time($second) {
$total_seconds = $second;
$seconds = intval($total_seconds%60);
if(strlen($seconds) == 1) $seconds = "0".$seconds;
$seconds .= ' сек.';
$total_minutes = intval($total_seconds/60);
$minutes = $total_minutes%60;
if($minutes == 0) {
$minutes = '';
} else {
if(strlen($minutes) == 1) $minutes = "0".$minutes;
$minutes .= ' м.';
}
$hours = intval($total_minutes/60);
if($hours == 0) {
$hours = '';
} else {
if(strlen($hours) == 1) $hours = "0".$hours;
$hours .= ' ч.';
}
return $hours." ".$minutes." ".$seconds;
}
require_once($_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php');
if ($_SESSION['id'] == 11874) {
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
ini_set('display_errors', 1);
}
set_time_limit(0);
ini_set('memory_limit', '8192M');
// hide_client_contacts: маскируем сырые телефоны в выгрузке звонков.
// ФИО сотрудников из mango_call_users НЕ маскируем — они не клиенты.
$hideContactsCalls = function_exists('should_hide_contacts_for_current_user')
&& should_hide_contacts_for_current_user();
$pdo = new MysqlPdo(hst, ndb, user, pass);
$pdo->query("SET NAMES 'utf8'");
$pdopg = new MysqlPdo(hstpg, ndbpg, userpg, passpg, false, '5432', true);
$columns = [
array('id'=>'date', 'title'=>'Дата звонка'),
array('id'=>'on_out', 'title'=>'Входящий/Исходящий'),
array('id'=>'phoneIn', 'title'=>'Телефон кто звонил'),
array('id'=>'phoneOut', 'title'=>'Номер, на который был звонок'),
array('id'=>'nameEmp', 'title'=>'Имя сотрудника, кто принял звонок'),
array('id'=>'status', 'title'=>'Статус (пропущенный, состоявшийся)'),
array('id'=>'duration', 'title'=>'Длительность звонка'),
array('id'=>'operator', 'title'=>'Оператор'),
];
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$agency_id = 0;
if(isset($_POST['agency_id']) && $_POST['agency_id'] > 0){
$agency_id = (int)$_POST['agency_id'];
}
$sheet->setTitle('Журнал звонков');
$arrColExccel = ['B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF'];
$columnStart = 'A';
$rowStart = 1;
$j = 0;
$arrayHeder = [
$columnStart => [
'title' => '№',
'width' => 5
]
];
$arrRows = [];
$arrSearch = [];
foreach($columns as $col) {
$header = [];
//if ($col['visible'] && ($col['id'] != 'edit' && $col['id'] != 'archive' && $col['id'] != 'links')) {
$arrSearch[$col['id']] = $col;
$header['title'] = $col['title'];
if ($col['id'] == 'fio') {
$header['width'] = 35;
} else {
$header['width'] = 20;
}
$arrayHeder[$arrColExccel[$j]] = $header;
$arrRows[$arrColExccel[$j]] = $col['id'];
$j++;
//}
}
$sql = '';
$calls = [];
if (isset($_SESSION['calls_query'])) {
$sql = $_SESSION['calls_query'];
}
$is_pg = false;
if (isset($_SESSION['calls_query_pg'])) {
$sql = $_SESSION['calls_query_pg'];
$is_pg = true;
}
session_write_close();
$starttime = microtime(true);
if(!empty($sql) && $agency_id > 0){
$users = get_users($agency_id, $pdo);
$calls_arr = array();
if($is_pg){
$qpg = $pdopg->query($sql, true);
while ($calla = $pdopg->fetch_assoc($qpg)) {
$calls_arr[] = $calla;
}
} else {
$q = $pdo->query($sql, true);
while ($calla = $pdo->fetch_assoc($q)) {
$calls_arr[] = $calla;
}
}
//var_dump($calls);
foreach($calls_arr as $r){
//var_dump($r);
$call = [];
$type = 'mango';
$call['start'] = date('d.m.Y H:i:s', $r['start']);
if(!empty($r['from_extension'])){
$call['on_out'] = 'Исходящий';
} else {
$call['on_out'] = 'Входящий';
}
if(isset($users[$r['from_number']])){
$call['from_number'] = $users[$r['from_number']]['fio'];
} else {
// Сырой номер — для hide_client_contacts маскируем как клиентский
$call['from_number'] = $hideContactsCalls
? mask_client_phone($r['from_number'])
: $r['from_number'];
}
if(isset($users[$r['entry_id']])){
$call['entry_id'] = $users[$r['entry_id']]['fio'];
} else {
$call['entry_id'] = $hideContactsCalls
? mask_client_phone($r['entry_id'])
: $r['entry_id'];
}
if(isset($users[$r['to_number']])) {
$call['to_number'] = $users[$r['to_number']]['fio'];
} else {
$call['to_number'] = $hideContactsCalls
? mask_client_phone($r['to_number'])
: $r['to_number'];
}
$call['answer'] = 'Не отвечен';
if($r['answer'] > 0) {
$call['answer'] = 'Отвечен';
}
$call['time'] = int_to_time((int)$r['finish'] - (int)$r['start']);
if($r['line_number'] == 'telphin'){
$type = 'telphin';
} else if($r['line_number'] == 'beeline'){
$type = 'beeline';
} else if($r['line_number'] == 'megafon'){
$type = 'megafon';
} else if($r['line_number'] == 'mts'){
$type = 'mts';
} else if($r['line_number'] == 'tele2'){
$type = 'tele2';
}
$call['line_number'] = $type;
$calls[] = $call;
}
// var_dump($arrSearch);
if (count($calls) > 0) {
foreach($arrayHeder as $h => $w) {
$sheet->setCellValue($h.$rowStart, $w['title']);
$sheet->getColumnDimension($h)->setWidth($w['width']);
$sheet->getStyle($h.$rowStart)->getFont()->applyFromArray( [ 'name' => 'Arial', 'bold' => TRUE, 'italic' => FALSE, 'strikethrough' => FALSE, 'color' => [ 'rgb' => '808080' ], ] );
$sheet->getStyle($h)->getAlignment()->setWrapText(true);
$sheet->getStyle($h.$rowStart)->getAlignment()->setWrapText(true);
}
$i = $rowStart+1;
$k = 1;
foreach ($calls as $call) {
$sheet->setCellValue('A'.$i, $k);
$j = 0;
if (isset($arrSearch['date'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['start']));
$j++;
}
if (isset($arrSearch['on_out'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['on_out']));
$j++;
}
if (isset($arrSearch['phoneIn'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['from_number']));
$j++;
}
if (isset($arrSearch['phoneOut'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['entry_id']));
$j++;
}
if (isset($arrSearch['nameEmp'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['to_number']));
$j++;
}
if (isset($arrSearch['status'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['answer']));
$j++;
}
if (isset($arrSearch['duration'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['time']));
$j++;
}
if (isset($arrSearch['operator'])) {
$sheet->setCellValue($arrColExccel[$j].$i, html_entity_decode($call['line_number']));
$j++;
}
$i++;
$k++;
}
$styleArray = [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => [
'rgb' => '808080'
],
],
],
];
$sheet->getStyle('A' . ($rowStart) . ':' . $arrColExccel[$j-1] . ($rowStart + count($objects)))->applyFromArray($styleArray);
}
$writer = new Xlsx($spreadsheet);
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/server/excel')) {
mkdir($_SERVER['DOCUMENT_ROOT'] . '/server/excel', 0777);
}
$name_file = null;
$name = null;
$date = date('d.m.Y_H-i-s');
$name_file = '/server/excel/my_calls_' . $date . '.xlsx';
$name = 'my_calls_'.$date.'.xlsx';
if ($name_file && $name) {
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($_SERVER['DOCUMENT_ROOT'] . $name_file);
echo json_encode([
'file' => $name_file,
'result' => 'ok',
'name' => $name,
]);
}
}