<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/* 	
 * 	Author 	: Wahyu / Tanggal : 02-10-2014	
 * 	Revisi 	: Ferry / Tanggal : 11-04-2015
 * 	Revisi 	: Dhidhik / Tanggal : 29-03-2016, array in model
 */

class Taxes extends CI_Controller {

    public $menu_id = 183;
    public $code = "Taxes Code";
    public $name = "Taxes Name";

    public function __construct() {
        parent::__construct();
        $this->load->helper('regex');
        $this->load->helper('message');
        $this->load->helper('status');
        $this->load->helper('convert_date');
        $this->load->helper('custom_string_helper');

        $this->load->model('m_menu');
        $this->load->model('m_taxes');
		$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();
			}
		}
    }

    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);

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

    public function add() {
        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_add($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);

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

    public function insert() {
        try {
            $txt_name = trim($this->input->post('txt_name'));
            $txt_rate = $this->input->post('txt_rate');
            $txt_code = $this->input->post('txt_code');
            $txt_desc = $this->input->post('txt_desc');
            $dataSend = [];
            $dataSend['data_taxes'] = [];
            if (!regex_code($txt_code)) {
                throw new Exception(msg_invalid_code($this->code));
            } else {
                if ($this->m_taxes->check_code_exist($txt_code) == TRUE) {
                    throw new Exception(msg_already_used($this->code));
                } else {
                    $dataSend['data_taxes'] = array(
                        'TaxesCode' => strtoupper($txt_code),
                        'TaxesName' => strtoupper($txt_name),
                        'TaxesRate' => $txt_rate,
                        'TaxesDesc' => $txt_desc,
                        'TransStatus' => 1,
                        'LastUser' => $this->session->userdata("username")
                    );
//                    $result_insert = $this->m_taxes->insert($data);

                    $rstDataSend = $this->m_taxes->datasend_taxes_insert($dataSend);
                    if ($rstDataSend) {
                        $result['status'] = true;
                        $result['message'] = get_notification('insert', 1);
                    } else {
                        $result['status'] = false;
                        $result['message'] = get_notification('insert', 0);
                    }
                    echo json_encode($result);
                }
            }
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    public function insert_LAMA() {
        $txt_name = trim($this->input->post('txt_name'));
        $txt_rate = $this->input->post('txt_rate');
        $txt_code = $this->input->post('txt_code');
        $txt_desc = $this->input->post('txt_desc');

        if (!regex_code($txt_code)) {
            $result['status'] = false;
            $result['message'] = msg_invalid_code($this->code);
        } else {
            if ($this->m_taxes->check_code_exist($txt_code) == TRUE) {
                $result['status'] = false;
                $result['message'] = msg_already_used($this->code);
            } else {
                $data = array(
                    'TaxesCode' => strtoupper($txt_code),
                    'TaxesName' => strtoupper($txt_name),
                    'TaxesRate' => $txt_rate,
                    'TaxesDesc' => $txt_desc,
                    'TransStatus' => 1,
                    'LastUser' => $this->session->userdata("username")
                );

                $result_insert = $this->m_taxes->insert($data);
                if ($result_insert == 0) {
                    $this->trans_rollback('insert');
                }

                if ($result_insert == 0) {
                    $this->db->trans_rollback();
                    $result['status'] = false;
                    $result['message'] = get_notification('insert', 0);
                } else {
                    $this->db->trans_commit();
                    $result['status'] = true;
                    $result['message'] = get_notification('insert', 1);
                }
            }
        }

        echo json_encode($result);
    }

    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');

                    $id = $this->uri->segment(4);
                    $data['detail'] = $this->m_taxes->get_detail($id);

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

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

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

                    // set for view
                    $this->load->view('v_header');
                    $this->load->view('others/taxes/v_edit_taxes', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function update() {
        try {
            $txt_name = trim($this->input->post('txt_name'));
            $txt_rate = $this->input->post('txt_rate');
            $txt_code = $this->input->post('txt_code');
            $txt_desc = $this->input->post('txt_desc');
            $dataSend = [];
            $dataSend['data_taxes'] = [];
            $dataSend['idcode'] = $txt_code;

            $dataSend['data_taxes'] = array(
                'TaxesCode' => strtoupper($txt_code),
                'TaxesName' => strtoupper($txt_name),
                'TaxesRate' => $txt_rate,
                'TaxesDesc' => $txt_desc,
                'TransStatus' => 1,
                'LastUser' => $this->session->userdata("username")
            );
//            $result_edit = $this->m_taxes->update($data, $txt_code);            
            $rstDataSend = $this->m_taxes->datasend_taxes_update($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('update', 1);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('update', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    public function update_LAMA() {
        $txt_name = trim($this->input->post('txt_name'));
        $txt_rate = $this->input->post('txt_rate');
        $txt_code = $this->input->post('txt_code');
        $txt_desc = $this->input->post('txt_desc');

        $data = array(
            'TaxesCode' => strtoupper($txt_code),
            'TaxesName' => strtoupper($txt_name),
            'TaxesRate' => $txt_rate,
            'TaxesDesc' => $txt_desc,
            'TransStatus' => 1,
            'LastUser' => $this->session->userdata("username")
        );

        $result_edit = $this->m_taxes->update($data, $txt_code);
        if ($result_edit == 0) {
            $this->trans_rollback('update');
        }

        if ($result_edit == 0) {
            $this->db->trans_rollback();
            $result['status'] = false;
            $result['message'] = get_notification('update', 0);
        } else {
            $this->db->trans_commit();
            $result['status'] = true;
            $result['message'] = get_notification('update', 1);
        }

        echo json_encode($result);
    }

    public function delete() {
        try {
            $id = $this->input->post('id');
            $dataSend = [];
            $dataSend['idcode'] = $id;

            $group_menu = $this->session->userdata('group_menu');
            $arr_group_id = $this->m_menu->get_menu_by_group($group_menu);
            $data['available_menu'] = array_unique($arr_group_id);

            if (!in_array($this->menu_id, $data['available_menu'])) {
                redirect(base_url() . 'error', 'refresh');
            }

//            $result_delete = $this->m_taxes->suspended($id);        
            $rstDataSend = $this->m_taxes->datasend_taxes_delete($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('delete', 1);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('delete', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    public function delete_LAMA() {
        $id = $this->input->post('id');

        $group_menu = $this->session->userdata('group_menu');
        $arr_group_id = $this->m_menu->get_menu_by_group($group_menu);
        $data['available_menu'] = array_unique($arr_group_id);

        if (!in_array($this->menu_id, $data['available_menu'])) {
            redirect(base_url() . 'error', 'refresh');
        }

        $result_delete = $this->m_taxes->suspended($id);
        if ($result_delete == 0) {
            $this->trans_rollback('update');
        }

        if ($result_delete == 0) {
            $this->db->trans_rollback();
            $result['status'] = false;
            $result['message'] = get_notification('delete', 0);
        } else {
            $this->db->trans_commit();
            $result['status'] = true;
            $result['message'] = get_notification('delete', 1);
        }

        echo json_encode($result);
    }

    public function suspend_data() {
        try {
            $id = $this->input->post('id');
            $dataSend = [];
            $dataSend['idcode'] = $id;
//            $result_delete = $this->m_taxes->suspended($id);

            $rstDataSend = $this->m_taxes->datasend_taxes_suspend($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('delete', 1);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('delete', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    public function to_preview($obj_string = null) {
        $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);

//        $data['list']       = $this->m_taxes->get_all();
        $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);

        $data_filter = arrayParamInUrlToStr($obj_string);
        $data['company_code'] = $data_filter['cmb_company'];
        $data['branch_code'] = $data_filter['cmb_branch'];
        $data['list'] = $this->m_taxes->get_all_to_print($data_filter);


        $this->load->view('v_header');
        $this->load->view('others/taxes/v_preview_taxes', $data);
        $this->load->view('v_footer');
    }

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

        $data['list'] = $this->m_taxes->get_all();

        $this->load->view('v_header');
        $this->load->view('others/taxes/v_print_taxes', $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('TaxesCode') ? $this->input->post('TaxesCode') : '';
        $Name = $this->input->post('TaxesName') ? $this->input->post('TaxesName') : '';

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

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

        if ($TransStatus != '') {
            $this->db->where_in('TransStatus', $TransStatus);
        } else {
            /*
              default adalah 2,6. Finished dan Need Approval
             */
            $this->db->where_in('TransStatus', array(0, 1));
        }
        /* ==========================end================================================================= */

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

        $offset = $count_data - ($rows * $page);
        if ($offset > 0) {
            $rows = $rows;
        } else {
            $rows = $count_data - ($rows * ($page - 1));
        }
        /* ========================end======================================= */
        $result = $this->m_taxes->get_all();
        for ($i = 0; $i <= ($rows - 1); $i++) {
            $TransStatus = get_status_master($result[$i]->TransStatus);

            $hasil[] = array(
                'TransNumber' => $result[$i]->TaxesCode,
                'TaxesName' => $result[$i]->TaxesName,
                'TaxesRate' => $result[$i]->TaxesRate,
                'TaxesDesc' => $result[$i]->TaxesDesc,
                'TransStatus' => $TransStatus
            );
        }

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

    public function trans_rollback($modus) {
        $this->db->trans_rollback();
        $result['status'] = false;
        $result['message'] = get_notification($modus, 0);

        echo json_encode($result);
        die();
    }

}
