<?php
if($_POST['t']=='delete'){
	$q=mysqli_query($connect, "UPDATE `transaksi` SET `hapus`='1' WHERE `id_`='$_POST[id]'");
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1);
	}
	echo json_encode($data);
}elseif($_POST['t']=='validasi'){
	$q=mysqli_query($connect, "SELECT * FROM `transaksi` WHERE `ID`='$_POST[id]' and hapus='0'");
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$b=mysqli_fetch_array($q);
		if($b['validated_trx']=='0'){
			$data = array('status'=>1,'valid'=>'Valid','msg'=>'<span class="label bg-blue">Transaksi ini Valid</span>');
			$up=mysqli_query($connect, "UPDATE `transaksi` SET `validated_trx`='1' WHERE id_='$b[id_]'");
		}elseif($b['validated_trx']=='1'){
			$data = array('status'=>1,'valid'=>'Tidak Berlaku lagi','msg'=>'<span class="label bg-orange">This Transaction Has Been Validated</span>');
		}else{
			$data = array('status'=>1,'valid'=>'Tidak Valid','msg'=>'<span class="label bg-red">This Transaction Not Found!</span>');
		}
	}
	echo json_encode($data);
}else{
	$params = $columns = $totalRecords = $data = array();
	$params = $_POST;
	$where = $sqlTot = $sqlRec = "";
	if( !empty($params['search']['value']) ) {   
		$where .=" AND ";
		$where .=" ( b.nama_toko LIKE '".$params['search']['value']."%' ";
		$where .=" OR a.waktu LIKE '".$params['search']['value']."%' ";
		$where .=" OR a.total LIKE '".$params['search']['value']."%' ";
		$where .=" OR c.nama LIKE '".$params['search']['value']."%' ) ";
	}
	$sql = "SELECT a.*,b.nama_toko,c.user_nama FROM `transaksi` a, `toko` b,`users` c WHERE b.ID=a.ID_toko and c.ID=a.ID_user and a.hapus='0'";
	$sqlTot .= $sql;
	$sqlRec .= $sql;
	if(isset($where) && $where != '') {

		$sqlTot .= $where;
		$sqlRec .= $where;
	}
	$sqlRec .=  " order by id_ desc LIMIT ".$params['start']." ,".$params['length']." ";
	$queryTot = mysqli_query($connect, $sqlTot) or die("database error:". mysqli_error($connect));
	$totalRecords = mysqli_num_rows($queryTot);
	$queryRecords = mysqli_query($connect, $sqlRec) or die("error to fetch $p");
	$items = array();
	$i=0;
	while( $b = mysqli_fetch_array($queryRecords) ) {
		$i++;
		$q_kasir=mysqli_query($connect, "select nama from karyawan where ID='$b[ID_karyawan]' and ID_user='$b[ID_user]'");
		if(mysqli_num_rows($q_kasir)){
			$kasir=mysqli_fetch_array($q_kasir);
		}else{
			$q_user=mysqli_query($connect, "select user_nama from users where ID='$b[ID_user]'");
			$kasir=mysqli_fetch_array($q_user);
		}
		$qty=mysqli_fetch_array(mysqli_query($connect, "select sum(qty) from transaksi_detail where ID_transaksi='$b[id_]'"));
		$aksi = '<a href="'.getConfig($connect, 'base_url').'?page='.$p.'&lihat='.$b['id_'].'" class="btn btn-xs bg-grey waves-effect" data-tooltip="true" data-placement="top" title="Lihat"><i class="material-icons">list</i></a>&nbsp;';
		$aksi.='<button data-delconfirm="be careful, deleting this data may affect other data<br>because this data is interconnected with other data" class="btn bg-red waves-effect btn-xs hapus_ajax" data-id="'.$b['id_'].'" data-p="'.$p.'" data-t="delete" data-token="'.getToken().'" title="Hapus"><i class="material-icons">delete</i></button>';
		$status = ($b['status']==1)?'<span class="label bg-blue">sale</span>':'<span class="label bg-orange">void</span>';
		$ppn = ($b['pajak']==1)?'<span class="label bg-orange">yes</span>':'<span class="label bg-grey">no</span>';
		$no_trx='<span title="ID - '.$b['nama_toko'].'">'.$b['ID_toko'].'</span>-<span title="No Struk">'.$b['no_struk'].'</span>';
		$items[] = array($i,$b['user_nama'],$b['nama_toko'],$kasir[0],'<small>'.$no_trx.'</small>',$status,$qty[0],number_format($b['subtotal'],0,',','.'),$b['diskon'].'%',$ppn,number_format($b['grandtotal'],0,',','.'),'<small>'.$b['waktu'].'</small>',$aksi);
	}	
	$json_data = array(
			"draw"            => intval( $params['draw'] ),   
			"recordsTotal"    => intval( $totalRecords ),  
			"recordsFiltered" => intval($totalRecords),
			"data"            => $items  // total data array
			);
	echo json_encode($json_data);
}