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

class Lap_beli extends CI_Controller {

	/**
	 * @keterangan : Controller untuk halaman profil
	 **/
	
	public function index()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			
			$d['prg']= $this->config->item('prg');
			$d['web_prg']= $this->config->item('web_prg');
			
			$d['nama_program']= $this->config->item('nama_program');
			$d['instansi']= $this->config->item('instansi');
			$d['usaha']= $this->config->item('usaha');
			$d['alamat_instansi']= $this->config->item('alamat_instansi');

			
			$d['judul']="Laporan Pembelian Barang";
			
			$text = "SELECT * FROM supplier ";
			$d['l_supp'] = $this->app_model->manualQuery($text);
			
			$text1 = "SELECT * FROM tbcomponent where Kode_Component='Bln' ";
			$d['l_bln_tempo'] = $this->app_model->manualQuery($text1);
			
			$d['content'] = $this->load->view('lap_beli/form', $d, true);		
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function lihat()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$kode = $this->input->post('kode');
			$tgl1 = $this->app_model->tgl_sql($this->input->post('tgl1'));
			$tgl2 = $this->app_model->tgl_sql($this->input->post('tgl2'));
			$supplier = $this->input->post('supplier');
			$bln_tempo = $this->input->post('bln_tempo');
			$th_tempo = $this->input->post('th_tempo');
			$bln_expr = $this->input->post('bln_expr');
			$th_expr = $this->input->post('th_expr');
			$pilih = $this->input->post('pilih');
			
			if($pilih=='all'){
				$where = ' ';
			}elseif($pilih=='tgl'){
				$where = " WHERE a.tglbeli BETWEEN '$tgl1' AND '$tgl2'";
			}elseif($pilih=='supplier'){
				$where = " WHERE a.kode_supplier='$supplier'";	
			}elseif($pilih=='tempo'){
				$where = " WHERE DATE_FORMAT(a.jth_tempo,'%Y')='$th_tempo' AND DATE_FORMAT(a.jth_tempo,'%m')='$bln_tempo'";	
			}elseif($pilih=='exprd'){
				$where = " WHERE DATE_FORMAT(b.expired,'%Y')='$th_expr' AND DATE_FORMAT(b.expired,'%m')='$bln_expr'";	
			}else{
				$where = " WHERE b.kode_barang='$kode'";
			}
			
			$text = "SELECT a.kodebeli,a.tglbeli,a.kode_supplier,a.jth_tempo,
					b.kode_barang,b.jmlbeli,b.hargabeli,b.expired,
					c.nama_barang,c.satuan,c.harga_jual,
					d.nama_supplier
					FROM h_beli as a
					JOIN d_beli as b
					JOIN barang as c
					JOIN supplier as d
					ON a.kodebeli=b.kodebeli AND b.kode_barang=c.kode_barang AND a.kode_supplier=d.kode_supplier
					$where 
					ORDER BY a.kodebeli ASC ";
			$d['data'] = $this->app_model->manualQuery($text);
			
			$this->load->view('lap_beli/view',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function cetak()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			
			$d['prg']= $this->config->item('prg');
			$d['web_prg']= $this->config->item('web_prg');
			
			$d['nama_program']= $this->config->item('nama_program');
			$d['instansi']= $this->config->item('instansi');
			$d['usaha']= $this->config->item('usaha');
			$d['alamat_instansi']= $this->config->item('alamat_instansi');
			$d['judul']="Laporan Pembelian Barang";
			
			$pilih = $this->uri->segment(3);
		
			if($pilih=='all'){
				$where = ' ';
				$d['filter'] = 'Semua Data';
			}elseif($pilih=='tgl'){
				$tgl1 = $this->app_model->tgl_sql($this->uri->segment(4));
				$tgl2 = $this->app_model->tgl_sql($this->uri->segment(5));
				
				$where = " WHERE a.tglbeli BETWEEN '$tgl1' AND '$tgl2'";
				$d['filter'] = 'Tanggal '.$this->app_model->tgl_indo($tgl1).' s.d '.$this->app_model->tgl_indo($tgl2);
			}elseif($pilih=='supplier'){
				$supplier = $this->uri->segment(4);
				$where = " WHERE a.kode_supplier='$supplier'";	
				$d['filter'] = 'Supplier '.$supplier;
			}elseif($pilih=='tempo'){
				$bln_tempo = $this->uri->segment(4);
				$th_tempo = $this->uri->segment(5);
				
				$where = " WHERE DATE_FORMAT(a.jth_tempo,'%Y')='$th_tempo' AND DATE_FORMAT(a.jth_tempo,'%m')='$bln_tempo'";	
				$d['filter'] = 'Jth Tempo Bulan '.$bln_tempo.'-'.$th_tempo;
			}elseif($pilih=='exprd'){
				$bln_expr = $this->uri->segment(4);
				$th_expr = $this->uri->segment(5);
				
				$where = " WHERE DATE_FORMAT(b.expired,'%Y')='$th_expr' AND DATE_FORMAT(b.expired,'%m')='$bln_expr'";	
				$d['filter'] = 'Expired Bulan '.$bln_expr.'-'.$th_expr;
			}else{
				$kode = $this->uri->segment(4);
				$where = " WHERE b.kode_barang='$kode'";
				$d['filter'] = 'Kode Barang '.$kode;
			}
			
			$text = "SELECT a.kodebeli,a.tglbeli,a.kode_supplier,
					b.kode_barang,b.jmlbeli,b.hargabeli,
					c.nama_barang,c.satuan,c.harga_jual,
					d.nama_supplier
					FROM h_beli as a
					JOIN d_beli as b
					JOIN barang as c
					JOIN supplier as d
					ON a.kodebeli=b.kodebeli AND b.kode_barang=c.kode_barang AND a.kode_supplier=d.kode_supplier
					$where 
					ORDER BY a.kodebeli ASC ";
			$d['data'] = $this->app_model->manualQuery($text);
			
			$this->load->view('lap_beli/cetak',$d);
		}else{
			header('location:'.base_url());
		}
	}

}

/* End of file profil.php */
/* Location: ./application/controllers/profil.php */