<?php
$rab_number_tot = 0;
$lrab_number_tot = 0;


echo "<table class='table table-bordered table-hover' >";
    echo "<tr>";
	echo "<th  class='text-center'>" . $n_rab_date . "</th>";
    echo "<th  class='text-center'>" . $nmonthly . " " . $desc_manager_2 . "</th>";
    echo "<th  class='text-center'>" . $ntotal . " " . $desc_manager_2 . "</th>";
    echo "<th  class='text-center'> % </th>";	
    echo "<th  class='text-center'>" . $nmonthly . " " . $desc_manager_expenses . "</th>";
    echo "<th  class='text-center'>" . $ntotal . " " . $desc_manager_expenses . "</th>";	
    echo "<th  class='text-center'> % </th>";		
    echo "</tr>";
include('r_dbc.php');
if ($cidx1 == "total") 
{
$sql="select x.grp,x.rab_date,x.rab_datec,sum(x.rab_number) as rab_number,sum(x.lrab_number) as lrab_number
			from 
			(
			SELECT 'PM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,r.rab_number,0 as lrab_number
			FROM ccmanager r, centers c
			WHERE r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 2
			and r.rab_number > 0			
			UNION ALL
			SELECT 'PM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,0 as rab_number,r.rab_number as lrab_number
			FROM ccmanager r, centers c
			WHERE r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 3

			) as x
			group by x.grp,x.rab_date,x.rab_datec
			order by x.rab_date";			
}
if ($cidx1 == "totalg") 
{
$sql="select x.grp,x.rab_date,x.rab_datec,sum(x.rab_number) as rab_number,sum(x.lrab_number) as lrab_number
			from 
			(
			SELECT 'PM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,r.rab_number,0 as lrab_number
			FROM ccmanager r, centers c
			WHERE c.code_center_group = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 2
			and r.rab_number > 0			
			UNION ALL
			SELECT 'PM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,0 as rab_number,r.rab_number as lrab_number
			FROM ccmanager r, centers c
			WHERE c.code_center_group = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 3
			and r.rab_number > 0			
			) as x
			group by x.grp,x.rab_date,date_format(x.rab_date,'%b-%y')			
			order by x.rab_date";
}
if ($cidx1 == "totalc") 
{
	$sql="select x.grp,x.rab_date,x.rab_datec,sum(x.rab_number) as rab_number,sum(x.lrab_number) as lrab_number,x.desc_center,x.code_center
			from 
			(
			SELECT 'PM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,r.rab_number,0 as lrab_number,c.desc_center,r.code_center
			FROM ccmanager r, centers c
			WHERE r.code_center = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 2
			and date_format(r.rab_date,'%y-%m') != date_format(now(),'%y-%m')			
			and r.rab_number > 0			
			UNION ALL
			SELECT 'PM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,0 as rab_number,r.rab_number as lrab_number,c.desc_center,r.code_center
			FROM ccmanager r, centers c
			WHERE r.code_center = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 3
			and date_format(r.rab_date,'%y-%m') != date_format(now(),'%y-%m')
			and r.rab_number > 0	
			UNION ALL
			SELECT 'CM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,r.rab_number,0 as lrab_number,c.desc_center,r.code_center
			FROM ccmanager r, centers c
			WHERE r.code_center = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 2
			and date_format(r.rab_date,'%y-%m') = date_format(now(),'%y-%m')			
			and r.rab_number > 0			
			UNION ALL
			SELECT 'CM' as grp,date_format(r.rab_date,'%y-%m') as rab_date,date_format(r.rab_date,'%M-%y') as rab_datec,0 as rab_number,r.rab_number as lrab_number,c.desc_center,r.code_center
			FROM ccmanager r, centers c
			WHERE r.code_center = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 3
			and date_format(r.rab_date,'%y-%m') = date_format(now(),'%y-%m')			
			and r.rab_number > 0	
			UNION ALL			
			SELECT 'D' as grp,date_format(r.rab_date,'%y-%m-%d') as rab_date,date_format(r.rab_date,'%d-%b') as rab_datec,r.rab_number,0 as lrab_number,c.desc_center,r.code_center
			FROM ccmanager r, centers c
			WHERE r.code_center = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 2
			and date_format(r.rab_date,'%y-%m') = date_format(now(),'%y-%m')
			and r.rab_number > 0			
			UNION ALL
			SELECT 'D' as grp,date_format(r.rab_date,'%y-%m-%d') as rab_date,date_format(r.rab_date,'%d-%b') as rab_datec,0 as rab_number,r.rab_number as lrab_number,c.desc_center,r.code_center
			FROM ccmanager r, centers c
			WHERE r.code_center = '".$cidx2."'
			and r.pid = '".$pidx."'
			and r.code_center = c.code_center
			and r.pid = c.pid
			and r.mid = 3
			and date_format(r.rab_date,'%y-%m') = date_format(now(),'%y-%m')			
			and r.rab_number > 0									
			) as x
			group by x.grp,x.rab_date,date_format(x.rab_date,'%b-%y'),x.desc_center,x.code_center			
			order by x.rab_date";
}
$result = mysqli_query($conn, $sql);
$rab_number_tot_previous = 0;
$lrab_number_tot_previous = 0;
if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {	
			$grp 		= $row['grp'];
			$rab_datec 	= $row['rab_datec'];
			$rab_number = $row['rab_number'];
			$lrab_number = $row['lrab_number'];			

			if ($grp == "PM") {
				$class_font_date 	= "text-center font-weight-bold";
				$class_font_num 	= "text-right";
				$rab_number_tot 	= $rab_number_tot + $rab_number;
				$lrab_number_tot 	= $lrab_number_tot + $lrab_number;			
			}
			if ($grp == "CM") {
				$class_font_date 	= "text-center font-weight-bold";
				$class_font_num 	= "text-right font-weight-bold";
				$rab_number_tot 	= $rab_number_tot + $rab_number;
				$lrab_number_tot 	= $lrab_number_tot + $lrab_number;			
			}			
			if ($grp == "D") {
				$class_font_date 	= "text-center font-italic";
				$class_font_num 	= "text-right font-italic";
			}
	
			if ($rab_number_tot_previous > 0)
			{
				$rab_number_daydiff = 100*($rab_number / $rab_number_tot_previous);
			}
			$rab_number_tot_previous = $rab_number_tot;
			
			if ($lrab_number_tot_previous > 0)
			{
				$lrab_number_daydiff = 100*($lrab_number / $lrab_number_tot_previous);
			}
			$lrab_number_tot_previous = $lrab_number_tot;			
			
			
			if ($rab_number == 0) 
				{ $c_rab_number = ""; }
			else if ($rab_number != 0) 				
				{ $c_rab_number	= number_format($rab_number,  $totdc, $septh, $sepdc); }

			if ($rab_number_tot == 0) 
				{ $c_rab_number_tot = ""; }
			else if ($rab_number_tot != 0) 
				{ $c_rab_number_tot	= number_format($rab_number_tot,  $totdc, $septh, $sepdc); }
			
			if ($lrab_number == 0) 
				{ $c_lrab_number = ""; }
			else if ($lrab_number != 0) 				
				{ $c_lrab_number	= number_format($lrab_number,  $totdc, $septh, $sepdc); }

			if ($lrab_number_tot == 0) 
				{ $c_lrab_number_tot = ""; }
			else if ($lrab_number_tot != 0) 
				{ $c_lrab_number_tot	= number_format($lrab_number_tot,  $totdc, $septh, $sepdc); }		
			
			if ($rab_number_daydiff == 0) 
				{ $c_rab_number_daydiff = ""; }
			else if ($rab_number_daydiff != 0) 				
				{ $c_rab_number_daydiff	= "+".number_format($rab_number_daydiff,  2, $septh, $sepdc)."%"; }	
			
			if ($lrab_number_daydiff == 0) 
				{ $c_lrab_number_daydiff = ""; }
			else if ($lrab_number_daydiff != 0) 				
				{ $c_lrab_number_daydiff	= "+".number_format($lrab_number_daydiff,  2, $septh, $sepdc)."%"; }			
			
			
    echo "<tr>";
	echo "<td class='" . $class_font_date . "'>" . $rab_datec . "</td>";
    echo "<td class='" . $class_font_num . "'>" . $c_rab_number . "</td>";
	if ($grp == "PM" || $grp == "CM") {
    echo "<td class='text-right'>" . $c_rab_number_tot . "</td>";
    echo "<td class='text-right'>" . $c_rab_number_daydiff . "</td>";	
	}
	if ($grp == "D") {
    echo "<td class='text-right'> </td>";
    echo "<td class='text-right'> </td>";
	}	

    echo "<td class='" . $class_font_num . "'>" . $c_lrab_number . "</td>";

	if ($grp == "PM" || $grp == "CM") {
	echo "<td class='text-right'>" . $c_lrab_number_tot . "</td>";
    echo "<td class='text-right'>" . $c_lrab_number_daydiff . "</td>";	
	}
	if ($grp == "D") {
    echo "<td class='text-right'> </td>";
    echo "<td class='text-right'> </td>";
	}		
    echo "</tr>";

			
}

}
    echo "<tr>";
	echo "<th class='text-center'>" . $ntotal . "</td>";
    echo "<th class='text-right'>" . $c_rab_number_tot . "</td>";
    echo "<th class='text-right'></td>";	
    echo "<th class='text-right'></td>";		
    echo "<th class='text-right'>" . $c_lrab_number_tot . "</td>";
    echo "<th class='text-right'></td>";
    echo "<th class='text-right'></td>";	
    echo "</tr>";
echo "</table>";
mysqli_close($conn);

$per_million = ($rab_number_tot / $population)*1000000;
$c_per_million	= number_format($per_million,  $totdc, $septh, $sepdc);

$lper_million = ($lrab_number_tot / $population)*1000000;
$c_lper_million	= number_format($lper_million,  $totdc, $septh, $sepdc);

echo "<table class='table table-bordered table-hover' >";
    echo "<tr>";
	echo "<th colspan=2 class='text-center'>".$desc_center." ".$nstatistics."</td>";
    echo "</tr>";
    echo "<tr>";
	echo "<td  class='text-left'>".$npopulation."</td>";
	echo "<td  class='text-right'>".$c_population."</td>";
    echo "</tr>";
    echo "<tr>";
	echo "<td  class='text-left'>".$desc_manager_2."</td>";
	echo "<td  class='text-right'>".$c_rab_number_tot."</td>";
    echo "</tr>";	
    echo "<tr>";
	echo "<td  class='text-left'>".$desc_manager_2." ".$nper." ".$nmillion."</td>";
	echo "<td  class='text-right'>".$c_per_million."</td>";
    echo "</tr>";	
    echo "<tr>";	
	echo "<td  class='text-left'>".$desc_manager_expenses."</td>";
	echo "<td  class='text-right'>".$c_lrab_number_tot."</td>";
    echo "</tr>";		
    echo "<tr>";		
	echo "<td  class='text-left'>".$desc_manager_expenses." ".$nper." ".$nmillion."</td>";
	echo "<td  class='text-right'>".$c_lper_million."</td>";
    echo "</tr>";		
echo "</table>";	



?>
