<?php

require_once("../../loader.php");

$db = new Conexion;
$user = new User;
$core = new Core;
$userData = $user->cdp_getUserData();

$range = cdp_sanitize($_REQUEST['range']);

$sWhere = "";
$name_off=$userData->name_off;

if (!empty($range)) {

	$fecha =  explode(" - ", $range);
	$fecha = str_replace('/', '-', $fecha);

	$fecha_inicio = date('Y-m-d', strtotime($fecha[0]));
	$fecha_fin = date('Y-m-d', strtotime($fecha[1]));


	$sWhere .= " and  a.created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "'";
}


// // pagination variables
$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
$per_page = 10; //how much records you want to show
$adjacents  = 4; //gap between pages after number of adjacents
$offset = ($page - 1) * $per_page;

//$sql = "SELECT * FROM expenses_master order by created_date desc";

if($name_off == 'undefined'){
    
  $sql = "SELECT * FROM expenses_master 
        Where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "'
        and currency = 'Ksh.'
        order by created_date desc";  
    
} else{
    
   $sql = "SELECT * FROM expenses_master
        Where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "'
        and currency = 'Ksh.'
        and name_off='$name_off'
        order by created_date desc"; 
    
}


$query_count = $db->cdp_query($sql);
$db->cdp_execute();
$numrows = $db->cdp_rowCount();

$db->cdp_query($sql . " limit $offset, $per_page");
$data = $db->cdp_registros();

$total_pages = ceil($numrows / $per_page);

if($name_off == 'undefined'){

$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='$'");
$db->cdp_execute();
$row = $db->cdp_registro();
$dexp = $row->total;

$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='Ksh.'");
$db->cdp_execute();
$row = $db->cdp_registro();
$kexp = $row->total;    
    
}else{
    
$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='$' and name_off='$name_off' ");
$db->cdp_execute();
$row = $db->cdp_registro();
$dexp = $row->total;

$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='Ksh.' and name_off='$name_off' ");
$db->cdp_execute();
$row = $db->cdp_registro();
$kexp = $row->total;

}

if ($numrows > 0) { ?>
	<div class="table-responsive">


		<table id="zero_config" class="table table-condensed table-hover table-striped custom-table-checkbox">
			<thead>
				<tr>

					<th><b>Expense ID</b></th>
					<th class="text-center"><b>User</b></th>
					<th class="text-center"><b>Created Date</b></th>
					<th class="text-center"><b>Expense Title</b></th>
					<th class="text-center"><b>Expense Type</b></th>
					<th class="text-center"><b>Expense Amount</b></th>
					<th class="text-center"><b>Expense Agency</b></th>
					<th class="text-center"></th>


				</tr>
			</thead>
			<tbody id="projects-tbl">


				<?php if (!$data) { ?>
					<tr>
						<td colspan="6">
							<?php echo "
				<i align='center' class='display-3 text-warning d-block'><img src='assets/images/alert/ohh_shipment.png' width='150' /></i>								
				", false; ?>
						</td>
					</tr>
				<?php } else { ?>

					<?php

					$sumador_total = 0;
					$sumador_totalus = 0;
					$count = 0;
					$ktotal = 0;
					$sdtotal = 0;

					foreach ($data as $row) {

						$db->cdp_query('SELECT  IFNULL(sum(expense_amount), 0) FROM expenses_master where currency ="Ksh." and MONTH(created_date)=MONTH(now()) ');
						$sumkes = $db->cdp_registro();
						
						$db->cdp_query('SELECT  IFNULL(sum(expense_amount), 0) FROM expenses_master where currency ="$" ');
						$sumador_totalus = $db->cdp_registro();
						
						$db->cdp_query("SELECT currency FROM expenses_master WHERE id='" . $row->id . "'");
                        $curr = $db->cdp_registro();

						$db->cdp_execute();


						
					?>
						<tr class="card-hovera">

							<td><b><?php echo $row->id; ?></b></td>

							<td class="text-center">
								<?php echo $row->pre_user; ?>
							</td>

							<td class="text-center">
								<?php echo $row->created_date; ?>
							</td>


							<td class="text-center">
								<?php echo $row->expense_title; ?>
							</td>
							
							<td class="text-center">
								<?php echo $row->expense_type; ?>
							</td>
							
							<td class="text-center">
								<?php echo $curr->currency; ?><?php echo number_format($row->expense_amount); ?>
							</td>
							
							<td class="text-center">
								<?php echo $row->name_off; ?>
							</td>

						</tr>
					<?php } ?>

				<?php } ?>
			</tbody>
			<tfoot>
				<tr class="card-hover">
					<td class="text-left"><b>TOTAL</b></td>

					<td colspan="5"><b>Ksh. <?php echo number_format($kexp); ?></b> </td>
					

				</tr>

			</tfoot>

		</table>

        <div class="pull-right">
			<?php echo cdp_paginate($page, $total_pages, $adjacents);	?>
		</div>

	</div>
<?php } ?>