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

/*	
 *	Author 	: Herpan
 *	Tanggal : 06-08-2014	
 *	Revisi 	: Fachri
 	Tgl 	: 20-03-2015
	create department, branch, admin ho user dan employee 
 */ 

class New_company extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 86;
	public $code = "Company Code";
	public $name = "Company Name";
	
	public function __construct() {
        parent::__construct();
		$this->load->helper('regex');
		$this->load->helper('message');
		$this->load->helper('convert_date');		
		
		$this->load->model('m_new_company');
		$this->load->model('list/m_department');
		$this->load->model('setup/m_branchinfo');
		$this->load->model('list/m_employee');
		$this->load->model('list/m_currency');	
		$this->load->model('list/m_items');
		$this->load->model('list/m_inventories');
		$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 quick_setup(){	
		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{
					// pengecekan otoritas
					$group_menu = $this->session->userdata('group_menu');
					
					// set data
					$data['menu_id'] =  $this->menu_id;
					
					// get link from tbl_menu 
					$data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
					
					// get label from tbl_menu 
					$data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);	
					
					$data['currency'] = $this->m_currency->get_all_data();	
					
					//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('new_company/v_quick_setup',$data);
					$this->load->view('v_footer');					
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
	public function trans_rollback($modus) {
        $this->db->trans_rollback();
        $result['status']   = false;
        $result['message']  = get_notification_transaksi($this->name, $modus, 0);
        
        echo json_encode($result);
        die();
    }
	
	
	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_phone_left = $this->input->post('txt_phone_left');
		$company_fax_left = $this->input->post('txt_fax_left');
		$company_date 		= $this->input->post('txt_company_date');
		
		$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');
		$CompanyStatus 		= $this->input->post('CompanyStatus');
		$CurrencyRate 		= $this->input->post('txt_new_rate');
		
		$result_insert 		= 0;
		$password 			= rand(000000,999999);
		$detail 			= json_decode($this->input->post('detail'));
		$dataSend               = [];
		
		if(!regex_code($company_code)) {
			// set nilai untuk ke form                     
                    throw new Exception(msg_invalid_code($this->code));
		}
		else {
			$cek_item_mgrs = $this->m_items->check_code_exist('ITM-MGRS');
			
			if($cek_item_mgrs == false){
                                throw new Exception("pembuatan company baru gagal, Item Migrasi tidak tersedia");
			}
			else if(!regex_name($company_name)) {
				// set nilai untuk ke form
                                throw new Exception(msg_invalid_name($this->name));
			}
			else {
				// cek apakah kode sudah digunakan
				if ($this->m_new_company->check_code_exist($company_code) == TRUE) {
					// set nilai untuk ke form
                                    throw new Exception(msg_already_used($this->code));
				}
				else {
					// inisialisasi array
					//langsung membuat branch
					$dataSend['data_branch'] = array(
						'BranchCode'            => strtoupper($company_code).'-'.'HO'.'-'.'TGR',
						'CompanyCode'           => strtoupper($company_code),
						'BranchName'            => strtoupper($company_code).' '.'HEAD OFFICE',
						'BranchCity'            => $cmb_city,
						'BranchStatus'          => 0,
						'BranchCity'			=> $cmb_city,
						'BranchState'			=> $cmb_state,
						'BranchCountry'			=> $cmb_country,
						'TransStatus' 			=> 1,
						'LastUser'              => $this->session->userdata("username")
					);
					
					//membuat department
					$dataSend['data_department_akunting'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'001',
						'DepartmentName' 	=> "ACCOUNTING",
						'DepartmentDesc'	=> "ACCOUNTING DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser'              => $this->session->userdata("username")
					);
					
					$dataSend['data_department_finance'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'002',
						'DepartmentName' 	=> "FINANCE",
						'DepartmentDesc'	=> "FINANCE",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_department_marketing'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'003',
						'DepartmentName' 	=> "MARKETING",
						'DepartmentDesc'	=> "MARKETING DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_department_exim'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'004',
						'DepartmentName' 	=> "EXIM",
						'DepartmentDesc'	=> "EXIM DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_department_purchase'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'005',
						'DepartmentName' 	=> "PURCHASING",
						'DepartmentDesc'	=> "PURCHASE DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_department_admin'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'006',
						'DepartmentName' 	=> "ADMINISTRATOR",
						'DepartmentDesc'	=> "ADMINISTRATOR",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_department_sales'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'007',
						'DepartmentName' 	=> "SALES",
						'DepartmentDesc'	=> "SALES",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_department_ppic'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'008',
						'DepartmentName' 	=> "PPIC",
						'DepartmentDesc'	=> "PPIC",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_department_migrasi'] = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'009',
						'DepartmentName' 	=> "MIGRATION",
						'DepartmentDesc'	=> "MIGRATION",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['admin_khusus'] = array(
						'EmployeeCode' 		=> "admin-".$company_code,
						'BranchCode' 		=> strtoupper($company_code).'-'.'HO'.'-'.'TGR',
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'TGR'.'-'.'006',
						'JobTitleCode' 		=> "ADMIN",
						'EmployeeName' 		=> "Admin Company",
						'EmployeeCity' 		=> $cmb_city,
						'EmployeeState' 	=> $cmb_state,
						'EmployeeCountry' 	=> $cmb_country,
						'HireDate' 		=> date('Y-m-d'),
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_user'] = array(
						'GroupID' 		=> 1,
						'EmployeeCode' 		=> "admin-".$company_code,
						'EmployeePassword' 	=> $password,
						'AdminAuthority' 	=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					//langsung membuat company
					$dataSend['data'] = array (
						'CompanyCode'		=> strtoupper($company_code),
						'CompanyName'		=> strtoupper($company_name),
						'CompanyAddress'	=> strtoupper($company_address),
						'CompanyPostalCode'	=> $company_postal_code,
						'CompanyDate' 		=> convert_to_ymd($company_date),
						'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,
						'CompanyStatus' 	=> $CompanyStatus,
						'CompBranchStatus' 	=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$dataSend['data_item'] = array(
						'BranchCode'    => strtoupper($company_code).'-'.'HO'.'-'.'JKT',
						'ItemCode' 		=> 'ITM-MGRS',
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					//cek currency dengan trans status 1(satu = active)
					$check_currency_master = $this->m_currency->check_currency(1);
					

					
//					$this->db->trans_begin();
//					// insert database
//					$result_insert = $this->m_new_company->insert($data);
//					$result_insert = $this->m_branchinfo->insert($data_branch);
//					$result_insert = $this->m_department->insert($data_department_akunting);
//					$result_insert = $this->m_department->insert($data_department_sales);
//					$result_insert = $this->m_department->insert($data_department_marketing);
//					$result_insert = $this->m_department->insert($data_department_exim);
//					$result_insert = $this->m_department->insert($data_department_purchase);
//					$result_insert = $this->m_department->insert($data_department_admin);
//					$result_insert = $this->m_department->insert($data_department_finance);
//					$result_insert = $this->m_department->insert($data_department_ppic);
//					$result_insert = $this->m_department->insert($data_department_migrasi);
//					$result_insert = $this->m_employee->insert($admin_khusus);
//					$result_insert = $this->m_new_company->insert_user($data_user);
//					$result_insert = $this->m_inventories->insert($data_item);
					
					foreach($detail as $row_currency) {
						$dataSend['currency_data'][] = array(
							'CurrencyCode' 		=> $row_currency->CurrencyCode,
							'CompanyCode' 		=> strtoupper($company_code),
							'CurrencyName' 		=> $row_currency->CurrencyName,
							'CurrencyRate' 		=> $row_currency->CurrencyRate,
							'CurrencyDesc' 		=> $row_currency->CurrencyDesc,
							'CurrencyCountry' 	=> $row_currency->CurrencyCountry,
							'CurrencySymbol' 	=> $row_currency->CurrencySymbol,
							'TransStatus' 		=> 1,
							'LastUser' 			=> $this->session->userdata("username")
						);
						
//						$this->m_currency->insert($currency_data);
					}
					$rstDataSend = $this->m_new_company->data_send_array($dataSend);
					if($rstDataSend) {
                                            $result['status'] = true;
                                            $result['message'] = "Admin Khusus Telah Dibuat Dengan Username : admin-".$company_code." Password : $password , Costing Method : Average";
					}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() {
		$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_phone_left = $this->input->post('txt_phone_left');
		$company_fax_left = $this->input->post('txt_fax_left');
		
		$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');
		$CompanyStatus 		= $this->input->post('CompanyStatus');
		$CurrencyRate 		= $this->input->post('txt_new_rate');
		
		$result_insert 		= 0;
		$password 			= rand(000000,999999);
		$detail 			= json_decode($this->input->post('detail'));
		
		
		if(!regex_code($company_code)) {
			// set nilai untuk ke form
			$result['status'] = false;
			$result['message'] = msg_invalid_code($this->code);
		}
		else {
			$cek_item_mgrs = $this->m_items->check_code_exist('ITM-MGRS');
			
			if($cek_item_mgrs == false){
				$result['status'] = false;
				$result['message'] = "pembuatan company baru gagal, Item Migrasi tidak tersedia";
			}
			else if(!regex_name($company_name)) {
				// set nilai untuk ke form
				$result['status'] = false;
				$result['message'] = msg_invalid_name($this->name);
			}
			else {
				// cek apakah kode sudah digunakan
				if ($this->m_new_company->check_code_exist($company_code) == TRUE) {
					// set nilai untuk ke form
					$result['status'] = false;
					$result['message'] = msg_already_used($this->code);
				}
				else {
					// inisialisasi array
					//langsung membuat branch
					$data_branch = array(
						'BranchCode'            => strtoupper($company_code).'-'.'HO'.'-'.'JKT',
						'CompanyCode'           => strtoupper($company_code),
						'BranchName'            => strtoupper($company_code).' '.'HEAD OFFICE',
						'BranchCity'            => $cmb_city,
						'BranchStatus'          => 0,
						'BranchCity'			=> $cmb_city,
						'BranchState'			=> $cmb_state,
						'BranchCountry'			=> $cmb_country,
						'TransStatus' 			=> 1,
						'LastUser'              => $this->session->userdata("username")
					);
					
					//membuat department
					$data_department_akunting = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'001',
						'DepartmentName' 	=> "ACCOUNTING",
						'DepartmentDesc'	=> "ACCOUNTING DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser'              => $this->session->userdata("username")
					);
					
					$data_department_finance = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'002',
						'DepartmentName' 	=> "FINANCE",
						'DepartmentDesc'	=> "FINANCE",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_department_marketing = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'003',
						'DepartmentName' 	=> "MARKETING",
						'DepartmentDesc'	=> "MARKETING DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_department_exim = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'004',
						'DepartmentName' 	=> "EXIM",
						'DepartmentDesc'	=> "EXIM DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_department_purchase = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'005',
						'DepartmentName' 	=> "PURCHASING",
						'DepartmentDesc'	=> "PURCHASE DEPARTMENT",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_department_admin = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'006',
						'DepartmentName' 	=> "ADMINISTRATOR",
						'DepartmentDesc'	=> "ADMINISTRATOR",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_department_sales = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'007',
						'DepartmentName' 	=> "SALES",
						'DepartmentDesc'	=> "SALES",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_department_ppic = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'008',
						'DepartmentName' 	=> "PPIC",
						'DepartmentDesc'	=> "PPIC",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_department_migrasi = array(
						'CompanyCode' 		=> strtoupper($company_code),
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'009',
						'DepartmentName' 	=> "MIGRATION",
						'DepartmentDesc'	=> "MIGRATION",
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$admin_khusus = array(
						'EmployeeCode' 		=> "admin-".$company_code,
						'BranchCode' 		=> strtoupper($company_code).'-'.'HO'.'-'.'JKT',
						'DepartmentCode' 	=> strtoupper($company_code).'-'.'HO'.'-'.'JKT'.'-'.'006',
						'JobTitleCode' 		=> "ADMIN",
						'EmployeeName' 		=> "Admin Company",
						'EmployeeCity' 		=> $cmb_city,
						'EmployeeState' 	=> $cmb_state,
						'EmployeeCountry' 	=> $cmb_country,
						'HireDate' 		=> date('Y-m-d'),
						'TransStatus' 		=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_user = array(
						'GroupID' 		=> 1,
						'EmployeeCode' 		=> "admin-".$company_code,
						'EmployeePassword' 	=> $password,
						'AdminAuthority' 	=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					//langsung membuat company
					$data = array (
						'CompanyCode'		=> strtoupper($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,
						'CompanyStatus' 	=> $CompanyStatus,
						'CompBranchStatus' 	=> 1,
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					$data_item = array(
						'BranchCode'    => strtoupper($company_code).'-'.'HO'.'-'.'JKT',
						'ItemCode' 		=> 'ITM-MGRS',
						'LastUser' 		=> $this->session->userdata("username")
					);
					
					//cek currency dengan trans status 1(satu = active)
					$check_currency_master = $this->m_currency->check_currency(1);
					

					
					$this->db->trans_begin();
					// insert database
					$result_insert = $this->m_new_company->insert($data);
					$result_insert = $this->m_branchinfo->insert($data_branch);
					$result_insert = $this->m_department->insert($data_department_akunting);
					$result_insert = $this->m_department->insert($data_department_sales);
					$result_insert = $this->m_department->insert($data_department_marketing);
					$result_insert = $this->m_department->insert($data_department_exim);
					$result_insert = $this->m_department->insert($data_department_purchase);
					$result_insert = $this->m_department->insert($data_department_admin);
					$result_insert = $this->m_department->insert($data_department_finance);
					$result_insert = $this->m_department->insert($data_department_ppic);
					$result_insert = $this->m_department->insert($data_department_migrasi);
					$result_insert = $this->m_employee->insert($admin_khusus);
					$result_insert = $this->m_new_company->insert_user($data_user);
					$result_insert = $this->m_inventories->insert($data_item);
					
					foreach($detail as $row_currency) {
						$currency_data = array(
							'CurrencyCode' 		=> $row_currency->CurrencyCode,
							'CompanyCode' 		=> strtoupper($company_code),
							'CurrencyName' 		=> $row_currency->CurrencyName,
							'CurrencyRate' 		=> $row_currency->CurrencyRate,
							'CurrencyDesc' 		=> $row_currency->CurrencyDesc,
							'CurrencyCountry' 	=> $row_currency->CurrencyCountry,
							'CurrencySymbol' 	=> $row_currency->CurrencySymbol,
							'TransStatus' 		=> 1,
							'LastUser' 			=> $this->session->userdata("username")
						);
						
						$this->m_currency->insert($currency_data);
					}
					
					if($this->db->trans_status() == FALSE) {
						$this->db->trans_rollback();
						$result['status'] = false;
						$result['message'] = get_notification('insert', 0);
					}
					else {
						$this->db->trans_commit();
						$result['status'] = true;
						$result['message'] = "Admin Khusus Telah Dibuat Dengan Username : admin-".$company_code." Password : $password , Costing Method : Average";
						//redirect(base_url() . 'login/process_logout', 'refresh');
					}			
				}
			}
		}	
		echo json_encode($result);
	}
	
	function currency_by_company(){
        $company_code = $this->session->userdata("company");
         echo json_encode($this->m_currency->get_all_data_new($company_code));
    }
}