<?php 
	$pid		= $_GET['pid'];
	$lang 		= $_GET['lang'];	
	$min_date 	= $_POST['min_date'];		
	$max_date 	= $_POST['max_date'];	
	$center 	= $_POST['center'];				
	$xfdc 		= $_GET['xfdc'];
	$xuid		= $_GET['xuid'];		

	include('users.php');	

	
	$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);
	
	$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'); 
	
	
	// CHECK FOR CODE_CATEG START

	if ($center != "") 
		{ 
		$sql01 = "and r.code_center = '".$center."'"; 
		
		include('r_dbc.php');
 		$sql = "select c.desc_center
		from centers c
		where c.pid = '".$pid."'
		and c.code_center = '".$center."'";
		$result = mysqli_query($conn, $sql);
		if (mysqli_num_rows($result) > 0) {
			while($row = mysqli_fetch_assoc($result)) {
				$desc_center = $row["desc_center"];
			}
		}
		mysqli_close($conn);
	
		$title_categs = $n_center." : ".$center." ".$desc_center;
	
		}
	// CHECK FOR CODE_CATEG END		
	
?>

<!DOCTYPE html>
<html>
<head>
<title>
	  <?php include('title.php'); ?>
</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 1px solid black;
}

th, td {
  font-family:verdana;
  padding: 8px;
  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 ?>;  
}

</style>
</head>
<body>

<p class="a">
<?php
		printf("<b>$htitle</b>");
		printf("<br>");		
		printf("<b>$title_categs</b>");
		printf("<br>");		
		printf("$nfrom : <b> $min_date </b>");
		printf("<br>");		
		printf("$nto : <b> $max_date</b>");
		printf("<br><br>");				
?>
</p>

<?php
// DATA IMPORT
include('r_categs_rep_sum.php');	
?>



<?php

include('r_dbc.php');

$sql = "SELECT DISTINCT x.ordr,x.ordr2,x.code_categ,x.desc_categ,
		x.col1,x.col2,x.col3,x.col4,x.col5,x.col6,x.col7,x.col8,x.col9,x.col10,x.col11,
		x.col_perc1,x.col_perc2,x.col_perc3,x.col_perc4,x.col_perc5,x.col_perc6,x.col_perc7,x.col_perc8,x.col_perc9,x.col_perc10,x.col_perc11
		from ccreport x
		where repid = '".$repid."'
		order by 2";

$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:categ' >$n_categ</td>
			<th style='text-align:categ' >$ndesc</td>");
			include('r_midcols.php');	
			printf("</tr>");	

      while($row = mysqli_fetch_assoc($result)) {
		$ordr 			= $row["ordr"];
        $group 			= $row["code_categ"];
		$desc_categ 	= $row["desc_categ"];
		$col1 			= $row["col1"];		
		$col2 			= $row["col2"];		
		$col3 			= $row["col3"];		
		$col4 			= $row["col4"];		
		$col5 			= $row["col5"];		
		$col6 			= $row["col6"];				
		$col7 			= $row["col7"];		
		$col8 			= $row["col8"];		
		$col9 			= $row["col9"];				
		$col10 			= $row["col10"];
		$col11 			= $row["col11"];		
		$col_perc1 			= $row["col_perc1"];		
		$col_perc2 			= $row["col_perc2"];		
		$col_perc3 			= $row["col_perc3"];		
		$col_perc4 			= $row["col_perc4"];		
		$col_perc5 			= $row["col_perc5"];		
		$col_perc6 			= $row["col_perc6"];				
		$col_perc7 			= $row["col_perc7"];		
		$col_perc8 			= $row["col_perc8"];		
		$col_perc9 			= $row["col_perc9"];				
		$col_perc10 		= $row["col_perc10"];		
		$col_perc11 		= $row["col_perc11"];				


		$c_col1			= number_format($col1, $totdc, $septh, $sepdc);
		$c_col2			= number_format($col2, $totdc, $septh, $sepdc);
		$c_col3			= number_format($col3, $totdc, $septh, $sepdc);
		$c_col4			= number_format($col4, $totdc, $septh, $sepdc);		
		$c_col5			= number_format($col5, $totdc, $septh, $sepdc);
		$c_col6			= number_format($col6, $totdc, $septh, $sepdc);
		$c_col7			= number_format($col7, $totdc, $septh, $sepdc);
		$c_col8			= number_format($col8, $totdc, $septh, $sepdc);
		$c_col9			= number_format($col9, $totdc, $septh, $sepdc);		
		$c_col10		= number_format($col10, $totdc, $septh, $sepdc);	
		$c_col11		= number_format($col11, $totdc, $septh, $sepdc);			

		$c_col_perc1			= number_format($col_perc1, $totdc, $septh, $sepdc);
		$c_col_perc2			= number_format($col_perc2, $totdc, $septh, $sepdc);
		$c_col_perc3			= number_format($col_perc3, $totdc, $septh, $sepdc);
		$c_col_perc4			= number_format($col_perc4, $totdc, $septh, $sepdc);		
		$c_col_perc5			= number_format($col_perc5, $totdc, $septh, $sepdc);
		$c_col_perc6			= number_format($col_perc6, $totdc, $septh, $sepdc);
		$c_col_perc7			= number_format($col_perc7, $totdc, $septh, $sepdc);
		$c_col_perc8			= number_format($col_perc8, $totdc, $septh, $sepdc);
		$c_col_perc9			= number_format($col_perc9, $totdc, $septh, $sepdc);		
		$c_col_perc10			= number_format($col_perc10, $totdc, $septh, $sepdc);				
		$c_col_perc11			= number_format($col_perc11, $totdc, $septh, $sepdc);						
		

		if ($ordr == '01') {
		printf("<tr>
			<td style='text-align:right' >$group</td>
			<td style='text-align:left'  >$desc_categ</td>");

		$prefix = "c_col";
		$prefixc = "c_col_perc";		
		$coln	= 1;
			do {
			$c_col = ${$prefix . $coln};
			$c_col_perc = ${$prefixc . $coln};			
			printf("<td style='text-align:right' >$c_col</td>");
			
			
			$sql_delta = "SELECT delta_col,delta_perc
					from ccmanager_id
					where pid = '".$pid."'
					and mid = '".$coln."'";
			$result_delta = mysqli_query($conn, $sql_delta);
			if (mysqli_num_rows($result_delta) > 0) {
				while($row_delta = mysqli_fetch_assoc($result_delta)) {
				$delta_col 			= $row_delta["delta_col"];			
				$delta_perc 		= $row_delta["delta_perc"];							
				}
			}
			if ($delta_perc == 1) {
				if (substr($c_col_perc,0,1) == '-') {	$perc_bg = ";background-color: orangered"; }
				if (substr($c_col_perc,0,1) != '-') {	$perc_bg = ";background-color: chartreuse"; }
				printf("<td style='text-align:center$perc_bg' >$c_col_perc</td>");			
			}
			if ($delta_perc == 0) {
				printf("");			
			}			
			
			
			
			$coln++;
			} while ($coln <= $midmax);
			
		printf("</tr>");
		}


		// SUB GROUPS FORMAT
		if ($ordr == '02') {
		printf("<tr>
			<td style='text-align:right;font-style:oblique;background-color:#ececec;font-weight:600;' >$group</td>
			<td style='text-align:left;font-style:oblique;background-color:#ececec;font-weight:600;'  >$desc_categ</td>");

		$prefix = "c_col";
		$prefixc = "c_col_perc";		
		$coln	= 1;
			do {
			$c_col = ${$prefix . $coln};
			$c_col_perc = ${$prefixc . $coln};			
			printf("<td style='text-align:right;font-style:oblique;background-color:#ececec;font-weight:600;' >$c_col</td>");
			
			
			$sql_delta = "SELECT delta_col,delta_perc
					from ccmanager_id
					where pid = '".$pid."'
					and mid = '".$coln."'";
			$result_delta = mysqli_query($conn, $sql_delta);
			if (mysqli_num_rows($result_delta) > 0) {
				while($row_delta = mysqli_fetch_assoc($result_delta)) {
				$delta_col 			= $row_delta["delta_col"];			
				$delta_perc 		= $row_delta["delta_perc"];							
				}
			}
			if ($delta_perc == 1) {
				if (substr($c_col_perc,0,1) == '-') {	$perc_bg = ";background-color: orangered"; }
				if (substr($c_col_perc,0,1) != '-') {	$perc_bg = ";background-color: chartreuse"; }
				printf("<td style='text-align:center;font-style:oblique;font-weight:600$perc_bg' >$c_col_perc</td>");			
			}
			if ($delta_perc == 0) {
				printf("");			
			}			
			
			
			
			$coln++;
			} while ($coln <= $midmax);
			
		printf("</tr>");
		}		
		
		// CODE_categ_GROUPS AND TOTALS FORMAT
		if ($ordr == '03' || $ordr == '04') {
			$perc_bg = "";
		printf("<tr>
			<th style='text-align:right;background-color:#d9d9d9' >$group</th>
			<th style='text-align:left;background-color:#d9d9d9'  >$desc_categ</th>");
		$prefix = "c_col";
		$prefixc = "c_col_perc";
		$coln	= 1;
			do {
			$c_col = ${$prefix . $coln};
			$c_col_perc = ${$prefixc . $coln};
			printf("<th style='text-align:right;background-color:#d9d9d9' >$c_col</th>");
			
			$sql_delta = "SELECT delta_col,delta_perc
					from ccmanager_id
					where pid = '".$pid."'
					and mid = '".$coln."'";
			$result_delta = mysqli_query($conn, $sql_delta);
			if (mysqli_num_rows($result_delta) > 0) {
				while($row_delta = mysqli_fetch_assoc($result_delta)) {
				$delta_col 			= $row_delta["delta_col"];			
				$delta_perc			= $row_delta["delta_perc"];							
				}
			}
			if ($delta_perc == 1) {
				if (substr($c_col_perc,0,1) == '-') {	$perc_bg = ";background-color: red"; }
				if (substr($c_col_perc,0,1) != '-') {	$perc_bg = ";background-color: greenyellow"; }				
				printf("<th style='text-align:center;background-color:#d9d9d9$perc_bg' >$c_col_perc</th>");			
			}
			if ($delta_perc == 0) {
				printf("");			
			}						
			
			
			$coln++;
			} while ($coln <= $midmax);
		printf("</tr>");
		}		
    }
} 

mysqli_close($conn);
?> 

</table>
</div>




<?php
// DATA CLEAR FROM REPORT TABLE
include('r_dbc.php');
	$sqlcleanrep = "delete from ccreport where repid = '".$repid."'";
	$resultcleanrep = mysqli_query($conn, $sqlcleanrep);
				
?> 


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

</body>
</html>