	<?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 purchasing_data WHERE no_invoice='$no_invoice'";
	$dp = mysqli_query($connect,$querydp);

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


	if(mysqli_num_rows($hasil) > 0 ){
	
	
	$responses = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
   
	$responses["data"] = array();

   
	$d = mysqli_fetch_array($dp);
  
  
	$datasupplier = mysqli_query($connect,"SELECT name_supplier FROM supplier WHERE id_supplier='$d[id_supplier]'");
	$p=mysqli_fetch_array($datasupplier);
	
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$t=mysqli_fetch_array($datatoko);
	
	$datatable = mysqli_query($connect,"SELECT name_table FROM db_table WHERE id_table='$d[id_table]'");
	$dt=mysqli_fetch_array($datatable);

  
  $struk = array(
  'name_store' 		=> $t['name_store'],
  'email' 			=> $t['email'],
  'nohp' 			=> $t['nohp'],
  'address' 		=> $t['address'],
  'no_invoice' 		=> $d['no_invoice'],
  'operator' 		=> $u['full_name'],
  'id_supplier' 	=> $d['id_supplier'],
  'name_supplier'	=> $p['name_supplier'],
  'id_table' 		=> 1,
  'date' 			=> $d['date'],
  'payment' 		=> $d['payment'],
  'totalorder' 		=> $d['totalorder'],
  'totallast' 		=> $d['totalorder'],
  'totalpay' 		=> $d['totalpay'],
  'changepay' 		=> $d['changepay'],
  'status' 			=> $d['status'],
  'note' 			=> $d['note'],
  'due_date' 		=> $d['due_date'],
  'footer' 		=> "",
  'img' 			=> "http://$_SERVER[HTTP_HOST]/geten/images/logo/$t[img]",
  'link_order' 		=> "http://$_SERVER[HTTP_HOST]/app/track.php?code=$d[no_invoice]"

  );
   
	$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);
	  
    $h['codeproduct'] 	= $b["codeproduct"];
    $h['name_product'] 	= $b["name_product"];
    $h['date']			= $x["date"];
    $h['amount'] 		= $x["amount"];
    $h['price'] 		= $x["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);	
	}
	?>