<?php
class M_new_company extends CI_Model {
    public $table = "am_company";
	
    function get_all(){
        $query = $this->db->get($this->table);
        return $query->result();
    }
	
    function get_detail($id){
        $query = $this->db->get_where($this->table, array('CompanyCode' => $id));
        return $query->result();
    }
	
    function check_code_exist($id) {
        $query = $this->db->get_where($this->table, array('CompanyCode' => $id), 1, 0);
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
	
    function insert($data) {
        $this->db->insert($this->table, $data);
//		
//        if ($this->db->trans_status() === FALSE) {
//			$this->db->trans_rollback();
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }
	
    function insert_branch($data_branch) {
        $this->db->insert('am_branch', $data_branch);
		
        if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
            return 0;
        }
        else {
            return 1;
        }
    }
	
    function insert_admin_khusus($admin_khusus) {
        $this->db->insert('am_employee', $admin_khusus);
		
        if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
            return 0;
        }
        else {
            return 1;
        }
    }
	
    function insert_user($data_user) {
        $this->db->insert('tbl_user', $data_user);
//		
//        if ($this->db->trans_status() === FALSE) {
//			$this->db->trans_rollback();
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function update($data, $id) {
        $this->db->trans_begin();
        $this->db->update($this->table, $data, array('CompanyCode' => $id));
		
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 0;
        }
        else {
            $this->db->trans_commit();
            return 1;
        }
    }
	
    function delete($id) {
        $this->db->trans_begin();
        $this->db->delete($this->table, array('CompanyCode' => $id));
		
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 0;
        }
        else {
            $this->db->trans_commit();
            return 1;
        }
    }
	
    function get_company_name($code) {
        $this->db->select('CompanyName');
        $this->db->from('am_company');
        $this->db->where('CompanyCode', $code); 
        
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $CompanyName = $row->CompanyName;
            }
            
            return $CompanyName;
        }
        else {
            return FALSE;
        }  	
    }
	
	function get_company_currency() {
        $this->db->select('CompanyName, CompanyCode');
        $this->db->from('am_company');
        //$this->db->where('CompanyCode', $code); 
        
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        else {
            return FALSE;
        }  	
    }
    
	function get_company_currency_new($code) {
        $this->db->select('CompanyName, CompanyCode');
        $this->db->from('am_company');
        $this->db->where('CompanyCode', $code); 
        
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        else {
            return FALSE;
        }  	
    }
	
    //=========== DHIDHIK, 15 Maret 2016 ============//
    function data_send_array($dataSend){        
        $this->db->trans_start();
        
        $this->insert($dataSend['data']);
	$this->m_branchinfo->insert($dataSend['data_branch']);
	$this->m_department->insert($dataSend['data_department_akunting']);
	$this->m_department->insert($dataSend['data_department_sales']);
	$this->m_department->insert($dataSend['data_department_marketing']);
	$this->m_department->insert($dataSend['data_department_exim']);
	$this->m_department->insert($dataSend['data_department_purchase']);
	$this->m_department->insert($dataSend['data_department_admin']);
	$this->m_department->insert($dataSend['data_department_finance']);
	$this->m_department->insert($dataSend['data_department_ppic']);
	$this->m_department->insert($dataSend['data_department_migrasi']);
	$this->m_employee->insert($dataSend['admin_khusus']);
	$this->insert_user($dataSend['data_user']);
	$this->m_inventories->insert($dataSend['data_item']);
                
        foreach($dataSend['currency_data'] as $key){
            $currency_data = array(
                'CurrencyCode' 		=> $key['CurrencyCode'],
                'CompanyCode' 		=> $key['CompanyCode'],
                'CurrencyName' 		=> $key['CurrencyName'],
                'CurrencyRate' 		=> $key['CurrencyRate'],
                'CurrencyDesc' 		=> $key['CurrencyDesc'],
                'CurrencyCountry' 	=> $key['CurrencyCountry'],
                'CurrencySymbol' 	=> $key['CurrencySymbol'],
                'TransStatus' 		=> $key['TransStatus'],
                'LastUser' 		=> $key['LastUser']
            );
            $this->m_currency->insert($currency_data);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    
    function updateDataCompany($dataSend){             
        $this->db->trans_start();
                
        $this->db->update($this->table, $dataSend['data'], array('CompanyCode' => $dataSend['company_code']));
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    //===========END DHIDHIK, 15 Maret 2016 ============//
}