<?php   
require_once('config.php');

 /* pChart library inclusions */
 include("pChartys/class/pData.class.php");
 include("pChartys/class/pDraw.class.php");
 include("pChartys/class/pImage.class.php");
 
$link = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
	if (mysqli_connect_errno($link)) {
		toLog("Can't connect to MySQL: " . mysqli_connect_error());
		exit;
	}

	if($_REQUEST['filter_date_start']) 	{ $filter_date_start = $link->real_escape_string($_REQUEST['filter_date_start']); }
	else { $filter_date_start = ''; }
	if($_REQUEST['filter_date_end']) 	{ $filter_date_end = $link->real_escape_string($_REQUEST['filter_date_end']);	}
	else { $filter_date_end = ''; }
	
									$period = $link->real_escape_string($_REQUEST['period']);
									$cat = $link->real_escape_string($_REQUEST['cat']);
		if($_REQUEST['mesyac']) {	$mesyac = $link->real_escape_string($_REQUEST['mesyac']); } else { unset($mesyac); }
 
 $q_sums = $i_sum = $j_sum = $i_cot = $i_luks = $i_stand = $j_cot = $j_luks = $j_stand = 0;

	if($filter_date_start != '') { 
	if($filter_date_end == '') { $filter_date_end = date("Y-m-d"); }
	$Requete = "SELECT `booking_date` FROM `oc_order_product` WHERE `booking_date` 
						BETWEEN '$filter_date_start' AND '$filter_date_end' GROUP BY `booking_date`"; 
						}
	elseif($filter_date_end != '') { 
	if($filter_date_start == '') { $filter_date_start = '2017-08-02'; }
	$Requete = "SELECT `booking_date` FROM `oc_order_product` WHERE `booking_date` 
						BETWEEN '$filter_date_start' AND '$filter_date_end' GROUP BY `booking_date`"; 
						} 
	else {					
	$Requete = "SELECT `booking_date` FROM `oc_order_product` GROUP BY `booking_date`";  // quantity
						}
	$inters = 0;
	if($filter_date_start != '') {
	$datetime1 = date_create($filter_date_start);
	$datetime2 = date_create($filter_date_end);
	$interval = date_diff($datetime1, $datetime2);
	$inters = $interval->format('%a');
	}

	$result = mysqli_query($link,$Requete);
	while (list($booking_date) = mysqli_fetch_row($result)) {

	$Req = "SELECT model , ( SELECT SUM(quantity) FROM `oc_order_product` WHERE `booking_date`='$booking_date' AND `model`= o.model) quantitys FROM `oc_order_product` o WHERE `booking_date`='$booking_date' ORDER BY o.`model` ASC";
	
	$norm = $q_cot = $q_luks = $q_stand = 0;
	$res = mysqli_query($link,$Req);
	while (list($model,$quantitys) = mysqli_fetch_row($res)) {
	
	if((  trim($model) == 'Vip-котедж' )	 OR (  trim($model) == 'Китайский домик' ) OR  (  trim($model) == 'Котедж' ))				
													{ $j_cot++; 	$q_cot = $quantitys;		}			
	elseif(  trim($model) == 'Люкс' )				{ $j_luks++;	$q_luks = $quantitys;		}						
	elseif(  trim($model) == 'Стандарт' )			{ $j_stand++;	$q_stand = $quantitys;		}
	
	};
	$j_sum += $j_cot + $j_luks + $j_stand;
	$q_sum += $q_cot + $q_luks + $q_stand;
	$i_cot += $j_cot;
	$i_luks += $j_luks;
	$i_stand += $j_stand;
	$i_sum += $j_sum;
	$q_sums += $q_sum;
	
	// echo " q_cot = '$q_cot'<br>q_luks = '$q_luks'<br>q_stand = '$q_stand'<br>q_sum = '$q_sum'<br><br>";
	
	$booking[$booking_date]['j_cot'] = $j_cot;
	$booking[$booking_date]['j_luks'] = $j_luks;
	$booking[$booking_date]['j_stand'] = $j_stand;
	$booking[$booking_date]['j_sum'] = $j_sum;
	
	$booking[$booking_date]['q_cot'] = $q_cot;
	$booking[$booking_date]['q_luks'] = $q_luks;
	$booking[$booking_date]['q_stand'] = $q_stand;
	$booking[$booking_date]['q_sum'] = $q_sum;

	$q_cot = $q_luks = $q_stand = $q_sum = $j_cot = $j_luks = $j_stand = $j_sum = 0;
	$n_cot  = $n_stand = $n_luks = $n_sum = 0;
	
};
	
$MyData = new pData();
		if($period == 1)
	{
 	$mes = array("Янв","Фев","Мар","Апр","Май","Июн","Июл","Авг","Сен","Окт","Ноя","Дек");
	} 
	elseif($period == 2) 
	{
	$mes = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
	}
	
	$mesyx = array(
	0 => "За Год",
    1 => "Январь",
    2 => "Февраль",
	3 => "Март",
    4 => "Апрель",
	5 => "Май",
    6 => "Июнь",
	7 => "Июль",
    8 => "Август",
	9 => "Сентябрь",
    10 => "Октябрь",
	11 => "Ноябрь",
    12 => "Декабрь",
	100 => "$filter_date_start и $filter_date_end",
);
			if($filter_date_start != '') 	{  $messy = $mesyx[100]; }
		elseif($mesyac) 					{  $messy = $mesyx[$mesyac]; }    	 
	else  { $messy = 'За Год'; } 
	$m=1;
foreach ($mes as $value) {
 $qs_cot[$m] = $qs_luks[$m] = $qs_stand[$m] = $qs_sum[$m] = $d_cot[$m] = $d_luks[$m] = $d_stand[$m] = $d_sum[$m] = 0; $m++;
}

	$ki = 0;
foreach ($booking as $key => $value) // 2017-08-26
	{ 		
	$key0 = substr($key, 5,2);  //  08  месяц
	$key2 = substr($key, -2);   //  22  дата
	
	    if($period == 1) { 	$key1 = $key0*1; }
	elseif($period == 2) {  $key1 = $key2*1; }
	
	if($mesyac == 100) { $mesyac = $key0; }
		
	for ($i = 1; $i <= $m; $i++) {

	if($mesyac) 
	{
		
		 if(( $key1 == $i ) AND ( $key0 == $mesyac ) )	
			{ 
		 $d_cot[$key1] += $value['j_cot'];
		 $d_luks[$key1] += $value['j_luks'];
		 $d_stand[$key1] += $value['j_stand'];
		 $d_sum[$key1] += $value['j_sum'];
		 
		$qs_cot[$key1] = $value['q_cot'];
		$qs_luks[$key1] = $value['q_luks'];
		$qs_stand[$key1] = $value['q_stand'];
		$qs_sum[$key1] = $value['q_sum'];
			}
		
	}  else {
	
		 if( $key1 == $i ) 	
			{ 
		 $d_cot[$key1] += $value['j_cot'];
		 $d_luks[$key1] += $value['j_luks'];
		 $d_stand[$key1] += $value['j_stand'];
		 $d_sum[$key1] += $value['j_sum'];
		 
		$qs_cot[$key1] = $value['q_cot'];
		$qs_luks[$key1] = $value['q_luks'];
		$qs_stand[$key1] = $value['q_stand'];
		$qs_sum[$key1] = $value['q_sum'];
			}	  
						}	
								}			
	
	$ki++;
	}
		
	$m = 1;
	$inter = $ki;
	$inter = 31;
	if($mesyac) 	{ $inter = 1; } else  { $inter = 31; }
	if($inters != 0) { $inter = $inters; }
	foreach ($mes as $value) {

	
	
	
	if($cat == 0)  {

	  $inter = 12;
	 $MyData->addPoints( round($d_cot[$m]/(13 * $inter)*100, 1),"Коттеджи");
	 $MyData->addPoints( round($d_stand[$m]/(7 * $inter)*100, 1),"Стандарт");
	 $MyData->addPoints( round($d_luks[$m]/(21 * $inter)*100, 1),"Люкс");
	 $MyData->addPoints( round($d_sum[$m]/(41 * $inter)*100, 1),"Общий"); 
	  
					}
	elseif ($cat == 1) { $MyData->addPoints( round( $d_cot[$m]/(13 * $inter )*100, 2),"Коттеджи"); }  	//  13
	elseif ($cat == 3) { $MyData->addPoints( round( $d_stand[$m]/(7 * $inter)*100, 2),"Стандарт"); } 	// 	7
	elseif ($cat == 2) { $MyData->addPoints( round( $d_luks[$m]/(21 * $inter)*100, 2),"Люкс"); }			// 	21
	elseif ($cat == 4) { $MyData->addPoints( round( $d_sum[$m]/(41 * $inter)*100, 2),"Общий"); }			//	41
	 
	 	if ($cat == 1) {  $n_cot += round( $d_cot[$m]/(13 * $inter )*100, 2);	}
	elseif ($cat == 3) {  $n_stand += round( $d_stand[$m]/(7 * $inter )*100, 2);	}
	elseif ($cat == 2) {  $n_luks += round( $d_luks[$m]/(21 * $inter )*100, 2);	}
	elseif ($cat == 4) {  $n_sum += round( $d_sum[$m]/(41 * $inter )*100, 2);	}
			
		
	if(($d_cot[$m] == 0) AND ($d_cot[$m-1] == 0) AND ($d_cot[$m+1] == 0)) { $d_cot[$m] = VOID; } 
	if(($d_stand[$m] == 0) AND ($d_stand[$m-1] == 0) AND ($d_stand[$m+1] == 0)) { $d_stand[$m] = VOID; } 
	if(($d_luks[$m] == 0) AND ($d_luks[$m-1] == 0) AND ($d_luks[$m+1] == 0)) { $d_luks[$m] = VOID; } 
	if(($d_sum[$m] == 0) AND ($d_sum[$m-1] == 0) AND ($d_sum[$m+1] == 0)) { $d_sum[$m] = VOID; } 	
		
	 $m++;
}
	
	
		if ($cat == 1) { $dan = $n_cot; 	}
	elseif ($cat == 3) { $dan = $n_stand; 	}
	elseif ($cat == 2) { $dan = $n_luks; 	}
	elseif ($cat == 4) { $dan = $n_sum; 	}
	
		if ($cat == 1) { $MyData->setSerieWeight( "Коттеджи",3); 	}
	elseif ($cat == 3) { $MyData->setSerieWeight( "Стандарт",3); 	}
	elseif ($cat == 2) { $MyData->setSerieWeight( "Люкс",3); 		}
	elseif ($cat == 4) { $MyData->setSerieWeight( "Общий",3); 		}



 // $MyData->setSerieWeight("Общий",2);

 $MyData->setAxisName(0," % Загрузки");

 
 $MyData->addPoints($mes,"Labels");
 $MyData->setSerieDescription("Labels","Месяцы");
 $MyData->setAbscissa("Labels");
 $MyData->setAbscissaName("2017 год"); 

 /* Create the pChart object */
 $myPicture = new pImage(1200,630,$MyData);

 /* Turn of Antialiasing */
 $myPicture->Antialias = FALSE;

 /* Draw the background */
 $Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
 $myPicture->drawFilledRectangle(0,0,1200,630,$Settings);

 /* Overlay with a gradient */
 $Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
 $myPicture->drawGradientArea(0,0,1200,630,DIRECTION_VERTICAL,$Settings);
 $myPicture->drawGradientArea(0,0,1200,30,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));

 /* Add a border to the picture */
 $myPicture->drawRectangle(0,0,1195,625,array("R"=>0,"G"=>0,"B"=>0));
 
 /* Write the chart title */ 
 $myPicture->setFontProperties(array("FontName"=>"fonts/tahoma.ttf","FontSize"=>12,"R"=>255,"G"=>255,"B"=>255));

 $myPicture->drawText(10,16,"Общий График, Загрузка за период работы $messy",array("FontSize"=>12,"Align"=>TEXT_ALIGN_BOTTOMLEFT));

 /* Set the default font */
 $myPicture->setFontProperties(array("FontName"=>"fonts/tahoma.ttf","FontSize"=>10,"R"=>0,"G"=>0,"B"=>0));

 /* Define the chart area */
 $myPicture->setGraphArea(60,50,1190,580);

 
 if($filter_date_start != '') 	{  
  /* Write some text */  
 $myPicture->setFontProperties(array("FontName"=>"fonts/tahoma.ttf","FontSize"=>6)); 
 $TextSettings = array("DrawBox"=>TRUE,"BoxRounded"=>TRUE,"R"=>0,"G"=>0,"B"=>0,"Angle"=>0,"FontSize"=>10); 
 $myPicture->drawText(70,90,"Общий % за период $messy составляет $dan %",$TextSettings); 
							}
 /* Draw the scale */
 $scaleSettings = array("XMargin"=>10,"YMargin"=>10,"Floating"=>TRUE,"GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE);
 $myPicture->drawScale($scaleSettings);

/* Draw the chart */ 
	$myPicture->Antialias = TRUE; 
	$myPicture->drawSplineChart(); 
	$myPicture->Antialias = FALSE; 

 /* Enable shadow computing */
 $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));

 /* Draw the line chart */
 $myPicture->drawLineChart();
 $myPicture->drawPlotChart(array("DisplayValues"=>TRUE,"PlotBorder"=>TRUE,"BorderSize"=>2,"Surrounding"=>-60,"BorderAlpha"=>80));

 /* Write the chart legend */
 $myPicture->drawLegend(900,9,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL,"FontR"=>255,"FontG"=>255,"FontB"=>255));

 /* Render the picture (choose the best way) */
 $myPicture->Stroke();
?>