<?php 

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

	function barang_list_keranjang(){
		 $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;

        
          $this->db->where('id_kantor',$id_kantor);
          $query = $this->db->get('tb_kantor');
          $row = $query->row();
          $data['nama_kantor']= $row->nama_kantor;

		// $hasil=$this->db->query("SELECT * FROM tb_jual_kasir where id_admin='$id_admin' and hold='0' ");
          $hasil=$this->db->query("SELECT tb_jual_kasir.id_barang,tb_jual_kasir.harga_jual,tb_jual_kasir.jumlah, tb_jual_kasir.harga_total, tb_barang.nama_barang  FROM tb_jual_kasir INNER JOIN tb_barang ON tb_barang.id = tb_jual_kasir.id_barang  where id_admin='$id_admin' and hold='0' ");

  //       $this->db->order_by('id', 'asc');
	 // 	$this->db->select('*');    
		// $this->db->from('tb_jual_kasir');
		// $this->db->where('tb_barang.id', 'tb_jual_kasir.id_barang');
		// $hasil = $this->db->get();
		return $hasil->result();
	}
	function data_akhir(){
		 $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;

        
          $this->db->where('id_kantor',$id_kantor);
          $query = $this->db->get('tb_kantor');
          $row = $query->row();
          $data['nama_kantor']= $row->nama_kantor;

		// $hasil=$this->db->query("SELECT * FROM tb_jual_kasir where id_admin='$id_admin' and hold='0' ");
          $hasil=$this->db->query("select  *  from tb_transaksi  inner join tb_member  on tb_member.id_member = tb_transaksi.id_member  where id_admin='$id_admin'  and  DATE(tb_transaksi.date) =  CURDATE()    order by tb_transaksi.id_transaksi desc limit 3   ");

  //       $this->db->order_by('id', 'asc');
	 // 	$this->db->select('*');    
		// $this->db->from('tb_jual_kasir');
		// $this->db->where('tb_barang.id', 'tb_jual_kasir.id_barang');
		// $hasil = $this->db->get();
		return $hasil->result();
	}
	function total_barang_list_keranjang(){
		 $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;

        
          $this->db->where('id_kantor',$id_kantor);
          $query = $this->db->get('tb_kantor');
          $row = $query->row();
          $data['nama_kantor']= $row->nama_kantor;

	 
          $hasil=$this->db->query("SELECT sum(tb_jual_kasir.harga_jual*tb_jual_kasir.jumlah) as total_belanja  FROM tb_jual_kasir INNER JOIN tb_barang ON tb_barang.id = tb_jual_kasir.id_barang  where id_admin='$id_admin' and hold='0' ");
 
		return $hasil->result();
	}
	public function data_admin()
	{
		$query = $this->db->get('tb_admin');
		return $query->result();
	}  
	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(30);
		$this->db->from("tb_barang");
		if($query != '')
		{
		    
			$this->db->like('barcode_ecer', $query);
			$this->db->or_like('nama_barang', $query);
			$this->db->or_like('barcode_dos', $query);
		 
		}
		$this->db->order_by('nama_barang', 'DESC');
		return $this->db->get();
	}
	function fetch_data_dos($query)
	{
		$this->db->select("*");
		$this->db->limit(10);
		$this->db->from("tb_barang");
		if($query != '')
		{
			$this->db->like('barcode_dos', $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'); 
		date_default_timezone_set('Asia/Jakarta');
        $bulan = date('m');
        $tahun = date('Y');
	 
			if($level =='manager' or $level =='admin' ){
				$this->db->order_by('date', 'desc');
				$this->db->where('month(tb_kasir.date)',$bulan);
				$this->db->where('year(tb_kasir.date)',$tahun);
			 	$this->db->where('id_kantor',$id_kantor);
				$query = $this->db->get('tb_kasir', $limit, $start);

			}else{
				$this->db->order_by('date', 'desc');
				$this->db->where('month(tb_kasir.date)',$bulan);
				$this->db->where('year(tb_kasir.date)',$tahun);
				$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);
		$this->db->where('hold','0');
		$query = $this->db->get('tb_jual_kasir');
		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);
		$this->db->where('hold','0');
		$query = $this->db->get('tb_jual_kasir');
		if($query->num_rows()>0)
		{
			return $query->row()->harga_total;
		}
		else
		{
			return 0;
		}
	}
	public function sub_total_pembelian_hemat($id_admin,$id_kantor)
	{
		$this->db->select_sum('total_hemat');
		$this->db->where('id_admin',$id_admin);
		$this->db->where('id_kantor',$id_kantor);
		$this->db->where('hold','0');
		$query = $this->db->get('tb_jual_kasir');
		if($query->num_rows()>0)
		{
			return $query->row()->total_hemat;
		}
		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);
		$this->db->where('hold','0');
		$query = $this->db->get('tb_jual_kasir');
		if($query->num_rows()>0)
		{
			return $query->row()->jumlah;
		}
		else
		{
			return 0;
		}
	}
	function laporan($jenis_laporan,$tanggal, $id_kantor_cari,$bulan,$tahun,	$tanggal_awal,	$tanggal_akhir,$id_admin){

		if($jenis_laporan =='harian'){
			if($id_kantor_cari == 'all'){
				$query = $this->db->query("select  *  from tb_kasir where date(tb_kasir.date) = '$tanggal'   order by tb_kasir.date asc ");
			}else{
				$query = $this->db->query("select  * from tb_kasir where date(tb_kasir.date) = '$tanggal' and tb_kasir.id_kantor ='$id_kantor_cari' order by tb_kasir.date asc ");
			}

		}else  if($jenis_laporan =='bulanan'){
			if($id_kantor_cari == 'all'){
				 $query = $this->db->query("select  * from tb_kasir where month(tb_kasir.date)='$bulan' and year(tb_kasir.date)='$tahun'  order by tb_kasir.date asc ");
			}else{
				 $query = $this->db->query("select  * from tb_kasir where month(tb_kasir.date)='$bulan' and year(tb_kasir.date)='$tahun' and tb_kasir.id_kantor ='$id_kantor_cari' order by tb_kasir.date asc ");
			}
				
		}else  if($jenis_laporan =='pilihan'){
			if($id_kantor_cari == 'all'){
				if($id_admin == 'all'){
					$query = $this->db->query("select  * from tb_kasir where  tb_kasir.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir'    order by tb_kasir.date asc ");
				}else{
					$query = $this->db->query("select  * from tb_kasir  where tb_kasir.id_kantor ='$id_kantor_cari'and tb_kasir.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir' and tb_kasir.id_admin ='$id_admin'    order by tb_kasir.date asc ");
				}

				  
			}else{
				if($id_admin == 'all'){
					$query = $this->db->query("select  * from tb_kasir  where tb_kasir.id_kantor ='$id_kantor_cari'and tb_kasir.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir'   order by tb_kasir.date asc ");
				}else{
					$query = $this->db->query("select  * from tb_kasir  where tb_kasir.id_kantor ='$id_kantor_cari'and tb_kasir.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir' and tb_kasir.id_admin ='$id_admin'    order by tb_kasir.date asc ");
				}

				 	
			}
		
		} 

        return $query->result();
    }
	function laporan_kasir_delete($jenis_laporan,$tanggal, $id_kantor_cari,$bulan,$tahun,	$tanggal_awal,	$tanggal_akhir,$id_admin){

	 
			if($id_kantor_cari == 'all'){
				if($id_admin == 'all'){
					$query = $this->db->query("select  * from tb_jual_kasir_arsip_delete WHERE 
 tb_jual_kasir_arsip_delete.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir' 
  order by tb_jual_kasir_arsip_delete.date asc 
");
				}else{
					$query = $this->db->query("select  * from tb_jual_kasir_arsip_delete  where tb_jual_kasir_arsip_delete.id_kantor ='$id_kantor_cari'and tb_jual_kasir_arsip_delete.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir' and tb_jual_kasir_arsip_delete.id_admin ='$id_admin'    order by tb_jual_kasir_arsip_delete.date asc ");
				}

				  
			}else{
				if($id_admin == 'all'){
					$query = $this->db->query("select  * from tb_jual_kasir_arsip_delete  where tb_jual_kasir_arsip_delete.id_kantor ='$id_kantor_cari'and tb_jual_kasir_arsip_delete.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir'   order by tb_jual_kasir_arsip_delete.date asc ");
				}else{
					$query = $this->db->query("select  * from tb_jual_kasir_arsip_delete  where tb_jual_kasir_arsip_delete.id_kantor ='$id_kantor_cari'and tb_jual_kasir_arsip_delete.date BETWEEN '$tanggal_awal' AND '$tanggal_akhir' and tb_jual_kasir_arsip_delete.id_admin ='$id_admin'    order by tb_jual_kasir_arsip_delete.date asc ");
				}
			}
		 

        return $query->result();
    } 
		 
}