<?php
date_default_timezone_set('Asia/Jakarta');
include "../config/koneksi.php";
include "../config/rupiah.php";
include "../config/library.php";

//url tagihan
$page_URL = (@$_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);
//identitas
$idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas LIMIT 1"));
$link_send = $idt['link_one_sender'];
$token_send = $idt['token'];
//tahun ajaran
$t = mysqli_query($conn, "SELECT idTahunAjaran as ta FROM tahun_ajaran WHERE aktif = 'Y'");
$ta = mysqli_fetch_array($t);
$thn_ajar = $ta['ta'];

$headers = array();
$headers[] = $token_send;
$headers[] = 'Content-Type: application/x-www-form-urlencoded';

if (isset($_POST['simpan_bulanan'])) {
	$tglBayar = date("Y-m-d H:i:s");
	$tgl = date('Y-m-d');


	$query = mysqli_query($conn, "UPDATE tagihan_bulanan SET  tglBayar='$tglBayar', tglUpdate='$_POST[tanggal_bayar]', statusBayar='1' , caraBayar='$_POST[caraBayar]' WHERE idTagihanBulanan='$_POST[id_tagihan_bulanan]'");


	$a = mysqli_query($conn, "SELECT nmBulan as Bulan,tagihan_bulanan.idSiswa as ids, nmSiswa as nama, noHpOrtu as hpo, noHpSis as hps, jumlahBayar as tagihan FROM siswa 
						 INNER JOIN tagihan_bulanan ON siswa.idSiswa = tagihan_bulanan.idSiswa 
						 INNER JOIN bulan ON tagihan_bulanan.idBulan = bulan.idBulan         
						 WHERE idTagihanBulanan='$_POST[id_tagihan_bulanan]'         
                   		");

	$re = mysqli_fetch_array($a);
	$sis = $re['ids'];
	$tagihan = $re['tagihan'];
	$siswa = $re['nama'];
	$hpo = $re['hpo'];
	$hps = $re['hps'];
	$bulan = $re['Bulan'];
	$link_url_tagihan = "$page_URL$_SERVER[HTTP_HOST]/" . $uri_segments[1] . '/kwitansi.php?tahun=' . $thn_ajar . '%26tgl=' . $tgl . '%26siswa=' . $sis;


	$msg_par = 'Assalamualaikum, Terima Kasih pembayaran Tagihan bulanan untuk

Bulan: ' . $bulan . ' 
Jumlah: *' . str_replace(".", ",", buatRp($tagihan)) . '* 
Nama: *' . $siswa . '*

*Lunas* 
Terima kasih. 
	
Download kwitansi : ' . $link_url_tagihan . '';
	$msg_kid = 'Assalamualaikum, Terima Kasih pembayaran Tagihan bulanan untuk 
	
Bulan: ' . $bulan . ' 
Jumlah: *' . str_replace(".", ",", buatRp($tagihan)) . '* 
Nama: *' . $siswa . '*

*Lunas* 
Terima kasih. 
	
Download kwitansi : ' . $link_url_tagihan . '';


	// par
	$phone = $hpo;
	$apikey = $token_send;
	$tujuan = $phone;
	$pesan = $msg_par;

	$curl = curl_init();

	curl_setopt_array($curl, array(
		CURLOPT_URL => 'https://starsender.online/api/sendText?message=' . rawurlencode($pesan) . '&tujuan=' . rawurlencode($tujuan . '@s.whatsapp.net'),
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_HTTPHEADER => array(
			'apikey: ' . $apikey
		),
	));

	$response = curl_exec($curl);

	curl_close($curl);
	
	//kid
	$phone1 = $hps;
	$apikey = $token_send;
	$tujuan = $phone1;
	$pesan = $msg_kid;

	$curl2 = curl_init();

	curl_setopt_array($curl2, array(
		CURLOPT_URL => 'https://starsender.online/api/sendText?message=' . rawurlencode($pesan) . '&tujuan=' . rawurlencode($tujuan . '@s.whatsapp.net'),
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_HTTPHEADER => array(
			'apikey: ' . $apikey
		),
	));

	$response = curl_exec($curl2);

	curl_close($curl2);


	header('Location: ' . $_POST['uri']);
} else {
	header('Location: ../index.php?view=pembayaran');
}
