<?php

function deposit($reference, $merchant_ref, $status, $payment_method)
{
    $ci = &get_instance();
    $ci->load->helper('fcm');

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

    //PANGGIL TRANSAKSI
    $deposit = $ci->db->query("SELECT * FROM tbl_trx_deposit WHERE trx_deposit_kode='$merchant_ref' AND trx_deposit_tripay_reference='$reference'");
    if ($deposit->num_rows() > 0) {
        foreach ($deposit->result() as $row) {
            $trx_deposit_user_kode = $row->trx_deposit_user_kode;
            $trx_deposit_jumlah = $row->trx_deposit_jumlah;
            $trx_deposit_admin = $row->trx_deposit_admin;
            $trx_deposit_tanggal = $row->trx_deposit_tanggal;
            $trx_deposit_rc = $row->trx_deposit_rc;
        }

        //TRANSAKSI BERHASIL
        if ($status == 'PAID') {
            //JIKA TRANSAKSI MASIH PENDING
            if ($trx_deposit_rc == '0') {
                //UPDATE tbl_trx_deposit
                $ci->db->query("UPDATE tbl_trx_deposit set 
                                trx_deposit_channel='$payment_method',
                                trx_deposit_notasi='Terimakasih sudah melakukan pengisian saldo Arifah Pulsa' ,
                                trx_deposit_rc='1', 
                                trx_deposit_status='Sukses'
                                where
                                trx_deposit_kode='$merchant_ref' and
                                trx_deposit_user_kode='$trx_deposit_user_kode' and
                                trx_deposit_tripay_reference='$reference'");

                //UPDATE STATUS tbl_trx_transaksi
                $ci->db->query("UPDATE tbl_trx_transaksi set 
                                trx_transaksi_notasi='',
                                trx_transaksi_rc='1', 
                                trx_transaksi_status='Sukses'
                                where
                                trx_transaksi_kode='$merchant_ref' and
                                trx_transaksi_user_kode='$trx_deposit_user_kode'");

                //UPDATE tbl_mst_saldo
                $ci->db->query("UPDATE tbl_mst_saldo set 
                                mst_saldo_jumlah=mst_saldo_jumlah + '$trx_deposit_jumlah'
                                where
                                mst_saldo_user_kode='$trx_deposit_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 (
                                '$merchant_ref', 
                                '$trx_deposit_user_kode', 
                                '$trx_transaksi_kategori',
                                '$trx_deposit_admin', 
                                '$trx_deposit_tanggal')");

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

                        //KIRIM NOTIFIKASI
                        $title = "Arifah Pulsa";
                        $body =  "Pengisian saldo Rp " . $trx_deposit_jumlah . " berhasil";
                        firebase_notification($title, $body, $mst_user_firebase);
                    }
                }
            }
        }

        //TRANSAKSI EXPIRED
        if ($status == 'EXPIRED') {
            //JIKA TRANSAKSI MASIH PENDING
            if ($trx_deposit_rc == '0') {
                //PANGGIL TRANSAKSI
                $deposit = $ci->db->query("SELECT * FROM tbl_trx_deposit WHERE trx_deposit_kode='$merchant_ref' AND trx_deposit_tripay_reference='$reference'");
                if ($deposit->num_rows() > 0) {
                    foreach ($deposit->result() as $row) {
                        $trx_deposit_user_kode = $row->trx_deposit_user_kode;
                    }

                    //UPDATE tbl_trx_deposit
                    $ci->db->query("UPDATE tbl_trx_deposit set 
                                trx_deposit_channel='$payment_method',
                                trx_deposit_notasi='Pengisian saldo sudah kadaluarsa. Jika kamu tidak melakukan transaksi 1 jam setelah membuat pengisian saldo, maka akan gagal otomatis.',
                                trx_deposit_rc='2', 
                                trx_deposit_status='Kadaluarsa'
                                where
                                trx_deposit_kode='$merchant_ref' and
                                trx_deposit_user_kode='$trx_deposit_user_kode' and
                                trx_deposit_tripay_reference='$reference'");

                    //UPDATE STATUS tbl_trx_transaksi
                    $ci->db->query("UPDATE tbl_trx_transaksi set 
                                trx_transaksi_notasi='Pengisian saldo sudah kadaluarsa',
                                trx_transaksi_rc='2', 
                                trx_transaksi_status='Kadaluarsa'
                                where
                                trx_transaksi_kode='$merchant_ref' and
                                trx_transaksi_user_kode='$trx_deposit_user_kode'");

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

                            //KIRIM NOTIFIKASI
                            $title = "Arifah Pulsa";
                            $body =  "Pengisian saldo kamu sudah kadaluarsa";
                            firebase_notification($title, $body, $mst_user_firebase);
                        }
                    }
                }
            }
        }

        //TRANSAKSI REFUND
        if ($status == 'REFUND') {
            //JIKA TRANSAKSI MASIH PENDING
            if ($trx_deposit_rc == '0') {
                //PANGGIL TRANSAKSI
                $deposit = $ci->db->query("SELECT * FROM tbl_trx_deposit WHERE trx_deposit_kode='$merchant_ref' AND trx_deposit_tripay_reference='$reference'");
                if ($deposit->num_rows() > 0) {
                    foreach ($deposit->result() as $row) {
                        $trx_deposit_user_kode = $row->trx_deposit_user_kode;
                    }

                    //UPDATE tbl_trx_deposit
                    $ci->db->query("UPDATE tbl_trx_deposit set 
                                trx_deposit_channel='$payment_method',
                                trx_deposit_notasi='Pengisian saldo gagal. Jika kamu tidak melakukan transaksi 1 jam setelah membuat pengisian saldo, maka akan gagal otomatis.',
                                trx_deposit_rc='2', 
                                trx_deposit_status='Refund'
                                where
                                trx_deposit_kode='$merchant_ref' and
                                trx_deposit_user_kode='$trx_deposit_user_kode' and
                                trx_deposit_tripay_reference='$reference'");

                    //UPDATE STATUS tbl_trx_transaksi
                    $ci->db->query("UPDATE tbl_trx_transaksi set 
                                trx_transaksi_notasi='Pengisian saldo gagal',
                                trx_transaksi_rc='2', 
                                trx_transaksi_status='Refund'
                                where
                                trx_transaksi_kode='$merchant_ref' and
                                trx_transaksi_user_kode='$trx_deposit_user_kode'");

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

                            //KIRIM NOTIFIKASI
                            $title = "Arifah Pulsa";
                            $body =  "Pengisian saldo kamu gagal, saldo di refund ke sumber pembayaran";
                            firebase_notification($title, $body, $mst_user_firebase);
                        }
                    }
                }
            }
        }

        //TRANSAKSI FAILED
        if ($status == 'FAILED') {
            //JIKA TRANSAKSI MASIH PENDING
            if ($trx_deposit_rc == '0') {
                //PANGGIL TRANSAKSI
                $deposit = $ci->db->query("SELECT * FROM tbl_trx_deposit WHERE trx_deposit_kode='$merchant_ref' AND trx_deposit_tripay_reference='$reference'");
                if ($deposit->num_rows() > 0) {
                    foreach ($deposit->result() as $row) {
                        $trx_deposit_user_kode = $row->trx_deposit_user_kode;
                    }

                    //UPDATE tbl_trx_deposit
                    $ci->db->query("UPDATE tbl_trx_deposit set 
                                trx_deposit_channel='$payment_method',
                                trx_deposit_notasi='Pengisian saldo gagal. Jika kamu tidak melakukan transaksi 1 jam setelah membuat pengisian saldo, maka akan gagal otomatis.',
                                trx_deposit_rc='3', 
                                trx_deposit_status='Gagal'
                                where
                                trx_deposit_kode='$merchant_ref' and
                                trx_deposit_user_kode='$trx_deposit_user_kode' and
                                trx_deposit_tripay_reference='$reference'");

                    //UPDATE STATUS tbl_trx_transaksi
                    $ci->db->query("UPDATE tbl_trx_transaksi set 
                                trx_transaksi_notasi='Pengisian saldo gagal',
                                trx_transaksi_rc='3', 
                                trx_transaksi_status='Gagal'
                                where
                                trx_transaksi_kode='$merchant_ref' and
                                trx_transaksi_user_kode='$trx_deposit_user_kode'");

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

                            //KIRIM NOTIFIKASI
                            $title = "Arifah Pulsa";
                            $body =  "Pengisian saldo kamu sudah gagal";
                            firebase_notification($title, $body, $mst_user_firebase);
                        }
                    }
                }
            }
        }

        $respon['success'] = true;
        echo json_encode($respon);
    }
}
