	<?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']);
	$id_pelanggan=mysqli_real_escape_string($connect, $_GET['id_customer']);
	
	
	$hariini	= gmdate("Y-m-d", time()+60*60*7);

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


	$queryutama = "SELECT * FROM customerdebthistory WHERE id_customer='$id_pelanggan' ORDER BY id_customerdebthistory DESC";
	$hasildata 	= mysqli_query($connect,$queryutama);

	$totaltagihan = mysqli_query($connect,"SELECT SUM(nominal) as total FROM customerdebthistory WHERE id_customer='$id_pelanggan' AND status  NOT IN ('cancel')");
	$n=mysqli_fetch_array($totaltagihan);


	$jumlahpiutang = mysqli_query($connect,"SELECT SUM(nominal) as total FROM customerdebthistory WHERE id_customer='$id_pelanggan' AND status='debt'");
	$j=mysqli_fetch_array($jumlahpiutang);

	if($n['total']==''){
	$jumlah_piutang=0;
	}else{
	$jumlah_piutang= $n['total'];
	}

	$totaldibayar = mysqli_query($connect,"SELECT SUM(nominal) as total FROM logscustomerdebtpayments WHERE id_customer='$id_pelanggan'");
	$t=mysqli_fetch_array($totaldibayar);

	if($t['total']==''){
	$total_dibayar=0;
	}else{
	$total_dibayar= $t['total'];
	}
	
	$sisa_piutang=$jumlah_piutang-$total_dibayar;
	
	$jatuhtempo = mysqli_query($connect,"SELECT due_date FROM sales_data WHERE id_customer='$id_pelanggan' AND status='debt' ORDER BY date DESC LIMIT 1");
	$jt=mysqli_fetch_array($jatuhtempo);

	if($jt['due_date']==''){
	$jatuh_tempo="2019-01-01";
	}else{
	$jatuh_tempo= $jt['due_date'];
	}

	$query = "SELECT * FROM logscustomerdebtpayments WHERE id_customer='$id_pelanggan' ORDER BY id_logscustomerdebtpayments DESC";
	$hasil = mysqli_query($connect,$query);


	if(mysqli_num_rows($hasildata) > 0 ){
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
   
  $response["data"] = array();
   
 $datapiutang = array(
  'total_tagihan' 	=> $n['total'],
  'jumlah_piutang' 	=> $sisa_piutang,
  'total_dibayar' 	=> $total_dibayar,
  'jatuh_tempo' 	=> $jatuh_tempo
  );
  
  $sudah_bayar = array();
  while($x = mysqli_fetch_array($hasil)){
	  
	$datapelanggan = mysqli_query($connect,"SELECT name_customer FROM customer WHERE id_customer='$x[id_customer]'");
	$p=mysqli_fetch_array($datapelanggan);

	  
    $h['id_customer'] 		= $x["id_customer"];
    $h['name_customer'] 	= $p["name_customer"];
    $h['no_invoice'] 		= $x["no_invoice"];
    $h['date'] 			= $x["date"];
    $h['nominal'] 			= $x["nominal"];
    array_push($sudah_bayar, $h);
  }
  
  $response["data"] = array(
	'datadebt' => $datapiutang,
	'sudah_bayar' => $sudah_bayar
  );
  
   array_push($response["data"]);
	
  echo json_encode($response);
	}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);	
	}
	?>