<?php
session_start();
    if ($_SESSION['company'] == "") { 
        exit("Please Login");
    }
    if ($_SESSION['CURRENT_USER'] == "") { 
        exit("Please Login");
    }
    date_default_timezone_set('Asia/Colombo');
if ($_SESSION["dev"]!=""){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cash Book</title>
<link media="screen" rel="stylesheet" type="text/css" href="css/admin.css"  />
</head>
<script language="JavaScript" src="js/rep_sales_summery.js"></script>
<body>
<center>
<b>Company <?php echo $_SESSION['company']; ?> - Cash Book on <?php echo "<div id='dtfrom'>".$_GET["dtfrom"]."</div>"; ?></b></br>
<table width="800" border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td width="600" align="center"><strong>Description</strong></td>
    <td width="100" align="center"><strong>Sale</strong></td>
    <td width="100" align="center"><strong>Receipt</strong></td>
    <td width="100" align="center"><strong>Issued</strong></td>
  </tr>
 
  <?php
  		//if ($_SESSION['company']=="A"){
			include('connectioni.php');
		/*} else if ($_SESSION['company']=="B"){
			include('connection_b.php');
		} else if ($_SESSION['company']=="C"){
			include('connection_c.php');
		}*/			
		
		
		$rct = "select * from cash_book_summery where  trn_date <'" . $_GET["dtfrom"]  . "' order by trn_date desc";
		$result_rct=mysqli_query($GLOBALS['dbinv'],$rct);
	 	$row_rct = mysqli_fetch_array($result_rct);
		
		$opening_bal=$row_rct["balance"];
		
  		echo "<tr>";
		echo "<td><b>".$row_rct["trn_date"]." B/F Balance</b></td>";
		echo "<td></td>";
		echo "<td align=right><b><div id='open_bal'>".number_format($row_rct["balance"], 2, ".", ",")."</div></b></td>";
		echo "<td></td>";
		echo "</tr>";
		
  		$tyre_sale=0;
		$battery_sale=0;
		$battery_charge=0;
		$dis_water=0;
		$ms_sale=0;
		$tyre_fitting=0;
		$tube_sale=0;
		$noncat_sale=0;
		
		$tot_pay=0;
		
		$tot_sale=$opening_bal;
		
		$rct = "select sum(subtot) as subtot,Cat1 from view_invo_mas where SDATE='" . $_GET["dtfrom"] . "'   and  CANCELL='0' group by Cat1 ";
			 
		$result_rct=mysqli_query($GLOBALS['dbinv'],$rct);
	 	while ($row_rct = mysqli_fetch_array($result_rct)) {
	 	    
	 	    if($row_rct['subtot']>0){
                echo "<tr>";
                echo "<td><b>&nbsp;&nbsp;&nbsp;&nbsp;".$row_rct['Cat1']." Sale</b></td>";
                echo "<td align=right>".number_format($row_rct['subtot'], 2, ".", ",")."</td>";
                echo "<td></td>";
		        echo "<td></td>";
                echo "</tr>";
	 	    }
			$cat_sale=$cat_sale+$row_rct['subtot'];
		}	
    
	echo "<tr>";
	echo "<td colspan='1'><b>&nbsp;</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td colspan='1'><b>Before Bill Cash</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "</tr>";
	 
	
	$sql_salma_sttr = "select * from view_sttr_salma WHERE STTR_CANCELL='0' AND  ST_DATE='" . $_GET["dtfrom"] . "'and  SDATE !='" . $_GET["dtfrom"] . "' and  CANCELL='0' and PAY_TYPE='Cash'  ";
	$result_salma_sttr=mysqli_query($GLOBALS['dbinv'],$sql_salma_sttr);
	while ($row_salma_sttr = mysqli_fetch_array($result_salma_sttr)) {
		echo "<tr>";
		echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;".$row_salma_sttr["REF_NO"]." -".$row_salma_sttr["NAME"]."</td>";
		echo "<td></td>";
		echo "<td align=right>".number_format($row_salma_sttr["ST_PAID"], 2, ".", ",")."</td>";
		echo "<td></td>";
		echo "</tr>";
	
		$sttr_cash=$sttr_cash+$row_salma_sttr["ST_PAID"];
	}
	
	
	echo "<tr>";
	echo "<td colspan='1'><b>Today Bill Cash</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "</tr>";
	 
	
	$sql_salma_sttr = "select * from view_sttr_salma WHERE STTR_CANCELL='0' AND  ST_DATE='" . $_GET["dtfrom"] . "'and  SDATE ='" . $_GET["dtfrom"] . "' and  CANCELL='0' and C_CODE !='V/0001' and PAY_TYPE='Cash'  ";
	$result_salma_sttr=mysqli_query($GLOBALS['dbinv'],$sql_salma_sttr);
	while ($row_salma_sttr = mysqli_fetch_array($result_salma_sttr)) {
		echo "<tr>";
		echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;".$row_salma_sttr["REF_NO"]." -".$row_salma_sttr["NAME"]."</td>";
		echo "<td></td>";
		echo "<td align=right>".number_format($row_salma_sttr["ST_PAID"], 2, ".", ",")."</td>";
		echo "<td></td>";
		echo "</tr>";
	
		$sttr_cash=$sttr_cash+$row_salma_sttr["ST_PAID"];
	}
	
	echo "<tr>";
	echo "<td colspan='1'><b>Today Bill Cash Customer</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "</tr>";
	 
	
	$sql_salma_sttr = "select sum(ST_PAID) as subtot,REF_NO,NAME,ST_PAID,C_CODE from view_sttr_salma WHERE STTR_CANCELL='0' AND  ST_DATE='" . $_GET["dtfrom"] . "'and  SDATE ='" . $_GET["dtfrom"] . "' and  CANCELL='0' and PAY_TYPE='Cash' and C_CODE ='V/0001'  ";
// 	echo $sql_salma_sttr;
	$result_salma_sttr=mysqli_query($GLOBALS['dbinv'],$sql_salma_sttr);
	while ($row_salma_sttr = mysqli_fetch_array($result_salma_sttr)) {
		echo "<tr>";
		echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;".$row_salma_sttr["C_CODE"]." -".$row_salma_sttr["NAME"]."</td>";
		echo "<td></td>";
		echo "<td align=right>".number_format($row_salma_sttr["subtot"], 2, ".", ",")."</td>";
		echo "<td></td>";
		echo "</tr>";
	
		$sttr_cash=$sttr_cash+$row_salma_sttr["subtot"];
	}
	
		echo "<tr>";
	echo "<td colspan='1'><b>Return Chq Cash</b></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "<td ></td>";
	echo "</tr>";
	
	
	$rct = "select * from s_crec where CA_DATE='" . $_GET["dtfrom"] . "'  and FLAG='RET' and pay_type!='Cash TT' and CANCELL!='1'  ";
// 	echo $rct;
	$result_rct=mysqli_query($GLOBALS['dbinv'],$rct);
	while ($row_rct = mysqli_fetch_array($result_rct)) {
	 
	 if ($row_rct["CA_CASSH"]>0){
	 	
		$ven = "select * from vendor where CODE='" . $row_rct["CA_CODE"] . "'  ";
		$result_ven=mysqli_query($GLOBALS['dbinv'],$ven);
		$row_ven = mysqli_fetch_array($result_ven);
		
		
		$ST_INVONO="";
		
		$sql_inv="select * from ch_sttr where ST_REFNO='".$row_rct["CA_REFNO"]."'";
	 	$result_inv=mysqli_query($GLOBALS['dbinv'],$sql_inv);
        while ($row_inv = mysqli_fetch_array($result_inv)){
			
			$ret = "select * from s_cheq where CR_REFNO='" . $row_inv["ST_INVONO"] . "'  ";
			$result_ret=mysqli_query($GLOBALS['dbinv'],$ret);
			$row_ret = mysqli_fetch_array($result_ret);
			$ST_INVONO=$ST_INVONO." ".$row_ret["CR_CHNO"];
		}
		
		echo "<tr>";
		echo "<td>Return Cheque Settlement - ".$row_ven["NAME"]." - ".$ST_INVONO."</td>";
		echo "<td></td>";
		echo "<td align=right>".number_format($row_rct["CA_CASSH"], 2, ".", ",")."</td>";
		echo "<td></td>";
		echo "</tr>";
		$ret_amt=$ret_amt+$row_rct["CA_CASSH"];
		
	  }	
	}
	
	echo "<tr>";
	echo "<td colspan='1'><b>Account DEB Cash</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "</tr>";
	
	 
	
	$rct = "select * from a_ledger where l_date='" . $_GET["dtfrom"] . "' and l_flag1='DEB'  and l_code='12502'";
// 	ECHO $rct;
		$result_rct=mysqli_query($GLOBALS['dbinv'],$rct);
		while ($row_rct = mysqli_fetch_array($result_rct)) {
			echo "<tr>";
			echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;".$row_rct["l_lmem"]."</td>";
		    echo "<td></td>";
			echo "<td align=right>".number_format($row_rct["l_amount"], 2, ".", ",")."</td>";
	        echo "<td ></td>";
			echo "</tr>";
		
			$deb_cash=$deb_cash+$row_rct["l_amount"];
		}	

	
	echo "<tr>";
	echo "<td colspan='1'><b>Account CRE Cash</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "</tr>";
	
	$rct = "select * from a_ledger where l_date='" . $_GET["dtfrom"] . "' and l_flag1='CRE'  and l_code='12502'";
// 	ECHO $rct;
		$result_rct=mysqli_query($GLOBALS['dbinv'],$rct);
		while ($row_rct = mysqli_fetch_array($result_rct)) {
			echo "<tr>";
			echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;".$row_rct["l_lmem"]."</td>";
			echo "<td></td>";
	        echo "<td ></td>";
			echo "<td align=right>".number_format($row_rct["l_amount"], 2, ".", ",")."</td>";
			echo "</tr>";
		
			$cre_cash=$cre_cash+$row_rct["l_amount"];
		}	
    
    echo "<td colspan='1'><b>Cash Book Update</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "</tr>";
	
    $sql_settle = "select * from cash_book_summery where trn_date='" . $_GET["dtfrom"] . "'  ";
// 	ECHO $rct;
	$result_settle=mysqli_query($GLOBALS['dbinv'],$sql_settle);
	while($row_settle = mysqli_fetch_array($result_settle)) {
			echo "<tr>";
			echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;Company -".$row_settle["company"]."</td>";
			echo "<td></td>";
	        echo "<td ></td>";
			echo "<td align=right>".number_format($row_settle["closing_bal"], 2, ".", ",")."</td>";
			echo "</tr>";
			
    $closing_bal=$row_settle["closing_bal"];
	}
	
	echo "<tr>
   	<td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  	</tr>";
  
	$bf=$opening_bal+$sttr_cash+$ret_amt+$deb_cash-($cre_cash+$closing_bal);
				
// 	echo "<tr>";
// 	echo "<td><b>Balance C/F </b></td>";
// 	echo "<td></td>";
// 	echo "<td align=right><b>".number_format($bf, 2, ".", ",")."</b></td>";
// 	echo "<td align=right><b>".number_format($cre_cash, 2, ".", ",")."</b></td>";
// 	echo "</tr>";
	
// 	echo "<tr>";
// 	echo "<td></td>";
// 	echo "<td></td>";
// 	echo "<td align=right><u><b><div id='tot_sale'>".number_format($opening_bal+$sttr_cash+$ret_amt+$deb_cash, 2, ".", ",")."</div></b></u></td>";
// 	echo "<td align=right><u><b>".number_format($opening_bal+$sttr_cash+$ret_amt+$deb_cash, 2, ".", ",")."</b></u></td>";
// 	echo "</tr>";
	
// 	echo "<tr>";
// 	echo "<td><b>Balance B/F </b></td>";
// 	echo "<td></td>";
// 	echo "<td align=right></td>";
// 	echo "<td align=right><input type=\"text\" name=\"txtbf\" id=\"txtbf\" value=\"".number_format($bf, 2, ".", ",")."\" class=\"text_purchase3\" /></td>";
// 	echo "</tr>";

    echo "<tr>";
	echo "<td></td>";
	echo "<td align=right><b>".number_format($cat_sale, 2, ".", ",")."</b></td>";
	echo "<td align=right><b>".number_format($sttr_cash+$ret_amt+$opening_bal+$deb_cash, 2, ".", ",")."</b></td>";
	echo "<td align=right><b>".number_format($closing_bal+$cre_cash, 2, ".", ",")."</b></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td colspan='1'><b>Cash Hand Over</b></td>";
	echo "<td ></td>";
	echo "<td ></td>";
	echo "<td></td>";
	echo "</tr>";
	
    $sql_handover = "select * from s_cash_handover where SDATE='" . $_GET["dtfrom"] . "' and  CANCELL='0'  ";
// 	echo $sql_handover;
	$result_handover=mysqli_query($GLOBALS['dbinv'],$sql_handover);
	while ($row_handover = mysqli_fetch_array($result_handover)) {
		echo "<tr>";
		echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;".$row_handover["USER"]." -".$row_handover["DATETIME"]."</td>";
		echo "<td></td>";
		echo "<td></td>";
		echo "<td align=right>".number_format($row_handover["AMOUNT"], 2, ".", ",")."</td>";
		echo "</tr>";
	
		$tot_handover=$tot_handover+$row_handover["AMOUNT"];
	}
	
	echo "<tr>";
	echo "<td colspan='1'><b>Total Cash Hand Over</b></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td align=right>".number_format($tot_handover, 2, ".", ",")."</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td colspan='1'><b>&nbsp;</b></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td align=right></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td colspan='1'><b>Difference</b></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td align=right>".number_format($sttr_cash+$ret_amt+$opening_bal+$deb_cash-$tot_handover, 2, ".", ",")."</td>";
	echo "</tr>";
  
echo "</table>";


$updateMsg = "0";
$rct = "select * from cash_book_summery where company='".$_SESSION['company']."' and trn_date = '" . $_GET["dtfrom"]  . "'";
$result_rct=mysqli_query($GLOBALS['dbinv'],$rct);
if ($row_rct = mysqli_fetch_array($result_rct)){
    $updateMsg = "1";
    echo "<p></p><p>
    <b>Updated \"Balance B/F\" is " .number_format($row_rct["balance"], 2, ".", ",")  . " on: ".$row_rct["stime"]. " by ".$row_rct["username"]."</b>  
    </p>";
}

echo "<p></p><p>
<b>Document Printed On: ".date("Y-m-d")." ".date("H:i:s"). " by ".$_SESSION["CURRENT_USER"]."</b>  
</p>";

if($updateMsg == "1"){
    echo "<p></p><p>
    <b>I hereby cerity that above values are true</b>  
    </p><p>
    <b>.............</b>  
    </p>";
}

?>
</body>
</html>
<?php

} else {

	echo "Invalied User !!!"; 
}
?>