<?php 
   include("include/db-connect.php");
 $mid=$_GET['id'];
 $mid=base64_decode($mid);
 
 $product_id=$_GET['pid'];
 $date=$_GET['date'];
 $tot_amount=$_GET['amt'];
 $quant=$_GET['qnty'];
 $paytype=$_GET['payopt'];
    
$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="UPDATE ".$PRODUCT." SET  quantity='$quantity1' WHERE product_id='$product_id'";
		 $result=mysql_query($sql);
		 
		 
		 
		 
		 //........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($paytype=='Cash')
{
	$query=mysql_query("select * from  ".$CASH." where cash_date='$date'");
	$run=mysql_fetch_array($query);
	
	$close=$run['close_cash'];
	$closecash=$close+$tot_amount;
	$collec=$run['collection'];
	$collection=$collec+$tot_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+$tot_amount;
	$close1=$run_query['close_cash'];
	$closecash1=$close1+$tot_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+$tot_amount;
	$collec=$run['collection'];
	$collection=$collec+$tot_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+$tot_amount;
	$close1=$run_query['closecash'];
	$closecash1=$close1+$tot_amount;
	
	$inset=mysql_query("update ".$BANK." set closecash='$closecash1',opencash='$open1' where cashdate='$date'");
	 }
}


	
$del_tab="DELETE FROM ".$purch." WHERE `id`='$mid'"; 
$exe_tab=mysql_query($del_tab);


//header("location:view_pur.php?delete=success");

?>
        <script>
	window.location="view_pur.php?delete=success";
	</script>
       