From 2b1fad8cc5b1afdb0f51a884b093a6de63ae3e5d Mon Sep 17 00:00:00 2001 From: mac Date: Thu, 2 Jul 2026 17:47:58 +0300 Subject: [PATCH] Deduble calendar fix --- ajax/calendarEvents.php | 243 +++++++++++++++++++++++----------------- 1 file changed, 138 insertions(+), 105 deletions(-) diff --git a/ajax/calendarEvents.php b/ajax/calendarEvents.php index 5639cb8..c264e0b 100644 --- a/ajax/calendarEvents.php +++ b/ajax/calendarEvents.php @@ -41,12 +41,9 @@ if ($_SESSION['id']) { $i = 0; while ($event = mysql_fetch_assoc($rez)) { - // Дедупликация сделок + // Deal-события обрабатываются отдельно во втором цикле if ($event['type'] == 'deal' && !empty($event['deal_id'])) { - if (in_array($event['deal_id'], $seenDealIds)) { - continue; - } - $seenDealIds[] = $event['deal_id']; + continue; } if ($i > 0) { @@ -57,8 +54,10 @@ if ($_SESSION['id']) { echo '"id" : "'.$i.'", '; echo '"table_id" : "'.$event['id'].'", '; echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", '; - $objectId = ($event['type'] == 'deal') ? '0' : $event['object_id']; - echo '"object_id" : "'.$objectId.'",'; + // Для сделок не выводим object_id чтобы клик шёл на client_id/req_id + if ($event['type'] != 'deal') { + echo '"object_id" : "'.$event['object_id'].'",'; + } $event['nazv'] = str_replace('"', "'", $event['nazv']); $event['nazv'] = str_replace('"', "'", $event['nazv']); @@ -198,128 +197,162 @@ if ($_SESSION['id']) { } $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"; - //echo $sql; $agencyId = User::getUserAgencyID(); if($agencyId != 7384){ $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"; } $rez = mysql_query($sql); + // Собираем все deal-события по deal_id + $dealEvents = array(); while ($event = mysql_fetch_assoc($rez)) { - // Дедупликация сделок if ($event['type'] == 'deal' && !empty($event['deal_id'])) { - if (in_array($event['deal_id'], $seenDealIds)) { - continue; + $did = (int)$event['deal_id']; + if (!isset($dealEvents[$did])) { + $dealEvents[$did] = array(); } - $seenDealIds[] = $event['deal_id']; - } - - if ($i > 0) { - echo ',{'; + $dealEvents[$did][] = $event; } else { - echo '{'; + // Не сделки — выводим сразу + 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]"; + $q_cl = mysql_query($sql_cl); + $fio = mysql_fetch_assoc($q_cl)['name']; + } + $fio = str_replace('"', "'", $fio); + $fio = str_replace('"', "'", $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'] == 'even') { + $className = "even"; + if ($event['calendar_view'] != 0) { $className .= " viewed"; } + echo '"title": "'.$event['name'].' '.$fio.'","className" : "'.$className.'"'; + } + echo "}"; + $i++; } - $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]"; + } - $q_cl = mysql_query($sql_cl); - $fio = mysql_fetch_assoc($q_cl)['name']; + // Выводим по одному событию на сделку с приоритетом + foreach ($dealEvents as $did => $events) { + // Подтягиваем данные сделки + if (!isset($deals[$did])) { + $q_deal = mysql_query("SELECT title, side_one_requisition_id, side_two_requisition_id FROM deals WHERE id = " . $did . " LIMIT 1"); + $deals[$did] = ($q_deal && mysql_num_rows($q_deal) > 0) ? mysql_fetch_assoc($q_deal) : null; + } + $deal = $deals[$did]; + $dealTitle = $deal ? $deal['title'] : ''; + $sideOneReqId = $deal ? (int)$deal['side_one_requisition_id'] : 0; + $sideTwoReqId = $deal ? (int)$deal['side_two_requisition_id'] : 0; + + // Приоритет: 1) заявка стороны 1, 2) заявка стороны 2, 3) объект + $bestEvent = null; + $bestType = ''; + + // 1. Ищем событие с req_id == side_one_requisition_id + foreach ($events as $ev) { + if ($sideOneReqId > 0 && (int)$ev['req_id'] == $sideOneReqId) { + $bestEvent = $ev; + $bestType = 'req_one'; + break; + } } - // Для сделок подтягиваем название сделки - 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']] = ''; + // 2. Ищем событие с req_id == side_two_requisition_id + if (!$bestEvent) { + foreach ($events as $ev) { + if ($sideTwoReqId > 0 && (int)$ev['req_id'] == $sideTwoReqId) { + $bestEvent = $ev; + $bestType = 'req_two'; + break; } } - $fio = $deals[$event['deal_id']] ?: $fio; } - $fio = str_replace('"', "'", $fio); - $fio = str_replace('"', "'", $fio); + // 3. Ищем событие с client_id > 0 + if (!$bestEvent) { + foreach ($events as $ev) { + if (!empty($ev['client_id'])) { + $bestEvent = $ev; + $bestType = 'client'; + break; + } + } + } - //$fio = $event['fio']; + // 4. Ищем событие с req_id > 0 + if (!$bestEvent) { + foreach ($events as $ev) { + if (!empty($ev['req_id'])) { + $bestEvent = $ev; + $bestType = 'req'; + break; + } + } + } + + // 5. Берём любое + if (!$bestEvent) { + $bestEvent = $events[0]; + $bestType = 'any'; + } + + if ($i > 0) { echo ',{'; } else { echo '{'; } 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'].'",'; + echo '"table_id" : "'.$bestEvent['id'].'", '; + echo '"start" : "'.date(DateTime::ATOM, strtotime($bestEvent['schedule_date'])).'", '; - if ($event['type'] == 'comment') { - - $className = "comment"; - if ($event['calendar_view'] != 0) { - $className .= " viewed"; - } - - echo '"title": "Комментарий '.$fio.'","className" : "'.$className.'"'; + // Для клика: передаём client_id или req_id (без object_id) + if (!empty($bestEvent['client_id'])) { + echo '"client_id" : "'.$bestEvent['client_id'].'",'; + echo '"req_id" : "0",'; + } else if (!empty($bestEvent['req_id'])) { + echo '"client_id" : "0",'; + echo '"req_id" : "'.$bestEvent['req_id'].'",'; + } else { + echo '"client_id" : "0",'; + echo '"req_id" : "0",'; } - 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.'"'; - } + $className = "deal"; + if ($bestEvent['calendar_view'] != 0) { $className .= " viewed"; } + echo '"title": "'.str_replace('"', "'", $dealTitle).'","className" : "'.$className.'"'; echo "}"; $i++; }