<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$tyrenum_veh='';

include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();

	$return_id = $_GET['sale_ret_id'];
	$saleReurn = mysql_query("SELECT * FROM `cf_sales_return` WHERE sales_return_id = '$return_id' Limit 1");	
	$saleReurnObj = mysql_fetch_array($saleReurn);

	$saleReurnSub = mysql_query("SELECT * FROM `cf_sales_return_sub` WHERE sale_returnid = '$return_id' ");	
	
	$cnt = mysql_num_rows($saleReurnSub);

	if(($_GET['return_type'] == "box")){
		
	}

	if(($_GET['return_type'] == "tin")){
		
		$qty_tintotal = $saleReurnObj['sales_return_tot'];
		$clientid = $saleReurnObj['sales_return_clientid'];
		mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`+$qty_tintotal where clientid=$clientid"); 

		while($val = mysql_fetch_array($saleReurnSub))
		{
			$id = $val["sale_sub_return_id"];			

			$notin = $val['sale_no_tin_return_kg'];
			//$tindamagereturn = $val['sale_pertin_return_rs'];

			$tin_sql=mysql_query("select * from cf_tin where status=1");
			if($tin_sql && mysql_num_rows($tin_sql)>0)
			{
				while($tin_row=mysql_fetch_array($tin_sql))
				{
					$tin_kg=$tin_row['kg_val'];
				}
			}
			else
			{
				$tin_kg=0;
			}
				
			$tin_tot=$tin_kg*($notin);
			$productid  = $val['return_prd_id'];

			mysql_query("update cf_stock set `stock_oil`=`stock_oil`-$tin_tot");
				
			if($productid == 1)
			{
				mysql_query("update cf_stock set `stock_palmoil`=`stock_palmoil`-$tin_tot");
			}
			else if($productid == 2)
			{
				mysql_query("update cf_stock set `stock_coconutoil`=`stock_coconutoil`-$tin_tot");
			}				
						
			mysql_query("update cf_stock set `stock_tin`=`stock_tin`-$notin");		

			mysql_query("DELETE FROM `cf_sales_return_sub` WHERE sale_sub_return_id = '$id' ");		
		}
	}

	if(($_GET['return_type'] == "kg")){

		$qty_jartotal = $saleReurnObj['sales_return_tot'];
		$clientid = $saleReurnObj['sales_return_clientid'];
		mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`+qty_jartotal where clientid=$clientid"); 

		while($val = mysql_fetch_array($saleReurnSub))
		{
			
			$id = $val["sale_sub_return_id"];
			
			$qtykg = $val['sale_no_tin_return_kg'];
			$product_kgid = $val['return_prd_id'];

			mysql_query("update cf_stock set `stock_oil`=`stock_oil`-$qtykg");
					
			if($product_kgid == 1)
			{	
				mysql_query("update cf_stock set `stock_palmoil`=`stock_palmoil`-$qtykg");
			}
			else if($product_kgid == 2)
			{
				mysql_query("update cf_stock set `stock_coconutoil`=`stock_coconutoil`-$qtykg");
			}

			mysql_query("DELETE FROM `cf_sales_return_sub` WHERE sale_sub_return_id = '$id' ");	
		}
	}

	if(($_GET['return_type'] == "jar")){

		$qty_kgtotal = $saleReurnObj['sales_return_tot'];
		$clientid = $saleReurnObj['sales_return_clientid'];
		mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`+$qty_kgtotal where clientid=$clientid"); 


		while($val = mysql_fetch_array($saleReurnSub))
		{
			
			$id = $val["sale_sub_return_id"];
			
			$nojar = $val['sale_no_tin_return_kg'];
			//$jardamagereturn = $val['damagereturn'];

			$jar_sql=mysql_query("select * from cf_jar where status=1");
			if($jar_sql && mysql_num_rows($jar_sql)>0)
			{
				while($jar_row=mysql_fetch_array($jar_sql))
				{
					$jar_kg=$jar_row['jar_kg'];
				}
			}
			else
			{
				$jar_kg=0;
			}

			$jar_tot=$jar_kg*($nojar);
			$product_jarid = $val['return_prd_id'];

			mysql_query("update cf_stock set `stock_oil`=`stock_oil`-$jar_tot");
				
			mysql_query("update cf_stock set `stock_jar`=`stock_jar`-$nojar");

			if($product_jarid == 1)
			{
				mysql_query("update cf_stock set `stock_palmoil`=`stock_palmoil`-$jar_tot");
			}
			else if($product_jarid == 2)
			{
				mysql_query("update cf_stock set `stock_coconutoil`=`stock_coconutoil`-jar_tot");
			}	

			mysql_query("DELETE FROM `cf_sales_return_sub` WHERE sale_sub_return_id = '$id' ");			
		}
	}

	if(($_GET['return_type'] == "barrel")){

		$qty_barreltotal = $saleReurnObj['sales_return_tot'];
		$clientid = $saleReurnObj['sales_return_clientid'];
		mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`+$qty_barreltotal where clientid=$clientid");


		while($val = mysql_fetch_array($saleReurnSub))
		{
			
			$id = $val["sale_sub_return_id"];
			
			$nobarrel = $val['sale_no_tin_kg'];
			
			$barrel_sql=mysql_query("select * from cf_barrel where status=1");
			if($barrel_sql && mysql_num_rows($barrel_sql)>0)
			{
				while($barrel_row=mysql_fetch_array($barrel_sql))
				{
					$barrel_kg=$barrel_row['barrel_kg'];
				}
			}
			else
			{
				$barrel_kg=0;
			}

			$barrel_tot=$barrel_kg*$nobarrel;
			$product_barrelid = $val['prd_id'];

			mysql_query("update cf_stock set `stock_barrel`=`stock_barrel`+$nobarrel");
				
			if($product_barrelid == 1)
			{
				mysql_query("update cf_stock set `stock_palmoil`=`stock_palmoil`+$barrel_tot");
			}
			else if($product_barrelid == 2)
			{
				mysql_query("update cf_stock set `stock_coconutoil`=`stock_coconutoil`+$barrel_tot");
			}
			
			mysql_query("DELETE FROM `cf_sales_return_sub` WHERE sale_sub_return_id = '$id' ");	
		}

	}

	//----Sales Return delete------------- 
	$purchaseAcc = mysql_query("SELECT * FROM `cf_purchase_account` WHERE purchaseid = '$return_id' ");	
	
	while($purchase_row=mysql_fetch_array($purchaseAcc))
	{	
		$purchase_acc_id = $purchase_row['pur_acc_id'];
		mysql_query("DELETE FROM `cf_purchase_account` WHERE pur_acc_id = '$purchase_acc_id' ");		
	}
	//----Sales Return delete- end--------- 	
	
	
	mysql_query("DELETE FROM `cf_sales_return` WHERE sales_return_id = '$return_id' ");		
	
	header("location:sales-return-list.php");

?>


		






