<?php 

class M_admin extends CI_Model{
	public function __construct()
	{
		$this->load->database();
	}

	public function data_admin()
	{
		$query = $this->db->get('tb_admin');
		return $query->result();
	} 
	public function data_rak()
	{
		$this->db->order_by('id_rak', 'asc');
		$query = $this->db->get('tb_rak');
		return $query->result();
	} 
	public function cetak_data_rak($id_rak,$urutan)
	{
		
	
    $username = $this->session->userdata("username");  
    $this->db->where('username',$username);
    $query1 = $this->db->get('tb_admin');
    $row = $query1->row();
    $data['id_admin']= $row->id;
    $data['nama_admin']= $row->nama_admin;
    $data['id_kantor']= $row->id_kantor;
    $id_admin = $row->id;
    $id_kantor = $row->id_kantor;

    if($urutan=='stok_aplikasi'){
    	$this->db->select('*');
    	$this->db->order_by('tb_stok.stok_aplikasi','asc');
    	$this->db->from('tb_stok');
    	$this->db->join('tb_kantor', 'tb_stok.id_kantor = tb_kantor.id_kantor');
    	$this->db->where('tb_kantor.id_kantor',$id_kantor);
    	$this->db->join('tb_barang', 'tb_stok.id_barang = tb_barang.id');
    	$this->db->join('tb_rak', 'tb_rak.id_rak = tb_barang.id_rak');
    	$this->db->where('tb_rak.id_rak',$id_rak);
    	$this->db->join('tb_selfing', 'tb_selfing.id_selfing = tb_barang.id_selfing');
    	$this->db->join('tb_katagori', 'tb_katagori.id_katagori = tb_barang.id_katagori');

    }else if($urutan=='selfing') {
    	$this->db->select('*');
    	$this->db->order_by('tb_selfing.nama_selfing','asc');
    	$this->db->from('tb_stok');
    	$this->db->join('tb_kantor', 'tb_stok.id_kantor = tb_kantor.id_kantor');
    	$this->db->where('tb_kantor.id_kantor',$id_kantor);
    	$this->db->join('tb_barang', 'tb_stok.id_barang = tb_barang.id');
    	$this->db->join('tb_rak', 'tb_rak.id_rak = tb_barang.id_rak');
    	$this->db->where('tb_rak.id_rak',$id_rak);
    	$this->db->join('tb_selfing', 'tb_selfing.id_selfing = tb_barang.id_selfing');
    	$this->db->join('tb_katagori', 'tb_katagori.id_katagori = tb_barang.id_katagori');	
    	
    }else if($urutan=='katagori') {
    	$this->db->select('*');
    	$this->db->order_by('tb_katagori.nama_katagori','asc');
    	$this->db->from('tb_stok');
    	$this->db->join('tb_kantor', 'tb_stok.id_kantor = tb_kantor.id_kantor');
    	$this->db->where('tb_kantor.id_kantor',$id_kantor);
    	$this->db->join('tb_barang', 'tb_stok.id_barang = tb_barang.id');
    	$this->db->join('tb_rak', 'tb_rak.id_rak = tb_barang.id_rak');
    	$this->db->where('tb_rak.id_rak',$id_rak);
    	$this->db->join('tb_selfing', 'tb_selfing.id_selfing = tb_barang.id_selfing');
    	$this->db->join('tb_katagori', 'tb_katagori.id_katagori = tb_barang.id_katagori');	
    	
    }else{
    	$this->db->select('*');
    	$this->db->order_by('tb_barang.nama_barang','asc');
    	$this->db->from('tb_stok');
    	$this->db->join('tb_kantor', 'tb_stok.id_kantor = tb_kantor.id_kantor');
    	$this->db->where('tb_kantor.id_kantor',$id_kantor);
    	$this->db->join('tb_barang', 'tb_stok.id_barang = tb_barang.id');
    	$this->db->join('tb_rak', 'tb_rak.id_rak = tb_barang.id_rak');
    	$this->db->where('tb_rak.id_rak',$id_rak);
    	$this->db->join('tb_selfing', 'tb_selfing.id_selfing = tb_barang.id_selfing');
    	$this->db->join('tb_katagori', 'tb_katagori.id_katagori = tb_barang.id_katagori');	
    }
    
     
    $query = $this->db->get();

    return $query->result();
	} 
	public function tampil_data_rak($id_rak)
	{
		
	
    $username = $this->session->userdata("username");  
    $this->db->where('username',$username);
    $query = $this->db->get('tb_admin');
    $row = $query->row();
    $data['id_admin']= $row->id;
    $data['nama_admin']= $row->nama_admin;
    $data['id_kantor']= $row->id_kantor;
    $id_admin = $row->id;
    $id_kantor = $row->id_kantor;

 		


    $query = $this->db->query("SELECT tb_stok.id_barang as id_barang,tb_stok.id_stok,tb_stok.stok_aplikasi, tb_kantor.id_kantor, tb_barang.barcode_ecer,  tb_barang.id_satuan_beli,tb_barang.id_selfing,tb_barang.harga_beli_ecer,tb_barang.barcode_ecer,  tb_selfing.nama_selfing, tb_barang.nama_barang, tb_barang.id_rak, tb_barang.id_katagori,
      tb_barang.id_satuan_jual, tb_barang.isi
      FROM tb_barang, tb_stok, tb_rak, tb_kantor , tb_selfing WHERE 
      tb_kantor.id_kantor = tb_stok.id_kantor AND
      tb_barang.id = tb_stok.id_barang AND tb_barang.id_selfing = tb_selfing.id_selfing AND
      tb_barang.id_rak = tb_rak.id_rak AND tb_barang.id_rak = '$id_rak' AND tb_kantor.id_kantor='$id_kantor' order by nama_selfing asc ");
    return $query->result();
	} 
	function get_admin($id){
        $hasil=$this->db->query("SELECT * FROM tb_admin WHERE id_kantor='$id'");
        return $hasil->result();
    }
	function struk($id_admin)
	{
		 
		 
	 	$this->db->where('id',$id_admin);
		 
		$query = $this->db->get('tb_admin');
		return $query->result();
	} 
	public function data_admin_login()
	{
		$username = $this->session->userdata("username");
		$this->db->where('username',$username);
		$query = $this->db->get('tb_admin');
		return $query->result();
	} 


	public function data_log()
	{
		$id=$this->uri->segment(3);
		$this->db->order_by('ID', 'desc');
		$this->db->limit(10);
		$this->db->where('id_admin',$id);
		$query = $this->db->get('tb_log');
		return $query->result();
	} 
	public function data_log_all()
	{
		 
		$this->db->order_by('id', 'desc');
		$this->db->limit(10);
		 
		$query = $this->db->get('tb_log');
		return $query->result();
	} 
	
	 
}