<?php
$budgets_cat_r = 0;
$budgetsc_cat_r = 0;
// CENTERS TOTALS 

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

	$sqlc = "SELECT DISTINCt r.code_categ, c.desc_categ,sum(r.rab_number) as budgets_cat,date_format(min(rab_date),'%d-%m-%y') as date_s,date_format(max(rab_date),'%d-%m-%y') as date_e
			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 budget = 1)
			group by r.code_categ
			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"];
				$date_s 		= $rowc["date_s"];
				$date_e 		= $rowc["date_e"];
				$budgets_cat	= $rowc["budgets_cat"]/$xdivider;		
				$c_budgets_cat	= number_format($budgets_cat, $totdc, $septh, $sepdc);
				$budgets_cat_r	= $budgets_cat_r + $budgets_cat;
				$c_budgets_cat_r	= number_format($budgets_cat_r, $totdc, $septh, $sepdc);					
		printf("<tr>");
		printf("<td style='text-align:left' rowspan=2 >$code_categ - $desc_categ</td>");
			if ($datayesno == 1) {											
				printf("<td style='text-align:right'  rowspan=2>$c_budgets_cat</td>");
			}
			if ($datayesno == 0) {											
				printf("");
			}			
		
	$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 = 3)
			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"]/$xdivider;
				$rmong_totalsx	= $rmong_totalsx + $mong_totalsx;
				
				if ($budgets_cat != 0) {
					$budget_cat_perc 	= ($mong_totalsx / $budgets_cat)*100;				
				}
				if ($budgets_cat == 0) {
					$budget_cat_perc 	= 0;				
				}				
				
				if ($mong_totalsx > 0 || $mong_totalsx < 0) 
						{		
							$c_mong_totalsx	= number_format($mong_totalsx, $totdc, $septh, $sepdc); 
							$c_budget_cat_perc	= number_format($budget_cat_perc, 0, $septh, $sepdc)."%%";
							$tdbg1 = "background-color:".$bgc_planned1;
						}
				if ($mong_totalsx == 0) {	$c_mong_totalsx	= "";  $c_budget_cat_perc	= ""; $tdbg1=""; } 
				
				if ($datayesno == 1) {									
						printf("<td style='text-align:right;$tdbg1' > $c_mong_totalsx <br> $c_budget_cat_perc </td>");
					}
				if ($datayesno == 0) {									
						printf("<td style='text-align:right;$tdbg1' > &nbsp </td>");
					}					
				}

				if ($budgets_cat != 0) {
						$budget_cat_perc_total 	= ($rmong_totalsx / $budgets_cat)*100;
				}				
				if ($budgets_cat == 0) {
						$budget_cat_perc_total 	= 1;
				}				


				$c_budget_cat_perc_total	= number_format($budget_cat_perc_total, 0, $septh, $sepdc)."%%"; 				
				
			$tdbg1 = "background-color:".$bgc_planned1;
			$c_rmong_totalsx	= number_format($rmong_totalsx, $totdc, $septh, $sepdc);
				if ($datayesno == 1) {							
					printf("<td style='text-align:right;$tdbg1' > $c_rmong_totalsx   <br> $c_budget_cat_perc_total</td>");
				}
				if ($datayesno == 0) {							
					printf("");
				}				
				
			}		
			
printf("<td style='text-align:center;$tdbg1'>$date_s</td><td style='text-align:center;$tdbg1'>$date_e</td>");

		printf("<tr>");
			
			
	$sqlmxc = "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>';
			
				
			
		$resultmxc = mysqli_query($conn, $sqlmxc);
			if (mysqli_num_rows($resultmxc) > 0) {
				
				$rmong_totalsxc = 0;
				
				while($rowmxc = mysqli_fetch_assoc($resultmxc)) {
				$mongtxc 		= $rowmxc["mongt"];
				$mong_totalsxc 	= $rowmxc["mong_totals"]/$xdivider;
				$rmong_totalsxc	= $rmong_totalsxc + $mong_totalsxc;
				
				if ($budgets_cat != 0) {
				$budgetc_cat_perc 	= ($mong_totalsxc / $budgets_cat)*100;				
				}
				if ($budgets_cat == 0) {
				$budgetc_cat_perc 	= 1;				
				}				
				
				if ($mong_totalsxc > 0 || $mong_totalsxc < 0) 
						{		
							$c_mong_totalsxc	= number_format($mong_totalsxc, $totdc, $septh, $sepdc); 
							$c_budgetc_cat_perc	= number_format($budgetc_cat_perc, 0, $septh, $sepdc)."%%";
							$tdbg2 = "background-color:".$bgc_costs1;
						}
				if ($mong_totalsxc== 0) {	$c_mong_totalsxc	= "";  $c_budgetc_cat_perc	= ""; $tdbg2=""; } 
				
				if ($datayesno == 1) {											
					printf("<td style='text-align:right;$tdbg2' > $c_mong_totalsxc <br> $c_budgetc_cat_perc </td>");
				}
				if ($datayesno == 0) {											
					printf("<td style='text-align:right;$tdbg2' > &nbsp </td>");
				}				
				}
				if ($budgets_cat != 0) {
					$budgetc_cat_perc_total 	= ($rmong_totalsxc / $budgets_cat)*100;
				}
				if ($budgets_cat == 0) {
					$budgetc_cat_perc_total 	= 1;
				}
				$c_budgetc_cat_perc_total	= number_format($budgetc_cat_perc_total, 0, $septh, $sepdc)."%%"; 				
				
			$tdbg2 = "background-color:".$bgc_costs1;
			$c_rmong_totalsx	= number_format($rmong_totalsxc, $totdc, $septh, $sepdc);
				if ($datayesno == 1) {														
					printf("<td style='text-align:right;$tdbg2' > $c_rmong_totalsx   <br> $c_budgetc_cat_perc_total</td>");
				}
				if ($datayesno == 0) {														
					printf("");
				}				
			}					


	$sqld_exp = "SELECT date_format(min(rab_date),'%d-%m-%y') as date_sexp,date_format(max(rab_date),'%d-%m-%y') as date_eexp
			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.code_categ = '".$code_categ."'
			and r.pid = '".$pid."'
			and r.mid in (select mid from ccmanager_id where pid = '".$pid."' and expenses = 1)";
		$resultd_exp = mysqli_query($conn, $sqld_exp);
			if (mysqli_num_rows($resultd_exp) > 0) {
				while($rowd_exp = mysqli_fetch_assoc($resultd_exp)) {
				$date_sexp 		= $rowd_exp["date_sexp"];
				$date_eexp 		= $rowd_exp["date_eexp"];		
				}
			}
		
		printf("<td style='text-align:center;$tdbg2'>$date_sexp</td><td style='text-align:center;$tdbg2'>$date_eexp</td>");		
		printf("</tr>");		
				}
			}
			
//		printf("<tr>");
//		printf("<td style='text-align:right' colspan=50>&nbsp</td>");
//		printf("</tr>");					
// }
			
?>















