Joywork/ajax/datasource/getClients.php
2026-05-22 21:21:54 +03:00

65 lines
2.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
// Number of records fetch
$numberOfRecords = 10;
$idAgent = (int)$_SESSION['id'];
$agencyId = User::getAgencyIdForUser($idAgent);
// Search text, need check
$search = isset($_POST['searchTerm'])
? mysql_escape_string($_POST['searchTerm'])
: null;
$page = isset($_POST['page']) ? (int)$_POST['page'] : 1;
$userIds = User::getAllAgencyUsers($agencyId);
$sql = "SELECT id FROM clients WHERE ((clients.who_work = $idAgent) OR (clients.who_work = 0 and clients.id_agent in (" . implode(',', $userIds) . "))) AND clients.deleted <> 1 AND clients.cancel = 0";
$andLikeFio = $search
? " and (fio like '%" . trim($search) . "%' or phone like '%" . trim($search) . "%' or email like '%" . trim($search) . "%')"
: " ";
$sqlUnion = " UNION SELECT requisitions.client_id FROM requisitions WHERE ((requisitions.who_work = $idAgent) OR (requisitions.who_work = 0 and requisitions.user_id in (" . implode(',', $userIds) . "))) AND requisitions.deleted <> 1 AND requisitions.cancel = 0";
$rez = mysql_query($sql . $andLikeFio . $sqlUnion);
$clientIds = [];
while ($client = mysql_fetch_assoc($rez)) {
if (!in_array((int)$client['id'], $clientIds)) {
$clientIds[] = (int)$client['id'];
}
}
$allCount = count($clientIds);
$ot = ($page - 1) * $numberOfRecords;
//$all_pages = ceil($allCount / $numberOfRecords); // not used
$orderBy = " ORDER BY fio, ID";
$limit = " LIMIT " . $ot . ', ' . $numberOfRecords;
$sqlClients = "SELECT id, fio, phone, email FROM clients where id IN (" . implode(',', $clientIds) . ")";
$sqlClientsCount = "SELECT COUNT(id) as cnt FROM clients where id IN (" . implode(',', $clientIds) . ")";
$rez = mysql_query($sqlClients . $andLikeFio . $orderBy . $limit);
$rezCount = mysql_query($sqlClientsCount . $andLikeFio);
$response = [];
$response['items'] = [];
$response['total_count'] = mysql_fetch_assoc($rezCount)['cnt'];
while ($block = mysql_fetch_assoc($rez)) {
// Маскируем контактные данные клиента, если у пользователя включено скрытие
mask_client_contacts_inplace($block);
$response['items'][] = array(
"id" => $block['id'],
"text" => $block['fio'] . " (" . $block['email'] . " " . $block['phone'] . ")"
);
}
echo json_encode($response);
exit();