<?php

class Model_transaksi_gas extends CI_Model
{

    var $column_order = array('mst_user_kode', 'mst_user_nama_lengkap', 'trx_gas_tanggal');
    var $column_search = array(
        'trx_gas_kode', 'trx_gas_user_kode',
        'trx_gas_nomor',  'trx_gas_nama', 'trx_gas_tanggal', 'mst_user_nama_lengkap'
    );
    var $order = array('trx_gas_tanggal' => 'desc');

    //FILTER BERDASARKAN TANGGAL
    private function _get_datatables_query_tanggal($tanggal_awal, $tanggal_akhir)
    {
        $this->load->database('db_arifahpulsa_api', true);
        $this->db_arifahpulsa_api = $this->load->database('db_arifahpulsa_api', true);

        $this->db_arifahpulsa_api->select("mst_user_nama_lengkap,
                                        trx_gas_kode,trx_gas_user_kode,
                                        trx_gas_nomor,trx_gas_nama,
                                        trx_gas_harga,trx_gas_admin,
                                        trx_gas_lembar_tagihan,
                                        trx_gas_periode,
                                        trx_gas_meter_awal,trx_gas_meter_akhir,
                                        trx_gas_sn,
                                        trx_gas_keterangan,trx_gas_notasi,trx_gas_tanggal,
                                        trx_gas_icon,trx_gas_rc,trx_gas_status")
            ->from("tbl_trx_gas, tbl_mst_user")
            ->where("trx_gas_user_kode=mst_user_kode
                    and trx_gas_tanggal BETWEEN '$tanggal_awal 00:00:00' AND '$tanggal_akhir 23:59:59'");

        $i = 0;
        foreach ($this->column_search as $item) // looping awal
        {
            if ($_POST['search']['value']) {
                if ($i === 0) {
                    $this->db_arifahpulsa_api->group_start();
                    $this->db_arifahpulsa_api->like($item, $_POST['search']['value']);
                } else {
                    $this->db_arifahpulsa_api->or_like($item, $_POST['search']['value']);
                }

                if (count($this->column_search) - 1 == $i)
                    $this->db_arifahpulsa_api->group_end();
            }
            $i++;
        }

        if (isset($_POST['order'])) {
            $this->db_arifahpulsa_api->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else if (isset($this->order)) {
            $order = $this->order;
            $this->db_arifahpulsa_api->order_by(key($order), $order[key($order)]);
        }
    }

    function get_datatables_tanggal($tanggal_awal, $tanggal_akhir)
    {
        $this->_get_datatables_query_tanggal($tanggal_awal, $tanggal_akhir);
        if ($_POST['length'] != -1) {
            $this->db_arifahpulsa_api->limit($_POST['length'], $_POST['start']);
            $query = $this->db_arifahpulsa_api->get();
        }
        return $query->result();
    }


    function count_filtered_tanggal($tanggal_awal, $tanggal_akhir)
    {
        $this->_get_datatables_query_tanggal($tanggal_awal, $tanggal_akhir);
        $query =  $this->db_arifahpulsa_api->get();
        return $query->num_rows();
    }

    public function count_all_tanggal($tanggal_awal, $tanggal_akhir)
    {
        $this->db_arifahpulsa_api->select("mst_user_nama_lengkap,
                                        trx_gas_kode,trx_gas_user_kode,
                                        trx_gas_nomor,trx_gas_nama,
                                        trx_gas_harga,trx_gas_admin,
                                        trx_gas_lembar_tagihan,
                                        trx_gas_periode,
                                        trx_gas_meter_awal,trx_gas_meter_akhir,
                                        trx_gas_sn,
                                        trx_gas_keterangan,trx_gas_notasi,trx_gas_tanggal,
                                        trx_gas_icon,trx_gas_rc,trx_gas_status")
            ->from("tbl_trx_gas, tbl_mst_user")
            ->where("trx_gas_user_kode=mst_user_kode
                    and trx_gas_tanggal BETWEEN '$tanggal_awal 00:00:00' AND '$tanggal_akhir 23:59:59'");
        return $this->db_arifahpulsa_api->count_all_results();
    }


    //FILTER BERDASARKAN PENCARIAN
    private function _get_datatables_query_cari($txt_cari)
    {
        $this->load->database('db_arifahpulsa_api', true);
        $this->db_arifahpulsa_api = $this->load->database('db_arifahpulsa_api', true);

        $this->db_arifahpulsa_api->select("mst_user_nama_lengkap,
                                        trx_gas_kode,trx_gas_user_kode,
                                        trx_gas_nomor,trx_gas_nama,
                                        trx_gas_harga,trx_gas_admin,
                                        trx_gas_lembar_tagihan,
                                        trx_gas_periode,
                                        trx_gas_meter_awal,trx_gas_meter_akhir,
                                        trx_gas_sn,
                                        trx_gas_keterangan,trx_gas_notasi,trx_gas_tanggal,
                                        trx_gas_icon,trx_gas_rc,trx_gas_status")
            ->from("tbl_trx_gas, tbl_mst_user")
            ->where("trx_gas_user_kode=mst_user_kode and 
                    trx_gas_kode LIKE '%$txt_cari%'
                    OR
                    trx_gas_user_kode=mst_user_kode and 
                    trx_gas_nama LIKE '%$txt_cari%'
                    OR
                    trx_gas_user_kode=mst_user_kode and 
                    trx_gas_status LIKE '%$txt_cari%'
                    OR
                    trx_gas_user_kode=mst_user_kode and 
                    trx_gas_nomor LIKE '%$txt_cari%'");

        $i = 0;
        foreach ($this->column_search as $item) // looping awal
        {
            if ($_POST['search']['value']) {
                if ($i === 0) {
                    $this->db_arifahpulsa_api->group_start();
                    $this->db_arifahpulsa_api->like($item, $_POST['search']['value']);
                } else {
                    $this->db_arifahpulsa_api->or_like($item, $_POST['search']['value']);
                }

                if (count($this->column_search) - 1 == $i)
                    $this->db_arifahpulsa_api->group_end();
            }
            $i++;
        }

        if (isset($_POST['order'])) {
            $this->db_arifahpulsa_api->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else if (isset($this->order)) {
            $order = $this->order;
            $this->db_arifahpulsa_api->order_by(key($order), $order[key($order)]);
        }
    }

    function get_datatables_cari($txt_cari)
    {
        $this->_get_datatables_query_cari($txt_cari);
        if ($_POST['length'] != -1) {
            $this->db_arifahpulsa_api->limit($_POST['length'], $_POST['start']);
            $query = $this->db_arifahpulsa_api->get();
        }
        return $query->result();
    }


    function count_filtered_cari($txt_cari)
    {
        $this->_get_datatables_query_cari($txt_cari);
        $query =  $this->db_arifahpulsa_api->get();
        return $query->num_rows();
    }

    public function count_all_cari($txt_cari)
    {
        $this->db_arifahpulsa_api->select("mst_user_nama_lengkap,
                                        trx_gas_kode,trx_gas_user_kode,
                                        trx_gas_nomor,trx_gas_nama,
                                        trx_gas_harga,trx_gas_admin,
                                        trx_gas_lembar_tagihan,
                                        trx_gas_periode,
                                        trx_gas_meter_awal,trx_gas_meter_akhir,
                                        trx_gas_sn,
                                        trx_gas_keterangan,trx_gas_notasi,trx_gas_tanggal,
                                        trx_gas_icon,trx_gas_rc,trx_gas_status")
            ->from("tbl_trx_gas, tbl_mst_user")
            ->where("trx_gas_user_kode=mst_user_kode and 
                    trx_gas_kode LIKE '%$txt_cari%'
                    OR
                    trx_gas_user_kode=mst_user_kode and 
                    trx_gas_nama LIKE '%$txt_cari%'
                    OR
                    trx_gas_user_kode=mst_user_kode and 
                    trx_gas_status LIKE '%$txt_cari%'
                    OR
                    trx_gas_user_kode=mst_user_kode and 
                    trx_gas_nomor LIKE '%$txt_cari%'");
        return $this->db_arifahpulsa_api->count_all_results();
    }
}
