diff --git a/ajax/calendarEvents.php b/ajax/calendarEvents.php index 9d6cad0..5639cb8 100644 --- a/ajax/calendarEvents.php +++ b/ajax/calendarEvents.php @@ -23,12 +23,16 @@ if ($_SESSION['id']) { $where = "calendar_view = 0"; } + // Общее хранилище deal_id для дедупликации + $seenDealIds = array(); + $deals = array(); + // Явные колонки (НЕ e.*): user_object_events получил 19-ю колонку moved_on_copy → e.* стал 20, а вторая ветка // UNION = 19 → запрос падал с «different number of columns» → задачи по своим объектам пропадали из календаря. - $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, 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"; + $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"; $sql .= "union - 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, + 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, 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){ @@ -37,6 +41,14 @@ if ($_SESSION['id']) { $i = 0; while ($event = mysql_fetch_assoc($rez)) { + // Дедупликация сделок + if ($event['type'] == 'deal' && !empty($event['deal_id'])) { + if (in_array($event['deal_id'], $seenDealIds)) { + continue; + } + $seenDealIds[] = $event['deal_id']; + } + if ($i > 0) { echo ',{'; } else { @@ -45,7 +57,8 @@ if ($_SESSION['id']) { echo '"id" : "'.$i.'", '; echo '"table_id" : "'.$event['id'].'", '; echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", '; - echo '"object_id" : "'.$event['object_id'].'",'; + $objectId = ($event['type'] == 'deal') ? '0' : $event['object_id']; + echo '"object_id" : "'.$objectId.'",'; $event['nazv'] = str_replace('"', "'", $event['nazv']); $event['nazv'] = str_replace('"', "'", $event['nazv']); @@ -105,7 +118,21 @@ if ($_SESSION['id']) { $className .= " viewed"; } - echo '"title": "Сделка '.$event['nazv'].'","className" : "'.$className.'"'; + // Подтягиваем название сделки + $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.'"'; } if ($event['type'] == 'even') { @@ -170,15 +197,23 @@ if ($_SESSION['id']) { $i++; } - $sql = "SELECT * 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(); if($agencyId != 7384){ - $sql = "SELECT * 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); while ($event = mysql_fetch_assoc($rez)) { + // Дедупликация сделок + if ($event['type'] == 'deal' && !empty($event['deal_id'])) { + if (in_array($event['deal_id'], $seenDealIds)) { + continue; + } + $seenDealIds[] = $event['deal_id']; + } + if ($i > 0) { echo ',{'; } else { @@ -196,6 +231,19 @@ if ($_SESSION['id']) { $fio = mysql_fetch_assoc($q_cl)['name']; } + // Для сделок подтягиваем название сделки + 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; + } + $fio = str_replace('"', "'", $fio); $fio = str_replace('"', "'", $fio);