Joywork/ajax/calendarEvents.php

563 lines
22 KiB
PHP
Raw Normal View History

2026-05-22 20:21:54 +02:00
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
echo '[';
if ($_SESSION['id']) {
$post = clearInputData($_POST);
$startDateWhere = "1";
$endDateWhere = "1";
if (isset($post['start'])) {
$startDateWhere = "schedule_date > '".$post['start']."'";
}
if (isset($post['end'])) {
$endDateWhere = "schedule_date < '".$post['end']."'";
}
$where = "1";
if(isset($post['active']) && $post['active'] == 1){
$where = "calendar_view = 0";
}
2026-07-02 15:52:44 +02:00
// Общее хранилище deal_id для дедупликации
$seenDealIds = array();
$deals = array();
2026-07-02 11:47:14 +02:00
// Явные колонки (НЕ e.*): user_object_events получил 19-ю колонку moved_on_copy → e.* стал 20, а вторая ветка
// UNION = 19 → запрос падал с «different number of columns» → задачи по своим объектам пропадали из календаря.
2026-07-02 15:52:44 +02:00
$sql = "SELECT e.id, e.user_id, e.from_id, e.object_id, e.req_id, e.create_date, e.type, e.address, e.sum, e.document_id, e.schedule_date, e.comment, e.platforms_advert, e.calendar_view, e.cancel, e.tel, e.max, e.name, e.deal_id, o.nazv from user_object_events e, objects o WHERE user_id = $_SESSION[id] and o.id=e.object_id and schedule_date is not null and $where and $startDateWhere and $endDateWhere";
2026-05-22 20:21:54 +02:00
$sql .= "union
2026-07-02 15:52:44 +02:00
SELECT s.id, s.user_for as user_id, 0 as from_id, e.object_id as object_id, 0 as req_id, created_at as create_date, 'sub_moderation' as type,
'' as address,null as sum, null as document_id, created_at as schedule_date, sub_comment as comment, '' as platforms_advert, e.calendar_view,e.cancel, 0 as tel, 0 as max, '' as name, 0 as deal_id,
2026-05-22 20:21:54 +02:00
o.nazv from user_sub_comments as s LEFT JOIN user_object_events as e on e.id=s.event_id LEFT JOIN objects as o on o.id=object_id WHERE section_id = 1 and user_for = $_SESSION[id] and $where and $startDateWhere and $endDateWhere";
$rez = mysql_query($sql);
/*if($_SESSION['id'] == 21113){
echo $sql;
}*/
$i = 0;
while ($event = mysql_fetch_assoc($rez)) {
2026-07-02 15:52:44 +02:00
// Дедупликация сделок
if ($event['type'] == 'deal' && !empty($event['deal_id'])) {
if (in_array($event['deal_id'], $seenDealIds)) {
continue;
}
$seenDealIds[] = $event['deal_id'];
}
2026-05-22 20:21:54 +02:00
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
2026-07-02 15:52:44 +02:00
$objectId = ($event['type'] == 'deal') ? '0' : $event['object_id'];
echo '"object_id" : "'.$objectId.'",';
2026-05-22 20:21:54 +02:00
$event['nazv'] = str_replace('"', "'", $event['nazv']);
$event['nazv'] = str_replace('&quot;', "'", $event['nazv']);
if ($event['type'] == 'moderation') {
$className = "moderation";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Модерация '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'sub_moderation') {
$className = "moderation";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Комментарий к модерация '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'call') {
$className = "call";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Звонок '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'meet') {
$className = "meet";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Встреча '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'show') {
$className = "showing";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Показ '.$event['nazv'].'","className" : "'.$className.'"';
}
if ($event['type'] == 'deal') {
$className = "deal";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
2026-07-02 15:52:44 +02:00
// Подтягиваем название сделки
$dealTitle = $event['nazv'];
if (!empty($event['deal_id'])) {
if (!isset($deals[$event['deal_id']])) {
$q_deal = mysql_query("SELECT title FROM deals WHERE id = " . intval($event['deal_id']) . " LIMIT 1");
if ($q_deal && mysql_num_rows($q_deal) > 0) {
$deals[$event['deal_id']] = mysql_fetch_assoc($q_deal)['title'];
} else {
$deals[$event['deal_id']] = '';
}
}
$dealTitle = $deals[$event['deal_id']] ?: $event['nazv'];
}
echo '"title": "'.str_replace('"', "'", $dealTitle).'","className" : "'.$className.'"';
2026-05-22 20:21:54 +02:00
}
if ($event['type'] == 'even') {
$className = "even";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "'.$event['name']. ' ' . $event['nazv'] .'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
2026-07-02 11:47:14 +02:00
// Найденные листинги парсера: задачи пользователя из своей таблицы. Offset-free: object_id = РЕАЛЬНЫЙ el.id
// (реальный el.id коллизит с objects.id), листинг различаем по флагу is_external=1.
// table_id = реальный id события — завершение адресует конкретную задачу. LEFT JOIN: листинг мог уйти по TTL,
// задача остаётся (адрес тогда пустой). Комментарии — не задачи, пропускаем. deal/moderation у листингов нет.
$sqlListing = "SELECT e.*, IFNULL(el.address_text, '') AS adres
FROM external_listing_events e
LEFT JOIN external_listings el ON el.id = e.external_listing_id
WHERE e.user_id = $_SESSION[id] AND e.schedule_date IS NOT NULL AND $where AND $startDateWhere AND $endDateWhere";
$rezListing = mysql_query($sqlListing);
while ($rezListing && ($event = mysql_fetch_assoc($rezListing))) {
if ($event['type'] == 'comment') {
continue;
}
$label = trim($event['adres']) !== '' ? $event['adres'] : ('EXT-' . $event['external_listing_id']);
$label = str_replace(array('"', '\\', "\r", "\n", '&quot;'), array("'", '/', ' ', ' ', "'"), $label);
$evName = str_replace(array('"', '\\', "\r", "\n", '&quot;'), array("'", '/', ' ', ' ', "'"), (string)$event['name']);
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", '; // РЕАЛЬНЫЙ id события: завершение адресует конкретную задачу
echo '"from" : "listing", '; // сигнал для setEventViewed-гарда: завершать в external_listing_events
echo '"is_external" : "1", '; // явный маркер листинга: openEventsWindow откроет окно с &src=ext (id-диапазон больше не различает)
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
echo '"object_id" : "'.(int)$event['external_listing_id'].'",'; // реальный el.id: клик открывает окно задач листинга (openEventsWindow)
$className = ($event['type'] == 'meet') ? 'meet' : (($event['type'] == 'show') ? 'showing' : (($event['type'] == 'even') ? 'even' : 'call'));
if ($event['calendar_view'] != 0) {
$className .= ' viewed';
}
if ($event['type'] == 'meet') {
echo '"title": "Встреча '.$label.'","className" : "'.$className.'"';
} else if ($event['type'] == 'show') {
echo '"title": "Показ '.$label.'","className" : "'.$className.'"';
} else if ($event['type'] == 'even') {
echo '"title": "'.$evName.' '.$label.'","className" : "'.$className.'"';
} else {
echo '"title": "Звонок '.$label.'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
2026-07-02 15:52:44 +02:00
$sql = "SELECT *, deal_id FROM user_client_events WHERE user_id = $_SESSION[id] and schedule_date is not null and cancel=0 and $where and $startDateWhere and $endDateWhere";
2026-05-22 20:21:54 +02:00
//echo $sql;
$agencyId = User::getUserAgencyID();
if($agencyId != 7384){
2026-07-02 15:52:44 +02:00
$sql = "SELECT *, deal_id FROM user_client_events WHERE user_id = $_SESSION[id] and megafon=0 and mango=0 and telphin=0 and beeline=0 and mts=0 and tele2=0 and allo=0 and schedule_date is not null and cancel=0 and $where and $startDateWhere and $endDateWhere";
2026-05-22 20:21:54 +02:00
}
$rez = mysql_query($sql);
while ($event = mysql_fetch_assoc($rez)) {
2026-07-02 15:52:44 +02:00
// Дедупликация сделок
if ($event['type'] == 'deal' && !empty($event['deal_id'])) {
if (in_array($event['deal_id'], $seenDealIds)) {
continue;
}
$seenDealIds[] = $event['deal_id'];
}
2026-05-22 20:21:54 +02:00
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = '';
if($event['client_id'] > 0){
$sql_cl = "SELECT fio from clients WHERE id=$event[client_id]";
$q_cl = mysql_query($sql_cl);
$fio = str_replace("\\","/", mysql_fetch_assoc($q_cl)['fio']);
} else if($event['req_id'] > 0) {
$sql_cl = "SELECT name from requisitions WHERE id=$event[req_id]";
2026-07-02 11:47:14 +02:00
2026-05-22 20:21:54 +02:00
$q_cl = mysql_query($sql_cl);
$fio = mysql_fetch_assoc($q_cl)['name'];
}
2026-07-02 15:52:44 +02:00
// Для сделок подтягиваем название сделки
if ($event['type'] == 'deal' && !empty($event['deal_id'])) {
if (!isset($deals[$event['deal_id']])) {
$q_deal = mysql_query("SELECT title FROM deals WHERE id = " . intval($event['deal_id']) . " LIMIT 1");
if ($q_deal && mysql_num_rows($q_deal) > 0) {
$deals[$event['deal_id']] = mysql_fetch_assoc($q_deal)['title'];
} else {
$deals[$event['deal_id']] = '';
}
}
$fio = $deals[$event['deal_id']] ?: $fio;
}
2026-05-22 20:21:54 +02:00
$fio = str_replace('"', "'", $fio);
$fio = str_replace('&quot;', "'", $fio);
//$fio = $event['fio'];
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
if($fio == ''){
$sql_u = "SELECT first_name, last_name, middle_name FROM users WHERE id = ".$event['user_id'];
$q_u =mysql_query($sql_u);
$r_u = mysql_fetch_assoc($q_u);
$fio = '(исполнитель '.trim($r_u['last_name'] . ' ' . $r_u['first_name'] . ' ' . $r_u['middle_name']).')';
}
echo '"client_id" : "'.$event['client_id'].'",';
echo '"req_id" : "'.$event['req_id'].'",';
if ($event['type'] == 'comment') {
$className = "comment";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Комментарий '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'call') {
$className = "call";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Звонок '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'meet') {
$className = "meet";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Встреча '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'show') {
$className = "showing";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Показ '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'deal') {
$className = "deal";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Сделка '.$fio.'","className" : "'.$className.'"';
}
if ($event['type'] == 'even') {
$className = "even";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "'.$event['name'].' '.$fio.'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
//if($_SESSION['id'] == 5238){
2026-07-02 11:47:14 +02:00
if (isset($post['start'])) {
// echo $post['start'];
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') >='".date('1930-m-d', strtotime($post['start']))."'";
/*if (date('m', strtotime($post['start']))=='12' && (date('m', strtotime($post['end']))=='01' || date('m', strtotime($post['end']))=='02')){
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') >='01-01'";
}*/
}
2026-05-22 20:21:54 +02:00
2026-07-02 11:47:14 +02:00
if (isset($post['end'])) {
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') < '".date('Y-m-d', strtotime($post['end']))."'";
//echo date('m', strtotime($post['end']))."\n";
/* if (date('m', strtotime($post['end']))=='01' && (date('m', strtotime($post['start']))=='12' || date('m', strtotime($post['start']))=='11')){
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') <='12-31'";
}*/
}
$sql = "SELECT id, fio, birthday FROM clients WHERE cancel=0 AND who_work = ".$_SESSION['id']." AND ".$startDateWhereBirthday." AND ".$endDateWhereBirthday;
/* if($_SESSION['id'] == 5238){
echo $sql;
}*/
$q = mysql_query($sql);
while ($b = mysql_fetch_assoc($q)) {
2026-05-22 20:21:54 +02:00
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = $b['fio'];
$fio = str_replace('"', "'", $fio);
$fio = str_replace('&quot;', "'", $fio);
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$b['id'].'", ';
echo '"start" : "'.date('Y-', strtotime($post['start'])).date('m-d', strtotime($b['birthday'])).'", ';
echo '"client_id" : "'.$b['id'].'",';
2026-07-02 11:47:14 +02:00
//if ($event['type'] == 'call') {
2026-05-22 20:21:54 +02:00
2026-07-02 11:47:14 +02:00
$className = "birthday";
/* if ($event['calendar_view'] != 0) {
$className .= " viewed";
}*/
2026-05-22 20:21:54 +02:00
2026-07-02 11:47:14 +02:00
echo '"title": "День рождения «'.$fio.'»","className" : "'.$className.'"';
// }
2026-05-22 20:21:54 +02:00
echo "}";
$i++;
2026-07-02 11:47:14 +02:00
if(date('Y-', strtotime($post['start'])) != date('Y-', strtotime($post['end']))){
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = $b['fio'];
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$b['id'].'", ';
echo '"start" : "'.date('Y-', strtotime($post['end'])).date('m-d', strtotime($b['birthday'])).'", ';
echo '"client_id" : "'.$b['id'].'",';
2026-05-22 20:21:54 +02:00
//if ($event['type'] == 'call') {
2026-07-02 11:47:14 +02:00
$className = "birthday";
/* if ($event['calendar_view'] != 0) {
$className .= " viewed";
}*/
2026-05-22 20:21:54 +02:00
2026-07-02 11:47:14 +02:00
echo '"title": "День рождения «'.$fio.'»","className" : "'.$className.'"';
2026-05-22 20:21:54 +02:00
// }
2026-07-02 11:47:14 +02:00
echo "}";
$i++;
}
}
// }
2026-05-22 20:21:54 +02:00
if (isset($post['start'])){
$startDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%Y-%m-%d') >= '".date('1930-m-d', strtotime($post['start']))."'";
2026-07-02 11:47:14 +02:00
/* if (date('m', strtotime($post['start']))=='12' && (date('m', strtotime($post['end']))=='01' || date('m', strtotime($post['end']))=='02')){
$startDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%m-%d') >= '01-01'";
}*/
2026-05-22 20:21:54 +02:00
}
if (isset($post['end'])){
$endDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%Y-%m-%d') < '".date('Y-m-d', strtotime($post['end']))."'";
2026-07-02 11:47:14 +02:00
//echo date('m', strtotime($post['end']))."\n";
/* if (date('m', strtotime($post['end']))=='01' && (date('m', strtotime($post['start']))=='12' || date('m', strtotime($post['start']))=='11')){
$endDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%m-%d') <= '12-31'";
}*/
2026-05-22 20:21:54 +02:00
}
$userId = $_SESSION['id'];
$user = new User;
$user->get($userId);
if ($_SESSION['users_admin']) {
$userId = $user->agencyId;
}
$sql = "SELECT employees.id,
employees.partner_id,
employees.name,
employees.birthday
FROM `partners_employees` AS employees
LEFT JOIN `users` AS users ON employees.created_by = users.id OR employees.updated_by = users.id
WHERE (users.id = $userId OR users.id_manager = $userId OR users.id_manager IN (
SELECT id FROM `users` WHERE id_manager = $userId
))
AND employees.birthday_notify > 0 AND employees.status > 0
AND $startDateWhereBirthday
AND $endDateWhereBirthday";
if (!$_SESSION['agency']) {
$sql .= " AND (
employees.created_by = " . $userId . " OR
employees.updated_by = " . $userId . " OR
employees.id IN (
SELECT clients.employee_id
FROM `clients` AS clients
WHERE clients.who_work = " . $userId . "
)
)";
}
$sql .= " GROUP BY employees.id";
$q = mysql_query($sql);
while ($b = mysql_fetch_assoc($q)) {
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"employee_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['start'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения партнёра «' . $b['name'] . '»", "className" : "birthday"';
echo "}";
$i++;
if(date('Y-', strtotime($post['start'])) != date('Y-', strtotime($post['end']))){
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"employee_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['end'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения партнёра «' . $b['name'] . '»", "className" : "birthday"';
echo "}";
$i++;
}
}
// Дни рождения сотрудников агенства
$agencyId = $_SESSION['id'];
$user = new User;
$user->get($userId);
if ($_SESSION['id_manager'])
$agencyId = $_SESSION['id_manager'];
if ($_SESSION['users_admin']) {
$agencyId = $user->agencyId;
}
if (isset($post['start'])){
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') >='".date('1930-m-d', strtotime($post['start']))."'";
2026-07-02 11:47:14 +02:00
/* if (date('m', strtotime($post['start']))=='12' && (date('m', strtotime($post['end']))=='01' || date('m', strtotime($post['end']))=='02')){
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') >='01-01'";
}*/
2026-05-22 20:21:54 +02:00
}
if (isset($post['end'])){
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%Y-%m-%d') < '".date('Y-m-d', strtotime($post['end']))."'";
2026-07-02 11:47:14 +02:00
/* if (date('m', strtotime($post['end']))=='01' && (date('m', strtotime($post['start']))=='12' || date('m', strtotime($post['start']))=='11')){
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') <= '12-31'";
}*/
2026-05-22 20:21:54 +02:00
}
$usersIds = User::getAllAgencyUsers($agencyId);
if (!empty($usersIds)) {
$sql = "SELECT id,
first_name,
last_name,
middle_name,
birthday,
phone
FROM users
WHERE (
id IN (". implode(',', $usersIds) .") AND
id != $_SESSION[id]
)
AND $startDateWhereBirthday
AND $endDateWhereBirthday
GROUP BY id";
//echo $sql;
$q = mysql_query($sql);
while ($b = mysql_fetch_assoc($q)) {
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"user_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['start'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения у «' . trim($b['last_name'] . ' ' . $b['first_name'] . ' ' . $b['middle_name']) . '», сотрудника Вашего агентства.", "className" : "birthday"';
echo "}";
$i++;
if(date('Y-', strtotime($post['start'])) != date('Y-', strtotime($post['end']))){
if ($i > 0)
echo ',{';
else
echo '{';
echo '"id" : "' . $i . '", ';
echo '"user_id" : "' . $b['id'] .'", ';
echo '"start" : "' . date('Y-', strtotime($post['end'])) . date('m-d', strtotime($b['birthday'])) . '", ';
echo '"title": "День рождения у «' . trim($b['last_name'] . ' ' . $b['first_name'] . ' ' . $b['middle_name']) . '», сотрудника Вашего агентства.", "className" : "birthday"';
echo "}";
$i++;
}
}
}
}
echo ']';