<?php
require('includes/head.php');
if(isset($_GET['purchase_id']))
{
	$id=$_GET['purchase_id'];
	
	echo $a="select * from `purchase_fatura_item` where `p_f`=$id ";
	$gett=$conn->query($a);
	while($get_row=mysqli_fetch_array($gett)){
		$code=$get_row['p_f_i_code'];
		$set=$get_row['p_f_i_set'];
		
		$gett2=$conn->query("select * from `item_set` where `barcode`='$code' ");
		$get_row2=mysqli_fetch_array($gett2);
		$old_num=$get_row2['i_set_number'];
		$new_num=$old_num-$set;
		
		$up=$conn->query("update item_set set `i_set_number`='$new_num' where `barcode`='$code' ");
		$del="delete from  `purchase_fatura_item` where `p_f_i_code`='$code'";
		$conn->query($del);
	}
	
	
	$del2="delete from  `purchase_fatura` where `p_f_id`=$id";
	$conn->query($del2);
	$del2="delete from  `purchase_inv` where `inv_code`=$id and `inv_type`='purchase'";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'purchase.php'</script>";	
}
if(isset($_GET['pay_id']))
{
	$id=$_GET['pay_id'];
	$del2="delete from  `pay` where `pay_id`=$id";
	$conn->query($del2);
	$del2="delete from  `purchase_inv` where `inv_code`=$id and `inv_type`='Pay'";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'pay.php'</script>";	
}

if(isset($_GET['sell_j_id']))
{
	$id=$_GET['sell_j_id'];
	$rem=$_GET['rem'];
	$c_id=$_GET['c_id'];
	
	
	$s_j=$conn->query("select * from `customer` where `customer_id`=$c_id ");
		$s_j_row=mysqli_fetch_array($s_j);
		$old_num=$s_j_row['statement'];
		$new_num=$old_num-$rem;
		
		$up=$conn->query("update customer set `statement`='$new_num' where `customer_id`=$c_id ");
	
	
	$gett=$conn->query("select * from `sell_j_item` where `sell_j`=$id ");
	while($get_row=mysqli_fetch_array($gett)){
		$code=$get_row['sell_j_i_code'];
		$set=$get_row['sell_j_i_set'];
		
		
		
		$get="select `i_set_number` from `item_set` where `i_s_barcode`='$code'";
	$get_r=$conn->query($get);
	$get_row=mysqli_fetch_array($get_r);
	$set_num_old=$get_row['i_set_number'];
	
	
	$new_set_num=$set_num_old+$set;
	
	$up="UPDATE `item_set` SET `i_set_number`='$new_set_num' WHERE `i_s_barcode`='$code'";
	$conn->query($up);
		
		
		
		
		
		
		$del="delete from  `sell_j_item` where `sell_j`=$id";
		$conn->query($del);
	}
	
	
	$del2="delete from  `sell_j` where `sell_j_id`=$id";
	$conn->query($del2);
	
	$gett2=$conn->query("select * from `get` where `sell_j`=$id");
	while($get_row2=mysqli_fetch_array($gett2)){
	$ddd=$get_row2['get_id'];
	
	$del2="delete from  `get` where `sell_j`=$id";
	$conn->query($del2);
	$del2="delete from  `sell_j_inv` where `inv_code`=$ddd and `inv_type`='get'";
	$conn->query($del2);
	}
	
	$del2="delete from  `sell_j_inv` where `inv_code`=$id and `inv_type`='sell'";
	$conn->query($del2);
	
	
	echo "<script type='text/javascript'>window.location.href = 'sell_j.php'</script>";	
}


if(isset($_GET['debt_id']))
{
	$id=$_GET['debt_id'];
	$a=$_GET['amount'];
	$c_id=$_GET['c_id'];
	
	
	$s_j=$conn->query("select * from `customer` where `customer_id`=$c_id ");
		$s_j_row=mysqli_fetch_array($s_j);
		$old_num=$s_j_row['statement'];
		$new_num=$old_num-$a;
		
		$up=$conn->query("update customer set `statement`='$new_num' where `customer_id`=$c_id ");
	
	
	
	
	
	$del2="delete from  `sell_j` where `sell_j_id`=$id";
	$conn->query($del2);
	
	
	
	$del2="delete from  `sell_j_inv` where `inv_code`=$id and `inv_type`='debt'";
	$conn->query($del2);
	
	
	echo "<script type='text/javascript'>window.location.href = 'old_debt.php'</script>";	
}

if(isset($_GET['sell_j_id2']))
{
	$id=$_GET['sell_j_id2'];
	
	$c_id=$_GET['c_id'];
	$gett=$conn->query("select * from `sell_j_item` where `sell_j`=$id ");
	while($get_row=mysqli_fetch_array($gett)){
		$code=$get_row['sell_j_i_code'];
		$num=$get_row['sell_j_i_number'];
		$set=$get_row['sell_j_i_set'];
		
		$gett2=$conn->query("select * from `items` where `code`=$code ");
		$get_row2=mysqli_fetch_array($gett2);
		$old_num=$get_row2['store_number'];
		$new_num=$old_num+$num;
		
		$old_set=$get_row2['store_set_number'];
		$new_set=$old_set+$set;
		
		$up=$conn->query("update items set `store_number`='$new_num',`store_set_number`='$new_set' where `code`=$code ");
		$del="delete from  `sell_j_item` where `sell_j`=$id";
		$conn->query($del);
	}
	
	
	$del2="delete from  `sell_j` where `sell_j_id`=$id";
	$conn->query($del2);
	
	$gett2=$conn->query("select * from `get` where `sell_j`=$id");
	while($get_row2=mysqli_fetch_array($gett2))
	{
	$ddd=$get_row2['get_id'];
	
	$del2="delete from  `get` where `sell_j`=$id";
	$conn->query($del2);
	$del2="delete from  `sell_j_inv` where `inv_code`=$ddd and `inv_type`='get'";
	$conn->query($del2);
	}
	$del2="delete from  `sell_j_inv` where `inv_code`=$id and `inv_type`='sell'";
	$conn->query($del2);
	
	
	echo "<script type='text/javascript'>window.location.href = 'sell_j2.php'</script>";	
}





if(isset($_GET['pay_id']))
{
	$id=$_GET['pay_id'];
	$del2="delete from  `pay` where `pay_id`=$id";
	$conn->query($del2);
	$del2="delete from  `purchase_inv` where `inv_code`=$id and `inv_type`='Pay'";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'pay.php'</script>";	
}
if(isset($_GET['get_id']))
{
	$id=$_GET['get_id'];
	$c_id=$_GET['c_id'];
	$a=$_GET['amount'];
	
	$s_j=$conn->query("select * from `customer` where `customer_id`=$c_id ");
		$s_j_row=mysqli_fetch_array($s_j);
		$old_num=$s_j_row['statement'];
		$new_num=$old_num+$a;
		
		$up=$conn->query("update customer set `statement`='$new_num' where `customer_id`=$c_id ");
	
	$del2="delete from  `get` where `get_id`=$id";
	$conn->query($del2);
	$del2="delete from  `sell_j_inv` where `inv_code`=$id and `inv_type`='get'";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'get.php'</script>";	
}

/*



if(isset($_GET['id2']))
{ 
	$id=$_GET['id2'];
	$del="delete from  `transfer_item2` where `trans_id`=$id";
	$conn->query($del);
	$del2="delete from  `transfer2` where `transfer_id`=$id";
	$conn->query($del2);
	$del2="delete from  `inv2` where `inv_code`=$id and `inv_type`=1";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'send2.php'</script>";	
}
if(isset($_GET['id_get']))
{
	$id=$_GET['id_get'];
	$del="delete from  `transfer_item_get` where `trans_id`=$id";
	$conn->query($del);
	$del2="delete from  `transfer_get` where `transfer_id`=$id";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'get.php'</script>";	
}

if(isset($_GET['id_ret2']))
{
	$id=$_GET['id_ret2'];
	$del="delete from  `return_item2` where `ret_id`=$id";
	$conn->query($del);
	$del2="delete from  `return2` where `return_id`=$id";
	$conn->query($del2);
	$del2="delete from  `inv2` where `inv_code`=$id and `inv_type`=3";
	$conn->query($del2);
	echo "<script type='text/javascript'>window.location.href = 'return2.php'</script>";	
}
*/


?>	