<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/* 	
 * 	Author 	: Ladur
 * 	Tanggal : 18-11-2014
 * 	Revisi 	: 
 */

class Inventories extends CI_Controller {

    // inisialisasi manual
    public $menu_id = 265;
    public $code = "Item Code";
    public $name = "Item Name";

    public function __construct() {
        parent::__construct();
        $this->load->helper('regex');
        $this->load->helper('message');
        $this->load->helper('status');
        $this->load->helper('custom_string_helper');
        $this->load->model('m_menu');
        $this->load->model('m_inventories');
        $this->load->model('m_items');
        $this->load->model('custom/m_custom');
        $this->load->model('setup/m_branchinfo');
		
		$this->load->model('login/m_login');
	
		$is_login = $this->m_login->get_session_id($this->session->userdata('session_id'),$this->session->userdata('username'));
		if($is_login){

		}else{
			$username       = $this->session->userdata('username');
			$session_id     = $this->session->userdata('session_id');
			$this->m_login->delete_session($username,$session_id);
			$this->session->sess_destroy();

			$uri_func = $this->uri->segment(3);
			if(($uri_func === FALSE) || ($uri_func =='add') || ($uri_func =='edit')){
				redirect(base_url() . 'login', 'refresh');
			}else if(in_array($uri_func,array('approve_data','approved_mgr','approved_dir','approve','approve_transsaction','reverse_data','reversed_data','reverse_transsaction','save','save_data','insert','update','update_data','suspend','suspend_data','suspend_transsaction','suspend_mgr','suspend_dir'))){
				$result['status']   = 'session';
				$result['message']  = get_notification('session_destroy',0);
				echo json_encode($result);
				die();
			}
		}
    }

    // Function Index	
    public function index() {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            $arr_group_id = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
            if (!empty($arr_group_id)) {
                $data['available_menu'] = array_unique($arr_group_id);
                if (!in_array($this->menu_id, $data['available_menu'])) {
                    redirect(base_url() . 'error', 'refresh');
                } else {
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

                    // set data
                    $data['menu_id'] = $this->menu_id;

                    // get link from tbl_menu 
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);

                    $data['list_branch'] = $this->m_custom->get_branch();

                    $this->load->view('v_header');
                    $this->load->view('inventories/v_inventories', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    // Function Edit
    public function edit() {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            /*
              untuk di index/list = get_menu_by_group,
              untuk di add = get_menu_by_group_add
              untuk di edit = get_menu_by_group_edit
             */
            $arr_group_id = $this->m_menu->get_menu_by_group_edit($group_menu, $this->menu_id);
            //--------------------------end-----------------------------------------------------------
            if (!empty($arr_group_id)) {
                $data['available_menu'] = array_unique($arr_group_id);
                if (!in_array($this->menu_id, $data['available_menu'])) {
                    redirect(base_url() . 'error', 'refresh');
                } else {
                    /*
                      modul untuk akses add masing masing modul berebda
                      jangan lupa copy $data nya (jika dia bener langsung ke add)
                     */
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

                    // set data
                    $data['menu_id'] = $this->menu_id;

                    // get link from tbl_menu 
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);

                    $data['list_branch'] = $this->m_custom->get_branch();
                    //$data['list'] = $this->m_inventories->join_all();

                    $id = $this->uri->segment(4);
                    //echo $id;
                    $data['detail'] = $this->m_inventories->item_edit($id);
                    //                echo $this->db->last_query(); die();


                    $this->load->view('v_header');
                    $this->load->view('inventories/v_edit_inventories', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    //Function preview data
    public function to_preview($obj_string = null) {
        // set data
        $data['menu_id'] = $this->menu_id;
        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
        $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

//		if($this->m_branchinfo->get_branch_status_by_code($this->session->userdata('branch')) == 0) {
//			$data['list'] = $this->m_inventories->join_all();
//		}
//		else {
//			$data['list'] = $this->m_inventories->join_all_by_branch($this->session->userdata('branch'));
//		}
//        $obj_string     = arrayInUrlToStr($data_grid);
        
//        $data_filter     = str_replace('%252D', '-', $obj_string);
//        $data_filter     = json_decode(urldecode($data_filter), TRUE);        
        $data_filter    = arrayParamInUrlToStr($obj_string);
        $data['list']   = $this->m_inventories->get_all_to_print($data_filter);

        $this->load->view('v_header');
        $this->load->view('inventories/v_preview_inventories', $data);
        $this->load->view('v_footer');
    }

    //Function To Print
    public function to_print() {
        // set data
        $data['menu_id'] = $this->menu_id;
        $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

        if ($this->m_branchinfo->get_branch_status_by_code($this->session->userdata('branch')) == 0) {
            $data['list'] = $this->m_inventories->join_all();
        } else {
            $data['list'] = $this->m_inventories->join_all_by_branch($this->session->userdata('branch'));
        }

        $this->load->view('v_header');
        $this->load->view('inventories/v_print_inventories', $data);
        $this->load->view('v_footer');
    }

    public function ajax() {
        $page = ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
        $rows = ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;

        $hasil = array();
        /* ===========rows*page tidak boleh melebihi dari jumlah data======= */

        /* ===========================declar post by find================================================ */
        $Code = $this->input->post('ItemCode') ? $this->input->post('ItemCode') : '';
        $Desc = $this->input->post('ItemDesc') ? $this->input->post('ItemDesc') : '';

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

        /* ========================jika kondisi tidak sama dengan kosong di find lakukan proses di if============ */
        if ($BranchCode != "") {
            $this->db->where('BranchCode', $BranchCode);
        } else {
            $this->db->where('BranchCode', $this->session->userdata('branch'));
        }

        if ($Code != "") {
            $this->db->like('ItemCode', $Code);
        }
        if ($Desc != "") {
            $this->db->join('am_item', 'am_item.ItemCode = at_inventories.ItemCode', 'inner');
            $this->db->like('am_item.ItemDesc', $Desc);
        }
        /* ==========================end================================================================= */

        $count_data = $this->db->count_all_results('at_inventories');

        $offset = $count_data - ($rows * $page);
        if ($offset > 0) {
            $rows = $rows;
        } else {
            $rows = $count_data - ($rows * ($page - 1));
        }
        /* ========================end======================================= */
        $result = $this->m_inventories->join_all();

        for ($i = 0; $i <= ($rows - 1); $i++) {

            if ($result[$i]->LockStatus == 1) {
                $Adjust_Lock = "Lock";
            } else {
                $Adjust_Lock = "Un Lock";
            }

            $hasil[] = array(
                'BranchName' => $result[$i]->BranchName,
                'ItemCode' => $result[$i]->ItemCode,
                'ItemDesc' => $result[$i]->ItemDesc,
                'ItemUnit' => $result[$i]->ItemUnit,
                'ItemQty' => $result[$i]->ItemQty,
                'LockStatus' => $Adjust_Lock,
                'TransNumber' => $result[$i]->InventoriesID,
                'TransStatus' => 'Need App. Mgr.'
            );
        }

        if ($hasil == "") {
            $result = array();
            echo json_encode($result);
        } else {
            $result = array();
            $result['total'] = $count_data;
            $result['rows'] = $hasil;
            echo json_encode($result);
        }
    }

}
