	<?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']);

	$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){


	$querydp = "SELECT * FROM salesreturn_data WHERE no_invoice='$no_invoice'";
	$dp = mysqli_query($connect,$querydp);

	$query = "SELECT * FROM salesreturn WHERE no_invoice='$no_invoice'";
	$hasil = mysqli_query($connect,$query);
      
    $queryes = "SELECT * FROM salesreturn WHERE no_invoice='$no_invoice'";
	$hasilgst = mysqli_query($connect,$queryes);


	if(mysqli_num_rows($hasil) > 0 ){
	
	
	$responses = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
   
   
	$responses["data"] = array();
	$d = mysqli_fetch_array($dp);
  
	$datapelanggan = mysqli_query($connect,"SELECT name_customer FROM customer WHERE id_customer='$d[id_customer]'");
	$p=mysqli_fetch_array($datapelanggan);
	
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$t=mysqli_fetch_array($datatoko);
  
	$dataop = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$d[operator]'");
	$o=mysqli_fetch_array($dataop);
	
	if($d['offline_inv']== $no_invoice){
		mysqli_query($connect,"UPDATE salesreturn SET no_invoice   = '$no_invoice' WHERE offline_inv 	   = '$ds[offline_inv]'");
	}
	
	if($d['service_charge']=='0.00'){
		$service_charge= null;
	}else{
		$service_charge= $d['service_charge'];
	}
	
	if($d['discount']=='0.00'){
		$discount= null;
	}else{
		$discount= $d['discount'];
	}
	if($d['changepay']=='0.00'){
		$changepay= null;
	}else{
		$changepay= $d['changepay'];
	}
	
  
  $struk = array(
  'name_store' 		=> $t['name_store'],
  'email' 			=> $t['email'],
  'nohp' 			=> $t['nohp'],
  'address' 		=> $t['address'],
  'footer' 			=> $t['footer'],
  'no_invoice' 		=> $d['no_invoice'],
  'operator' 		=> $o['full_name'],
  'payment' 		=> $d['payment'],
  'id_customer' 	=> $d['id_customer'],
  'name_customer' 	=> $p['name_customer'],
  'date' 			=> "$d[date] $d[hour]",
  'totalorder' 		=> $d['totalprice'],
  'totallast' 		=> $d['totalorder'],
  'totalpay' 		=> $d['totalpay'],
  'changepay' 		=> $changepay,
  'status' 			=> $d['status'],
  'note'	 		=> $d['note'],
  'due_date' 		=> $d['due_date'],
  'service_charge' 	=> $service_charge,
  'discount' 		=> $discount,
  'tax' 			=> "",
  'img' 			=> "http://$_SERVER[HTTP_HOST]/geten/images/logo/$t[img]",
  'qrcode' 			=> $t['qrcode'],
  );
	$produk = array();
	while($x = mysqli_fetch_array($hasil)){
		
	
	$databarang = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$x[id_product]'");
	$b=mysqli_fetch_array($databarang);
	$nama_barang="$b[name_product]";
	$kodebarang="$b[codeproduct]";
      
      
    $h['codeproduct'] 	= $kodebarang;
    $h['id_product'] 	= $x['id_product'];
    $h['no_invoice'] 	= $x['no_invoice'];
    $h['name_product'] 	= $nama_barang;
    $h['date'] 			= $x["date"];
    $h['amount'] 		= $x["amount"];
    $h['price'] 		= $x["price"];
    $h['wholesale_price'] 	= $x["wholesale_price"];
    $h['totalprice'] 	= $x["totalprice"];
    $h['status'] 		= $x["status"];
    $h['note'] 			= $x["note"];
    $h['stock'] 		= $b["stock"];
	$h['have_stock'] 	= $b["have_stock"];
    $h['img'] 			= "http://$_SERVER[HTTP_HOST]/geten/images/$b[folder]/small_$b[img]";
    array_push($produk, $h);
	}     
      
	$responses["data"] = array(
	'struk' 	=> $struk,
	'data' 		=> $produk
	);
  
	array_push($responses["data"]);
	

	echo json_encode($responses);
	}else {
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '02',
	'msg' 		=> 'no data'
	);
	echo json_encode($response);
	}


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

    echo json_encode($json);	
	}
	?>