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

	require_once("../connection.php");
	$content 	= trim(file_get_contents("php://input"));
	$decoded 	= json_decode($content, true);
	$tanggal	= gmdate("Y-m-d", time()+60*60*7);
	
	$key								= $decoded["key"];
	$no_invoice								= $decoded["id"];
	$payment_type					= $decoded["payment_type"];
	$id_discount							= $decoded["id_discount"];
	$card								= $decoded["card"];
	$note							= $decoded["note"];
	$payment_amounts							= $decoded["payment_amount"];

	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	   		= mysqli_fetch_array($getusers);
	$jumlah   	= mysqli_num_rows($getusers);
	$tanggal	= gmdate("Y-m-d", time()+60*60*7);
	
	
	
	
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);

	if ($jumlah > 0){
		


	$query = "SELECT * FROM sales_data WHERE user='$m[phone_number]' AND no_invoice='$no_invoice'";
	$hasil = mysqli_query($connect,$query);
	$p=mysqli_fetch_array($hasil);
	
	if($payment_type==1){
		$payment="cash";
		$status="paid off";
		$payment_amount=$payment_amounts;
	}else{
		$payment="non cash";
		$status="paid off";
		$total_order=$p['totalorder']-$payment_amounts;
		$payment_amount=$p['totalorder'];
	}

	
	if($id_discount==''){
			$hargasetelahdiskon=$p['totalorder'];
	}else{
		
	$datadiskon = mysqli_query($connect,"SELECT * FROM discount WHERE id_discount='$id_discount'");
	$d=mysqli_fetch_array($datadiskon);
	if($d['type']=='prosentase'){
		$diskons=$p['totalorder']*$d['nominal']/100;
		$diskon=round($diskons);
	}else{
		$diskon=$d['nominal'];
	}
	
	$hargasetelahdiskon=$p['totalorder']-$diskon;
		
	}
	
	$totalbayar=$payment_amount;

	
	if($payment_type==1){
		$kembalian=$totalbayar-$hargasetelahdiskon;
	}else{
		$kembalian=$payment_amounts;
	}

	if(mysqli_num_rows($hasil) > 0 ){
	
	
	  mysqli_query($connect,"UPDATE sales_data SET    totalpay    = '$totalbayar',
											note		 = '$card $note',
											totalorder		 = '$hargasetelahdiskon',
											payment		 = '$payment',
											status    	 = 'paid off',
											changepay		 = '$kembalian'
										WHERE no_invoice 	 = '$no_invoice'");
										
							
	$datapenjualan = mysqli_query($connect,"SELECT * FROM sales_data WHERE no_invoice='$no_invoice'");
	$db=mysqli_fetch_array($datapenjualan);
	
	
	if($db['totalorder'] <= $payment_amount){
	mysqli_query($connect,"UPDATE sales SET status    = 'paid off' WHERE no_invoice = '$no_invoice'");
	mysqli_query($connect,"UPDATE db_table SET status   = '0' WHERE id_table  = '$db[id_table]'");
	
	
		mysqli_query($connect,"INSERT INTO history_job(no_invoice,
								  note,
								  operator,
								  status,
								  date,
                                  user) 
					VALUES('$no_invoice',
						   'Transaction $no_invoice Paid Off by $u[full_name]',
						   '$u[phone_number]',
						   'paid off',
						   '$tanggal $time',
                           '$m[phone_number]')");
	
	}
	
	
	
	$responses = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
 
	$responses["data"] = array(
	'invoice' 	=> $no_invoice
	);
	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);	
	}
	?>