<?php

/* 	
 * 	Author 	: Ladur
 * 	Tanggal : 01-10-2014	
 * 	Revisi 	: 
 */

class M_shipment extends CI_Model {

    // deklarasi nama table
    public $table = "am_shipment";

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

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

        $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_shipment.TransStatus, am_shipment.ShipmentID, am_shipment.ShipmentName, am_shipment.ShipmentDesc");
        $this->db->from($this->table);

        if ($Code != "") {
            $this->db->like('am_shipment.ShipmentName', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_shipment.ShipmentDesc', $Name);
        }

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

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

    /* function get_all(){
      $this->db->select('*');
      $this->db->from($this->table);
      $query 	= $this->db->get();
      $result = $query->result();

      return $result;
      } */

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

    // Function Join
    // Function Count	
    function check_code_exist($shipment_name) {
        $query = $this->db->get_where($this->table, array('ShipmentName' => $shipment_name), 1, 0);

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

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

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

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

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

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

    function datasend_shipment_suspend($dataSend) {
        $this->db->trans_start();
        $this->suspend($dataSend['data_shipment'], $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) {
        //default status untuk keluarin di list.
        $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_shipment.ShipmentID, am_shipment.ShipmentName, am_shipment.ShipmentDesc,"
            . "(case 
                    when am_shipment.TransStatus = 0 then 'Waiting' 
                    when am_shipment.TransStatus = 1 then 'On Progress' 
                    when am_shipment.TransStatus = 2 then 'Active' 
                    when am_shipment.TransStatus = 3 then 'Suspended' 
                    when am_shipment.TransStatus = 4 then 'Need Mgr. App.' 
                    when am_shipment.TransStatus = 5 then 'Need Dir. App.' 
                    when am_shipment.TransStatus = 6 then 'Need Dir & Mgr App.' end) as Status");
        $this->db->from($this->table);

        if ($Code != "") {
            $this->db->like('am_shipment.ShipmentName', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_shipment.ShipmentDesc', $Name);
        }

        if (!empty($TransStatus)) {
            $this->db->where_in('am_shipment.TransStatus', $TransStatus);
        } else {
            $this->db->where_in('am_shipment.TransStatus', $status);
        }

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

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