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

886 lines
30 KiB
PHP
Raw 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");
$start = microtime(true);
$cl = new Clients();
$usersArr = json_decode($_POST['responsible'], true);
$usersId = $cl -> getUsersFilter($usersArr, $_POST['user_id'], $_POST['users_admin']);
$gmtmsk = get_time_zone_user();
$time_zone = '';
if($gmtmsk >= 0){
$time_zone = '+'.$gmtmsk.' hour';
} else {
$time_zone = '-'.$gmtmsk.' hour';
}
$this_datetime = ($time_zone == '') ? date("Y-m-d H:i:s") : date("Y-m-d H:i:s", strtotime($time_zone));
$where = '';
$whereObj = '';
if(isset($_POST['tasks'])){
$view = $_POST['tasks'];
}
/* if($view == 'open_tasks'){
$where = " AND e.calendar_view = 0 AND DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') >= '".date("Y-m-d H:i:s")."'";
} else if($view == 'overdue_tasks'){
$where = " AND calendar_view = 0 AND DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') < '".date("Y-m-d H:i:s")."'";
} else if($view == 'completed_tasks'){
$where = " AND calendar_view = 1";
}*/
if($view == 'open_tasks'){
$where = " AND e.calendar_view = 0 AND DATE_FORMAT(e.schedule_date, '%Y-%m-%d %H:%i:%s') >= '".$this_datetime."'";
} else if($view == 'overdue_tasks'){
$where = " AND e.calendar_view = 0 AND DATE_FORMAT(e.schedule_date, '%Y-%m-%d %H:%i:%s') < '".$this_datetime."'";
} else if($view == 'completed_tasks'){
$where = " AND e.calendar_view = 1";
}
if(isset($_POST['event'])){
$where .= " AND `e`.`type` = '".$_POST['event']."'";
}
$whereObj = $where;
//printf($_POST);
$_SESSION['filter_emp'] = $_POST['responsible'];
$bd_usl_managers = '';
$bd_doers = '';
$bd_usl = '';
$arrIdUser = array();
$arrIdAgent = array();
foreach($usersId as $userId){
$arrIdUser[] = "who_work = $userId";
$arrIdAgent[] = "id_agent = $userId";
}
$period = 'all';
$whereTasks = "";
$bd_piriod = '';
$bd_do_period = '';
$eobj = true;
$isReq = false;
$table = "clients";
$dateAdd = "date_add";
$ecl = true;
$eall = false;
if(isset($_POST['type_calendar'])){
if($_POST['type_calendar'] == 'client' || $_POST['type_calendar'] == 'req'){
$eobj = false;
if($_POST['type_calendar'] == 'req'){
$isReq = true;
}
}
if($_POST['type_calendar'] == 'object'){
$ecl = false;
}
} else {
$eall = true;
}
if($_POST['period']){
$period = $_POST['period'];
$onDate = $_POST['start'];
$offDate = $_POST['end'];
}
else {
unset($_SESSION['client_period']);
}
$dates = getPeriod($period, $onDate, $offDate);
$date_between = $dates['between'];
$whereTasks = $dates['whereTasks'];
$date_add = 'date_add';
if($isReq){
$table = 'requisitions';
$date_add = 'created_at';
$agentId = 'user_id as id_agent';
$period = 'all';
$whereTasks = "";
if($_POST['period']){
$period = $_POST['period'];
}
$dates = getPeriod($period,$_POST['onDate'],$_POST['offDate']);
$date_between = $dates['between'];
$whereTasks = $dates['whereTasks'];
$bd_piriod = " AND created_at ".$date_between;
$bd_do_period = " AND created_at < '".$dates['dateStart']."'";
}
if(isset($_POST['charts_type'])){
$typesArr = json_decode(html_entity_decode($_POST['charts_type']), ENT_QUOTES);
if(!empty($typesArr)){
$typeArr = array();
foreach($typesArr as $typeCl){
$typeArr[] = "work_type like '%&quot;{$typeCl}&quot;%'";
}
$bd_usl .= " AND (".implode(" OR ",$typeArr).")";
}
$_SESSION['filter_client_type'] = $_POST['charts_type'];
} else {
unset($_SESSION['filter_client_type']);
}
$part_task = '';
if($isReq){
if (isset($_POST['charts_activities']) && !empty($_POST['charts_activities']) && $_POST['charts_activities'] != 'null') {
$activitiesArr = json_decode(html_entity_decode($_POST['charts_activities']), ENT_QUOTES);
$bd_usl .= " AND `id` IN (
SELECT req_id FROM requisitions_activities
WHERE activity_id IN (" . implode(", ", $activitiesArr) . ")
)";
$_SESSION['filter_activities'] = $_POST['charts_activities'];
} else {
unset($_SESSION['filter_activities']);
}
} else {
if (isset($_POST['charts_activities']) && !empty($_POST['charts_activities']) && $_POST['charts_activities'] != 'null') {
$activitiesArr = json_decode(html_entity_decode($_POST['charts_activities']), ENT_QUOTES);
$bd_usl .= " AND id IN (
SELECT client_id FROM clients_activities
WHERE activity_id IN (" . implode(", ", $activitiesArr) . ")
)";
$_SESSION['filter_activities'] = $_POST['charts_activities'];
} else {
unset($_SESSION['filter_activities']);
}
}
/*if (isset($_POST['charts_activities']) && !empty($_POST['charts_activities']) && $_POST['charts_activities'] != 'null') {
$activitiesArr = json_decode(html_entity_decode($_POST['charts_activities']), ENT_QUOTES);
$bd_usl .= " AND id IN (
SELECT client_id FROM clients_activities
WHERE activity_id IN (" . implode(", ", $activitiesArr) . ")
)";
$_SESSION['filter_activities'] = $_POST['charts_activities'];
} else {
unset($_SESSION['filter_activities']);
}*/
if (isset($_POST['charts_activities']) && !empty($_POST['charts_activities']) && $_POST['charts_activities'] != 'null') {
$activitiesArr = json_decode(html_entity_decode($_POST['charts_activities']), ENT_QUOTES);
$part_task .= " AND req_id IN (
SELECT req_id FROM requisitions_activities
WHERE activity_id IN (" . implode(", ", $activitiesArr) . "))";
}
if(isset($_POST['charts_objects_type'])){
$typesArr = json_decode(html_entity_decode($_POST['charts_objects_type']), ENT_QUOTES);
if(!empty($typesArr)){
$typeArr = array();
foreach($typesArr as $typeCl){
$typeArr[] = "`type` like '%&quot;{$typeCl}&quot;%'";
}
$bd_usl .= " AND (".implode(" OR ",$typeArr).")";
}
$_SESSION['filter_client_objects_type'] = $_POST['charts_objects_type'];
} else {
unset($_SESSION['filter_client_objects_type']);
}
if(isset($_POST['charts_rol'])){
$rolesArr = json_decode(html_entity_decode($_POST['charts_rol']), ENT_QUOTES);
if(!empty($rolesArr)){
$sroleArr = array();
foreach($rolesArr as $roleCl){
$roleArr[] = "`role` like '%&quot;{$roleCl}&quot;%'";
}
$bd_usl .= " AND (".implode(" OR ",$roleArr).")";
}
$_SESSION['filter_client_rol'] = $_POST['charts_rol'];
} else {
unset($_SESSION['filter_client_rol']);
}
if(isset($_POST['source'])){
$sourcesArr = json_decode(html_entity_decode($_POST['source']), ENT_QUOTES);
$sourceArr = array();
if(!empty($sourcesArr )){
foreach($sourcesArr as $sourceCl){
if($sourceCl == 0){
$sourceArr[] = "`source` is null";
$sourceArr[] = "`source` = ''";
$sourceArr[] = "`source` = 'null'";
} else {
$sourceArr[] = "`source` = '{$sourceCl}'";
}
}
$bd_usl .= " AND (".implode(" OR ",$sourceArr).")";
$_SESSION['filter_source'] = $_POST['source'];
} else {
unset($_SESSION['filter_source']);
}
} else {
unset($_SESSION['filter_source']);
}
if(isset($_POST['funnel']) && $_POST['funnel'] != 'all'){
$funnelArr = json_decode(html_entity_decode($_POST['funnel']), ENT_QUOTES);
if(!empty($funnelArr)){
if(is_array($funnelArr))
$bd_usl .= " AND funnel_id in (".implode(', ',$funnelArr).")";
else
$bd_usl .= " AND funnel_id in ({$funnelArr})";
}
}
if(isset($_POST['step'])){
$step = (int)$_POST['step'];
$bd_usl .= " AND step_id = $step";
}
//if($isReq){
$bd_usl .= " AND cancel=0";
//}
$bd_usl_managers = " AND (".implode(" OR ",$arrIdUser).")";
foreach ($usersId as $us) {
if($bd_doers != ''){
$bd_doers .= ' or ';
}
$bd_doers .= "`doers` like '%&quot;".$us."&quot;:1%'";
}
$post = clearInputData($_POST);
$startDateWhere = "1";
$endDateWhere = "1";
if (isset($post['start'])) {
$startDateWhere = "DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') >= '".$post['start']." 00:00:00'";
}
if (isset($post['end'])) {
$endDateWhere = "DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') <= '".$post['end']." 23:59:59'";
}
if($post['emp']){
$agencyId = $post['emp'];
if($post['filter_stage'] && $post['filter_stage'] == 'filter_work_add'){
$bd_usl_managers .= " AND id_agent in (SELECT id FROM `users` WHERE id=$agencyId or id_manager=$agencyId or id_manager in (select id from users where id_manager=$agencyId))";
// $usersId = [$agencyId];
} else {
$bd_usl_managers .= " AND who_work in (SELECT id FROM `users` WHERE id=$agencyId or id_manager=$agencyId or id_manager in (select id from users where id_manager=$agencyId))";
}
$where .= " AND user_id in (SELECT id FROM `users` WHERE id=$agencyId or id_manager=$agencyId or id_manager in (select id from users where id_manager=$agencyId))";
$bd_doers = '';
$usersId = [$agencyId];
}
$sqlCl = "SELECT `id` FROM $table WHERE 1 $bd_usl_managers $bd_usl ";
if($bd_doers != ''){
$sqlCl .= " UNION ";
$sqlCl .= "SELECT `id` FROM $table WHERE 1 $bd_usl and (".$bd_doers.") ";
}
if($_POST['step']){
$sqlCl = "SELECT `id` FROM $table WHERE 1 $bd_usl_managers $bd_usl and $date_add $date_between ";
}
//echo $sqlCl;
$q = mysql_query($sqlCl);
$row = mysql_num_rows($q);
$whereEv = '';
$tempEv = array();
/*if(!$isReq){
$tempEv[] = 0;
}*/
if($row > 0){
while($r = mysql_fetch_assoc($q)){
$tempEv[] = $r['id'];
}
$whereEv = ' AND (client_id in ('.implode(', ', $tempEv).') ';
if(isset($_POST['type_calendar'])){
if($_POST['type_calendar'] == 'client'){
$whereEv = ' AND (client_id in ('.implode(', ', $tempEv).') AND req_id = 0';
} else if($_POST['type_calendar'] == 'req'){
$whereEv = ' AND (req_id in ('.implode(', ', $tempEv).') AND client_id = 0';
}
}
}
if($eall){
$user_this = new User();
$user_this->get($_SESSION['id']);
$wh = new WebHookIn();
$allUsersIds = array();
if($user_this->agencyId > 0){
$allUsersIds = $wh->getAllUserAgency($user_this->agencyId);
}
if(!empty($allUsersIds)){
$sqlCl2 = "SELECT `id` FROM requisitions WHERE user_id in (".implode(',', $allUsersIds).") AND cancel = 0";
$q2 = mysql_query($sqlCl2);
$row2 = mysql_num_rows($q2);
$tempEv = array();
while($r2 = mysql_fetch_assoc($q2)){
$tempEv[] = $r2['id'];
}
if(!empty($tempEv)){
if($whereEv == ''){
$whereEv .= ' AND (req_id in ('.implode(', ', $tempEv).') OR (req_id = 0 && client_id = 0)';
} else {
$whereEv .= ' OR req_id in ('.implode(', ', $tempEv).') OR (req_id = 0 && client_id = 0)';
}
}
}
}
if($whereEv != ''){
$whereEv .= ")";
}
$from_users = array();
$sql_user = "SELECT id, CONCAT(last_name, ' ', first_name, ' ', middle_name) as user_fio FROM users WHERE id in (".implode(',', $usersId).")
UNION SELECT user_id as id, CONCAT(last_name, ' ', first_name, ' ', middle_name, ' (удален)') as user_fio FROM users_delete WHERE user_id in (".implode(',', $usersId).")";
$q_user = mysql_query($sql_user);
while( $from_user = mysql_fetch_assoc($q_user)){
$from_users[$from_user['id']] = $from_user['user_fio'];
}
/*$time = microtime(true) - $start;
if($_SESSION['id'] == 6635){
echo "\n".$time." сек.";
}*/
echo '[';
//echo $sql;
if (isset($_POST['user_id'])) {
if($eobj){
$sql = "SELECT e.*, o.nazv, o.adres, o.dom, o.korpus, o.litera from user_object_events e, objects o WHERE user_id in (". implode(', ', $usersId).") $whereObj $part_task and o.id=e.object_id and e.schedule_date is not null 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, o.adres, o.dom, o.korpus, o.litera,
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 in (". implode(', ', $usersId).") $whereObj $part_task and $startDateWhere and $endDateWhere";
$rez = mysql_query($sql);
$i = 0;
while ($event = mysql_fetch_assoc($rez)) {
$from_id = $event['user_id'];
if(!empty($event['from_id'])) $from_id = $event['from_id'];
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
echo '"id" : "'.$i.'", ';
// echo '"$sql" : "'.$sql.'", ';
echo '"table_id" : "'.$event['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
echo '"object_id" : "'.$event['object_id'].'",';
$address = $event['nazv'] .' '.$event['adres'];
if ($event['dom']) {
$address = $address . ", " . $event['dom'];
}
if ($event['korpus']) {
$address = $address . ", корпус " . $event['korpus'];
}
if ($event['litera']) {
$address = $address . ", литера" . $event['litera'];
}
$address = str_replace("\\","/", $address);
$address = str_replace("\"","'", $address);
if ($event['type'] == 'call') {
$className = "call";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Звонок '.$address;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'meet') {
$className = "meet";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Встреча '.$address;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'show') {
$className = "showing";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Показ '.$address;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'deal') {
$className = "deal";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Сделка '.$address;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'moderation') {
$className = "moderation";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Модерация '.$event['nazv'];
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'sub_moderation') {
$className = "moderation";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Комментарий к модерация '.$event['nazv'];
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'even') {
$className = "even";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "'.$event['name']. ' ' . $event['nazv'] .'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
}
/*$time = microtime(true) - $start;
if($_SESSION['id'] == 6635){
echo "\nобъекты".$time." сек.";
}*/
if($ecl){
// echo $whereTasks;
if ($startDateWhere != "1" || $endDateWhere != "1") {
$whereTasks = " and 1 ";
}
$agency_id = (int)$_POST['agency_id'];
if($agency_id != 7384){
$where .= ' and megafon=0 and mango=0 and telphin=0 and beeline=0 and mts=0 and tele2=0 and allo=0';
}
$sql = "SELECT * FROM user_client_events as e WHERE user_id in (". implode(', ', $usersId).") $whereTasks $whereEv $where $part_task and schedule_date is not null and cancel=0 and $startDateWhere and $endDateWhere";
if($isReq && empty($whereEv))
$sql = "SELECT * FROM user_client_events as e WHERE user_id in (". implode(', ', $usersId).") and req_id = '-1' and schedule_date is not null and cancel=0 and $startDateWhere and $endDateWhere";
$rez = mysql_query($sql);
//$result['sql'] = $sql;
/*if($_SESSION['id'] == 22687){
echo "\n".$sql."\n";
}*/
//echo $sql;
//Начитки
$events = array();
$clientsIds = array();
$reqsIds = array();
$usersEventIds = array();
$clients = array();
$reqs = array();
$usersEvent = array();
while ($event = mysql_fetch_assoc($rez)){
$events[] = $event;
if($event['client_id'] > 0) $clientsIds[] = (int)$event['client_id'];
if($event['req_id'] > 0) $reqsIds[] = (int)$event['req_id'];
if($event['user_id'] > 0) $usersEventIds[] = (int)$event['user_id'];
}
if(!empty($clientsIds)){
$sql_cl = "SELECT id, fio from clients WHERE id in (".implode(',',$clientsIds).")";
$q_cl = mysql_query($sql_cl);
while($r_cl = mysql_fetch_assoc($q_cl)){
$clients[$r_cl['id']] = str_replace("\\","/", $r_cl['fio']);
}
}
if(!empty($reqsIds)){
$sql_cl = "SELECT id, name, cancel from requisitions WHERE id in (".implode(',',$reqsIds).")";
$q_cl = mysql_query($sql_cl);
while($r_cl = mysql_fetch_assoc($q_cl)){
$reqs[$r_cl['id']] = $r_cl;
}
}
if(!empty($usersEventIds)){
$sql_u = "SELECT id, first_name, last_name, middle_name FROM users WHERE id in (".implode(',',$usersEventIds).")";
$q_u = mysql_query($sql_u);
while($r_u = mysql_fetch_assoc($q_u)){
$usersEvent[$r_u['id']] = $r_u;
}
}
foreach ($events as $event) {
$from_id = $event['user_id'];
$cancel = 0;
if(!empty($event['from_id'])) $from_id = $event['from_id'];
$fio = '';
if($event['client_id'] > 0 && isset($clients[$event['client_id']])){
$fio = $clients[$event['client_id']];
} else if($event['req_id'] > 0 && isset($reqs[$event['req_id']])) {
$fio = $reqs[$event['req_id']]['name'];
$cancel = (int)$reqs[$event['req_id']]['cancel'];
}
if($cancel == 0){
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 == ''){
if(isset($usersEvent[$event['user_id']])){
$fio = '(исполнитель '.trim($usersEvent[$event['user_id']]['last_name'] . ' ' . $usersEvent[$event['user_id']]['first_name'] . ' ' . $usersEvent[$event['user_id']]['middle_name']).')';
}
}
echo '"client_id" : "'.$event['client_id'].'",';
echo '"req_id" : "'.$event['req_id'].'",';
$fio = str_replace("\\","/", $fio);
$fio = str_replace("\"","'", $fio);
if ($event['type'] == 'comment') {
$className = "comment";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Комментарий '.$fio;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'call') {
$className = "call";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Звонок '.$fio;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'meet') {
$className = "meet";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Встреча '.$fio;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'show') {
$className = "showing";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Показ '.$fio;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'deal') {
$className = "deal";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Сделка '.$fio;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
if ($event['type'] == 'even' || $event['type'] == 'event') {
$className = "even";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "'.$event['name'].' '.$fio;
if(isset($from_users[$from_id])){
echo ' - '.$from_users[$from_id];
}
echo '","className" : "'.$className.'"';
}
echo "}";
$i++;
}
}
/*$time = microtime(true) - $start;
if($_SESSION['id'] == 6635){
echo "\n клиенты".$time." сек.";
}*/
if($view !='overdue_tasks' && !isset($_POST['event'])){
if (isset($post['start'])) {
$startDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') >='".date('m-d', strtotime($post['start']))."'";
}
if (isset($post['end'])) {
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') < '".date('m-d', strtotime($post['end']))."'";
}
$sql = "SELECT id, fio, birthday FROM clients WHERE cancel=0 AND who_work = ".$_SESSION['id']." $bd_usl $part_task AND ".$startDateWhereBirthday." AND ".$endDateWhereBirthday;
//echo $sql;
$q = mysql_query($sql);
while ($b = mysql_fetch_assoc($q)) {
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = $b['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'].'",';
//if ($event['type'] == 'call') {
$className = "birthday";
/* if ($event['calendar_view'] != 0) {
$className .= " viewed";
}*/
echo '"title": "День рождения «'.$fio.'»","className" : "'.$className.'"';
// }
echo "}";
$i++;
}
if (isset($post['start']))
$startDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%m-%d') >= '".date('m-d', strtotime($post['start']))."'";
if (isset($post['end']))
$endDateWhereBirthday = "DATE_FORMAT(employees.birthday, '%m-%d') < '".date('m-d', strtotime($post['end']))."'";
$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++;
}
// Дни рождения сотрудников агенства
$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, '%m-%d') >='".date('m-d', strtotime($post['start']))."'";
if (isset($post['end']))
$endDateWhereBirthday = "DATE_FORMAT(birthday, '%m-%d') < '".date('m-d', strtotime($post['end']))."'";
$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++;
}
}
}
/* $time = microtime(true) - $start;
if($_SESSION['id'] == 6635){
echo "\n дни ".$time." сек.";
}*/
}
}
echo ']';
/*$time = microtime(true) - $start;
if($_SESSION['id'] == 6635){
echo "\n".$time." сек.";
}*/