<?php 

	$pid		= $_GET['pid'];
	$lang 		= $_GET['lang'];	
	$min_date 	= $_POST['min_date'];		
	$max_date 	= $_POST['max_date'];	
	$tagx 		= $_GET['tagx'];	
	$invoice 	= $_POST['invoice'];	
	$xfdc 		= $_GET['xfdc'];
	$xuid		= $_GET['xuid'];		

	include('users.php');
	
	if (isset($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);
		$qryin = "and r.rab_date >= '".$min_datec."' and r.rab_date <= '".$max_datec."'";
	}
	
	if (isset($tagx)) {
		$qryin = "and r.cid in (select distinct cid from ccmanager_tags where tag='".$tagx."')";
	}
	if (isset($invoice)) {
		$qryin = "and r.invoice = '".$invoice."'";
	}	
	
	include('config.php');
	include('field_names.php'); 
	
?>

<!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;
}

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

th {
  font-family:verdana;
  padding: 8px;
  border: 1px solid black;
  background-color: #D6D6D6;
  font-size: <?php echo $rep_font_size12 ?>;    
}

tr:nth-child(even){background-color: #f2f2f2}


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_calendar</b>");
		if (isset($min_date)) {
		printf("<br>");		
		printf("$nfrom : <b> $min_date </b>");
		printf("<br>");		
		printf("$nto : <b> $max_date</b>");
		}
		if (isset($tagx)) {
		printf("<br>");		
		printf("<b> $n_tag : $tagx </b>");
		}
		if (isset($invoice)) {
		printf("<br>");		
		printf("<b> $n_invoice : $invoice </b>");
		}		
		printf("<br><br>");				
?>
</p>



<?php	
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		= $rowmid["desc_manager"];
    }
} 

?>


<?php



$sql = "SELECT x.ordr,x.code_center,x.desc_center,x.code_categ,x.desc_categ,x.um,x.rab_date,x.rab_date_c,x.rab_qty,x.rab_uc,x.rab_number,x.rab_date_ordr
		from (
		SELECT '01' as ordr,r.code_center,g.desc_center,r.code_categ,k.desc_categ,k.um,r.rab_date,date_format(r.rab_date,'%d-%m-%Y') as rab_date_c,r.rab_qty,r.rab_uc,r.rab_number,date_format(r.rab_date,'%Y%m%d') as rab_date_ordr
		from ccmanager r, centers g, categs k
		where r.code_center = g.code_center
		and r.pid = g.pid
		and r.code_categ = k.code_categ
		and r.pid = k.pid
		and g.pid = ".$pid."
		".$qryin."
		".$user_cc2."
		".$user_ct2."
		and r.mid in (select x.mid from ccmanager_id x where x.pid = r.pid and x.expenses = 1)
		UNION
		SELECT '02' as ordr,concat('TOTAL ',date_format(r.rab_date,'%M %Y')) as code_center,'' as desc_center,'' as code_categ, '' as desc_categ,'' as um,'' AS rab_date,date_format(r.rab_date,'%m-%Y') as rab_date_c,sum(r.rab_qty) as rab_qty,0 as rab_uc,SUM(r.rab_number) as rab_number,CONCAT(date_format(r.rab_date,'%Y%m'),'99') as rab_date_ordr
		from ccmanager r, centers g, categs k
		where r.code_center = g.code_center
		and r.pid = g.pid
		and r.code_categ = k.code_categ
		and r.pid = k.pid
		and g.pid = ".$pid."
		".$qryin."
		".$user_cc2."
		".$user_ct2."		
		and r.mid in (select x.mid from ccmanager_id x where x.pid = r.pid and x.expenses = 1)
		group by date_format(r.rab_date,'%m-%Y')
		UNION
		SELECT '03' as ordr,concat('TOTAL ',date_format(r.rab_date,'%Y')) as code_center,'' as desc_center,'' as code_categ, '' as desc_categ,'' as um,'' AS rab_date,date_format(r.rab_date,'%Y') as rab_date_c,sum(r.rab_qty) as rab_qty,0 as rab_uc,SUM(r.rab_number) as rab_number,CONCAT(date_format(r.rab_date,'%Y'),'9999') as rab_date_ordr
		from ccmanager r, centers g, categs k
		where r.code_center = g.code_center
		and r.pid = g.pid
		and r.code_categ = k.code_categ
		and r.pid = k.pid
		and g.pid = ".$pid."
		".$qryin."
		".$user_cc2."
		".$user_ct2."		
		and r.mid in (select x.mid from ccmanager_id x where x.pid = r.pid and x.expenses = 1)
		group by date_format(r.rab_date,'%Y')		
		) as x
		order by x.rab_date_ordr,x.code_center,x.code_categ";
		
		// echo $sql;

$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' >$n_center</td>
			<th style='text-align:center' >$ndesc</td>
			<th style='text-align:center' >$n_categ</td>
			<th style='text-align:center' >$ndesc</td>			
			<th style='text-align:center' >$n_rab_date</td>");
		
			if ($umqtyuc != 0) {
			printf("<th style='text-align:center' >$num</td>						
			<th style='text-align:center' >$n_rab_qty</td>
			<th style='text-align:center' >$n_rab_uc</td>");
			}
			
			printf("<th style='text-align:center' >$desc_manager</td>
			</tr>");	
      while($row = mysqli_fetch_assoc($result)) {
		$ordr 			= $row["ordr"];
        $code_center	= $row["code_center"];
		$desc_center 	= $row["desc_center"];
        $code_categ		= $row["code_categ"];
		$desc_categ 	= $row["desc_categ"];		
		$um 			= $row["um"];	
		$rab_date_c		= $row["rab_date_c"];			
		$rab_qty 		= $row["rab_qty"];
		$rab_uc 		= $row["rab_uc"];		
		$rab_number 	= $row["rab_number"];	

			if ($um == "--") { $um = ""; $rab_qty = ""; $rab_uc = ""; }		
		
		if ($ordr == '01') 
			{
				$c_rab_qty		= number_format(floatval($rab_qty), $totdcqty, $septh, $sepdc);
				$c_rab_uc		= number_format(floatval($rab_uc), $totdc, $septh, $sepdc);
			}
		if ($ordr == '03' || $ordr == '02') 
			{
				$c_rab_qty		= "";
				$c_rab_uc		= "";				
			}			
		$c_rab_number	= number_format(floatval($rab_number), $totdc, $septh, $sepdc);											
		
		if ($ordr == '01') {
		printf("<tr>
			<td style='text-align:right' >$code_center</td>
			<td style='text-align:left'  >$desc_center</td>
			<td style='text-align:right' >$code_categ</td>
			<td style='text-align:left'  >$desc_categ</td>			
			<td style='text-align:center'  >$rab_date_c</td>");

			if ($umqtyuc != 0) {			
		printf("<td style='text-align:left'  >$um</td>						
			<td style='text-align:right'  >$c_rab_qty</td>									
			<td style='text-align:right'  >$c_rab_uc</td>");
			}
		printf("<td style='text-align:right' >$c_rab_number</td>
			</tr>");
		}
		if ($ordr == '02' || $ordr == '03') {
		printf("<tr>
			<th style='text-align:right' >$code_center</th>
			<th style='text-align:left'  >$desc_center</th>
			<th style='text-align:right' >$code_categ</th>
			<th style='text-align:left'  >$desc_categ</th>			
			<th style='text-align:center' >$rab_date_c</th>");

			if ($umqtyuc != 0) {						
			printf("<th style='text-align:left'  >$um</th>						
			<th style='text-align:right'  >$c_rab_qty</th>
			<th style='text-align:right'  >$c_rab_uc</th>");
			}

		printf("<th style='text-align:right' >$c_rab_number</th>			
			</tr>");
		}		
    }
} 

mysqli_close($conn);
?> 

</table>
</div>

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

</body>
</html>