<? include("header.php");
//session_start();
//error_reporting(0);
include "checksession.php";
include ("include/db-connect.php");
$supplier_name=$_REQUEST['supplier_name'];
$bill_no=$_REQUEST['bill_no'];

if(isset($_REQUEST['submit']))
	{ 
	
	
	$product_namee= $_REQUEST['product_namee'];
	 $array_prname = explode(',', $product_namee);
	   list($Getproduct_name,$product_id) = $array_prname;
		 
		 $date= $_REQUEST['date'];
		 $quant= $_REQUEST['quantity'];
		 $paytype= $_REQUEST['paytype'];
		 $prno= $_REQUEST['prno'];
		 $supplier_name= $_REQUEST['supplier_name'];
		 $bill_no=$_REQUEST['bill_no'];
		
		 $query_select1=mysql_query("SELECT per_pro_amnt,pur_tax FROM  ".$purch." WHERE supplier_name='$supplier_name' and product_id='$product_id' order by id desc");
		 $qnty=mysql_fetch_array($query_select1);
		
		 $pur_tax= $qnty['pur_tax'];
		 $pptax=$pur_tax*$quant;
		 
		 
		 $per_pro_amnt= $qnty['per_pro_amnt'];
		 		 
		 $tot_pro_amnt=$per_pro_amnt*$quant;
		 
		 //..........................Tax Decrement.................................................................. 
		 $query_sel1=mysql_query("SELECT tot_tax FROM purchase_tax WHERE supplier_name='$supplier_name' and bill_num='$bill_no' and product_id='$product_id'");
		 $ttfet=mysql_fetch_array($query_sel1);
		
		 $tot_taxxxx=$ttfet['tot_tax'];
		 $decre_tax=$tot_taxxxx-$pptax;
		 $update_purtax=("update purchase_tax set tot_tax='$decre_tax' where supplier_name='$supplier_name' and bill_num='$bill_no' and product_id='$product_id'");
		 mysql_query($update_purtax);
		 
		 
		 //............................................................................................
		 if($paytype=='Credit')
		 {
   		 $set=mysql_query("select bal_amount from supplier where supplier_name='$supplier_name'");
   		 $fft=mysql_fetch_array($set);
   		 $bal_amount=$fft['bal_amount'];
   
   		 $havetopay=$bal_amount-$tot_pro_amnt;
   		 $update=mysql_query("update supplier set bal_amount='$havetopay' where supplier_name='$supplier_name'");
		 }
		 
		// .................................................................................................
		  $query_select2=mysql_query("select * from  ".$PRODUCT." where `product_id`='$product_id'");
		 $qnty1=mysql_fetch_array($query_select2);
		  $qn1= $qnty1['quantity'];
		 $price= $qnty1['price'];
		 $quantity1=$qn1-$quant;
		 
		  $sql=mysql_query("UPDATE ".$PRODUCT." SET  quantity='$quantity1' WHERE product_id='$product_id'");
		
		 //.....................................................................................................

		 $ins_qur = "insert into ".$PUR_RET."(product_id,quantity,supplier_name,return_date,paytype,per_pro_amnt,tot_pro_amnt,pr_no) 
					 values('$product_id','$quant','$supplier_name','$date','$paytype','$per_pro_amnt','$tot_pro_amnt','$prno')";					  
		 mysql_query($ins_qur);
		 
		 
		 //........COUNT DATE...........................

 
 
 $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($paytype=='Cash')
{
	$query=mysql_query("select * from  ".$CASH." where cash_date='$date'");
	$run=mysql_fetch_array($query);
	
	$close=$run['close_cash'];
	$closecash=$close+$tot_pro_amnt;
	$collec=$run['collection'];
	$collection=$collec+$tot_pro_amnt;
	
	$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+$tot_pro_amnt;
	$close1=$run_query['close_cash'];
	$closecash1=$close1+$tot_pro_amnt;
	
	$inset=mysql_query("update ".$CASH." set close_cash='$closecash1',open_cash='$open1' where cash_date='$date'");
}
}


		header("Location:view_pur_return.php?add=success");
		
		?>
        <script>
	window.location="view_pur_return.php?add=suc";
	</script>
        <?php
  
	}
	

?>		 
			

<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">
       <!-- <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="" />Add Purchase Return</h1>
    </div>
    <div class="content">
      <div class="overview" style="width:50%; margin-left:300px;">
        <div class="dashboard-heading"> Add Purchase Return</div>
        <div class="dashboard-content">
        
 
 <?php if($_REQUEST['ed']=='su'){ echo "<div class='alert_succes'>Contact Edited 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>-->
  
               <?php
				$dis1=("select count(*) as num  from purchase_return ");		
				$total_pages = mysql_fetch_array(mysql_query($dis1));
     			$total_pages = $total_pages[num];
				$pr=$total_pages+1;
 				?>
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style=" width:90px;">PR No<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" ><input type="text" name="prno" size="55" value="<?=$pr;?>" readonly></div>
                </div>
                
                <br />
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style=" width:90px;">Supplier Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" ><input type="text" name="supplier_name" size="55" value="<?=$supplier_name;?>" readonly></div>
                </div>
                 <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style=" width:90px;">Bill No<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" ><input type="text" name="bill_no" size="55" value="<?=$bill_no;?>" readonly></div>
                </div>
                 <br />
                   <link rel="stylesheet" type="text/css" media="all" href="Auto_complete/ad_comple.css">
  <script type="text/javascript" src="Auto_complete/jquery-1.9.1.min.js"></script>
  <script type="text/javascript" src="Auto_complete/jquery.autocomplete.min.js"></script>  
  <script type="text/javascript">
  $(function(){
  var currencies = [
 <?php $qur="SELECT * FROM `purchase_management` where `bill_num`='$bill_no'"; 
		$result = mysql_query($qur);
		while ($row_section = mysql_fetch_array($result)) {?>	  
    { value:'<?php echo $row_section['product_name']; ?>,<?php echo $row_section['product_id']; ?>' },<?php } ?> ];
  $('#autocomplete').autocomplete({
    lookup: currencies,
  }); });
</script>

                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style=" width:90px;">Product Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" ><input type="text" name="product_namee" id="autocomplete" autofocus="autofocus" size="55" class="required" ></div>
                </div>
                 <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style=" width:90px;">Quantity<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" ><input type="text" name="quantity" size="55" class="required" ></div>
                </div>
                 <br />
                <!--<div class="mainrow">
                <div class="maintext">Supplier Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" style="margin-left:30px;">
                <select name="supplier_name">
                <option value="Select">Select One</option>
                <?php
				$set=mysql_query("select supplier_name from supplier");
				while($fft=mysql_fetch_array($set))
				{
				?>
                <option value="<?=$fft['supplier_name'];?>"><?=$fft['supplier_name'];?></option>
                <?php } ?>
                </select>
                </div>
                </div>-->
                
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style=" width:90px;">Pay Type<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" >
                <select name="paytype">
                <option value="Select">Select One</option>
                <option value="Cash">Cash</option>
                <option value="Credit">Credit</option>
                </select>
                </div>
                </div>
                 <br />
                <div class="mainrow" style="margin-left:100px;">
                <div class="maintext" style=" width:90px;">Return Date<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox" >
                <input type="text" required class="TextBox" name="date" value="<? echo date("Y-m-d"); ?>" 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"><font color="#FF0000"><b></b></font> </div>
                <div class="maintextbox" style="margin-left:30px;"><input type="submit" name="submit"value="Submit" ></div></div>
 
 
 
 
 
   </form>
        </div>
      </div>
      
      
    </div>
  </div>
</div>
</div>
<div id="footer">Copyright &copy; 2014 | Powered by <a href="http://linikantech.com/" target="_blank">Linikantech</a><!--<a href="http://www.opencart.com">OpenCart</a> &copy; 2009-2016 All Rights Reserved.<br />Version 1.5.6.4--></div>
</body></html>