<?php 
 include('nf_config.php');
 $rab_numbert = 0;
?>
<?php	
// EXPENSES DESC MANAGER
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_expenses		= $rowmid["desc_manager"];
    }
} 
mysqli_close($conn);
?>

<!doctype html>
<html lang="en-US" class="no-js">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title><?php echo $n_centers ?></title>
	
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css" >
    <link rel="stylesheet" href="./fontawesome/css/all.css" >
</head>

<body>
	
	<div class="bg-light border-bottom shadow-sm sticky-top">
	</div>

	<div class="container">
		<h1><?php echo $desc_manager_expenses." ".$n_calendar; ?></h1>
    	<hr>
		
		<table class="table table-bordered table-striped">
				
				
				<?php	
					include('r_dbc.php');
					$sqlmon = "select distinct date_format(calday,'%b-%Y') as rmon
						from calendar
						where calday in (select distinct rab_date from ccmanager where pid='".$pid."') 
						order by calday";

						$resultmon = mysqli_query($conn, $sqlmon);

						if (mysqli_num_rows($resultmon) > 0) {
							  while($rowmon = mysqli_fetch_assoc($resultmon)) {
								$rmon	= $rowmon["rmon"];
									
						printf("<tr><th class='text-center' colspan=7 > $rmon </th></tr>");
						
					
						$sqlwday = "select distinct date_format(calday,'%a') as rwday
						from calendar
						order by date_format(calday,'%w')";

						$resultwday = mysqli_query($conn, $sqlwday);

						if (mysqli_num_rows($resultwday) > 0) {
							printf("<tr>");
							  while($rowday = mysqli_fetch_assoc($resultwday)) {
								$rwday	= $rowday["rwday"];
								printf("<th class='text-center'> $rwday </th>");	
							}
							  printf("</tr>");
						}

									$sqlwdayn = "select distinct calday,date_format(calday,'%e') as rwdayn,
															date_format(calday,'%w') as rwdaynx,
															date_format(calday,'%D') as rwdayn_suf,
															0 as sposo
									from calendar
									where date_format(calday,'%b-%Y') = '$rmon'
									and calday not in (select rab_date from ccmanager where pid='".$pid."' and mid in (select mid from ccmanager_id where pid='".$pid."' and expenses = 1))
									union
									select distinct rab_date as calday,date_format(rab_date,'%e') as rwdayn,
															date_format(rab_date,'%w') as rwdaynx,
															date_format(rab_date,'%D') as rwdayn_suf,
															sum(rab_number) as sposo
									from ccmanager
									where date_format(rab_date,'%b-%Y') = '$rmon'
									and pid='".$pid."' and mid in (select mid from ccmanager_id where pid='".$pid."' and expenses = 1)
									group by rab_date
									order by 1";
							
// echo $sqlwdayn;
// echo "<br><br>";
									$resultwdayn = mysqli_query($conn, $sqlwdayn);

									if (mysqli_num_rows($resultwdayn) > 0) {

										  while($rowdayn = mysqli_fetch_assoc($resultwdayn)) {
											$rwdayn		= $rowdayn["rwdayn"];
											$rwdayn_suf	= $rowdayn["rwdayn_suf"];
											$rwdaynx	= $rowdayn["rwdaynx"];
											$sposo		= $rowdayn["sposo"];
											
											if ($sposo == 0) { $c_sposo = ""; }
											if ($sposo <> 0) { $c_sposo	= "<strong>".number_format($sposo, $totdc, $septh, $sepdc)."</strong>"; }
											
											if ($rwdaynx == 0) { $trs="<tr>";	$trf=""; }
											if ($rwdaynx == 1) { $trs="";		$trf=""; }
											if ($rwdaynx == 2) { $trs="";		$trf=""; }
											if ($rwdaynx == 3) { $trs="";		$trf=""; }
											if ($rwdaynx == 4) { $trs="";		$trf=""; }
											if ($rwdaynx == 5) { $trs="";		$trf=""; }
											if ($rwdaynx == 6) { $trs="";		$trf="</tr>"; }
			
											$tdc = "";
											if ($rwdaynx == 0 and $rwdayn == 1) { $tdc =""; }
											if ($rwdaynx == 1 and $rwdayn == 1) { $tdc ="<td colspan=$rwdaynx ></td>"; }
											if ($rwdaynx == 2 and $rwdayn == 1) { $tdc ="<td colspan=$rwdaynx ></td>"; }
											if ($rwdaynx == 3 and $rwdayn == 1) { $tdc ="<td colspan=$rwdaynx ></td>"; }
											if ($rwdaynx == 4 and $rwdayn == 1) { $tdc ="<td colspan=$rwdaynx ></td>"; }											
											if ($rwdaynx == 5 and $rwdayn == 1) { $tdc ="<td colspan=$rwdaynx ></td>"; }
											if ($rwdaynx == 6 and $rwdayn == 1) { $tdc ="<td colspan=$rwdaynx ></td>"; }
											
											printf("$trs $tdc <td class='text-center'> $rwdayn_suf <br> $c_sposo </td> $trf");	

										}

										}						
						
								printf("<tr><td colspan=7><br><br></td></tr>");
								printf("<tr><td colspan=7><br><br></td></tr>");
						
						
							}
						} 
						mysqli_close($conn);
					?>
			
		</table>
		
        
    </div> <!--/.container-->
	
    <script src="./jquery-3.4.1.js"></script>
    <script src="popper.min.js" ></script>
    <script src="./bootstrap/js/bootstrap.min.js" ></script>
		
  
</body>
</html>