Deduble calendar fix

This commit is contained in:
mac 2026-07-02 17:47:58 +03:00
parent 07ce316c53
commit 2b1fad8cc5

View File

@ -41,12 +41,9 @@ if ($_SESSION['id']) {
$i = 0; $i = 0;
while ($event = mysql_fetch_assoc($rez)) { while ($event = mysql_fetch_assoc($rez)) {
// Дедупликация сделок // Deal-события обрабатываются отдельно во втором цикле
if ($event['type'] == 'deal' && !empty($event['deal_id'])) { if ($event['type'] == 'deal' && !empty($event['deal_id'])) {
if (in_array($event['deal_id'], $seenDealIds)) { continue;
continue;
}
$seenDealIds[] = $event['deal_id'];
} }
if ($i > 0) { if ($i > 0) {
@ -57,8 +54,10 @@ if ($_SESSION['id']) {
echo '"id" : "'.$i.'", '; echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", '; echo '"table_id" : "'.$event['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", '; echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
$objectId = ($event['type'] == 'deal') ? '0' : $event['object_id']; // Для сделок не выводим object_id чтобы клик шёл на client_id/req_id
echo '"object_id" : "'.$objectId.'",'; if ($event['type'] != 'deal') {
echo '"object_id" : "'.$event['object_id'].'",';
}
$event['nazv'] = str_replace('"', "'", $event['nazv']); $event['nazv'] = str_replace('"', "'", $event['nazv']);
$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"; $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(); $agencyId = User::getUserAgencyID();
if($agencyId != 7384){ 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"; $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); $rez = mysql_query($sql);
// Собираем все deal-события по deal_id
$dealEvents = array();
while ($event = mysql_fetch_assoc($rez)) { while ($event = mysql_fetch_assoc($rez)) {
// Дедупликация сделок
if ($event['type'] == 'deal' && !empty($event['deal_id'])) { if ($event['type'] == 'deal' && !empty($event['deal_id'])) {
if (in_array($event['deal_id'], $seenDealIds)) { $did = (int)$event['deal_id'];
continue; if (!isset($dealEvents[$did])) {
$dealEvents[$did] = array();
} }
$seenDealIds[] = $event['deal_id']; $dealEvents[$did][] = $event;
}
if ($i > 0) {
echo ',{';
} else { } 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;
}
} }
// Для сделок подтягиваем название сделки // 2. Ищем событие с req_id == side_two_requisition_id
if ($event['type'] == 'deal' && !empty($event['deal_id'])) { if (!$bestEvent) {
if (!isset($deals[$event['deal_id']])) { foreach ($events as $ev) {
$q_deal = mysql_query("SELECT title FROM deals WHERE id = " . intval($event['deal_id']) . " LIMIT 1"); if ($sideTwoReqId > 0 && (int)$ev['req_id'] == $sideTwoReqId) {
if ($q_deal && mysql_num_rows($q_deal) > 0) { $bestEvent = $ev;
$deals[$event['deal_id']] = mysql_fetch_assoc($q_deal)['title']; $bestType = 'req_two';
} else { break;
$deals[$event['deal_id']] = '';
} }
} }
$fio = $deals[$event['deal_id']] ?: $fio;
} }
$fio = str_replace('"', "'", $fio); // 3. Ищем событие с client_id > 0
$fio = str_replace('"', "'", $fio); 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 '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", '; echo '"table_id" : "'.$bestEvent['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", '; echo '"start" : "'.date(DateTime::ATOM, strtotime($bestEvent['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') { // Для клика: передаём client_id или req_id (без object_id)
if (!empty($bestEvent['client_id'])) {
$className = "comment"; echo '"client_id" : "'.$bestEvent['client_id'].'",';
if ($event['calendar_view'] != 0) { echo '"req_id" : "0",';
$className .= " viewed"; } else if (!empty($bestEvent['req_id'])) {
} echo '"client_id" : "0",';
echo '"req_id" : "'.$bestEvent['req_id'].'",';
echo '"title": "Комментарий '.$fio.'","className" : "'.$className.'"'; } else {
echo '"client_id" : "0",';
echo '"req_id" : "0",';
} }
if ($event['type'] == 'call') { $className = "deal";
if ($bestEvent['calendar_view'] != 0) { $className .= " viewed"; }
$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 '"title": "'.str_replace('"', "'", $dealTitle).'","className" : "'.$className.'"';
echo "}"; echo "}";
$i++; $i++;
} }