Joywork/ajax/calendarChartsObject.php

543 lines
24 KiB
PHP
Raw Normal View History

2026-05-22 20:21:54 +02:00
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
$cl = new Clients();
$usersArr = json_decode($_POST['responsible'], true);
$usersId = $cl -> getUsersFilter($usersArr, $_POST['user_id'], $_POST['users_admin']);
$where = '';
if(isset($_POST['tasks'])){
$view = $_POST['tasks'];
}
if($view == 'open_tasks'){
$where = " AND e.calendar_view = 0 AND DATE_FORMAT(e.schedule_date, '%Y-%m-%d %H:%i:%s') >= '".date("Y-m-d H:i:s")."'";
} else if($view == 'overdue_tasks'){
$where = " AND e.calendar_view = 0 AND DATE_FORMAT(e.schedule_date, '%Y-%m-%d %H:%i:%s') < '".date("Y-m-d H:i:s")."'";
} else if($view == 'completed_tasks'){
$where = " AND e.calendar_view = 1";
}
//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 = '';
if($_POST['period']){
//$_SESSION['client_period'] = $_POST['period'];
$period = $_POST['period'];
/*if($period == 'dates'){
$_SESSION['client_period'] = 'period';
$onDate = date('Y-m-d 00:00:00', strtotime($_POST['onDate']));
if(empty($onDate)) $onDate = '1970-01-01 00:00:00';
if(!empty($_POST['offDate'])) {
$offDate = date('Y-m-d 23:59:59', strtotime($_POST['offDate']));
} else {
$offDate = date('Y-m-d 23:59:59');
}
// $bd_piriod .= " AND `date_add` between '{$onDate}' AND '{$offDate}'";
//$bd_do_period .= " AND `date_add` < '{$onDate}'";
$_SESSION['on_date'] = $_POST['onDate'];
$_SESSION['off_date'] = $_POST['offDate'];
} else {
unset($_SESSION['on_date']);
unset($_SESSION['off_date']);
$arrPeriod = ['today' => '-0 day', 'week' => '-6 day', 'month' => '-30 day', 'quarter' => '-120 day', 'year' => '-365', 'all'=>'-4000'];
$days = date('Y-m-d', strtotime($arrPeriod[$_POST['period']]));
//$bd_piriod .= " AND `date_add` > '{$days}'";
//$bd_do_period .= " AND `date_add` < '{$days}'";
//$whereTasks = " and DATE(create_date) > '{$days}'";
$_SESSION['client_period'] = $_POST['period'];
}*/
$months = array(1,2,3,4,5,6,7,8,9,10,11,12);
$years = array();
$firstDay = '';
$lastDay = '';
$totalDays = 365;
$readPeriod = '';
$diffDays = 0;
$firstNameMonth = $lastNameMonth = $nameMonth = '';
$years[] = date('Y');
$date_between = "between '1970-01-01 00:00:00' AND '3000-12-31 23:59:59'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '1970-01-01 00:00:00' AND '3000-12-31 23:59:59'";
switch ($period){
case 'month': {
$months = array();
$months[] = (int)date('m');
$firstDay = '01';
$totalDays = cal_days_in_month(CAL_GREGORIAN, (int)date('m'), date('Y'));
$readPeriod = $mounthArrray[(int)date('m') - 1].' '.date('Y').' года';
$datetime1 = new DateTime(date('Y').'-'.date('m').'-01');
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->d;
$lastDay = $totalDays;
$procentDays = (int)(($diffDays/$totalDays) * 100);
$firstNameMonth = $lastNameMonth = $nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$date_between = "between '".date('Y')."-".date('m')."-01 00:00:00' AND '".date('Y')."-".date('m')."-".$lastDay." 23:59:59'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date('Y')."-".date('m')."-01 00:00:00' AND '".date('Y')."-".date('m')."-".$lastDay." 23:59:59'";
break;
}
case 'quarter' : {
$nowMounth = (int)date('m');
if ($nowMounth < 4) {
$months = array();
$months[] = 1;
$months[] = 2;
$months[] = 3;
$years[] = date('Y');
$firstDay = '01';
$lastDay = '31';
$firstNameMonth = $treplace[$mounthArrray[0]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[2]];
$datetime1 = new DateTime(date('Y').'-01-01');
$datetime2 = new DateTime(date('Y').'-03-31');
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date('Y').'-01-01');
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
$date_between = "between '".date('Y')."-01-01 00:00:00' AND '".date('Y')."-03-31 23:59:59'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date('Y')."-01-01 00:00:00' AND '".date('Y')."-03-31 23:59:59'";
} else if($nowMounth >= 4 && $nowMounth < 7){
$months = array();
$months[] = 4;
$months[] = 5;
$months[] = 6;
$years[] = date('Y');
$firstDay = '01';
$lastDay = '30';
$firstNameMonth = $treplace[$mounthArrray[3]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[5]];
$datetime1 = new DateTime(date('Y').'-04-01');
$datetime2 = new DateTime(date('Y').'-06-30');
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date('Y').'-04-01');
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
$date_between = "between '".date('Y')."-04-01 00:00:00' AND '".date('Y')."-06-30 23:59:59'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date('Y')."-04-01 00:00:00' AND '".date('Y')."-06-30 23:59:59'";
} else if ($nowMounth >= 7 && $nowMounth < 10){
$months = array();
$months[] = 7;
$months[] = 8;
$months[] = 9;
$years[] = date('Y');
$firstDay = '01';
$lastDay = '30';
$firstNameMonth = $treplace[$mounthArrray[6]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[8]];
$datetime1 = new DateTime(date('Y').'-07-01');
$datetime2 = new DateTime(date('Y').'-09-30');
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date('Y').'-07-01');
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
$date_between = "between '".date('Y')."-07-01 00:00:00' AND '".date('Y')."-09-30 23:59:59'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date('Y')."-07-01 00:00:00' AND '".date('Y')."-09-30 23:59:59'";
} else if ($nowMounth >= 10 && $nowMounth < 13){
$months = array();
$months[] = 10;
$months[] = 11;
$months[] = 12;
$years[] = date('Y');
$firstDay = '01';
$lastDay = '31';
$firstNameMonth = $treplace[$mounthArrray[9]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[11]];
$readPeriod = 'c '.$firstDay.' '.$firstNameMonth. ' по ' . $lastDay . ' ' .$lastNameMonth;
$datetime1 = new DateTime(date('Y').'-10-01');
$datetime2 = new DateTime(date('Y').'-12-31');
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date('Y').'-10-01');
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
$date_between = "between '".date('Y')."-10-01 00:00:00' AND '".date('Y')."-12-31 23:59:59'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date('Y')."-10-01 00:00:00' AND '".date('Y')."-12-31 23:59:59'";
}
break;
}
case 'week': {
$months = array();
$months[] = (int)date('m');
$years = array();
$years[] = date('Y');
$firstDay = date("d", strtotime("monday this week"));
$lastDay = date("d", strtotime("sunday this week"));
$firstNameMonth = $treplace[$mounthArrray[(int)date("m", strtotime("monday this week")) - 1]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[(int)date("m", strtotime("sunday this week")) - 1]];
$readPeriod = 'c '.$firstDay.' '.$firstNameMonth. ' по ' . $lastDay . ' ' .$lastNameMonth;
$datetime1 = new DateTime(date("Y-m-d", strtotime("monday this week")));
$datetime2 = new DateTime(date("Y-m-d", strtotime("sunday this week")));
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date("Y-m-d", strtotime("monday this week")));
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
$date_between = "between '".date("Y-m-d 00:00:00", strtotime("monday this week"))."' AND '".date("Y-m-d 23:59:59", strtotime("sunday this week"))."'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date("Y-m-d 00:00:00", strtotime("monday this week"))."' AND '".date("Y-m-d 23:59:59", strtotime("sunday this week"))."'";
break;
}
case 'dates' : {
$onDate = $_POST['start'];
//var_dump($_POST);
//if(empty($onDate)) $onDate = '1970-01-01 00:00:00';
$offDate = $_POST['end'];
if(empty($offDate)) $offDate = date('Y-m-d 23:59:59');
$months = array();
$years = array();
$months[] = date("m", strtotime($onDate));
$months[] = date("m", strtotime($offDate));
$years[] = date("Y", strtotime($onDate));
$years[] = date("Y", strtotime($offDate));
$firstDay = date("d", strtotime($onDate));
$lastDay = date("d", strtotime($offDate));
$firstNameMonth = $treplace[$mounthArrray[(int)date("m", strtotime($onDate)) - 1]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[(int)date("m", strtotime($offDate)) - 1]];
$readPeriod = 'c '.$firstDay.' '.$firstNameMonth. ' по ' . $lastDay . ' ' .$lastNameMonth;
$datetime1 = new DateTime(date("Y-m-d", strtotime($onDate)));
$datetime2 = new DateTime(date("Y-m-d", strtotime($offDate)));
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date("Y-m-d", strtotime($onDate)));
$datetime2 = new DateTime(date('Y-m-d'));
if($datetime1 < $datetime2){
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
}
$procentDays = 0;
$date_between = "between '".date("Y-m-d 00:00:00", strtotime($onDate))."' AND '".date("Y-m-d 23:59:59", strtotime($offDate))."'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date("Y-m-d 00:00:00", strtotime($onDate))."' AND '".date("Y-m-d 23:59:59", strtotime($offDate))."'";
break;
}
case 'today':{
$months = array();
$months[] = (int)date('m');
$years = array();
$years[] = date('Y');
$firstDay = date("d");
$lastDay = date("d");
$firstNameMonth = $treplace[$mounthArrray[(int)date("m") - 1]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[(int)date("m") - 1]];
$readPeriod = 'c '.$firstDay.' '.$firstNameMonth. ' по ' . $lastDay . ' ' .$lastNameMonth;
$datetime1 = new DateTime(date("Y-m-d"));
$datetime2 = new DateTime(date("Y-m-d"));
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date("Y-m-d"));
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
$date_between = "between '".date("Y-m-d 00:00:00")."' AND '".date("Y-m-d 23:59:59")."'";
$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date("Y-m-d 00:00:00")."' AND '".date("Y-m-d 23:59:59")."'";
break;
}
default: {
$firstDay = '01';
$lastDay = '31';
$firstNameMonth = $treplace[$mounthArrray[0]];
$nameMonth = $treplace[$mounthArrray[(int)date('m') - 1]];
$lastNameMonth = $treplace[$mounthArrray[11]];
$readPeriod = date('Y').' год';
$datetime1 = new DateTime(date('Y').'-01-01');
$datetime2 = new DateTime(date('Y').'-12-31');
$difference = $datetime1->diff($datetime2);
$totalDays = $difference->days + 1;
$datetime1 = new DateTime(date('Y').'-01-01');
$datetime2 = new DateTime(date('Y-m-d'));
$difference = $datetime1->diff($datetime2);
$diffDays = $difference->days;
$procentDays = (int)(($diffDays/$totalDays) * 100);
//$whereTasks = " and DATE_FORMAT(schedule_date, '%Y-%m-%d %H:%i:%s') between '".date('Y')."-".date('m')."-01 00:00:00' AND '".date('Y')."-".date('m')."-".$lastDay." 23:59:59'";
break;
}
}
} else {
unset($_SESSION['client_period']);
}
$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))";
} 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 = '';
}
$sql = "SELECT `id` FROM clients WHERE 1 $bd_usl_managers $bd_usl and deleted <> 1 and confirm = 1";
$sql .= " UNION ";
$sql .= "SELECT `id` FROM clients WHERE 1 $bd_usl and deleted <> 1 and confirm = 1 and (".$bd_doers.")";
//echo $sql;
$q = mysql_query($sql);
$row = mysql_num_rows($q);*/
$whereEv = '';
$tempEv = array();
$tempEv[] = 0;
if($row > 0){
while($r = mysql_fetch_assoc($q)){
$tempEv[] = $r['id'];
}
$whereEv = ' AND client_id in ('.implode(', ', $tempEv).') ';
}
echo '[';
//echo $sql;
if (isset($_POST['user_id'])) {
$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).") $where and o.id=e.object_id and schedule_date is not null and $startDateWhere and $endDateWhere";
$rez = mysql_query($sql);
//echo $sql;
$i = 0;
while ($event = mysql_fetch_assoc($rez)) {
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'];
}
if ($event['type'] == 'call') {
$className = "call";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Звонок '.$address.'","className" : "'.$className.'"';
}
if ($event['type'] == 'meet') {
$className = "meet";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Встреча '.$address.'","className" : "'.$className.'"';
}
if ($event['type'] == 'show') {
$className = "showing";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Показ '.$address.'","className" : "'.$className.'"';
}
if ($event['type'] == 'deal') {
$className = "deal";
if ($event['calendar_view'] != 0) {
$className .= " viewed";
}
echo '"title": "Сделка '.$address.'","className" : "'.$className.'"';
}
echo "}";
$i++;
}
// echo $whereTasks;
/* $sql = "SELECT e.*, c.fio from user_client_events as e left join clients as c on c.id=e.client_id WHERE user_id in (". implode(', ', $usersId).") $whereTasks $whereEv $where and schedule_date is not null and $startDateWhere and $endDateWhere";
$rez = mysql_query($sql);
//$result['sql'] = $sql;
//echo $sql;
while ($event = mysql_fetch_assoc($rez)) {
if ($i > 0) {
echo ',{';
} else {
echo '{';
}
$fio = $event['fio'];
echo '"id" : "'.$i.'", ';
echo '"table_id" : "'.$event['id'].'", ';
echo '"start" : "'.date(DateTime::ATOM, strtotime($event['schedule_date'])).'", ';
if($event['client_id'] == 0){
$sql_u = "SELECT fio FROM users WHERE id = ".$event['user_id'];
$q_u =mysql_query($sql_u);
$r_u = mysql_fetch_assoc($q_u);
$fio = '(исполнитель '.$r_u['fio'].')';
}
echo '"client_id" : "'.$event['client_id'].'",';
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($view !='overdue_tasks'){
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 who_work = ".$_SESSION['id']." 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++;
}
}*/
}
echo ']';