<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*	
 *	Author 	: Wahyu / Tanggal : 15-09-2014	
 *	Revisi 	: Ferry / Tanggal : 20-03-2015
 *	Revisi 	: Dhidhik, 29 Maret 2016
 */
 
class Supporting extends CI_Controller {
    // inisialisasi manual
    public $menu_id = 258;
    public $menu_model = 'm_supporting';

    public function __construct() {
       	parent::__construct();
		$this->load->model($this->menu_model);
		$this->load->helper('regex');
		$this->load->helper('message');
		$this->load->helper('status');
		$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;
					//$data['menu_model'] = $this->menu_model;
					
					//panggil model hak akses untuk autoriti
					$data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
					
					// get link from tbl_menu 
					$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

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

    //City Controller 
    public function insert_city() {
        try{
            $StateCode  = $this->input->post('StateCode');
            $CityCode   = strtoupper($this->input->post('CityCode'));
            $CityName   = $this->input->post('CityName');
            $CityDesc   = $this->input->post('CityDesc');
            $dataSend   = [];
            $dataSend['data_city']  = [];
            if ($this->m_supporting->check_code_exist('am_city', 'CityCode', $CityCode) == TRUE) {
                throw new Exception('City Code telah digunakan');
            }else {
                $dataSend['data_city'] = array (
                    'StateCode'     => $StateCode,
                    'CityCode'      => $CityCode,
                    'CityName'      => $CityName,
                    'CityDesc'      => $CityDesc,
                    'LastUser'      => $this->session->userdata("username")
                );
//                $result_insert = $this->m_supporting->insert('am_city', $data);

                $rstDataSend = $this->m_supporting->datasend_supporting_city_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 update_city() {
        try{
            $id         = $this->input->post('id');
            $StateCode  = $this->input->post('StateCode');
            $CityCode   = strtoupper($this->input->post('CityCode'));
            $CityName   = $this->input->post('CityName');
            $CityDesc   = $this->input->post('CityDesc');
            $dataSend   = [];
            $dataSend['data_city']  = [];
            $dataSend['idcode']     = $id;
            
            if($this->m_supporting->check_code_exist('am_city', 'CityCode', $CityCode) == TRUE && $id != $CityCode){	
                throw new Exception(msg_data_used());
            }else{				
                $dataSend['data_city'] = array (
                    'StateCode'     => $StateCode,
                    'CityCode'      => $CityCode,
                    'CityName'      => $CityName,
                    'CityDesc'      => $CityDesc,
                    'LastUser'      => $this->session->userdata("username")
                );

                //$result_edit = $this->m_supporting->update($data, 'am_city', 'CityCode',$id);//
                
                $rstDataSend = $this->m_supporting->datasend_supporting_city_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 delete_city() {
        try{
            if($this->input->post('id')){
                $id = $this->input->post('id');
                $dataSend   = [];
                $dataSend['idcode']=$id;
                //Delete Table City
//                $result_delete = $this->m_supporting->delete('am_city', 'CityCode', $id);//delete

                $rstDataSend = $this->m_supporting->datasend_supporting_city_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 insert_city_LAMA() {
        $StateCode  = $this->input->post('StateCode');
        $CityCode   = $this->input->post('CityCode');
        $CityName   = $this->input->post('CityName');
        $CityDesc   = $this->input->post('CityDesc');
				
        if ($this->m_supporting->check_code_exist('am_city', 'CityCode', $CityCode) == TRUE) {
            $result['status'] = false;
            $result['message'] = 'City Code telah digunakan';
        }
        else {
            $data = array (
                        'StateCode'     => $StateCode,
                        'CityCode'      => $CityCode,
                        'CityName'      => $CityName,
                        'CityDesc'      => $CityDesc,
                        'LastUser'      => $this->session->userdata("username")
                    );
			
            $this->db->trans_begin();
            $result_insert = $this->m_supporting->insert('am_city', $data);

            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 update_city_LAMA() {
        $id         = $this->input->post('id');
        $StateCode  = $this->input->post('StateCode');
        $CityCode   = $this->input->post('CityCode');
        $CityName   = $this->input->post('CityName');
        $CityDesc   = $this->input->post('CityDesc');
		
        if($this->m_supporting->check_code_exist('am_city', 'CityCode', $CityCode) == TRUE && $id != $CityCode){			
            $result['status'] = false;
            $result['message'] = msg_data_used();						
        }
        else{				
            $data = array (
                        'StateCode'     => $StateCode,
                        'CityCode'      => $CityCode,
                        'CityName'      => $CityName,
                        'CityDesc'      => $CityDesc,
                        'LastUser'      => $this->session->userdata("username")
                    );
			
            $this->db->trans_begin();
            $result_edit = $this->m_supporting->update($data, 'am_city', 'CityCode',$id);
            
            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_city_LAMA() {
        if($this->input->post('id')){
            $id = $this->input->post('id');
			
            //Delete Table City
            $this->db->trans_begin();
            $result_delete = $this->m_supporting->delete('am_city', 'CityCode', $id);
            
            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);
	}
    }
	
    //State Controller    
    public function insert_state() {
        try{
            $CountryCode    = $this->input->post('CountryCode');
            $StateCode      = strtoupper($this->input->post('StateCode'));
            $StateName      = $this->input->post('StateName');
            $StateDesc      = $this->input->post('StateDesc');
            $dataSend   = [];
            $dataSend['data_state'] = [];

            if ($this->m_supporting->check_code_exist('am_state', 'StateCode', $StateCode) == TRUE) {
                throw new Exception('State Code telah digunakan');
            }
            else {
                $dataSend['data_state'] = array (
                    'CountryCode'   => $CountryCode,
                    'StateCode'     => $StateCode,
                    'StateName'	=> $StateName,
                    'StateDesc' 	=> $StateDesc,
                    'LastUser' 	=> $this->session->userdata("username")
                );

                // insert database
//                $result_insert = $this->m_supporting->insert('am_state',$data);

                $rstDataSend = $this->m_supporting->datasend_supporting_state_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 update_state() {
        try{
            $id             = $this->input->post('id');
            $CountryCode    = $this->input->post('CountryCode');
            $StateCode      = strtoupper($this->input->post('StateCode'));
            $StateName      = $this->input->post('StateName');
            $StateDesc      = $this->input->post('StateDesc');
            $dataSend   = [];
            $dataSend['data_state'] = [];
            $dataSend['idcode']     = $id;

            if ($this->m_supporting->check_code_exist('am_state','StateCode',$StateCode) == TRUE && $id!=$StateCode ) {
                throw new Exception('State Code telah digunakan');
            }else{				
                $dataSend['data_state'] = array (
                    'CountryCode'   => $CountryCode,
                    'StateCode'     => $StateCode,
                    'StateName'	=> $StateName,
                    'StateDesc' 	=> $StateDesc,
                    'LastUser' 	=> $this->session->userdata("username")
                );

                // update database
//                $result_edit = $this->m_supporting->update($data, 'am_state', 'StateCode',$id);

                $rstDataSend = $this->m_supporting->datasend_supporting_state_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 delete_state() {
        try{
            if($this->input->post('id')){
                $id = $this->input->post('id');
                $dataSend   = [];
                $dataSend['idcode'] = $id;

//                $result_delete = $this->m_supporting->delete('am_state','StateCode',$id);
				
				if ($this->m_supporting->check_code_exist('am_city','StateCode',$id) == TRUE) {
					throw new Exception('Data State tidak berhasil disimpan, karena telah digunakan');
				}
				else{
					$rstDataSend = $this->m_supporting->datasend_supporting_state_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 insert_state_LAMA() {
        $CountryCode    = $this->input->post('CountryCode');
        $StateCode      = $this->input->post('StateCode');
        $StateName      = $this->input->post('StateName');
        $StateDesc      = $this->input->post('StateDesc');

        if ($this->m_supporting->check_code_exist('am_state', 'StateCode', $StateCode) == TRUE) {
            $result['status'] = false;
            $result['message'] = 'State Code telah digunakan';
        }
        else {
            $data = array (
                        'CountryCode'   => $CountryCode,
                        'StateCode'     => $StateCode,
                        'StateName'	=> $StateName,
                        'StateDesc' 	=> $StateDesc,
                        'LastUser' 	=> $this->session->userdata("username")
                    );

            // insert database
            $this->db->trans_begin();
            $result_insert = $this->m_supporting->insert('am_state',$data);

            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 update_state_LAMA() {
        $id             = $this->input->post('id');
        $CountryCode    = $this->input->post('CountryCode');
        $StateCode      = $this->input->post('StateCode');
        $StateName      = $this->input->post('StateName');
        $StateDesc      = $this->input->post('StateDesc');
				
        if ($this->m_supporting->check_code_exist('am_state','StateCode',$StateCode) == TRUE && $id!=$StateCode ) {
            $result['status'] = false;
            $result['message'] = 'State Code telah digunakan';
        }
        else{				
            $data = array (
                        'CountryCode'   => $CountryCode,
                        'StateCode'     => $StateCode,
                        'StateName'	=> $StateName,
                        'StateDesc' 	=> $StateDesc,
                        'LastUser' 	=> $this->session->userdata("username")
                    );
			
            // update database
            $this->db->trans_begin();
            $result_edit = $this->m_supporting->update($data, 'am_state', 'StateCode',$id);
            
            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_state_LAMA() {
        if($this->input->post('id')){
            $id = $this->input->post('id');

            $this->db->trans_begin();
            $result_delete = $this->m_supporting->delete('am_state','StateCode',$id);

            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);
        }		
    }
	
    //Country Controller   
    public function insert_country() {
        try{
            $CountryCode=  strtoupper($this->input->post('CountryCode'));
            $CountryName=$this->input->post('CountryName');
            $CountryDesc=$this->input->post('CountryDesc');
            $dataSend   = [];
            $dataSend['data_country']   = [];

            if ($this->m_supporting->check_code_exist('am_country','CountryCode',$CountryCode) == TRUE) {
                throw new Exception('Data Country tidak berhasil disimpan, karena telah digunakan');
            }else {
                $dataSend['data_country'] = array (
                    'CountryCode'       => $CountryCode,
                    'CountryName'       => $CountryName,
                    'CountryDesc'       => $CountryDesc,
                    'LastUser'          => $this->session->userdata("username")
                );
//                $result_insert = $this->m_supporting->insert('am_country', $data);

                $rstDataSend = $this->m_supporting->datasend_supporting_country_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 update_country($id) {
        try{
            $id=$this->input->post('id');
            $CountryCode=  strtoupper($this->input->post('CountryCode'));
            $CountryName=$this->input->post('CountryName');
            $CountryDesc=$this->input->post('CountryDesc');
            $dataSend   = [];
            $dataSend['data_country']   = [];
            $dataSend['idcode']         = $id;

            if ($this->m_supporting->check_code_exist('am_country','CountryCode',$CountryCode) == TRUE && $id!=$CountryCode) {
                throw new Exception('Country Code telah digunakan');
            }else {
                $dataSend['data_country'] = array (
                    'CountryCode'       => $CountryCode,
                    'CountryName'       => $CountryName,
                    'CountryDesc'       => $CountryDesc,
                    'LastUser'          => $this->session->userdata("username")
                );
//                $result_edit = $this->m_supporting->update($data, 'am_country', 'CountryCode', $id);
                
                $rstDataSend = $this->m_supporting->datasend_supporting_country_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 delete_country() {
        try{
            if($this->input->post('id')){
                $id     = $this->input->post('id');
                $dataSend   = [];
                $dataSend['idcode'] = $id;
//                $result_delete = $this->m_supporting->delete('am_country', 'CountryCode', $id);
                
				if ($this->m_supporting->check_code_exist('am_state','CountryCode',$id) == TRUE) {
					throw new Exception('Data Country tidak berhasil disimpan, karena telah digunakan');
				}
				else{
					$rstDataSend = $this->m_supporting->datasend_supporting_country_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 insert_country_LAMA() {
        $CountryCode=$this->input->post('CountryCode');
        $CountryName=$this->input->post('CountryName');
        $CountryDesc=$this->input->post('CountryDesc');
				
        if ($this->m_supporting->check_code_exist('am_country','CountryCode',$CountryCode) == TRUE) {
            $result['status'] = false;
            $result['message'] = 'Data Country tidak berhasil disimpan, karena telah digunakan';
        }
        else {
            $data = array (
                        'CountryCode'       => $CountryCode,
                        'CountryName'       => $CountryName,
                        'CountryDesc'       => $CountryDesc,
                        'LastUser'          => $this->session->userdata("username")
                    );
			
            $this->db->trans_begin();
            $result_insert = $this->m_supporting->insert('am_country', $data);

            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 update_country_LAMA($id) {
        $id=$this->input->post('id');
        $CountryCode=$this->input->post('CountryCode');
        $CountryName=$this->input->post('CountryName');
        $CountryDesc=$this->input->post('CountryDesc');
				
        if ($this->m_supporting->check_code_exist('am_country','CountryCode',$CountryCode) == TRUE && $id!=$CountryCode) {
            $result['status'] = false;
            $result['message'] = 'Country Code telah digunakan';
        }
        else {
            $data = array (
                        'CountryCode'       => $CountryCode,
                        'CountryName'       => $CountryName,
                        'CountryDesc'       => $CountryDesc,
                        'LastUser'          => $this->session->userdata("username")
                    );
			
            $this->db->trans_begin();
            $result_edit = $this->m_supporting->update($data, 'am_country', 'CountryCode', $id);
	
            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_country_LAMA() {
        if($this->input->post('id')){
            $id     = $this->input->post('id');

            $this->db->trans_begin();
            $result_delete = $this->m_supporting->delete('am_country', 'CountryCode', $id);
	
            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);
        }		
    }
        
    //JobTitle Controller    
    public function insert_jobtitle() {
        try{
            $JobTitleCode       = $this->input->post('JobTitleCode');
            $JobTitleName       = $this->input->post('JobTitleName');
            $JobTitleStatus     = $this->input->post('JobTitleStatus');
            $Description        = $this->input->post('Description');
            $JobTitleLevel      = $this->input->post('JobTitleLevel');
            $dataSend   = [];
            $dataSend['data_jobtitle']   = [];
            
            // cek apakah kode sudah digunakan
            if ($this->m_supporting->check_code_exist('am_jobtitle','JobTitleCode',$JobTitleCode) == TRUE) {
                throw new Exception('Data Job Title Code telah digunakan');
            }
            else {
                $dataSend['data_jobtitle'] = array (
                    'JobTitleCode'      => strtoupper($JobTitleCode),
                    'JobTitleName'      => $JobTitleName,
                    'JobTitleStatus'    => $JobTitleStatus,
                    'Description'       => $Description,
                    'JobTitleLevel'     => $JobTitleLevel,
                    'TransStatus' 	=> 1,
                    'LastUser'          => $this->session->userdata("username")
                );
//                $result_insert = $this->m_supporting->insert('am_jobtitle',$data);
                
                $rstDataSend = $this->m_supporting->datasend_supporting_jobtitle_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 update_jobtitle($id) {
        try{
            $id=$this->input->post('id');
            $JobTitleCode       = strtoupper($this->input->post('JobTitleCode'));
            $JobTitleName       = $this->input->post('JobTitleName');
            $JobTitleStatus     = $this->input->post('JobTitleStatus');
            $Description        = $this->input->post('Description');
            $JobTitleLevel      = $this->input->post('JobTitleLevel');
            $dataSend   = [];
            $dataSend['data_jobtitle']  = [];
            $dataSend['idcode']         = $id;

            if ($this->m_supporting->check_code_exist('am_jobtitle','JobTitleCode',$JobTitleCode) == TRUE && $id != $JobTitleCode) {
                throw new Exception('Data Job Title Code telah digunakan');
            }
            else{
                $dataSend['data_jobtitle'] = array (
                    'JobTitleCode'      => strtoupper($JobTitleCode),
                    'JobTitleName'      => $JobTitleName,
                    'JobTitleStatus'    => $JobTitleStatus,
                    'Description'       => $Description,
                    'JobTitleLevel'     => $JobTitleLevel,
                    'TransStatus' 	=> 1,
                    'LastUser'          => $this->session->userdata("username")
                );
//                $result_edit = $this->m_supporting->update($data, 'am_jobtitle', 'JobTitleCode', $id);
               
                $rstDataSend = $this->m_supporting->datasend_supporting_jobtitle_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 delete_jobtitle() {
        try{
            if($this->input->post('id')){
                $id = $this->input->post('id');
                $dataSend   = [];
                $dataSend['idcode']   = $id;
//                $result_delete = $this->m_supporting->delete('am_jobtitle', 'JobTitleCode', $id);
                
                $rstDataSend = $this->m_supporting->datasend_supporting_jobtitle_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 insert_jobtitle_LAMA() {
        $JobTitleCode       = $this->input->post('JobTitleCode');
        $JobTitleName       = $this->input->post('JobTitleName');
        $JobTitleStatus     = $this->input->post('JobTitleStatus');
        $Description        = $this->input->post('Description');
        $JobTitleLevel      = $this->input->post('JobTitleLevel');
				
        // cek apakah kode sudah digunakan
        if ($this->m_supporting->check_code_exist('am_jobtitle','JobTitleCode',$JobTitleCode) == TRUE) {
            $result['status'] = false;
            $result['message'] = 'Data Job Title Code telah digunakan';
        }
        else {
            $data = array (
                        'JobTitleCode'      => $JobTitleCode,
                        'JobTitleName'      => $JobTitleName,
                        'JobTitleStatus'    => $JobTitleStatus,
                        'Description'       => $Description,
                        'JobTitleLevel'     => $JobTitleLevel,
						'TransStatus' 		=> 1,
                        'LastUser'          => $this->session->userdata("username")
                    );
			
            $this->db->trans_begin();
            $result_insert = $this->m_supporting->insert('am_jobtitle',$data);

            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 update_jobtitle_LAMA($id) {
        $id=$this->input->post('id');
        $JobTitleCode       = $this->input->post('JobTitleCode');
        $JobTitleName       = $this->input->post('JobTitleName');
        $JobTitleStatus     = $this->input->post('JobTitleStatus');
        $Description        = $this->input->post('Description');
        $JobTitleLevel      = $this->input->post('JobTitleLevel');
				
        if ($this->m_supporting->check_code_exist('am_jobtitle','JobTitleCode',$JobTitleCode) == TRUE && $id != $JobTitleCode) {
            $result['status']   = false;
            $result['message']  = 'Data Job Title Code telah digunakan';
        }
        else{
            $data = array (
                        'JobTitleCode'      => $JobTitleCode,
                        'JobTitleName'      => $JobTitleName,
                        'JobTitleStatus'    => $JobTitleStatus,
                        'Description'       => $Description,
                        'JobTitleLevel'     => $JobTitleLevel,
						'TransStatus' 		=> 1,
                        'LastUser'          => $this->session->userdata("username")
                    );
			
            $this->db->trans_begin();
            $result_edit = $this->m_supporting->update($data, 'am_jobtitle', 'JobTitleCode', $id);
	
            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_jobtitle_LAMA() {
       if($this->input->post('id')){
            $id = $this->input->post('id');

            $this->db->trans_begin();
            $result_delete = $this->m_supporting->delete('am_jobtitle', 'JobTitleCode', $id);

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