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

class Stok_Opname 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');
			$tgl = $this->app_model->tgl_sql($this->input->post('cari_tgl'));
			
			$where = " WHERE kode_penyesuaian<>''";
			if(!empty($cari)){
				$where .= " AND kode_barang LIKE '%$cari%' OR nama_barang LIKE '%$cari%'";
			}
			if(!empty($tgl)){
				$where .= " AND tgl_penyesuaian='$tgl'";
			}
			
			$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 Stok Opname";
			
			//paging
			$page=$this->uri->segment(3);
			$limit=$this->config->item('limit_data');
			if(!$page):
			$offset = 0;
			else:
			$offset = $page;
			endif;
			
			$text = "SELECT * FROM vwstok_opname $where ";		
			$tot_hal = $this->app_model->manualQuery($text);		
			
			$d['tot_hal'] = $tot_hal->num_rows();
			
			$config['base_url'] = site_url() . '/stok_opname/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;
			
			date_default_timezone_set("Asia/Jakarta");
			$tgl	= date('d-m-Y');
			$d['tgl']	= $tgl;

			$text = "SELECT * FROM vwstok_opname $where
					LIMIT $limit OFFSET $offset";
			$d['data'] = $this->app_model->manualQuery($text);
			
			
			$d['content'] = $this->load->view('stok_opname/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 Stok Opname";
			
			$kode	= $this->app_model->MaxKodeBeli();
			$tgl	= date('d-m-Y');
			
			$tgl		    = date('d-m-Y');
			$d['tgl']	    = $tgl;
			$d['kode_brg']	= '';
			$d['stok']      = '';
			$d['stok_sblm'] = '';
			
			$d['content'] = $this->load->view('stok_opname/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']="Edit Data Stok Opname";
			
			$id = $this->uri->segment(3);
			$text = "SELECT * FROM tbpenyesuaian_stok WHERE kode_penyesuaian='$id'";
			$data = $this->app_model->manualQuery($text);
			if($data->num_rows() > 0){
				foreach($data->result() as $db){
					$d['kode_penyesuaian']	= $id;
					$d['tgl']	= $this->app_model->tgl_str($db->tgl_penyesuaian);
					$d['kode_brg']	= $db->kode_barang;
					$d['stok']	    = $db->jml;
					$d['stok_sblm']	= $db->jml_sebelum;
				}
			}else{
					$d['kode_penyesuaian']	=$id;
					$d['tgl']	='';
					$d['kode_brg']	='';
			}
			
			$text = "SELECT * FROM supplier";
			$d['l_supp'] = $this->app_model->manualQuery($text);
									
			$d['content'] = $this->load->view('stok_opname/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 tbpenyesuaian_stok WHERE kode_penyesuaian='$id'");
			echo "<meta http-equiv='refresh' content='0; url=".base_url()."index.php/stok_opname'>";			
		}else{
			header('location:'.base_url());
		}
	}
	public function hapus_detail()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){			
			$id = $this->uri->segment(3);
			$kode = $this->uri->segment(4);
			$this->app_model->manualQuery("DELETE FROM d_beli WHERE kodebeli='$id' AND kode_barang='$kode'");
			
			$this->edit();
		}else{
			header('location:'.base_url());
		}
	}
	
	public function simpan()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
				$tgl = $this->app_model->tgl_sql($this->input->post('tgl'));
				$up['kode_barang']		= $this->input->post('kode_brg');
				$up['jml']				= $this->input->post('jml');
				$up['jml_sebelum']		= $this->input->post('jml_sblm');
				$up['tgl_penyesuaian']	= $tgl;
				
				$id['kode_barang']		= $this->input->post('kode_brg');
				$id['tgl_penyesuaian']	= $tgl;
				
				$data = $this->app_model->getSelectedData("tbpenyesuaian_stok",$id);
				if($data->num_rows()>0){
					$this->app_model->updateData("tbpenyesuaian_stok",$up,$id);
					echo 'Update data Sukses';
				}else{
					$this->app_model->insertData("tbpenyesuaian_stok",$up);
					echo 'Simpan data Sukses';		
				}
		}else{
				header('location:'.base_url());
		}
	
	}
	
	public function DataDetail()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			
			$id = $this->input->post('kode');
			$text = "SELECT a.kodebeli,a.kode_barang,a.jmlbeli,a.hargabeli,a.expired,
					b.nama_barang,b.satuan
					FROM d_beli as a 
					JOIN barang as b
					ON a.kode_barang=b.kode_barang
					WHERE a.kodebeli='$id'";
			$d['data']= $this->app_model->manualQuery($text);

			$this->load->view('stok_opname/detail',$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'] = "Faktur Pembelian";
			
			$id = $this->uri->segment(3);
			$text = "SELECT * FROM tbpenyesuaian_stok WHERE kodebeli='$id'";
			$data = $this->app_model->manualQuery($text);
			if($data->num_rows() > 0){
				foreach($data->result() as $db){
					$d['kode_beli']	= $id;
					$d['tgl_beli']	= $this->app_model->tgl_indo($db->tglbeli);
					$d['supplier']	= $db->kode_supplier.' - '.$this->app_model->NamaSupp($db->kode_supplier);
				}
			}else{
					$d['kode_beli']		=$id;
					$d['tgl_beli']	='';
					$d['supplier']	='';
			}
			
			$text = "SELECT a.kodebeli,a.kode_barang,a.jmlbeli,a.hargabeli,
					b.nama_barang,b.satuan
					FROM d_beli as a 
					JOIN barang as b
					ON a.kode_barang=b.kode_barang
					WHERE a.kodebeli='$id'";
			$d['data']= $this->app_model->manualQuery($text);
									
			$this->load->view('stok_opname/cetak',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
}

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