<?php

function unipin($ref_id, $rc, $status, $sn)
{
    $ci = &get_instance();
    $ci->load->helper('fcm');

    //UNIPIN
    if (strtok($ref_id, "-") == "UNIPIN") {

        //KATEGORI TRANSAKSI
        $trx_transaksi_kategori =  $ci->config->item('transaksi_unipin');

        //PANGGIL TRANSAKSI
        $unipin = $ci->db->query("SELECT * FROM tbl_trx_unipin WHERE trx_unipin_kode='$ref_id'");
        if ($unipin->num_rows() > 0) {
            foreach ($unipin->result() as $row) {
                $trx_unipin_user_kode = $row->trx_unipin_user_kode;
                $trx_unipin_nomor = $row->trx_unipin_nomor;
                $trx_unipin_nama = $row->trx_unipin_nama;
                $trx_unipin_admin = $row->trx_unipin_admin;
                $trx_unipin_total = $row->trx_unipin_harga + $row->trx_unipin_admin;
                $trx_unipin_poin = $row->trx_unipin_poin;
                $trx_unipin_tanggal = $row->trx_unipin_tanggal;
                $trx_unipin_rc = $row->trx_unipin_rc;
            }

            //TRANSAKSI BERHASIL
            if ($rc == '00') {
                //JIKA TRANSAKSI MASIH PENDING
                if ($trx_unipin_rc == '03') {
                    //UPDATE tbl_trx_unipin
                    $ci->db->query("UPDATE tbl_trx_unipin set 
                        trx_unipin_rc='$rc',
                        trx_unipin_status='$status',
                        trx_unipin_notasi='',
                        trx_unipin_sn='$sn'
                        where
                        trx_unipin_kode='$ref_id' and
                        trx_unipin_user_kode='$trx_unipin_user_kode'");

                    //UPDATE STATUS tbl_trx_transaksi
                    $ci->db->query("UPDATE tbl_trx_transaksi set 
                        trx_transaksi_notasi='',
                        trx_transaksi_rc='$rc', 
                        trx_transaksi_status='$status'
                        where
                        trx_transaksi_kode='$ref_id' and
                        trx_transaksi_user_kode='$trx_unipin_user_kode'");


                    //INSERT KE tbl_trx_pendapatan
                    $ci->db->query("insert into tbl_trx_pendapatan (
                        trx_pendapatan_kode, 
                        trx_pendapatan_user_kode, 
                        trx_pendapatan_kategori, 
                        trx_pendapatan_jumlah, 
                        trx_pendapatan_tanggal) values (
                        '$ref_id', 
                        '$trx_unipin_user_kode', 
                        '$trx_transaksi_kategori',
                        '$trx_unipin_admin', 
                        '$trx_unipin_tanggal')");


                    //INSERT KE tbl_trx_poin
                    $ci->db->query("insert into tbl_trx_poin (
                        trx_poin_kode, 
                        trx_poin_user_kode, 
                        trx_poin_nama, 
                        trx_poin_keterangan, 
                        trx_poin_jumlah,
                        trx_poin_tanggal) values (
                        '$ref_id', 
                        '$trx_unipin_user_kode', 
                        '$trx_transaksi_kategori',
                        'Poin dari transaksi unipin', 
                        '$trx_unipin_poin', 
                        '$trx_unipin_tanggal')");

                    //UPDATE tbl_mst_poin
                    $ci->db->query("UPDATE tbl_mst_poin set 
                        mst_poin_jumlah=mst_poin_jumlah + '$trx_unipin_poin'
                        where
                        mst_poin_user_kode='$trx_unipin_user_kode'");


                    //PANGGIL USER
                    $user = $ci->db->query("SELECT * FROM tbl_mst_user WHERE mst_user_kode='$trx_unipin_user_kode'");
                    if ($user->num_rows() > 0) {
                        foreach ($user->result() as $row) {
                            $mst_user_firebase = $row->mst_user_firebase;
                            $mst_user_upline = $row->mst_user_upline;
                        }

                        //PANGGIL UPLINE
                        $upline = $ci->db->query("SELECT * FROM tbl_mst_user WHERE mst_user_kode='$mst_user_upline'");
                        if ($upline->num_rows() > 0) {
                            foreach ($upline->result() as $row) {
                                $mst_user_referal_bonus = $row->mst_user_referal_bonus;
                            }

                            //CEK UPLINE
                            if (strlen($mst_user_upline) > 0) {
                                //UPLINE BONUS
                                //INSERT KE tbl_trx_poin
                                $ci->db->query("insert into tbl_trx_poin (
                                          trx_poin_kode, 
                                          trx_poin_user_kode, 
                                          trx_poin_nama, 
                                          trx_poin_keterangan, 
                                          trx_poin_jumlah,
                                          trx_poin_tanggal) values (
                                          '$ref_id', 
                                          '$mst_user_upline', 
                                          'Downline',
                                          'Bonus dari transaksi downline', 
                                          '$mst_user_referal_bonus', 
                                          '$trx_unipin_tanggal')");

                                //UPDATE tbl_mst_poin
                                $ci->db->query("UPDATE tbl_mst_poin set 
                                          mst_poin_jumlah=mst_poin_jumlah + '$mst_user_referal_bonus'
                                          where
                                          mst_poin_user_kode='$mst_user_upline'");
                            }
                        }

                        //KIRIM NOTIFIKASI
                        $title = "Arifah Pulsa";
                        $body =  "Pengisian unipin berhasil ke nomor " . $trx_unipin_nomor . " - " . $trx_unipin_nama;
                        firebase_notification($title, $body, $mst_user_firebase);
                    }
                }
            } else {
                //TRANSAKSI GAGAL
                //JIKA TRANSAKSI MASIH PENDING
                if ($trx_unipin_rc == '03') {
                    //UPDATE tbl_trx_unipin
                    $ci->db->query("UPDATE tbl_trx_unipin set 
                    trx_unipin_rc='$rc',
                    trx_unipin_status='$status',
                    trx_unipin_notasi='Pembelian unipin gagal, saldo kamu telah dikembalikan!',
                    trx_unipin_sn='-'
                    where
                    trx_unipin_kode='$ref_id' and
                    trx_unipin_user_kode='$trx_unipin_user_kode'");

                    //UPDATE STATUS tbl_trx_transaksi
                    $ci->db->query("UPDATE tbl_trx_transaksi set 
                    trx_transaksi_notasi='Pembelian unipin gagal, saldo kamu telah dikembalikan!',
                    trx_transaksi_rc='$rc', 
                    trx_transaksi_status='$status'
                    where
                    trx_transaksi_kode='$ref_id' and
                    trx_transaksi_user_kode='$trx_unipin_user_kode'");


                    //UPDATE tbl_mst_saldo
                    $ci->db->query("UPDATE tbl_mst_saldo set 
                    mst_saldo_jumlah=mst_saldo_jumlah + '$trx_unipin_total'
                    where
                    mst_saldo_user_kode='$trx_unipin_user_kode'");

                    //PANGGIL USER
                    $user = $ci->db->query("SELECT * FROM tbl_mst_user WHERE mst_user_kode='$trx_unipin_user_kode'");
                    if ($user->num_rows() > 0) {
                        foreach ($user->result() as $row) {
                            $mst_user_firebase = $row->mst_user_firebase;
                            $mst_user_referal_bonus = $row->mst_user_referal_bonus;
                            $mst_user_upline = $row->mst_user_upline;
                        }

                        //KIRIM NOTIFIKASI
                        $title = "Arifah Pulsa";
                        $body =  "Pengisian unipin gagal ke nomor " . $trx_unipin_nomor . " - " . $trx_unipin_nama . ". Saldo telah dikembalikan ke akun kamu";
                        firebase_notification($title, $body, $mst_user_firebase);
                    }
                }
            }
        }
    }
}
