<?php
include('config/koneksi2.php');

$select=mysql_query("SELECT a.kode_transaksi,b.id_signal from notif_order a JOIN tab_user b ON a.pemesan=b.user_nama WHERE a.status='pending' and b.id_signal <>'' group BY a.kode_transaksi");

while($row=mysql_fetch_array($select))
{
    $kode_transaksi=$row["kode_transaksi"];
    //echo $kode_transaksi ;
    $query=mysql_query("SELECT CONCAT(GROUP_CONCAT(c1)) as sms,kode,id_signal FROM (SELECT CONCAT(a.nama_sms,'=', SUM(a.kirim),' pcs') c1,a.kode_transaksi as kode,b.id_signal FROM notif_order a INNER JOIN tab_user b ON SUBSTRING(a.pemesan,-9)=SUBSTRING(b.user_nama,-9) where a.kode_transaksi='$kode_transaksi' and a.status='pending' and a.nama_sms <>'' GROUP BY a.nama_sms HAVING SUM(a.kirim) > 0) t");
    	$cek=mysql_num_rows($query);

	$data=mysql_fetch_array($query);
	$sms=$data["sms"];
	$kode=$data["kode"];
	$id_signal=$data["id_signal"];
	
	$deskripsi="ORDER BSIS no. ".$kode.": ".$sms." Sudah Dikirim";

		$content = array(
			"en" => $deskripsi
			);
		
		$fields = array(
			'app_id' => "4072ad35-ce24-48b6-9512-73c03da978dd",
			'include_player_ids' => array($id_signal),
			'data' => array("foo" => "bar"),
			'contents' => $content
		);
		
		$fields = json_encode($fields);
    	//print("\nJSON sent:\n");
    	print($fields);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
												   'Authorization: Basic OTljNDJjNzUtYTAzMS00NWJmLTg2MmEtNGRlNTE2OTE2ZTc4'));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

		$response = curl_exec($ch);
		curl_close($ch);
	//	echo $response;
	//	return $response;
		/*	if($id_signal !="" or $id_signal !="NULL")
	{
				//	$send = sendsms($hp, $deskripsi);
					
						$update=mysql_query("update sms_order set status='terkirim' where kode_transaksi='$kode'");
		}
		else
		{
						$update=mysql_query("update sms_order set status='failed' where kode_transaksi='$kode'");
		}*/
		
		$update=mysql_query("update notif_order set status='terkirim' where kode_transaksi='$kode'");
		$insert=mysql_query("insert into notif_terkirim (isi_notif,id_signal,date) values ('$deskripsi','$id_signal',NOW())");

}
	
?>