<?php

// CENTERS TOTALS 

if ($det == '1' && $code_center == $code_centerx) {

	$sqlc = "SELECT DISTINCt r.code_categ, c.desc_categ
			from ccmanager r, categs c
			where r.rab_date >= '".$min_datec."' and r.rab_date <= '".$max_datec."'
			and r.pid = c.pid and r.code_categ = c.code_categ
			and r.code_center = '".$code_centerx."'
			and r.pid = '".$pid."'
			order by 1";
			
				// echo $sqlc;
				// echo '<br><br><br>';
			
		$resultc = mysqli_query($conn, $sqlc);
			if (mysqli_num_rows($resultc) > 0) {
				while($rowc = mysqli_fetch_assoc($resultc)) {
				$code_categ 	= $rowc["code_categ"];
				$desc_categ 	= $rowc["desc_categ"];			
		printf("<tr>");
		printf("<td style='text-align:left' >&nbsp &nbsp &nbsp <i>$code_categ - $desc_categ</i></td>");
		
	$sqlmx = "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_centerx."'
			and r.code_categ = '".$code_categ."'
			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 $sqlmx;
			// echo '<br><br><br>';
			
				
			
		$resultmx = mysqli_query($conn, $sqlmx);
			if (mysqli_num_rows($resultmx) > 0) {
				
				$rmong_totalsx = 0;
				
				while($rowmx = mysqli_fetch_assoc($resultmx)) {
				$mongtx 		= $rowmx["mongt"];
				$mong_totalsx 	= $rowmx["mong_totals"];
				$rmong_totalsx	= $rmong_totalsx + $mong_totalsx;
				
				if ($mong_totalsx > 0 || $mong_totalsx < 0) {		$c_mong_totalsx	= number_format($mong_totalsx, $totdc, $septh, $sepdc); }
				if ($mong_totalsx == 0) {	$c_mong_totalsx	= ""; }
				
		
					printf("<td style='text-align:right' ><i> $c_mong_totalsx </i></td>");
				}
				
			$c_rmong_totalsx	= number_format($rmong_totalsx, $totdc, $septh, $sepdc);
			printf("<td style='text-align:right' ><i> $c_rmong_totalsx </i></td>");
				
			}		
		
		
		
		
		
		
		
		
		
		printf("</tr>");		
				}
			}
			
		printf("<tr>");
		printf("<td style='text-align:right' colspan=50>&nbsp</td>");
		printf("</tr>");					
}
			
?>















