<?php 

class M_stok_opname extends CI_Model{
	public function __construct()
	{
		$this->load->database();
	}
	 
	function get_data_barang_bykode($kode){
        $hsl=$this->db->query("SELECT * FROM tb_barang WHERE barcode_ecer='$kode'");
        if($hsl->num_rows()>0){
            foreach ($hsl->result() as $data) {
                $hasil=array(
                    'id_barang' => $data->id,
                    'nama_barang' => $data->nama_barang,
                    'harga_jual_ecer' => $data->harga_jual_ecer,
                    
                    );
            }
        }
        return $hasil;
    } 
    function fetch_data($query)
	{
		$this->db->select("*");
		$this->db->limit(10);
		$this->db->from("tb_barang");
		if($query != '')
		{
			$this->db->like('barcode_ecer', $query);
			$this->db->or_like('nama_barang', $query);
		 
		}
		$this->db->order_by('nama_barang', 'DESC');
		return $this->db->get();
	}
	function kasir_moneycash($limit, $start){
    	$username = $this->session->userdata("username");  
		$this->db->where('username',$username);
		$query = $this->db->get('tb_admin');
		$row = $query->row();
		$data['id_admin_login']= $row->id;
		$id_admin = $row->id;
		$level = $row->level;
		$id_kantor = $row->id_kantor;
		$cari = $this->input->post('cari'); 
	 
			if($level =='manager'){
				$this->db->order_by('date', 'desc');
				$query = $this->db->get('tb_kasir', $limit, $start);

			}else{
				$this->db->order_by('date', 'desc');
				$this->db->where('id_admin',$id_admin);
				$this->db->where('id_kantor',$id_kantor);
				$query = $this->db->get('tb_kasir', $limit, $start);
			}

		
    	
        return $query;
    }


	public function keranjang($id_admin,$id_kantor)
	{
		 
		$this->db->order_by('id', 'desc');
	 	$this->db->where('id_admin',$id_admin);
		$this->db->where('id_kantor',$id_kantor);
		$query = $this->db->get('tb_stok_opname_detail_keranjang');
		return $query->result();
	} 
	function search_blog($title){
		$this->db->like('blog_title', $title , 'both');
		$this->db->order_by('blog_title', 'ASC');
		$this->db->limit(10);
		return $this->db->get('blog')->result();
	}

	 

	function pencarian($title){
		$this->db->like('nama_barang', $title , 'both');
		$this->db->order_by('nama_barang', 'ASC');
		$this->db->limit(10);
		return $this->db->get('tb_barang')->result();
	}
	/*
	public function hitungJumlahAsset($id_admin,$id_kantor)
	{   
	    $query = $this->db->get('tb_asset');
	    if($query->num_rows()>0)
	    {
	      return $query->num_rows();
	    }
	    else
	    {
	      return 0;
	    }
	}
	*/
	public function sub_total_pembelian($id_admin,$id_kantor)
	{
		$this->db->select_sum('harga_total');
		$this->db->where('id_admin',$id_admin);
		$this->db->where('id_kantor',$id_kantor);
		$query = $this->db->get('tb_stok_opname_detail_keranjang');
		if($query->num_rows()>0)
		{
			return $query->row()->harga_total;
		}
		else
		{
			return 0;
		}
	}
	public function jum_pembelian($id_admin,$id_kantor)
	{
		$this->db->select_sum('jumlah');
		$this->db->where('id_admin',$id_admin);
		$this->db->where('id_kantor',$id_kantor);
		$query = $this->db->get('tb_stok_opname_detail_keranjang');
		if($query->num_rows()>0)
		{
			return $query->row()->jumlah;
		}
		else
		{
			return 0;
		}
	}
		 
}