<?php session_start();  ?>
<!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['COMCODE']; ?> - 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>Recieved</strong></td>
    <td width="100" align="center"><strong>Issued</strong></td>
  </tr>
 
  <?php
  		 
			include('connection_a.php');
		 		
		
		
		$rct = "select * from cash_book_summery where company='".$_SESSION['COMCODE']."' and trn_date <'" . $_GET["dtfrom"]  . "' order by trn_date desc";
		$result_rct=mysql_query($rct, $dbinv);
	 	$row_rct = mysql_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 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 ST_INVONO,CUS_NAME,CA_DATE,DESCRIPTION,sum(ST_PAID) as ST_PAID,CA_CASSH from view_crec_sttr_invo_smas where CA_DATE='" . $_GET["dtfrom"] . "' and left(CA_REFNO, 1)='A' and pay_type='Cash' and  CANCELL='0' and CA_CASSH>0 ";
	        if($_SESSION['salEx']!=""){
                	$user = "select * from user_mast where sal_ex='".$_SESSION['salEx']."'";
            		$result_user=mysql_query($user, $dbinv);
            	 	$row_user = mysql_fetch_array($result_user);
                	 	 
             $rct .= "  and DEPARTMENT='".$row_user['sal_dep']."'"; 
	        }
	        
	 		 $rct .= "  group by DEPARTMENT"; 
	 	
	 
		$result_rct=mysql_query($rct, $dbinv);
	 	while ($row_rct = mysql_fetch_array($result_rct)) {
		
			
	 		if ($row_rct["CA_CASSH"] > 0) {
			
				// if ($row_rct["CA_DATE"]!=$row_rct["SDATE"]){
				// 	echo "<tr>";
				// 	echo "<td>".$row_rct["ST_INVONO"]." - ".$row_rct["CUS_NAME"]."</td>";
				// 	echo "<td align=right>".number_format($row_rct["ST_PAID"], 2, ".", ",")."</td>";
				// 	echo "<td></td>";
				// 	echo "</tr>";
					
					
				// } else {
				
				//   if ($row_rct["TYPE"]=="CR"){
				//   	echo "<tr>";
				// 	echo "<td>".$row_rct["ST_INVONO"]." - ".$row_rct["CUS_NAME"]."</td>";
				// 	echo "<td align=right>".number_format($row_rct["ST_PAID"], 2, ".", ",")."</td>";
				// 	echo "<td></td>";
				// 	echo "</tr>";
				//   }  
				// }
					echo "<tr> 
					
					<td>".$row_rct["DESCRIPTION"]."</td>
					<td align=right><u><b><div id='tot_sale'>".number_format($row_rct["ST_PAID"], 2, ".", ",")."</div></b></u></td>
					<td></td>
					</tr>";
				$tot_sale=$tot_sale+$row_rct["ST_PAID"];
			}
			
			
		}	
		
  	 
	
	$ex = "select * from expense where sdate='" . $_GET["dtfrom"] . "' ";
	 if($_SESSION['salEx']!=""){
	     $user = "select * from user_mast where sal_ex='".$_SESSION['salEx']."'";
            		$result_user=mysql_query($user, $dbinv);
            	 	$row_user = mysql_fetch_array($result_user);
	     $ex.=" and depcode='".$row_user['sal_dep']."'";
	 }
	$result_ex=mysql_query($ex, $dbinv);
	while ($ex = mysql_fetch_array($result_ex)) {
	    echo "<tr>";
    	echo "<td>".$ex['name']."- ".$ex['dep']."</td>";
    	echo "<td></td>";
    	echo "<td align=right>".number_format($ex['amount'], 2, ".", ",")."</td>";
    	echo "</tr>";
    		$tot_pay=$tot_pay+$ex['amount'];
	}
	
	$rct = "select * from s_crec where CA_DATE='" . $_GET["dtfrom"] . "'  and FLAG='RET' and pay_type!='Cash TT' and CANCELL!='1'";
	$result_rct=mysql_query($rct, $dbinv);
	while ($row_rct = mysql_fetch_array($result_rct)) {
	 
	 if ($row_rct["CA_CASSH"]>0){
	 	
		$ven = "select * from vendor where CODE='" . $row_rct["CA_CODE"] . "'  ";
		$result_ven=mysql_query($ven, $dbinv);
		$row_ven = mysql_fetch_array($result_ven);
		
		
		$ST_INVONO="";
		
		$sql_inv="select * from ch_sttr where ST_REFNO='".$row_rct["CA_REFNO"]."'";
	 	$result_inv=mysql_query($sql_inv, $dbinv);
        while ($row_inv = mysql_fetch_array($result_inv)){
			
			$ret = "select * from s_cheq where CR_REFNO='" . $row_inv["ST_INVONO"] . "'  ";
			$result_ret=mysql_query($ret, $dbinv);
			$row_ret = mysql_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 align=right>".number_format($row_rct["CA_CASSH"], 2, ".", ",")."</td>";
		echo "<td></td>";
		echo "</tr>";
		$ret_amt=$ret_amt+$row_rct["CA_CASSH"];
		
	  }	
	}
	
	
	
	$tot_sale=$tot_sale+$ret_amt;
	
	 
 
	echo "<tr>
   	<td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  	</tr>";
  
	$bf=$tot_sale-$tot_pay;
				
	echo "<tr>";
	echo "<td><b>Balance C/F </b></td>";
	echo "<td align=right><u><b><div id='tot_sale'>".number_format($tot_sale, 2, ".", ",")."</div></b></u></td>";
	echo "<td align=right><b>".number_format($tot_pay, 2, ".", ",")."</b></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td></td>";
 	echo "<td></td>";
	echo "<td align=right><u><b>".number_format($bf, 2, ".", ",")."</b></u></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td><b>Balance B/F </b></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 "</table>";
	 if($_SESSION['salEx']==""){
echo "<p>
  <input type=\"button\" name=\"button\" id=\"button\" value=\"Update Balance\" class=\"btn_purchase1\" onclick=\"update_bal('".$_GET["dtfrom"]."', ".$opening_bal.", ".$tot_sale.", ".$bf.");\"/>
  
</p>";
	 }
	
?>
 
</body>
</html>
