<?php 
	$pid			= $_GET['pid'];
	$lang 			= $_GET['lang'];	
	$datayesno		= $_POST['datayesno'];		
	$periodin		= $_POST['periodin'];		

	$bgc_planned  	= "#74c0fc";
	$bgc_planned1  	= "#d0ebff";
	$bgc_costs  	= "#ffc107";
	$bgc_costs1  	= "#ffecb3";

include('r_dbc.php');

$sql = "SELECT date_format(min(r.rab_date),'%d-%m-%Y') as min_rab_date,
		date_format(max(r.rab_date),'%d-%m-%Y') as max_rab_date
		from ccmanager r
		where r.pid = ".$pid."";
		
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
		$min_date = $row["min_rab_date"];
		$max_date = $row["max_rab_date"];		

    }
} 

mysqli_close($conn);

	
	if ($min_date != "") {
	$min_datec	= substr($min_date,6,4)."-".substr($min_date,3,2)."-".substr($min_date,0,2);
	$max_datec	= substr($max_date,6,4)."-".substr($max_date,3,2)."-".substr($max_date,0,2);
	}
	if ($min_date == "") {
	$min_date	= substr($min_datec,8,2)."-".substr($min_datec,5,2)."-".substr($min_datec,0,4);
	$max_date	= substr($max_datec,8,2)."-".substr($max_datec,5,2)."-".substr($max_datec,0,4);
	}	
	
	
	$repid1 = date("YmdHis");
	$repid2 = date("Y")*date("m")*date("d")+date("s")+rand(10,100);
	$repid3 = date("d")*date("d")*date("s")+date("s")+rand(100,10000);
	$repid = $repid1.$repid2.$repid3;	
	
	
	include('config.php');
	include('field_names.php'); 
	

/* COUNT CENTERS & CATEGORIES FOR HEIGHT OF BAR CHARTS */
include('r_dbc.php');

$sql = "select count(distinct r.code_center) as cnt_center,count(distinct r.code_categ) as cnt_categ
		from ccmanager r
		where r.pid = '".$pid."'
		and r.mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 3)";
		
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
		$cnt_center 	= $row["cnt_center"];
		$cnt_categ 		= $row["cnt_categ"];
		}		
    }
 
mysqli_close($conn);	
	
// FIND MONTHS

include('r_dbc.php');

$sqlm = "select count(distinct date_format(calday,'%m-%Y')) as count_months
		from calendar
		where calday >= '".$min_datec."' and calday <= '".$max_datec."'";
$resultm = mysqli_query($conn, $sqlm);		
if (mysqli_num_rows($resultm) > 0) {
      while($rowm = mysqli_fetch_assoc($resultm)) {
		$count_months	= $rowm["count_months"];
		}		
    }	
mysqli_close($conn);

if ($periodin == 'pdd')
	{ 
		$dateqry0 = "date_format(calday,'%d-%m')";
		$dateqry01 = "date_format(calday,'%Y%m%d')";
		$dateqry1 = "date_format(r.rab_date,'%d-%m') as mongt";
		$dateqry11 = "date_format(min(r.rab_date),'%Y%m%d')";
		$dateqry2 = "date_format(r.rab_date,'%d-%m')"; 
		$title_chart = $title_chart_line1_daily;
	}
if ($periodin == 'pww')
	{ 
		$dateqry0 = "concat('".$nweek."',' ',date_format(calday,'%u-%y'))";
		$dateqry01 = "date_format(calday,'%Y%u')";
		$dateqry1 = "concat('".$nweek."',' ',date_format(r.rab_date,'%u-%y')) as mongt";
		$dateqry11 = "date_format(min(r.rab_date),'%Y%u')";
		$dateqry2 = "date_format(r.rab_date,'%u-%y')"; 
		$title_chart = $title_chart_line1_weekly;
	}
if ($periodin == 'pmm')
	{ 
		$dateqry0 = "date_format(calday,'%b-%y')";
		$dateqry01 = "date_format(calday,'%Y%m')";
		$dateqry1 = "date_format(r.rab_date,'%b-%y') as mongt";
		$dateqry11 = "date_format(min(r.rab_date),'%Y%m')";
		$dateqry2 = "date_format(r.rab_date,'%b-%y')"; 
		$title_chart = $title_chart_line1_monthly;
	}
if ($periodin == 'pqq')
	{ 
		$dateqry0 = "concat('Q',quarter(calday),'-',date_format(calday,'%Y'))"; 
		$dateqry01 = "concat(date_format(calday,'%Y'),'Q',quarter(calday))";
		$dateqry1 = "concat('Q',quarter(r.rab_date),'-',date_format(r.rab_date,'%Y')) as mongt"; 
		$dateqry1 = "concat('Q',quarter(r.rab_date),'-',date_format(r.rab_date,'%Y')) as mongt"; 		
		$dateqry11 = "concat(date_format(r.rab_date,'%Y'),'Q',quarter(r.rab_date))";		
		$dateqry2 = "quarter(r.rab_date),date_format(r.rab_date,'%Y')"; 
		$title_chart = $title_chart_line1_quarter;
	}
if ($periodin == 'pyy')
	{ 
		$dateqry0 = "date_format(calday,'%Y')"; 
		$dateqry01 = "date_format(calday,'%Y')";
		$dateqry1 = "date_format(r.rab_date,'%Y') as mongt"; 
		$dateqry1 = "date_format(r.rab_date,'%Y') as mongt"; 		
		$dateqry11 = "date_format(r.rab_date,'%Y')";		
		$dateqry2 = "date_format(r.rab_date,'%Y')"; 
		$title_chart = $title_chart_line1_year;
	}


?>

<?php	
// EXPENSES DESC MANAGER
include('r_dbc.php');
$sqlmid = "select desc_manager
			from ccmanager_id 
			where pid='".$pid."' 
			and expenses = 1";

$resultmid = mysqli_query($conn, $sqlmid);

if (mysqli_num_rows($resultmid) > 0) {
      while($rowmid = mysqli_fetch_assoc($resultmid)) {
		$desc_manager_expenses		= $rowmid["desc_manager"];
    }
} 
mysqli_close($conn);
?>

<?php	
// PLANNED DESC MANAGER
include('r_dbc.php');
$sqlmid = "select desc_manager
			from ccmanager_id 
			where pid='".$pid."' 
			and expenses = 3";

$resultmid = mysqli_query($conn, $sqlmid);

if (mysqli_num_rows($resultmid) > 0) {
      while($rowmid = mysqli_fetch_assoc($resultmid)) {
		$desc_manager_planned		= $rowmid["desc_manager"];
    }
} 
mysqli_close($conn);
?>


<?php	
// BUDGET DESC MANAGER
include('r_dbc.php');
$sqlmid = "select desc_manager
			from ccmanager_id 
			where pid='".$pid."' 
			and budget = 1";

$resultmid = mysqli_query($conn, $sqlmid);

if (mysqli_num_rows($resultmid) > 0) {
      while($rowmid = mysqli_fetch_assoc($resultmid)) {
		$desc_manager_budget	= $rowmid["desc_manager"];
    }
} 
mysqli_close($conn);
?>

<!DOCTYPE html>
<html>
<head>
<title>
<?php 
	include('title.php'); 
	  $rep_font_size12  = "9px";				// FONT SIZE ON DETAILS OF REPORTS REDUCE ON THIS REPORT
	  $rep_font_size14  = "11px";				// FONT SIZE ON HEADERS OF REPORTS REDUCE ON THIS REPORT
?>
</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 0px solid black;
}

th, td {
  font-family:verdana;
  padding: 3px;
  border: 1px solid black;
  font-size: <?php echo $rep_font_size12 ?>;      
}


p.a {
  font-family:verdana;
  text-align:right;
  font-size: <?php echo $rep_font_size14 ?>;      
}

p.b {
  font-family:verdana;
  text-align:right;
  width=50;
  background-color:tomato;
  font-size: <?php echo $rep_font_size14 ?>;      
}

</style>

 </head>
<body>

<table style='border-collapse:collapse;border-spacing: 0;width: 100%;border: 0px solid black;'>
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='text-align:right;border:0px;font-family:verdana;padding: 1px;font-size: <?php echo $rep_font_size14 ?>'><?php printf("<b>$htitle</b>"); ?></td>
<tr>
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='text-align:right;border:0px;font-family:verdana;padding: 1px;font-size: <?php echo $rep_font_size14 ?>'><?php printf("<b>$title_gantt_centers</b>"); ?></td>
<tr>
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='text-align:right;border:0px;font-family:verdana;padding: 1px;font-size: <?php echo $rep_font_size14 ?>'><?php printf("$nfrom : <b> $min_date </b>"); ?></td>
<tr>
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='text-align:right;border:0px;font-family:verdana;padding: 1px;font-size: <?php echo $rep_font_size14 ?>'><?php printf("$nto : <b> $max_date </b>"); ?></td>
<tr>
<?php 
// CHECK DATA YES OR NO
	if ($datayesno == 0) { $datayesno_desc = $n_data.": <b>".$nno."</b>"; }
	if ($datayesno == 1) { $datayesno_desc = $n_data.": <b>".$nyes."</b>"; }
?>	
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='text-align:right;border:0px;font-family:verdana;padding: 1px;font-size: <?php echo $rep_font_size14 ?>'><?php printf("$datayesno_desc"); ?></td>
<tr>
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='border:0px'> &nbsp </td>
<tr>
<tr>
	<td width=90% style='border:0px'></td>
	<td style='text-align:right;border: 1px solid black;font-family:verdana;padding:3px;background-color:<?php echo $bgc_planned ?>;font-size: <?php echo $rep_font_size14 ?>'><?php printf("<b> $desc_manager_planned </b>"); ?></td>
<tr>
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='text-align:right;border: 1px solid black;font-family:verdana;padding:3px;background-color:<?php echo $bgc_costs ?>;font-size: <?php echo $rep_font_size14 ?>'><?php printf("<b> $desc_manager_expenses </b>"); ?></td>
<tr>
<tr>
	<td width=90% style='border:0px'> </td>
	<td style='text-align:right;border:0px;font-family:verdana;padding: 1px;font-size: <?php echo $rep_font_size14 ?>'><?php printf("$ninthousands"); ?></td>
<tr>
</table>

<br>



<?php
$month_top_row = "";
include('r_dbc.php');

$sql = "SELECT date_format(calday,'%M-%Y') as month_top, count(calday) as month_top_days
		from calendar
		where calday >= '".$min_datec."' and calday <= '".$max_datec."'		
		group by date_format(calday,'%M-%Y')
		order by calday";
		
// echo $sql;		

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
			$month_top 			= $row["month_top"];
			$month_top_days		= $row["month_top_days"];
			
			$month_top_row		= $month_top_row."<th style='text-align:center;background-color:#bbbbbb' colspan=".$month_top_days.">".$month_top."</th>";
		}		
    }

mysqli_close($conn);
		if ($datayesno == 1) {		
			$month_top_row = "".$month_top_row."<th style='text-align:center;background-color:#bbbbbb' colspan=3 ></th><tr>";
		}
		if ($datayesno == 0) {		
			$month_top_row = "".$month_top_row."<tr>";
		}		

?> 





<?php

$myear_td = "";

include('r_dbc.php');

$sql = "SELECT DISTINCT ".$dateqry0." as mong
		from calendar
		where calday >= '".$min_datec."' and calday <= '".$max_datec."'		
		order by calday";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
	printf("<div style='overflow-x:auto;'>");
	printf("<table>");
		printf("<tr><th style='text-align:center;background-color:#bbbbbb' rowspan=2 >$n_centers</th>");
		if ($datayesno == 1) {																	
			printf("<th style='text-align:center;background-color:#bbbbbb' rowspan=2 >$desc_manager_budget</th>");
		}
		if ($datayesno == 0) {															
				printf("");
		}				
		if ($periodin == 'pdd') {	
				printf("$month_top_row");	
				}		
	
		if ($periodin != 'pdd') {	
				printf("<tr>");	
	
				}		
		
      while($row = mysqli_fetch_assoc($result)) {
			$mong 			= $row["mong"];
			if ($periodin == 'pdd') { $mong = substr($mong,0,2); }
			printf("<th style='text-align:center;background-color:#bbbbbb' >$mong</th>");
			$myear_td = $myear_td.",'".$mong."'";		
		}		
		if ($datayesno == 1) {															
				printf("<th style='text-align:center;background-color:#bbbbbb' >$ntotal</th>");
		}
		if ($datayesno == 0) {															
				printf("");
		}		
		
			printf("<th style='text-align:center;background-color:#bbbbbb' >$nfrom</th>");		
				printf("<th style='text-align:center;background-color:#bbbbbb' >$nto</th>");									
		printf("</tr>");		

    }
mysqli_close($conn);
?> 


<?php
$xdivider = 1000;
$budgets_r = 0;
// CENTERS TOTALS 
include('r_dbc.php');
	$sqlg = "SELECT DISTINCt r.code_center, c.desc_center,sum(r.rab_number) as budgets,date_format(min(rab_date),'%d-%m-%y') as date_stot,date_format(max(rab_date),'%d-%m-%y') as date_etot
			from ccmanager r, centers c
			where r.pid = c.pid and r.code_center = c.code_center
			and r.pid = '".$pid."'
			and r.mid in (select mid from ccmanager_id where pid = '".$pid."' and budget = 1)
			group by r.code_center
			order by 1";
		$resultg = mysqli_query($conn, $sqlg);
			if (mysqli_num_rows($resultg) > 0) {
				while($rowg = mysqli_fetch_assoc($resultg)) {
				$code_center 	= $rowg["code_center"];
				$code_centerx   = $code_center;
				$desc_center 	= $rowg["desc_center"];			
				$date_stot 		= $rowg["date_stot"];				
				$date_etot 		= $rowg["date_etot"];				
				$budgets 		= $rowg["budgets"]/$xdivider;		
				$c_budgets		= number_format($budgets, 2, $septh, $sepdc);
				$budgets_r		= $budgets_r + $budgets;
				$c_budgets_r	= number_format($budgets_r, 2, $septh, $sepdc);				
		printf("<tr>");
		
		
		printf("<td style='text-align:left;background-color:#eaeaea' rowspan=2 >");
		// if ($cnt_categ > 1) {
		// printf("<a href='z_r_gantt_centers.php?pid=$pid&lang=$lang&min_datec=$min_datec&max_datec=$max_datec&det=1'>");
		// }
		
		if ($datayesno == 1) {
			$desc_manager_planned_show_center = "(".$desc_manager_planned.")";
		}
		if ($datayesno == 0) {
			$desc_manager_planned_show_center = "";
		}		
			
		printf("<b> $code_center - $desc_center $desc_manager_planned_show_center </b>");
		if ($cnt_categ > 1) {
		printf("</a>");
		}
		printf("</td>");
			if ($datayesno == 1) {													
				printf("<td style='text-align:right;background-color:$bgc_planned'  rowspan=2 ><b>$c_budgets</b></td>");
			}
			if ($datayesno == 0) {													
				printf("");
			}			
		
	$sqlm = "select x.mongt,sum(x.mong_total) as mong_totals,x.mindate
			from
			(
			SELECT ".$dateqry1.",sum(r.rab_number) as mong_total,".$dateqry11." as mindate
			from ccmanager r
			where r.pid = '".$pid."'
			and r.mid in (select x.mid from ccmanager_id x where x.pid = r.pid and x.expenses = 3)
			and r.code_center = '".$code_center."'
			and r.rab_date >= '".$min_datec."' and r.rab_date <= '".$max_datec."'
			group by ".$dateqry2."
			UNION
			SELECT distinct ".$dateqry0." as mongt,0 as mong_total,".$dateqry01." as mindate
			from calendar
			where calday >= '".$min_datec."' and calday <= '".$max_datec."'
			order by 3
			) as x
			group by x.mindate,x.mongt
			";
			// echo $sqlm;
			// echo '<br><br><br>';
			
				
			
		$resultm = mysqli_query($conn, $sqlm);
			if (mysqli_num_rows($resultm) > 0) {
				
				$rmong_totals = 0;
				
				while($rowm = mysqli_fetch_assoc($resultm)) {
				$mongt 			= $rowm["mongt"];
				$mong_totals 	= $rowm["mong_totals"]/$xdivider;
				$rmong_totals	= $rmong_totals + $mong_totals;
				
				$budget_perc 	= ($mong_totals / $budgets)*100;
				$budget_perc_100 = 100 - $budget_perc;
				
				if ($mong_totals > 0 || $mong_totals < 0) 
						{		
							$c_mong_totals	= number_format($mong_totals, $totdc, $septh, $sepdc); 
							$c_budget_perc	= number_format($budget_perc, 0, $septh, $sepdc)."%%"; 
							$bgc_plannedx = $bgc_planned; 							
						}
				if ($mong_totals == 0) 
						{	
							$c_mong_totals	= ""; 
							$c_budget_perc	= ""; 
							$bgc_plannedx = "white"; 
						}
				
					if ($datayesno == 1) {
						printf("<td style='text-align:right;background-color:$bgc_plannedx' ><b> $c_mong_totals </b> <br> $c_budget_perc</td>");
					}
					if ($datayesno == 0) {
						printf("<td style='text-align:right;background-color:$bgc_plannedx' >&nbsp</td>");
					}					
				}

				$budget_perc_total 	= ($rmong_totals / $budgets)*100;
				$c_budget_perc_total	= number_format($budget_perc_total, 0, $septh, $sepdc)."%%"; 				
				
			$c_rmong_totals	= number_format($rmong_totals, $totdc, $septh, $sepdc);
					if ($datayesno == 1) {			
						printf("<th style='text-align:right;background-color:$bgc_planned' ><b> $c_rmong_totals </b>  <br> $c_budget_perc_total</th>");
					}
					if ($datayesno == 0) {			
						printf("");
					}					
				
			}
			
printf("<td style='text-align:center;background-color:$bgc_planned'><b>$date_stot</b></td><td style='text-align:center;background-color:$bgc_planned'><b>$date_etot</b></td>");			

		printf("</tr>");
		
		
		
	$sqlmc = "select x.mongt,sum(x.mong_total) as mong_totals,x.mindate
			from
			(
			SELECT ".$dateqry1.",sum(r.rab_number) as mong_total,".$dateqry11." as mindate
			from ccmanager r
			where r.pid = '".$pid."'
			and r.mid in (select x.mid from ccmanager_id x where x.pid = r.pid and x.expenses = 1)
			and r.code_center = '".$code_center."'
			and r.rab_date >= '".$min_datec."' and r.rab_date <= '".$max_datec."'
			group by ".$dateqry2."
			UNION
			SELECT distinct ".$dateqry0." as mongt,0 as mong_total,".$dateqry01." as mindate
			from calendar
			where calday >= '".$min_datec."' and calday <= '".$max_datec."'
			order by 3
			) as x
			group by x.mindate,x.mongt
			";
			// echo $sqlmc;
			// echo '<br><br><br>';
			
				
			
		$resultmc = mysqli_query($conn, $sqlmc);
			if (mysqli_num_rows($resultmc) > 0) {
				
				$rmong_totalsc = 0;
				
				while($rowmc = mysqli_fetch_assoc($resultmc)) {
				$mongtc			= $rowmc["mongt"];
				$mong_totalsc 	= $rowmc["mong_totals"]/$xdivider;
				$rmong_totalsc	= $rmong_totalsc + $mong_totalsc;
				
				$budget_perc_c 	= ($mong_totals_c / $budgets)*100;
				$budget_perc_100_c = 100 - $budget_perc_c;
				
				if ($mong_totalsc > 0 || $mong_totalsc < 0) 
						{		
							$c_mong_totalsc		= number_format($mong_totalsc, $totdc, $septh, $sepdc); 
							$c_budget_perc_c	= number_format($budget_perc_c, 0, $septh, $sepdc)."%%";
							$bgc_costsx			= $bgc_costs;
						}
				if ($mong_totalsc == 0) 
						{	
							$c_mong_totalsc	= ""; 
							$c_budget_perc_c	= "";
							$bgc_costsx			= "white";
						}
				
					if ($datayesno == 1) {
						printf("<td style='text-align:right;background-color:$bgc_costsx' ><b> $c_mong_totalsc </b> <br> $c_budget_perc_c</td>");
					}
					if ($datayesno == 0) {
						printf("<td style='text-align:right;background-color:$bgc_costsx' >&nbsp</td>");
					}					
				}

				$budget_perc_total_c 	= ($rmong_totalsc / $budgets)*100;
				$c_budget_perc_total_c	= number_format($budget_perc_total_c, 0, $septh, $sepdc)."%%"; 				
				
			$c_rmong_totalsc	= number_format($rmong_totalsc, $totdc, $septh, $sepdc);
					if ($datayesno == 1) {			
						printf("<th style='text-align:right;background-color:$bgc_costs' ><b> $c_rmong_totalsc </b>  <br> $c_budget_perc_total_c</th>");
					}
					if ($datayesno == 0) {			
						printf("");
					}					
				
			}

	$sqld_expcg = "SELECT date_format(min(rab_date),'%d-%m-%y') as date_sexpcg,date_format(max(rab_date),'%d-%m-%y') as date_eexpcg
			from ccmanager r, categs c
			where r.pid = c.pid and r.code_categ = c.code_categ
			and r.code_center = '".$code_centerx."'
			and r.pid = '".$pid."'
			and r.mid in (select mid from ccmanager_id where pid = '".$pid."' and expenses = 1)";
		$resultd_expcg = mysqli_query($conn, $sqld_expcg);
			if (mysqli_num_rows($resultd_expcg) > 0) {
				while($rowd_expcg = mysqli_fetch_assoc($resultd_expcg)) {
				$date_sexpcg 		= $rowd_expcg["date_sexpcg"];
				$date_eexpcg 		= $rowd_expcg["date_eexpcg"];		
				}
			}
		
		printf("<td style='text-align:center;background-color:$bgc_costs'>$date_sexpcg</td><td style='text-align:center;background-color:$bgc_costs'>$date_eexpcg</td>");		

		printf("</tr>");		

  include ('r_gantt_centers_rep_det.php');		
			}
			}





mysqli_close($conn);

?>




<?php

// TOTALS MONTHS
include('r_dbc.php');
		printf("<tr>");
		// printf("<th style='text-align:left;background-color:#eaeaea' ><a href='z_r_gantt_centers.php?pid=$pid&lang=$lang&min_datec=$min_datec&max_datec=$max_datec'>$ntotal</a></th>");
		printf("<td style='text-align:left;background-color:#bbbbbb' rowspan=2  ><b>$ntotal</b></td>");
		if ($datayesno == 1) 
			{
				printf("<td style='text-align:right;background-color:#bbbbbb'  rowspan=2 ><b>$c_budgets_r</b></td>");
			}
		if ($datayesno == 0) 
			{
				printf("");
			}			
		
	$sqls = "select x.mongt as mongtf,sum(x.mong_total) as mong_totalsf,x.mindate as mindatef
			from
			(
			SELECT ".$dateqry1.",sum(r.rab_number) as mong_total,".$dateqry11." as mindate
			from ccmanager r
			where r.pid = '".$pid."'
			and r.mid in (select x.mid from ccmanager_id x where x.pid = r.pid and x.expenses = 3)
			and r.rab_date >= '".$min_datec."' and r.rab_date <= '".$max_datec."'			
			group by ".$dateqry2."
			UNION
			SELECT distinct ".$dateqry0." as mongt,0 as mong_total,".$dateqry01." as mindate
			from calendar
			where calday >= '".$min_datec."' and calday <= '".$max_datec."'
			) as x
			group by x.mindate,x.mongt";

			// echo '<br><br><br>';
			// echo $sqls;
			
			
				
			
		$results = mysqli_query($conn, $sqls);
			if (mysqli_num_rows($results) > 0) {
				
				$rmong_totalsf = 0;
				
				while($rows = mysqli_fetch_assoc($results)) {
				$mongtf 		= $rows["mongtf"];
				$mong_totalsf 	= $rows["mong_totalsf"]/$xdivider;
				$rmong_totalsf	= $rmong_totalsf + $mong_totalsf;
				
				$budget_totalsf_perc 	= ($mong_totalsf / $budgets_r)*100;
				
				if ($mong_totalsf > 0) 
							{	
								$c_mong_totalsf	= number_format($mong_totalsf, $totdc, $septh, $sepdc); 
								$c_budget_totalsf_perc	= number_format($budget_totalsf_perc, 1, $septh, $sepdc)."%%"; 
								$bgc_plannedxx = $bgc_planned; 															
							}
								
				if ($mong_totalsf == 0) {	
									$c_mong_totalsf	= ""; 
									$c_budget_totalsf_perc	= "";									
									$bgc_plannedxx = "white";
								}

				$mtotalf   = $mtotalf.",".$mong_totalsf;						
				
					if ($datayesno == 1) {		
						printf("<td style='text-align:right;background-color:$bgc_plannedxx' ><b>$c_mong_totalsf</b><br>$c_budget_totalsf_perc</td>");
					}
					if ($datayesno == 0) {		
						printf("<td style='text-align:right;background-color:$bgc_plannedxx' >&nbsp</td>");
					}					
				}
				
			
			$c_rmong_totalsf	= number_format($rmong_totalsf, $totdc, $septh, $sepdc);
			$budget_rtotalsf_perc 	= ($rmong_totalsf / $budgets_r)*100;
			$c_budget_rtotalsf_perc	= number_format($budget_rtotalsf_perc, 1, $septh, $sepdc)."%%"; 
			if ($datayesno == 1) {							
				printf("<td style='text-align:right;background-color:$bgc_planned' ><b>$c_rmong_totalsf</b><br>$c_budget_rtotalsf_perc</td>");				
			}
			if ($datayesno == 0) {							
				printf("");				
			}			
				
			}
			

	$sqld_plant = "SELECT date_format(min(rab_date),'%d-%m-%y') as date_sexplant,date_format(max(rab_date),'%d-%m-%y') as date_eexplant
			from ccmanager r
			where r.pid = '".$pid."'
			and r.mid in (select mid from ccmanager_id where pid = '".$pid."' and expenses = 3)";
		$resultd_plant = mysqli_query($conn, $sqld_plant);
			if (mysqli_num_rows($resultd_plant) > 0) {
				while($rowd_plant = mysqli_fetch_assoc($resultd_plant)) {
				$date_sexplant 		= $rowd_plant["date_sexplant"];
				$date_eexplant 		= $rowd_plant["date_eexplant"];		
				}
			}
		
		printf("<td style='text-align:center;background-color:$bgc_planned'><b>$date_sexplant</b></td><td style='text-align:center;background-color:$bgc_planned'><b>$date_eexplant</b></td>");					


	printf("</tr>");


mysqli_close($conn);
?>


<?php

// TOTALS MONTHS
include('r_dbc.php');
		printf("<tr>");
		// printf("<th style='text-align:left;background-color:#eaeaea' ><a href='z_r_gantt_centers.php?pid=$pid&lang=$lang&min_datec=$min_datec&max_datec=$max_datec'>$ntotal</a></th>");
		
	$sqlsc = "select x.mongt as mongtf,sum(x.mong_total) as mong_totalsf,x.mindate as mindatef
			from
			(
			SELECT ".$dateqry1.",sum(r.rab_number) as mong_total,".$dateqry11." as mindate
			from ccmanager r
			where r.pid = '".$pid."'
			and r.mid in (select x.mid from ccmanager_id x where x.pid = r.pid and x.expenses = 1)
			and r.rab_date >= '".$min_datec."' and r.rab_date <= '".$max_datec."'			
			group by ".$dateqry2."
			UNION
			SELECT distinct ".$dateqry0." as mongt,0 as mong_total,".$dateqry01." as mindate
			from calendar
			where calday >= '".$min_datec."' and calday <= '".$max_datec."'
			) as x
			group by x.mindate,x.mongt";

			// echo '<br><br><br>';
			// echo $sqls;
			
			
				
			
		$resultsc = mysqli_query($conn, $sqlsc);
			if (mysqli_num_rows($resultsc) > 0) {
				
				$rmong_totalsfc = 0;
				
				while($rowsc = mysqli_fetch_assoc($resultsc)) {
				$mongtfc 		= $rowsc["mongtf"];
				$mong_totalsfc 	= $rowsc["mong_totalsf"]/$xdivider;
				$rmong_totalsfc	= $rmong_totalsfc + $mong_totalsfc;
				
				$budget_totalsfc_perc 	= ($mong_totalsfc / $budgets_r)*100;
				
				if ($mong_totalsfc > 0) 
							{	
								$c_mong_totalsfc	= number_format($mong_totalsfc, $totdc, $septh, $sepdc); 
								$c_budget_totalsfc_perc	= number_format($budget_totalsfc_perc, 1, $septh, $sepdc)."%%"; 	
								$bgc_costsxx	= $bgc_costs;
							}
				if ($mong_totalsfc == 0) 
							{	
								$c_mong_totalsfc	= ""; 
								$c_budget_totalsfc_perc	= "";
								$bgc_costsxx	= "white";
							}

				$mtotalfc   = $mtotalfc.",".$mong_totalsfc;						
				
				if ($datayesno == 1) {				
						printf("<td style='text-align:right;background-color:$bgc_costsxx' ><b>$c_mong_totalsfc</b><br>$c_budget_totalsfc_perc</td>");
					}
				if ($datayesno == 0) {				
						printf("<td style='text-align:right;background-color:$bgc_costsxx' >&nbsp</td>");
					}					
				}
				
			
			$c_rmong_totalsfc	= number_format($rmong_totalsfc, $totdc, $septh, $sepdc);
			$budget_rtotalsfc_perc 	= ($rmong_totalsfc / $budgets_r)*100;
			$c_budget_rtotalsfc_perc	= number_format($budget_rtotalsfc_perc, 1, $septh, $sepdc)."%%"; 							

					if ($datayesno == 1) {				
						printf("<td style='text-align:right;background-color:$bgc_costs' ><b>$c_rmong_totalsfc</b><br>$c_budget_rtotalsfc_perc</td>");				
					}
					if ($datayesno == 0) {				
						printf("");				
					}					
				
			}
			

	$sqld_ct = "SELECT date_format(min(rab_date),'%d-%m-%y') as date_sexct,date_format(max(rab_date),'%d-%m-%y') as date_eexct
			from ccmanager r
			where r.pid = '".$pid."'
			and r.mid in (select mid from ccmanager_id where pid = '".$pid."' and expenses = 1)";
		$resultd_ct = mysqli_query($conn, $sqld_ct);
			if (mysqli_num_rows($resultd_ct) > 0) {
				while($rowd_ct = mysqli_fetch_assoc($resultd_ct)) {
				$date_sexct 		= $rowd_ct["date_sexct"];
				$date_eexct 		= $rowd_ct["date_eexct"];		
				}
			}
		
		printf("<td style='text-align:center;background-color:$bgc_costs'><b>$date_sexct</b></td><td style='text-align:center;background-color:$bgc_costs'><b>$date_eexct</b></td>");					


			

		printf("</tr>");


mysqli_close($conn);
?>

</table>


<?php
	include('system_date.php');	
?>	

</body>
</html>