<?php
session_start();
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
require_once 'includes/database.php';
require_once 'includes/display.php';
require_once 'includes/funcs.php';
echo $htmlheaders;  echo '<head>';
echo $js;
echo $recss;
echo '<style>body{overflow-y:scroll;}</style>';
echo '</head><body>';
$user = $_SESSION['reusername'];
$db=new MySQLDatabase();
$db->open_connection();

$sd = substr($_REQUEST['sdate'],6,4)."-".substr($_REQUEST['sdate'],3,2)."-".substr($_REQUEST['sdate'],0,2);
$ed = substr($_REQUEST['edate'],6,4)."-".substr($_REQUEST['edate'],3,2)."-".substr($_REQUEST['edate'],0,2);

    echo '<p><a $href="#" id="printnow" class="printnow" onclick="myprint();" style="float:left;">Print</a>';
    echo '<a href="#" id="closenow" class="closenow" onclick="window.close();">Close</a></p>';
	echo '<div id="printblk">';
    echo '<table id="treport" style="width:90%;margin-left:20px;font-family:Trebuchet MS, Arial;font-size:12px;">
    <tr><td colspan="6"><center><span style="font-size:18px;font-weight:normal;float:center;"> <b>EXPENSES LIST </b></span><span style="font-size:22px;font-weight:bold"> </span><center>
    <br/><span style="font-size:16px;font-weight:bold;"><center>'. str_repeat('&nbsp;',25).'</center><br/><span style="font-size:16px;font-weight:bold;"><center>'. str_repeat('&nbsp;',25).'From: &nbsp;'.$_REQUEST['sdate'].' &nbsp; To: &nbsp;'.$_REQUEST['edate'].'</center></span></td></tr>';
    echo'<tr><u> <th>Date</th><th>Doc. No.</th><th>Naration</th> <th>Expense Account</th> <th>Bank Account</th> <th>Amount</th></u></tr>';
    echo '<tr><td colspan="7"><hr/></td></tr>'; 
    $am = 0;   $gam = 0;   $acc='0'; 
																		// get expenses 
	if($_SESSION['reusergroup'] === 2){ 
		$query = "SELECT dexpno, ddate, CONCAT(pmode, '-', chqno, ' - ', chqdet, '-', rmks) as nar, expacc, bacc, amount FROM dexptrans WHERE bacc = 'PETTY CASH' AND ddate >= '$sd' AND ddate <= '$ed' ORDER BY expacc, ddate, dexpno";
	}
	else { $query = "SELECT dexpno, ddate, CONCAT(pmode, '-', chqno, ' - ', chqdet, '-', rmks) as nar, expacc, bacc, amount FROM dexptrans WHERE ddate >= '$sd' AND ddate <= '$ed' ORDER BY expacc, ddate, dexpno";
	}
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        
        $rdate = substr($row['ddate'],8,2)."-".substr($row['ddate'],5,2)."-".substr($row['ddate'] ,0,4);
		if ($acc === $row['expacc'] || $acc === '0') {
				$am = $am + intval($row['amount']);
				echo '<tr><td style="width:110px;">'.$rdate.'</td><td><a href="expvchprint.php?dno='.$row['dexpno'].'">'.$row['dexpno'].'</a></td><td>'.$row['nar'].'</td><td>'.$row['expacc'].'</td><td>'.$row['bacc'].'</td><td align="right">'.number_format($row['amount'],0).'</td></tr>';  }
		else {
			echo '<tr><td colspan="6"><hr/></td></tr>';  
			echo '<tr><td colspan="5">'.$acc.' TOTAL</td><td align="right">'.number_format($am,0).'</td></tr>'; 
			echo '<tr><td colspan="6"><hr/></td></tr>'; 
			echo '<tr><td>'.$rdate.'</td><td><a href="expvchprint.php?dno='.$row['dexpno'].'">'.$row['dexpno'].'</a></td><td>'.$row['nar'].'</td><td>'.$row['expacc'].'</td><td>'.$row['bacc'].'</td><td align="right">'.number_format($row['amount'],0).'</td></tr>';  
			$am = floatval($row['amount']);	
		}	
		$acc = $row['expacc'];
		$gam = $gam + floatval($row['amount']);
    }
    echo '<tr><td colspan="6"><hr/></td></tr>';  
	echo '<tr><td colspan="5">'.$acc.' TOTAL</td><td align="right">'.number_format($am,0).'</td></tr>'; 
    echo '<tr><td colspan="6"><hr/></td></tr>';
	echo '<tr><td colspan="4"></td><td>GRAND TOTAL</td><td align="right">'.number_format($gam,0).'</td></tr>';  
    echo '<tr><td colspan="6"><hr/></td></tr>';  
    echo '</table>'; 

    echo '<span align="left" style="font-size:x-small">Printed by: '.$user.' &nbsp;&nbsp; '.date("d-m-Y H:i:s"). '</span> </div> <br />';
    echo '<div style="page-break-after:always"> </div>';


echo '</body></html>';
?>
<script type="text/javascript">
function myprint(){
	var w = window.open('', '', 'width=800,height=600,resizeable,scrollbars');
	 w.document.write($("#printblk").html());
	 w.document.close(); 										// needed for chrome and safari
	 javascript:w.print();
	 w.close();
};	
</script>