<?php

/* 	
 * 	Author 	: Ladur / Tanggal : 01-10-2014	
 * 	Revisi 	: Ferry / Tanggal : 11-04-2015
 */

class M_taxes extends CI_Model {

    public $table = "am_taxes";

    function get_all() {
        //default status untuk keluarin di list.
        $status = array(0, 1);

        /* ===========================declar post by find================================================ */
        $Code = $this->input->post('TaxesCode') ? $this->input->post('TaxesCode') : '';
        $Name = $this->input->post('TaxesName') ? $this->input->post('TaxesName') : '';

        $TransStatus = $this->input->post('TransStatus') ? $this->input->post('TransStatus') : '';
        /* ========================================end================================================== */

        /* ======================================scrol view 1-150======================================= */
        $page = ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
        $rows = ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;

        $beginning = ($page - 1) * $rows;
        $end = $page * $rows;
        /* ============================================================================================== */

        $this->db->select("am_taxes.TransStatus, am_taxes.TaxesCode, am_taxes.TaxesName, am_taxes.TaxesRate, am_taxes.TaxesDesc");
        $this->db->from($this->table);

        if ($Code != "") {
            $this->db->like('am_taxes.TaxesCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_taxes.TaxesName', $Name);
        }

        if ($TransStatus != '') {
            if ($TransStatus == 0 || $TransStatus == 1) {
                $this->db->where_in('am_taxes.TransStatus', $status);
            } else {
                $this->db->where_in('am_taxes.TransStatus', $TransStatus);
            }
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('am_taxes.TransStatus', $status);
        }

        $query = $this->db->order_by('am_taxes.TaxesCode', 'ASC');
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function get_by_code($taxes_code) {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('TaxesCode', $taxes_code);
        $query = $this->db->get();

        return $query->row();
    }

    function get_detail($id) {
        $query = $this->db->get_where($this->table, array('TaxesCode' => $id));
        return $query->row();
    }

    function check_code_exist($id) {
        $query = $this->db->get_where($this->table, array('TaxesCode' => $id), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function insert($data) {
//        $this->db->trans_begin();
        $this->db->insert($this->table, $data);
//		
//        if ($this->db->trans_status() == FALSE) {
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function update($data, $id) {
//        $this->db->trans_begin();
        $this->db->update($this->table, $data, array('TaxesCode' => $id));
//		
//        if ($this->db->trans_status() == FALSE) {
//            return 0;
//        }
//        else {
//            $this->db->trans_commit();
//            return 1;
//        }
    }

    function delete($id) {
//        $this->db->trans_begin();
        $this->db->delete($this->table, array('TaxesCode' => $id));
//		
//        if ($this->db->trans_status() == FALSE) {
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function suspended($id) {
//        $this->db->trans_begin();
        $this->db->set('TransStatus', 3);
        $this->db->where('TaxesCode', $id);
        $this->db->update($this->table);

//        if ($this->db->trans_status() == FALSE) {
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    //============= DHIDHIK, 28 Maret 2016 ===============================//
    function datasend_taxes_insert($dataSend) {
        $this->db->trans_start();
        $this->insert($dataSend['data_taxes']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function datasend_taxes_update($dataSend) {
        $this->db->trans_start();
        $this->update($dataSend['data_taxes'], $dataSend['idcode']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function datasend_taxes_delete($dataSend) {
        $this->db->trans_start();
        $this->delete($dataSend['idcode']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function datasend_taxes_suspend($dataSend) {
        $this->db->trans_start();
        $this->suspended($dataSend['idcode']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function get_all_to_print($data_filter) {
        $status = array(0, 1);
        /* ===========================declar post by find================================================ */
        $Code           = $data_filter['txt_no'];
        $Name           = $data_filter['txt_desc'];
        $TransStatus    = $data_filter['chk_TransStatus'];
        /* ========================================end================================================== */

        $this->db->select("am_taxes.TransStatus, am_taxes.TaxesCode, am_taxes.TaxesName, am_taxes.TaxesRate, am_taxes.TaxesDesc");
        $this->db->from($this->table);

        if ($Code != "") {
            $this->db->like('am_taxes.TaxesCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_taxes.TaxesName', $Name);
        }

        if (!empty($TransStatus)) {
            if ($TransStatus == 0 || $TransStatus == 1) {
                $this->db->where_in('am_taxes.TransStatus', $status);
            } else {
                $this->db->where_in('am_taxes.TransStatus', $TransStatus);
            }
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('am_taxes.TransStatus', $status);
        }

        $query = $this->db->order_by('am_taxes.TaxesCode', 'ASC');
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    //============= End DHIDHIK, 28 Maret 2016 ===============================//
}
