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

$now = date('m');
$b = mysqli_query($conn, "SELECT nmBulan as bulan, urutan as urt, idBulan as id_bln FROM bulan WHERE idBulan = $now");
$bl = mysqli_fetch_array($b);
$id_bln = $bl['id_bln'];
$bulan = $bl['bulan'];
$urut_bln = $bl['urt'];

//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'];

//url tagihan
$page_URL = (@$_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);

//whatsapp api
$idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas LIMIT 1"));
$link_send = $idt['link_one_sender'];
$token_send = $idt['token'];

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

if (isset($_POST['send'])) {

    $lst_siswa = mysqli_query($conn, "SELECT * FROM siswa ");
    while ($sws = mysqli_fetch_array($lst_siswa)) {

        $link_url_tagihan = "$page_URL$_SERVER[HTTP_HOST]/" . 'laporan_tagihan_siswa.php?tahun=' . $thn_ajar . '&siswa=' . $sws['idSiswa'];
        $rincian_tagihan = '';
        $total_tagihan = 0;
        $footer = 'Balas Ya atau Tidak';
        $button1 = 'Iya';
        $button2 = 'Tidak';
        $no = 1;

        $msg_wa = array();
        $number_wa = array();

        // tagihan bulan 
        $tag_bln = mysqli_query($conn, "SELECT tagihan_bulanan.idSiswa, tagihan_bulanan.jumlahBayar,
                                  jenis_bayar.idPosBayar, 
                                  jenis_bayar.nmJenisBayar, 
                                  tahun_ajaran.nmTahunAjaran,
                                  pos_bayar.nmPosBayar,
                                  bulan.nmBulan,
                                  bulan.urutan
                          FROM tagihan_bulanan 
                          LEFT JOIN jenis_bayar ON tagihan_bulanan.idJenisBayar = jenis_bayar.idJenisBayar
                          LEFT JOIN tahun_ajaran ON jenis_bayar.idTahunAjaran = tahun_ajaran.idTahunAjaran
                          LEFT JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
                          LEFT JOIN bulan ON tagihan_bulanan.idBulan = bulan.idBulan
                          WHERE tagihan_bulanan.idSiswa='$sws[idSiswa]' AND jenis_bayar.idTahunAjaran<='$thn_ajar' AND tagihan_bulanan.statusBayar='0' AND bulan.urutan<='$urut_bln'
                          order by bulan.urutan asc ");
        while ($tBln = mysqli_fetch_array($tag_bln)) {
            if ($tBln['jumlahBayar'] <> 0) {
                $pisah_TA = explode('/', $tBln['nmTahunAjaran']);
                if ($tBln['urutan'] <= 6) {
                    $nmBulan = $tBln['nmBulan'] . ' ' . $pisah_TA[0];
                } else {
                    $nmBulan = $tBln['nmBulan'] . ' ' . $pisah_TA[1];
                }
                $rincian_tagihan = $rincian_tagihan . $no++ . ". " . $tBln['nmJenisBayar'] . " - T.A" . $tBln['nmTahunAjaran'] . " - (" . $nmBulan . ") => *" . str_replace('.', ',', buatRp($tBln['jumlahBayar'])) . "*
";
                $total_tagihan += $tBln['jumlahBayar'];
            }
        }

        // tagihan bebas
        $tag_bebas = mysqli_query($conn, "SELECT tagihan_bebas.*, 
                                  SUM(tagihan_bebas.totalTagihan) as totalTagihanBebas, 
                                  jenis_bayar.idPosBayar, 
                                  jenis_bayar.nmJenisBayar, 
                                  tahun_ajaran.nmTahunAjaran,
                                  pos_bayar.nmPosBayar,
                                  bulan.nmBulan,
                                  bulan.urutan
                          FROM tagihan_bebas 
                          LEFT JOIN jenis_bayar ON tagihan_bebas.idJenisBayar = jenis_bayar.idJenisBayar
                          LEFT JOIN tahun_ajaran ON jenis_bayar.idTahunAjaran = tahun_ajaran.idTahunAjaran
                          LEFT JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
                          LEFT JOIN bulan ON tagihan_bebas.idBulan = bulan.idBulan
                          WHERE tagihan_bebas.idSiswa='$sws[idSiswa]' AND jenis_bayar.idTahunAjaran<='$thn_ajar' AND tagihan_bebas.statusBayar!='1' AND bulan.urutan<='$urut_bln'
                          GROUP BY tagihan_bebas.idJenisBayar order by bulan.urutan asc");

        while ($tBbs = mysqli_fetch_array($tag_bebas)) {
            if ($tBbs['totalTagihanBebas'] <> 0) {
                $pisah_TA = explode('/', $tBbs['nmTahunAjaran']);
                if ($tBbs['urutan'] <= 6) {
                    $nmBulan = $tBbs['nmBulan'] . ' ' . $pisah_TA[0];
                } else {
                    $nmBulan = $tBbs['nmBulan'] . ' ' . $pisah_TA[1];
                }
                $bayar_bebas = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(jumlahBayar) as totalBayarBebas FROM tagihan_bebas_bayar WHERE idTagihanBebas='$tBbs[idTagihanBebas]'"));
                $sisa_tag_bebas = $tBbs['totalTagihanBebas'] - $bayar_bebas['totalBayarBebas'];
                if ($sisa_tag_bebas <> 0) {
                    $rincian_tagihan = $rincian_tagihan . $no++ . ". " . $tBbs['nmJenisBayar'] . " - T.A" . $tBbs['nmTahunAjaran'] . " - (" . $nmBulan . ") => *" . str_replace('.', ',', buatRp($sisa_tag_bebas)) . "*
";
                    $total_tagihan += $sisa_tag_bebas;
                }
            }
        }

        if ($total_tagihan <> 0) {
            //pesan whatsapp siswa 
            $number_wa[] = $sws['noHpSis'];
            $msg_wa[] = 'Assalamualaikum, Harap menyelesaikan pembayaran Tagihan 
            
Jumlah:  *' . str_replace(".", ",", buatRp($total_tagihan)) . '* 
Nama Siswa: *' . $sws['nmSiswa'] . '*
dengan rincian di bawah ini:
            
' . $rincian_tagihan . '
Download Tagihan : ' . $link_url_tagihan . '

Apakah anda bersedia membayar ?';

            //pesan whatsapp ortu 
            $number_wa[] = $sws['noHpOrtu'];
            $msg_wa[] = 'Assalamualaikum, Harap menyelesaikan pembayaran Tagihan 
Jumlah: *' . str_replace(".", ",", buatRp($total_tagihan)) . '* 
Nama Siswa: *' . $sws['nmSiswa'] . '*
dengan rincian di bawah ini:
            
' . $rincian_tagihan . '
Download Tagihan : ' . $link_url_tagihan . '

Apakah anda bersedia membayar ?';

            for ($i = 0; $i < count($number_wa); $i++) {

                $apikey = $token_send;
                $tujuan = $number_wa[$i];
                $pesan = $msg_wa[$i];
                $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);
                echo   '<script>document.location="index.php?view=whatsapp&sukses";</script><br>';
            }
        }
    }
}


?>

<section class="content">
    <div class="col-xs-12 ">
        <div class="box box-solid">
            <div class="box-header with-border">
                <h3 class="box-title"> </h3>
                <form class="form-horizontal" method="POST">
                    <div class="box-body">
                        <?php
                        if (isset($_GET['sukses'])) {
                            echo "<div class='alert alert-info alert-dismissible fade in' role='alert'> 
					<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
					<span aria-hidden='true'>×</span></button> <strong>Sukses!</strong> - Pesan telah terkirim,..
                    </div>";
                        } elseif (isset($_GET['gagal'])) {
                            echo "<div class='alert alert-danger alert-dismissible fade in' role='alert'> 
					<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
					<span aria-hidden='true'>×</span></button> <strong>Gagal!</strong> - Data tidak Di Proses, terjadi kesalahan dengan data..
					</div>";
                        }
                        ?>
                        <div class="form-group">
                            <div class="col-sm-12 ">
                                <p>Tombol dibawah ini untuk memberikan notif kepada siswa dan orang tua, bagi siswa yang memiliki tanggungan pembayaran hinggan bulan ini</p>
                                <button type="submit" name="send" class="btn btn-danger pull-left">Kirim</button>
                            </div>
                        </div>
                    </div>

                </form>
            </div>
        </div>
    </div>

</section>