	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	*/

	require_once("../connection.php");
	
					$key=mysqli_real_escape_string($connect, $_GET['key']);
					$no_invoice=mysqli_real_escape_string($connect, $_GET['no_invoice']);
					$id_product=mysqli_real_escape_string($connect, $_GET['id_product']);


	$getusers = mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	      = mysqli_fetch_array($getusers);
	$jumlah   = mysqli_num_rows($getusers);
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);

	if ($jumlah > 0){
		
	$dataorder = mysqli_query($connect,"SELECT * FROM sales_data WHERE no_invoice='$no_invoice'");
	$d=mysqli_fetch_array($dataorder);
	
	
		if($d['status']=='debt' OR $d['status']=='cancel'){
			
	$json = array(
    'status' => 'true',
    'errCode' => '02',
	'msg' => 'Product items cannot be deleted for debt transactions'
	);

    echo json_encode($json);	
		}else{
	
	$datasales = mysqli_query($connect,"SELECT * FROM sales WHERE no_invoice='$no_invoice' AND id_product='$id_product'");
	$s=mysqli_fetch_array($datasales);
	
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$dt=mysqli_fetch_array($datatoko);
	
	$query1	=mysqli_query($connect,"SELECT * FROM product WHERE id_product ='$id_product'");
	$b = mysqli_fetch_array($query1);
	if($b['have_stock']==1){
	$newstock=($b['stock']+$s['amount']);
	$query3	=mysqli_query($connect,"UPDATE product SET stock='$newstock' WHERE id_product ='$id_product'");
	$query30	=mysqli_query($connect,"UPDATE stock SET stock='$newstock' WHERE id_product ='$id_product' ");		
	}
	
	
	$updatetotalprice 		= $d['totalprice']-$s['totalprice'];
	$updatetotalorder 		= $d['totalorder']-$s['totalprice'];

	
	$nominalscc=$updatetotalprice*$dt['service_charge']/100;
    $nominalsc=$nominalscc;
	$total_service_charge=$updatetotalprice+$nominalsc;
	
	$nominalpajaks=$total_service_charge*$dt['tax']/100;
	$nominalpajak=$nominalpajaks;
	$total_bayar=$total_service_charge+$nominalpajak;
	
	
	mysqli_query($connect,"UPDATE sales_data SET totalprice 	= '$updatetotalprice',
												 totalorder 	= '$total_bayar',
												 tax 			= '$nominalpajak',
												 service_charge = '$nominalsc'
                             WHERE no_invoice  = '$no_invoice'");
	 
		mysqli_query($connect,"DELETE FROM sales WHERE no_invoice='$no_invoice' AND id_product='$id_product'");
		
		
		
	$amountsales = mysqli_query($connect,"SELECT * FROM sales WHERE no_invoice='$no_invoice'");
	$amounts   = mysqli_num_rows($amountsales);
		
		if ($amounts <= 0){
		mysqli_query($connect,"DELETE FROM sales_data WHERE no_invoice='$no_invoice'");
		}
     
 
 
	$response = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success' 
	);	
	
  echo json_encode($response);
  
	}

	}else{
	
	
	$json = array(
    'status' => 'true',
    'errCode' => '00',
	'msg' => 'User Not Found'
	);

    echo json_encode($json);	
	}
	?>