<? include("header.php");

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

include "checksession.php";

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

if(isset($_REQUEST['submit']))
	{
	 $product_id= $_REQUEST['product_id'];
	 $amount= $_REQUEST['Amount'];
	 $Payment_option= $_REQUEST['Payment_option'];
	 $pay_reason= $_REQUEST['pay_reason'];
	 $Cheque_No= $_REQUEST['Cheque_No'];
	 $pay_meth= $_REQUEST['expence'];
	 $billno= $_REQUEST['billno'];
	 $sup_name= $_REQUEST['sup_name'];
	 $date= $_REQUEST['date'];
	 
	 	
	
   $insety=mysql_query("insert into ".$pay." (Supplier_Name,Amount,Payment_option,pay_reason,Cheque_No,date) values('$sup_name','$amount','$Payment_option','$pay_reason','$Cheque_No','$date')");
   
   
   //............................................................................................
   $set=mysql_query("select bal_amount from supplier where supplier_name='$sup_name'");
   $fft=mysql_fetch_array($set);
   $bal_amount=$fft['bal_amount'];
   
   $havetopay=$bal_amount-$amount;
   
   $update=mysql_query("update supplier set bal_amount='$havetopay' where supplier_name='$sup_name'");
   
   
 //........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['expense'];
	$collection=$collec+$amount;
	
	$inset=mysql_query("update ".$CASH." set close_cash='$closecash',expense='$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['expense'];
	$collection=$collec+$amount;
	
	$inset=mysql_query("update ".$BANK." set closecash='$closecash',expense='$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:view_pay.php?add=succ");
	}
?>


<script type="text/javascript">
function showstate(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHintstate").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","loadData1.php?q="+str,true);
xmlhttp.send();
}
function showcity(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHintcity").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","city_ajax.php?q="+str,true);
xmlhttp.send();
}
</script>

<!----------------Start Date Picker Code-------->
<script type="text/javascript" src="JavaScript/calendar.js">
</script>
<script type="text/javascript" src="JavaScript/calendar-en.js">
</script>
<script type="text/javascript" src="JavaScript/calendar-setup.js">
</script>
<link rel="stylesheet" type="text/css" media="all" href="JavaScript/calendar-win2k-cold-1.css" title="win2k-cold-1" />
<!----------------End Date Picker Code-------->
<div id="content">
  <div class="breadcrumb">
      </div>
              <div class="box">
    <div class="heading">
      <h1><img src="view/image/home.png" alt="" />Add Payment</h1>
    </div>
    <div class="content">
      <div class="overview" style="width:50%; margin-left:300px;">
        <div class="dashboard-heading">Add Payment</div>
        <div class="dashboard-content">
        
 <?php if($_REQUEST['no']=='suc'){ echo "<div class='alert_succes'>Enter Again</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">
                <div class="maintext" style="width:100px;">Bill No<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="billno"  class="required">
                </div>               
                </div>
                
                <div class="mainrow">
                <div class="maintext" style="width:100px;">Product ID<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="product_id"  class="required">
                </div>               
                </div>-->
                <a href="view_pay.php">View Payment</a>
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:100px;">Supplier Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox">
                <select name="sup_name" required style="width:300px;height:30px;" autofocus="autofocus">
                <option value="" hidden="">Select One</option>
                <?php
				$query = mysql_query("SELECT * FROM ".$SUPPLIER."");
				while ($rows = mysql_fetch_array($query))
				{ ?>
                <option value="<?php echo $rows['supplier_name']; ?>"><?php echo ucwords($rows['supplier_name']); ?></option>
                <?php } ?>
                </select>
                </div>               
                </div>
                <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:100px;">Amount<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="Amount" required style="width:300px;height:25px;">
                </div>               
                </div>
                
                
                <br />
                 <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:100px;"> Reason<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="pay_reason"  required="required" style="width:300px;height:25px;">
                </div>               
                </div>
                
                <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:100px;">Payment option<font color="#FF0000"><b>*</b></font> </div>
                <div class="maintextbox">
                 <select name="Payment_option" required style="width:300px;height:30px;">
                <option value="" hidden="">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:100px;">Cheque No<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="Cheque_No" required style="width:300px;height:25px;">
                </div>               
                </div>
                
                <br />
               
               <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style="width:100px;">Date<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" value="<? echo date("Y-m-d"); ?>" style="width:300px;height:25px;" 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:100px;"><font color="#FF0000"><b></b></font> </div>
                <div class="maintextbox"><input type="submit" name="submit" value="Submit" style="background:#090;color:#FFF;width:80px;height:30px;border:0px;border-radius:10px 6px;cursor:pointer;"></div></div>
 
 
 
 
 
   </form>
        </div>
      </div>
      
      
    </div>
  </div>
</div>
</div>
<?  include("footer.php"); ?>



