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

/*	
 *	Author 	: Tagor
 *	Tanggal : 04-08-2014	
 *	Revisi 	: Dhidhik, 15 Maret 2016, update dibikin array yang pengesekusiannya di model
 */ 

class Company_Info extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 89;
	public $code = "Company Code";
	public $name = "Company Name";
	
	public function __construct() {
        parent::__construct();
		
		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');	
					$this->load->model('file/m_new_company');
				}
				else{
					$this->load->helper('message');
					$this->load->helper('regex');
					$this->load->helper('convert_date');
					
					$this->load->model('file/m_new_company');		
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
	// Function Index	
	public function index() {
		// pengecekan otoritas
		$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);
		
		// get link from tbl_menu 
		$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);
		
		// get data compnay
		$comp_code = $this->session->userdata('company');
		$data['detail'] = $this->m_new_company->get_detail($comp_code);
		
		// set for view
		$this->load->view('v_header');
		$this->load->view('company_info/v_company_info', $data);
		$this->load->view('v_footer');
	}	
	
	// Function Insert
        
	public function insert() {
            try{
		$company_code = trim($this->input->post('txt_company_code'));
		$company_name = trim($this->input->post('txt_companyname'));
		$address1 = $this->input->post('txt_address');
		$address2 = $this->input->post('txt_addressdetail1');
		$company_address = "$address1 $address2";
		$company_postal_code = $this->input->post('txt_zipcode');
		$company_phone = $this->input->post('txt_phoneno');
		$company_fax = $this->input->post('txt_faxno');
		$cmb_city = $this->input->post('cmb_city');
		$cmb_state = $this->input->post('cmb_state');
		$cmb_country = $this->input->post('cmb_country');
		$tax_address1 = $this->input->post('txt_taxaddress');
		$tax_address2 = $this->input->post('txt_taxaddressdetail1');
		$tax_address3 = $this->input->post('txt_taxaddressdetail2');
		$tax_address = "$tax_address1 $tax_address2 $tax_address3";
		$tax_sn_form = $this->input->post('txt_taxformserialnumber');
		$tax_reg_num = $this->input->post('txt_taxregistnumber');
		$tax_comp_num = $this->input->post('txt_taxablecompanysno');
		$tax_comp_date = $this->input->post('txt_taxablecompanysdate');
		$tax_type = $this->input->post('txt_taxtype');
		$tax_klu = $this->input->post('txt_taxklu');
		$company_phone_left = $this->input->post('txt_phone_left');
		$company_fax_left = $this->input->post('txt_fax_left');
		$dataSend = [];
                
		if(!regex_name($company_name)) {
			// set nilai untuk ke form
                    throw new Exception(msg_invalid_name($this->name));
		}
		else {
			// inisialisasi array
			$dataSend['data'] = array (
				//'CompanyCode'		=> $company_code,
				'CompanyName'		=> strtoupper($company_name),
				'CompanyAddress'	=> strtoupper($company_address),
				'CompanyPostalCode'	=> $company_postal_code,
				'PhoneNumber'		=> $company_phone,
				'InitialPhone' 		=> $company_phone_left,
				'InitialFax' 		=> $company_fax_left,
				'FacsimileNumber'	=> $company_fax,
				'CompanyCity'		=> $cmb_city,
				'CompanyState'		=> $cmb_state,
				'CompanyCountry'	=> $cmb_country,
				'CompanyTaxAddress'	=> strtoupper($tax_address),
				'CompTaxSNForm'		=> $tax_sn_form,
				'CompTaxRegNumber'	=> $tax_reg_num,
				'CompTaxNumber'		=> $tax_comp_num,
				'CompTaxDate'		=> convert_to_ymd($tax_comp_date),
				'CompTaxType'		=> $tax_type,
				'CompTaxKLU'		=> $tax_klu,
				'LastUser' 			=> $this->session->userdata("username")
			);
			$dataSend['company_code'] = $company_code;
                        $rstDataSend = $this->m_new_company->updateDataCompany($dataSend);
//                        $rstDataSend = TRUE;
//                        $result['dataSend']=$dataSend;
			// insert database
			//$action = $this->m_new_company->update($data, $company_code);
                        
			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 insert_LAMA() {
		$company_code = trim($this->input->post('txt_company_code'));
		$company_name = trim($this->input->post('txt_companyname'));
		$address1 = $this->input->post('txt_address');
		$address2 = $this->input->post('txt_addressdetail1');
		$company_address = "$address1 $address2";
		$company_postal_code = $this->input->post('txt_zipcode');
		$company_phone = $this->input->post('txt_phoneno');
		$company_fax = $this->input->post('txt_faxno');
		$cmb_city = $this->input->post('cmb_city');
		$cmb_state = $this->input->post('cmb_state');
		$cmb_country = $this->input->post('cmb_country');
		$tax_address1 = $this->input->post('txt_taxaddress');
		$tax_address2 = $this->input->post('txt_taxaddressdetail1');
		$tax_address3 = $this->input->post('txt_taxaddressdetail2');
		$tax_address = "$tax_address1 $tax_address2 $tax_address3";
		$tax_sn_form = $this->input->post('txt_taxformserialnumber');
		$tax_reg_num = $this->input->post('txt_taxregistnumber');
		$tax_comp_num = $this->input->post('txt_taxablecompanysno');
		$tax_comp_date = $this->input->post('txt_taxablecompanysdate');
		$tax_type = $this->input->post('txt_taxtype');
		$tax_klu = $this->input->post('txt_taxklu');
		$company_phone_left = $this->input->post('txt_phone_left');
		$company_fax_left = $this->input->post('txt_fax_left');
		
		if(!regex_name($company_name)) {
			// set nilai untuk ke form
			$result['status'] = false;
			$result['message'] = msg_invalid_name($this->name);
		}
		else {
			// inisialisasi array
			$data = array (
				//'CompanyCode'		=> $company_code,
				'CompanyName'		=> strtoupper($company_name),
				'CompanyAddress'	=> strtoupper($company_address),
				'CompanyPostalCode'	=> $company_postal_code,
				'PhoneNumber'		=> $company_phone,
				'InitialPhone' 		=> $company_phone_left,
				'InitialFax' 		=> $company_fax_left,
				'FacsimileNumber'	=> $company_fax,
				'CompanyCity'		=> $cmb_city,
				'CompanyState'		=> $cmb_state,
				'CompanyCountry'	=> $cmb_country,
				'CompanyTaxAddress'	=> strtoupper($tax_address),
				'CompTaxSNForm'		=> $tax_sn_form,
				'CompTaxRegNumber'	=> $tax_reg_num,
				'CompTaxNumber'		=> $tax_comp_num,
				'CompTaxDate'		=> convert_to_ymd($tax_comp_date),
				'CompTaxType'		=> $tax_type,
				'CompTaxKLU'		=> $tax_klu,
				'LastUser' 			=> $this->session->userdata("username")
			);
			
			// insert database
			$action = $this->m_new_company->update($data, $company_code);
			if($action == 0){
				$result['status'] = false;
				$result['message'] = get_notification('update', 0);
			}
			else {
				$result['status'] = true;
				$result['message'] = get_notification('update', 1);
			}
		}		
		
		echo json_encode($result);
	}
}