<? include("header.php");


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

include "checksession.php";

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


 if(isset($_REQUEST['submit']))
	{ 
  		$order_id= $_REQUEST['order_id'];
		$product_id= $_REQUEST['product_id'];
		$customer_name= $_REQUEST['customer_name'];
		$date= $_REQUEST['date'];
		$quant= $_REQUEST['quantity'];
		$amountt= $_REQUEST['amount'];
		$amount=$quant*$amountt;
		$payment_option=$_REQUEST['payment_option'];
		
		/*$query_select1=mysql_query("SELECT * FROM ".$purch." WHERE `product_id`='$product_id' ORDER BY id DESC");
		$qnty=mysql_fetch_array($query_select1);
		$pid= $qnty['id'];
		$qn= $qnty['quantity'];
		$quantity=$qn+$quant;				
		$sql="UPDATE ".$purch." SET  quantity='$quantity' WHERE id='$pid' ORDER BY id DESC";
		$result=mysql_query($sql);*/
		
		$query_select2=mysql_query("select * from  ".$PRODUCT." where product_id='$product_id'");
		$qnty1=mysql_fetch_array($query_select2);
		$qn1= $qnty1['quantity'];
		$quantity1=$qn1+$quant;
		$sql="UPDATE ".$PRODUCT." SET  quantity='$quantity1' WHERE product_id='$product_id'";
		$result=mysql_query($sql);
		 

		$ins_qur = "insert into ".$SAL_RET."(order_id,product_id,quantity,customer_name,return_date,amount,payment_option) 
					 values('$order_id','$product_id','$quant','$customer_name','$date','$amount','$payment_option')";					  
		mysql_query($ins_qur);
		
		
		//....................................
		$sale_tax=mysql_query("select * from profit where product_id='$product_id' and order_id='$order_id'");
		$sal_fet=mysql_fetch_array($sale_tax);
		$quany=$sal_fet['quantity'];
		$qntty=$quany-$quant;
		
		$profit=$sal_fet['profit'];
		$pro_sal=$profit*$quant;
		$total_profit=$sal_fet['total_profit'];
		$tot_sal_profit=$total_profit-$pro_sal;
		
		
		$sale_tax=$sal_fet['sale_tax'];
		$tax_sal=$sale_tax*$quant;
		$tot_tax=$sal_fet['tot_tax'];
		$tot_sale_tax=$tot_tax-$tax_sal;
		
		$update=mysql_query("update profit set quantity='$qntty',total_profit='$tot_sal_profit',tot_tax='$tot_sale_tax' where product_id='$product_id' and order_id='$order_id'");
		//.........................................
		
	
	 //........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'");	
	
	//end insert open cash	
	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'");
	}
}
if($payment_option=='Bank')
{
	$query=mysql_query("select closecash,expense 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_sales_return.php?add=success");?>
		<script>
	window.location="view_sales_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="" />Sales Return</h1>
    </div>
    <div class="content">
      <div class="overview" style="width:50%; margin-left:300px;">
        <div class="dashboard-heading"> Add Sales 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>-->
 
                <div class="mainrow" style="margin-left:200px;">
                <div class="maintext" style="width:150px;">Order ID<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="order_id" >
                </div>               
                </div>
                <br />
                <div class="mainrow" style="margin-left:200px;">
                <div class="maintext" style="width:150px;">Product ID<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="product_id">
                </div>               
                </div>
                <br />
                <div class="mainrow" style="margin-left:200px;">
                <div class="maintext" style="width:150px;">Quantity<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="quantity">
                </div>               
                </div>
                <br />
                <div class="mainrow" style="margin-left:200px;">
                <div class="maintext" style="width:150px;">Amount per Quantity<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="amount">
                </div>               
                </div>
                <br />
                <div class="mainrow" style="margin-left:200px;">
                <div class="maintext" style="width:150px;">Customer Name<font color="#FF0000"><b> *</b></font> </div>
                <div class="maintextbox"><input type="text" name="customer_name">
                </div>               
                </div>
                <br />
                <div class="mainrow" style="margin-left:200px;">
                <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:200px;">
                <div class="maintext" style="width:150px;">Cheque No</div>
                <div class="maintextbox"><input type="text" name="cheque_no" >
                </div>               
                </div>
                <br />
                <div class="mainrow" style="margin-left:200px;">
                <div class="maintext" style="width:150px;">Return 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:200px;">
    			<div class="maintext" style="width:150px;"><font color="#FF0000"><b></b></font> </div>
                <div class="maintextbox"><input type="submit" name="submit"value="Submit" ></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"); ?>