<? include("header.php");


//session_start();
//error_reporting(0);

include "checksession.php";

include ("include/db-connect.php");


if(isset($_REQUEST['submit']))
	{
	$customer_Name= $_REQUEST['customer_Name'];
	$reason= $_REQUEST['reason'];
	$amount= $_REQUEST['amount'];
	$payment_option= $_REQUEST['payment_option'];
	$cheque_no= $_REQUEST['cheque_no'];
	$recpt_no= $_REQUEST['recpt_no'];
	$date= $_REQUEST['date'];
	
	
	$insety=mysql_query("insert into ".$receipt." (customer_Name,reason,amount,payment_option,cheque_no,receipt_no,date)
								values ('$customer_Name','$reason','$amount','$payment_option','$cheque_no','$recpt_no','$date')");
   

//........COUNT DATE...........................

 $dis1="select COUNT(*) as num  from ".$BANK." WHERE cashdate='$date'";		
 $total_pages = mysql_fetch_array(mysql_query($dis1));
 $total_pages = $total_pages[num];
 if($total_pages=='0')
 {
	$result = mysql_query("select max(cashdate) from ".$BANK." where cashdate<'$date'");
 	$row = mysql_fetch_row($result);
    $highest_date = $row[0];
	 
	$query_select=mysql_query("select * from ".$BANK." where cashdate='$highest_date'");
	$run_query=mysql_fetch_array($query_select);
    $closecash=$run_query['closecash'];
	
	$insety=mysql_query("insert ignore into ".$BANK."(opencash,closecash,cashdate,expense,collection)values ('$closecash','$closecash','$date',0,0)");
 }
 
 
 $dis="select COUNT(*) as num  from ".$CASH." WHERE cash_date='$date'";		
 $total_pages1 = mysql_fetch_array(mysql_query($dis));
 $total_pages1 = $total_pages1[num];
 if($total_pages1=='0')
 {
	$result = mysql_query("select max(cash_date) from ".$CASH." where cash_date<'$date'");
 	$row = mysql_fetch_row($result);
    $highest_date = $row[0];
	 
 $query_select=mysql_query("select * from  ".$CASH." WHERE cash_date='$highest_date'");
 $run_query=mysql_fetch_array($query_select);
 $closecash=$run_query['close_cash'];
	
 $insety=mysql_query("insert ignore into ".$CASH."(open_cash,close_cash,cash_date,expense,collection)values ('$closecash','$closecash','$date',0,0)");
 }
//..........END COUNT DATE.......................	

	
	//start insert open cash
	date_default_timezone_set('UTC');
	$date = $_REQUEST['date'];
	$end_date = date("Y-m-d");
	
if($payment_option=='Cash')
{
	$query=mysql_query("select * from  ".$CASH." where cash_date='$date'");
	$run=mysql_fetch_array($query);
	
	$close=$run['close_cash'];
	$closecash=$close+$amount;
	$collec=$run['collection'];
	$collection=$collec+$amount;
	
	$inset=mysql_query("update ".$CASH." set close_cash='$closecash',collection='$collection' where cash_date='$date'");
	
 //.................................
	while (strtotime($date) <= strtotime($end_date))
	 {
		 $dt= "$date\n"; echo "<br>";
		 $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
		
	$query_select=mysql_query("select * from  ".$CASH." where cash_date='$date'");
    $run_query=mysql_fetch_array($query_select);
	
	$open_cash=$run_query['open_cash'];
	$open1=$open_cash+$amount;
	$close1=$run_query['close_cash'];
	$closecash1=$close1+$amount;
	
	 $inset=mysql_query("update ".$CASH." set close_cash='$closecash1',open_cash='$open1' where cash_date='$date'");
}
}
else
{
	$query=mysql_query("select * from  ".$BANK." where cashdate='$date'");
	$run=mysql_fetch_array($query);
	
	$close=$run['closecash'];
	$closecash=$close+$amount;
	$collec=$run['collection'];
	$collection=$collec+$amount;
	
	$inset=mysql_query("update ".$BANK." set closecash='$closecash',collection='$collection' where cashdate='$date'");
	//...................................
	while (strtotime($date) <= strtotime($end_date))
	 {
		 $dt= "$date\n"; echo "<br>";
		 $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
	
		 $query_select=mysql_query("select * from  ".$BANK." where cashdate='$date'");
    	 $run_query=mysql_fetch_array($query_select);
	
		 $open_cash=$run_query['opencash'];
		 $open1=$open_cash+$amount;
		 $close1=$run_query['closecash'];
		 $closecash1=$close1+$amount;
	
		 $inset=mysql_query("update ".$BANK." set closecash='$closecash1',opencash='$open1' where cashdate='$date'");
	 }
}
   
  
     header("Location:receipt_add.php?add=succ&&ad=$recpt_no");
				?>
                <script>
	window.location="receipt_add.php?add=succ&&ad=<?=$recpt_no;?>";
	</script>
                <?php

}

?>		 
<div id="content">
  <div class="breadcrumb">
       <!-- <a href="http://amarasudhan.com/admin/index.php?route=common/home&amp;token=b735cc089ec2d51abb9a8e1901f501fb">Home</a>-->
      </div>
              <div class="box">
    <div class="heading">
      <h1><img src="view/image/home.png" alt="" />Receipt</h1>
    </div>
    <div class="content">
      <div class="overview" style="width:50%; margin-left:300px;">
        <div class="dashboard-heading"> Add Receipt</div>
        <div class="dashboard-content">
        
 
   <?php 
 $sel="select * from ".$MAIL_CON." where id=1";
 $run=mysql_query($sel);
 $array=mysql_fetch_array($run);
 $name=$array['image_name'];
  
 ?>
 
 <?php if($_REQUEST['add']=='succ'){ echo "<div class='alert_succes'>Receipt added Successfully</div>"; } else { echo "";}?>

 <form method="post" id="client" enctype="multipart/form-data">
 <!-- <script type="text/javascript" src="../prototype.js"></script> 
  <script type="text/javascript" src="../validation.js"></script>-->
 
 			    
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;"> Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox">
                <input type="text" name="customer_Name" class="required" value="" >
                </div>               
                
                </div>
                 <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;">Receipt Reason<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="reason" class="required" value="" >
                </div>               
                </div>
                 <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;">Amount<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="amount" class="required" value="" >
                </div>               
                </div>
                 <br />
                
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;">Payment option<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox">
                 <select name="payment_option">
                <option value="Select">Select One</option>
                <option value="Cash">Cash</option>
                <option value="Bank">Bank</option>
                </select>
                </div>               
                </div>
                 <br />
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;">Cheque No<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="cheque_no" >
                </div>               
                </div>
               
                 <br />
            	<div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;">Receipt No<font color="#FF0000"><b> *</b></font> </div>
                 <?php
								$sel="SELECT * FROM ".$receipt." ORDER BY id DESC LIMIT 1";
								 $run=mysql_query($sel);
								 $array=mysql_fetch_array($run);
								 $pid=$array['receipt_no'];
							 $pid1=$pid+1;
 							?>		
                <div class="maintextbox"><input type="text" name="recpt_no" value="<?php echo $pid1; ?>" >
                </div>               
                </div>
                
                 <br />
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;">Date<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox">
                <input type="text" required class="TextBox" name="date" id="f_date_b1">
				<img src="images/cal.gif" id="f_trigger_b1" >
				<script type="text/javascript">
				Calendar.setup({inputField :"f_date_b1",ifFormat:"%Y-%m-%d",showsTime:true,button:"f_trigger_b1",singleClick:true});
				</script>
                </div>               
                </div>
               <br />
                
                 <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:150px;"><font color="#FF0000"><b></b></font> </div>
                <div class="maintextbox"><input type="submit" name="submit"value="Submit" >
                <br><br>
               <!-- <a href="javascript: history.go(-1)"><img src="images/bac.jpg"></a>--></div></div>
 
 
 
 
 
   </form>
        </div>
      </div>
      
      
    </div>
  </div>
</div>
<!--[if IE]>
<script type="text/javascript" src="view/javascript/jquery/flot/excanvas.js"></script>
<![endif]--> 
<script type="text/javascript" src="view/javascript/jquery/flot/jquery.flot.js"></script> 
<script type="text/javascript"><!--
function getSalesChart(range) {
	$.ajax({
		type: 'get',
		url: 'index.php?route=common/home/chart&token=b735cc089ec2d51abb9a8e1901f501fb&range=' + range,
		dataType: 'json',
		async: false,
		success: function(json) {
			var option = {	
				shadowSize: 0,
				lines: { 
					show: true,
					fill: true,
					lineWidth: 1
				},
				grid: {
					backgroundColor: '#FFFFFF'
				},	
				xaxis: {
            		ticks: json.xaxis
				}
			}

			$.plot($('#report'), [json.order, json.customer], option);
		}
	});
}

getSalesChart($('#range').val());
//--></script> 
</div>
<?  include("footer.php"); ?>