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

	$query = "SELECT * FROM label WHERE no_invoice='$no_invoice' AND user='$u[phone_number]'";
	$hasil = mysqli_query($connect,$query);


	if(mysqli_num_rows($hasil) > 0 ){
	
	
	$responses = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
 
   
	$responses["data"] = array();  
  $struk = array(
  'no_invoice' 		=> $no_invoice
  );
   
   
	$produk = array();
	while($x = mysqli_fetch_array($hasil)){
		
		
	$stoore = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$s=mysqli_fetch_array($stoore);
		
	
	$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]";
	
	if($b['codeproduct']==''){
		$kodebarang="00123000$b[id_product]";
	}else{
		$kodebarang="$b[codeproduct]";
	}
	
	for ($i = 0; $i < $x["amount"]; $i++){
    $h['name_store'] 	= $s['name_store'];
    $h['codeproduct'] 	= $kodebarang;
    $h['id_product'] 	= $x['id_product'];
    $h['no_invoice'] 	= $x['no_invoice'];
    $h['name_product'] 	= $nama_barang;
    $h['amount'] 		= $x["amount"];
    $h['price'] 		= $b["selling_price"];
    $h['description'] 		= $b["description"];
    $h['img'] 			= "http://$_SERVER[HTTP_HOST]/geten/label/barcode.php?text=$kodebarang&codetype=code128&print=true&size=55";
	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);	
	}
	?>