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

class M_itemprice extends CI_Model {
    public $table = "am_itemprice";	
	
    function get_all(){
        $item_desc      = $this->input->post('ItemDesc') ? $this->input->post('ItemDesc') : '';
        $branch_name  = $this->input->post('BranchName') ? $this->input->post('BranchName') : '';
        
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
        $this->db->select("am_item.ItemCode, am_item.ItemDesc, am_branch.BranchName, am_itemprice.CurrencyCode, am_itemprice.ItemPrice,am_item.ItemUnit");
        $this->db->from($this->table);
        $this->db->join('am_branch',$this->table.'.BranchCode=am_branch.BranchCode','INNER');
        $this->db->join('am_item',$this->table.'.ItemCode=am_item.ItemCode','INNER');
        $this->db->join('am_itemcategory','am_itemcategory.ItemcategoryID=am_item.ItemcategoryID','INNER');
        $this->db->like('am_item.ItemDesc',$item_desc);
        $this->db->where($this->table.'.BranchCode',$this->session->userdata('branch'));
        $this->db->like('am_branch.BranchName',$branch_name);
		
		$query  = $this->db->limit($end, $beginning);
		$query = $this->db->get();
		return $query->result();	
    }
	
    function get_all_branch($BranchStatus){
        $query = $this->db->get_where('am_branch', array('BranchStatus' => $BranchStatus));
        return $query->result();
    }

    function insert($data) {
        $this->db->insert($this->table, $data);		
    }
	
    function update($data, $item_code,$branch_code,$currency_code) {        
        $this->db->update($this->table, $data, array('ItemCode' => $item_code,'BranchCode'=> $branch_code,'CurrencyCode'=> $currency_code));		
    }
	
    function check_code_exist($item_code, $BranchCode) {		
        //$query = $this->db->get_where('at_inventories', array('ItemCode' => $item_code, 'BranchCode' => $BranchCode), 1, 0);
        $query = $this->db->get_where('at_inventories', array('ItemCode' => $item_code, 'BranchCode' => $BranchCode));
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }	
    }
	
    function check_price_exist($item_code,$branch_code,$currency_code) {
//        $query = $this->db->get_where($this->table, array('ItemCode' => $item_code,'BranchCode' => $branch_code,'CurrencyCode'=> $currency_code), 1, 0);
        $query = $this->db->get_where($this->table, array('ItemCode' => $item_code,'BranchCode' => $branch_code,'CurrencyCode'=> $currency_code));
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }	
    }
    
//    function check_currency_exist($currency_code,$branch_code,$company_code) {
//        $query = $this->db->get_where("am_currency inner join am_branch on(am_branch.CompanyCode=am_currency.CompanyCode AND am_currency.CompanyCode='".$company_code."' AND am_branch.BranchCode='".$branch_code."' )", array('am_currency.CurrencyCode' => $currency_code, 'am_currency.TransStatus' => 1));
//        if ($query->num_rows() > 0) {
//            return TRUE;
//        }else {
//            return FALSE;
//        }	
//    }
    
//    function check_branch_exist($branch_code) {
//        $query = $this->db->get_where("am_branch", array('BranchCode' => $branch_code,'TransStatus'=> '1'));		
//        if ($query->num_rows() > 0) {
//            return TRUE;
//        }else {
//            return FALSE;
//        }	
//    }
    
    function check_company_match($branchOther) {
        $companyOri  = $this->session->userdata("company");
        $branchOther = cropCompanyOfBranch($branchOther);		
        if ($companyOri == $branchOther) {
            return TRUE;
        }else {
            return FALSE;
        }	
    }
    
    function transaction_started(){
        $this->db->trans_begin();
    }
    
    function transaction_ended(){
        if($this->db->trans_status() == FALSE){
                $this->db->trans_rollback();
                $result['status']   = false;
                $result['message']  = "Data tidak berhasil di import silahkan lihat kembali file anda";
        }else{
                $this->db->trans_commit();
                $result['status']   = true;
                $result['message'] = "Data berhasil di import kedalam sistem";
        }
        return $result;
    }
    function import_itemprice($data){        
        $this->db->trans_begin();
        foreach($data['dataUpdate'] as $key){
            $BranchCode         = $key['BranchCode'];
            $ItemCode		= $key['ItemCode'];
            $itemPrice 	 	= $key['ItemPrice'];
            $Currency	 	= $key['CurrencyCode'];
            $LastUser           = $key['LastUser'];
            $dataUpdate = array (
                'ItemPrice' => $itemPrice,
                'LastUser'  => $LastUser
            );
            $this->update($dataUpdate,$ItemCode,$BranchCode,$Currency); 
        }
        
        foreach($data['dataInsert'] as $key){
            $BranchCode         = $key['BranchCode'];
            $ItemCode		= $key['ItemCode'];
            $itemPrice 	 	= $key['ItemPrice'];
            $Currency	 	= $key['CurrencyCode'];
            $LastUser           = $key['LastUser'];              
                //insert item price
                $dataInsert = array (
                        'BranchCode'    => $BranchCode,
                        'ItemCode'      => $ItemCode,
                        'ItemPrice'     => $itemPrice,
                        'CurrencyCode'  => $Currency,
                        'LastUser'      => $LastUser
                );
                $this->insert($dataInsert);  
        }
        $rstTransEnd = $this->transaction_ended();
        $result['status']   = $rstTransEnd['status'];
        $result['message']  = $rstTransEnd['message'];
        
        return $result;
    }
}

/* End of file m_itemprice.php */
/* Location: ./application/modules_core/file/models/m_itemprice.php */