<?php
if($userData->l==1){
	$trx = json_decode($formData->trx);
	foreach($trx as $t){
		$t->ID_user=$userData->ID;
		$cek=mysqli_num_rows(mysqli_query($connect, "select id_ from transaksi where ID='".$t->ID."' and email_karyawan='".$t->email_karyawan."' and ID_user='".$t->ID_user."'"));
		if(!$cek){
			$no_struk=mysqli_fetch_array(mysqli_query($connect, "select no_struk from transaksi where ID_user='".$t->ID_user."' and ID_toko='".$t->ID_toko."' order by ABS(no_struk) DESC LIMIT 0,1"));
			$id_txBaru=sprintf('%05d',((int)$no_struk[0])+1);
			$q=mysqli_query($connect, "INSERT INTO `transaksi` (`ID`, `ID_user`, `ID_toko`, `email_karyawan`,`no_struk`, `subtotal`, `diskon`, `pajak`, `grandtotal`, `jenis_bayar`,`bayar`, `kembali`, `waktu`, `status`, `kode_trx`, `validated_trx`)
											 VALUES ('".$t->ID."', '".$t->ID_user."', '".$t->ID_toko."', '".$t->email_karyawan."', '$id_txBaru', '".$t->subtotal."', '".$t->diskon."', '".$t->pajak."', '".$t->grandtotal."','".$t->jenis_bayar."', '".$t->bayar."', '".$t->kembali."', '".$t->waktu."', '".$t->status."', '".$t->kode_trx."', '".$t->validated_trx."');");
			$lid=mysqli_fetch_array(mysqli_query($connect, "select id_ from transaksi where ID='".$t->ID."' and email_karyawan='".$t->email_karyawan."' and ID_user='".$t->ID_user."' order by id_ desc limit 0,1"));
			foreach($t->detail as $d){
				$qd=mysqli_query($connect, "INSERT INTO `transaksi_detail` (`ID_transaksi`, `ID_produk`, `qty`, `total`)
														 VALUES ('$lid[0]', '".$d->ID_produk."', '".$d->qty."', '".$d->total."');");
				$qp=mysqli_query($connect, "UPDATE `produk_stok_toko` SET `stok`=stok-'".$d->qty."' WHERE ID_produk='".$d->ID_produk."' and ID_user='".$t->ID_user."' and ID_toko='".$t->ID_toko."' and `stok`!='-1'");
				$qh=mysqli_query($connect, "UPDATE `produk` SET `hit`=hit+'".$d->qty."' WHERE ID='".$d->ID_produk."' and ID_user='".$t->ID_user."'");
			}
		}
		//validasi
    	$cek=mysqli_query($connect, "select id_ from transaksi where ID='".$t->ID."' and email_karyawan='".$t->email_karyawan."' and ID_user='".$t->ID_user."' order by id_ desc");
    	if(mysqli_num_rows($cek)>=2){
    	    $id=mysqli_fetch_array($cek);
    	    mysqli_query($connect, "delete from transaksi where id_='$id[0]'");
    	    mysqli_query($connect, "delete from transaksi_detail where ID_transaksi='$id[0]'");
    	}
	}
	
	$qt=mysqli_query($connect, "select * from toko where ID_user='".$userData->ID."'") or die(mysqli_error($connect));
	$toko =array();
	while($bt=mysqli_fetch_assoc($qt)){
		$toko[]=$bt;
	}
	$qt=mysqli_query($connect, "select a.*,b.stok,b.ID_toko from produk a, produk_stok_toko b where b.ID_produk=a.ID and b.ID_user=a.ID_user and a.ID_user='".$userData->ID."'") or die(mysqli_error($connect));
	$produk =array();
	while($bt=mysqli_fetch_assoc($qt)){
		$produk[]=$bt;
	}
	
	$qt=mysqli_query($connect, "select * from karyawan a where a.ID_user='".$userData->ID."'") or die(mysqli_error($connect));
	$kasir = array();
	while($bt=mysqli_fetch_assoc($qt)){
		$qtk = mysqli_query($connect, "select b.nama_toko from karyawan_toko a, toko b where a.ID_toko=b.ID and a.ID_karyawan='$bt[ID]' and a.ID_user='".$userData->ID."'");
		$bt['access_toko']=array();
		while($btk=mysqli_fetch_assoc($qtk)){
			$bt['access_toko'][] = $btk;
		}
		unset($bt['ID']);
		unset($bt['password']);
		unset($bt['invitationID']);
		$kasir[]=$bt;
	}
	$qt=mysqli_query($connect, "select * from karyawan_toko a where a.ID_user='".$userData->ID."'") or die(mysqli_error($connect));
	$kasir_toko =array();
	while($bt=mysqli_fetch_assoc($qt)){
		$kasir_toko[]=$bt;
	}
	$trx =array();
	$qt=mysqli_query($connect, "select a.*,b.user_nama as nama_kasir,b.user_email as email,c.nama_toko,c.alamat_toko from transaksi a, users b, toko c where c.ID=a.ID_toko and a.email_karyawan=b.user_email and a.ID_user=b.ID and a.hapus='0' and a.ID_user='".$userData->ID."'") or die(mysqli_error($connect));
	while($bt=mysqli_fetch_assoc($qt)){
		$bt['by']=1;
		$qd=mysqli_query($connect, "select a.*,b.nama_produk,b.harga_jual from transaksi_detail a, produk b where a.ID_produk=b.ID and b.ID_user='".$userData->ID."' and ID_transaksi='$bt[id_]'");
		while($bd=mysqli_fetch_assoc($qd)){
			$bt['detail'][]=$bd;
		}
		$trx[]=$bt;
	}
	$qt=mysqli_query($connect, "select a.*,b.nama as nama_kasir,c.nama_toko,c.alamat_toko from transaksi a, karyawan b, toko c where c.ID=a.ID_toko and a.email_karyawan=b.email and a.ID_user=b.ID_user and a.ID_user='".$userData->ID."'") or die(mysqli_error($connect));
	while($bt=mysqli_fetch_assoc($qt)){
		$bt['by']=2;
		$bt['detail']=array();
		$qd=mysqli_query($connect, "select a.*,b.nama_produk,b.harga_jual from transaksi_detail a, produk b where a.ID_produk=b.ID and b.ID_user='".$userData->ID."' and ID_transaksi='$bt[id_]'");
		while($bd=mysqli_fetch_assoc($qd)){
			$bt['detail'][]=$bd;
		}
		$trx[]=$bt;
	}
	$udata = array('toko'=>$toko,'produk'=>$produk,'customers'=>$customers,'kasir'=>$kasir,'kasir_toko'=>$kasir_toko,'trx'=>$trx);
}elseif($userData->l==2){
	$trx = json_decode($formData->trx);
	foreach($trx as $t){
		$t->ID_user=$userData->ID_user;
		$cek=mysqli_num_rows(mysqli_query($connect, "select id_ from transaksi where ID='".$t->ID."' and email_karyawan='".$t->email_karyawan."' and ID_user='".$t->ID_user."'"));
		if(!$cek){
			$no_struk=mysqli_fetch_array(mysqli_query($connect, "select no_struk from transaksi where ID_user='".$t->ID_user."' and ID_toko='".$t->ID_toko."' order by ABS(no_struk) DESC LIMIT 0,1"));
			$id_txBaru=sprintf('%05d',((int)$no_struk[0])+1);
			$q=mysqli_query($connect, "INSERT INTO `transaksi` (`ID`, `ID_user`, `ID_toko`, `email_karyawan`,`no_struk`, `subtotal`, `diskon`, `pajak`, `grandtotal`, `bayar`, `kembali`, `waktu`, `status`, `kode_trx`, `validated_trx`)
											 VALUES ('".$t->ID."', '".$t->ID_user."', '".$t->ID_toko."', '".$t->email_karyawan."','$id_txBaru', '".$t->subtotal."', '".$t->diskon."', '".$t->pajak."', '".$t->grandtotal."', '".$t->bayar."', '".$t->kembali."', '".$t->waktu."', '".$t->status."', '".$t->kode_trx."', '".$t->validated_trx."');");
			$lid=mysqli_fetch_array(mysqli_query($connect, "select id_ from transaksi where ID='".$t->ID."' and email_karyawan='".$t->email_karyawan."' and ID_user='".$t->ID_user."' order by id_ desc limit 0,1"));
			foreach($t->detail as $d){
				$qd=mysqli_query($connect, "INSERT INTO `transaksi_detail` (`ID_transaksi`, `ID_produk`, `qty`, `total`)
														 VALUES ('$lid[0]', '".$d->ID_produk."', '".$d->qty."', '".$d->total."');");
				$qp=mysqli_query($connect, "UPDATE `produk_stok_toko` SET `stok`=stok-'".$d->qty."' WHERE ID='".$d->ID_produk."' and ID_user='".$t->ID_user."' and ID_toko='".$t->ID_toko."' and `stok`!='-1'");
			}
		}
		//validasi
    	$cek=mysqli_query($connect, "select id_ from transaksi where ID='".$t->ID."' and email_karyawan='".$t->email_karyawan."' and ID_user='".$t->ID_user."' order by id_ desc");
    	if(mysqli_num_rows($cek)>=2){
    	    $id=mysqli_fetch_array($cek);
    	    mysqli_query($connect, "delete from transaksi where id_='$id[0]'");
    	    mysqli_query($connect, "delete from transaksi_detail where ID_transaksi='$id[0]'");
    	}
	}
	
	
	$qt=mysqli_query($connect, "select b.* from karyawan_toko a,toko b where a.ID_user='".$userData->ID_user."' and a.ID_karyawan='".$userData->ID."' and b.ID=a.ID_toko") or die(mysqli_error($connect));
	$toko =array();
	while($bt=mysqli_fetch_assoc($qt)){
		$toko[]=$bt;
	}
	$qt=mysqli_query($connect, "select a.*,b.stok,b.ID_toko from produk a, produk_stok_toko b where b.ID_produk=a.ID and b.ID_user=a.ID_user and a.ID_user='".$userData->ID_user."' and b.ID_toko in (select ID_toko from karyawan_toko where ID_user='".$userData->ID_user."' and ID_karyawan='".$userData->ID."' )") or die(mysqli_error($connect));
	$produk =array();
	while($bt=mysqli_fetch_assoc($qt)){
		$produk[]=$bt;
	}
	
	$qt=mysqli_query($connect, "select a.*,b.nama as nama_kasir,b.email,c.nama_toko,c.alamat_toko from transaksi a, karyawan b, toko c where c.ID=a.ID_toko and a.email_karyawan=b.email and a.ID_user=b.ID_user and a.hapus='0' and a.ID_user='".$userData->ID_user."' and a.email_karyawan='".$userData->email."'") or die(mysqli_error($connect));
	$trx =array();
	while($bt=mysqli_fetch_assoc($qt)){
		$bt['by']=2;
		$qd=mysqli_query($connect, "select a.*,b.nama_produk,b.harga_jual from transaksi_detail a, produk b where a.ID_produk=b.ID and b.ID_user='".$userData->ID_user."' and ID_transaksi='$bt[id_]'");
		while($bd=mysqli_fetch_assoc($qd)){
			$bt['detail'][]=$bd;
		}
		$trx[]=$bt;
	}
	$udata = array('toko'=>$toko,'produk'=>$produk,'trx'=>$trx);
}
$data=array('status'=>1,'data'=>$udata);