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

class Barang extends CI_Controller {

	/**
	 * @keterangan : Controller untuk halaman profil
	 **/
	
	public function index()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$cari = $this->input->post('txt_cari');
			
			if(empty($cari)){
				$where = ' ';
				$kata = $this->session->userdata('cari');
			}else{
				$sess_data['cari'] = $this->input->post("txt_cari");
				$this->session->set_userdata($sess_data);
				$cari = $this->session->userdata('cari');
				$where = " WHERE kode_barang LIKE '%$cari%' OR nama_barang LIKE '%$cari%'";
				
			}
			
			$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']="Data Barang";
			
			//paging
			$page=$this->uri->segment(3);
			$limit=$this->config->item('limit_data');
			if(!$page):
			$offset = 0;
			else:
			$offset = $page;
			endif;
			
			$text = "SELECT * FROM vwbarang $where ";		
			$tot_hal = $this->app_model->manualQuery($text);		
			
			$d['tot_hal'] = $tot_hal->num_rows();
			
			$config['base_url'] = site_url() . '/barang/index/';
			$config['total_rows'] = $tot_hal->num_rows();
			$config['per_page'] = $limit;
			$config['uri_segment'] = 3;
			$config['next_link'] = 'Lanjut &raquo;';
			$config['prev_link'] = '&laquo; Kembali';
			$config['last_link'] = '<b>Terakhir &raquo; </b>';
			$config['first_link'] = '<b> &laquo; Pertama</b>';
			$this->pagination->initialize($config);
			$d["paginator"] =$this->pagination->create_links();
			$d['hal'] = $offset;
			

			$text = "SELECT * FROM vwbarang $where 
					ORDER BY kode_barang ASC 
					LIMIT $limit OFFSET $offset";
			$d['data'] = $this->app_model->manualQuery($text);
			
			$d['content'] = $this->load->view('barang/view', $d, true);		
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function tambah()
	{
		$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']="Data Barang";
			
			$kode	= $this->app_model->MaxKodeBarang();
			$d['kode_brg']	=$kode;
			$d['nama_brg']	='';
			$d['satuan']	='';
			$d['hrg_beli']	='';
			$d['hrg_jual']	='';
			$d['stok_awal']	='';	
			
			$text1 = "SELECT * FROM tbjenis";
			$text2 = "SELECT * FROM tbpersen_laba";
			$d['l_jns'] = $this->app_model->manualQuery($text1);
			$d['l_bandul'] = $this->app_model->manualQuery($text2);
			$d['content'] = $this->load->view('barang/form', $d, true);		
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function edit()
	{
		$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'] = "Data Barang";
			
			$id = $this->uri->segment(3);
			$text = "SELECT * FROM vwbarang WHERE kode_barang='$id'";
			$data = $this->app_model->manualQuery($text);
			if($data->num_rows() > 0){
				foreach($data->result() as $db){
					$d['kode_brg']		=$id;
					$d['nama_brg']	=$db->nama_barang;
					$d['jns']		=$db->kode_jenis;
					$d['satuan']	=$db->satuan;
					$d['hrg_beli']	=$db->harga_beli;
					$d['hrg_jual']	=$db->harga_jual;
					$d['stok_awal']	=$db->stok_awal;
					$d['bandul']	=$db->kode_kategori;
				}
			}else{
					$d['kode_brg']	=$id;
					$d['nama_brg']	='';
					$d['jns']		='';
					$d['satuan']	='';
					$d['hrg_beli']	='';
					$d['hrg_jual']	='';
					$d['stok_awal']	='';
					$d['bandul']	='';
			}
			
			$text1 = "SELECT * FROM tbjenis";
			$d['l_jns'] = $this->app_model->manualQuery($text1);
			
			$text2 = "SELECT * FROM tbpersen_laba";
			$d['l_bandul'] = $this->app_model->manualQuery($text2);		
			$d['content'] = $this->load->view('barang/form', $d, true);		
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function hapus()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){			
			$id = $this->uri->segment(3);
			$this->app_model->manualQuery("DELETE FROM barang WHERE kode_barang='$id'");
			echo "<meta http-equiv='refresh' content='0; url=".base_url()."index.php/barang'>";			
		}else{
			header('location:'.base_url());
		}
	}
	
	public function simpan()
	{
		
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
				
				$up['kode_barang']=$this->input->post('kode_brg');
				$up['nama_barang']=$this->input->post('nama_brg');
				$up['kode_jenis']=$this->input->post('jns');
				$up['satuan']=$this->input->post('satuan');
				$up['harga_beli']=$this->input->post('hrg_beli');
				$up['harga_jual']=$this->input->post('hrg_jual');
				$up['stok_awal']=$this->input->post('stok_awal');
				$up['kode_kategori']=$this->input->post('bandul');
				
				$id['kode_barang']=$this->input->post('kode_brg');
				
				$data = $this->app_model->getSelectedData("barang",$id);
				if($data->num_rows()>0){
					$this->app_model->updateData("barang",$up,$id);
					echo 'Update data Sukses';
				}else{
					$this->app_model->insertData("barang",$up);
					echo 'Simpan data Sukses';		
				}
		}else{
				header('location:'.base_url());
		}
	
	}
	
}

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