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

class Lap_selisih_stok 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 Selisih Stok Opname";

			$text = "SELECT tgl_penyesuaian FROM tbpenyesuaian_stok ORDER BY tgl_penyesuaian DESC LIMIT 1";
			$d['data'] = $this->app_model->manualQuery($text);

            $tgl1	= $this->app_model->TglSoSebelum();
            $d['tglso_sebelum']	= $tgl1;

			
			$d['content'] = $this->load->view('lap_selisih_stok/form', $d, true);
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function lihat()
	{
	    date_default_timezone_set("Asia/Jakarta");
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$tgl1	= $this->input->post('tgl1');
            $tgl2	= $this->input->post('tgl2');
			$pilih 	= $this->input->post('pilih');

			$text = "SELECT
	waringin_aptwaringin2.barang.kode_barang AS kode_barang,
	waringin_aptwaringin2.barang.nama_barang AS nama_barang,
	ifnull(a.jml_beli, 0) AS jml_beli,
	ifnull(b.jml_jual, 0) AS jml_jual,
	ifnull(c.jml_stok, 0) AS jml_stok,
	ifnull(a.jml_beli, 0) - ifnull(b.jml_jual, 0) + ifnull(c.jml_stok, 0) AS sisa_stok,
	ifnull(d.jml_stok_terbaru, 0) AS stok_terbaru,
	ifnull(a.jml_beli, 0) - ifnull(b.jml_jual, 0) + ifnull(c.jml_stok, 0) - ifnull(d.jml_stok_terbaru, 0) AS selisih,
	waringin_aptwaringin2.barang.satuan AS satuan,
	waringin_aptwaringin2.barang.harga_beli AS harga_beli
FROM
	(
		(
			(
				(
					waringin_aptwaringin2.barang
					LEFT JOIN (
						SELECT
							sum(vwpembelian.jmlbeli) AS jml_beli,
							vwpembelian.kode_barang AS kode_barang
						FROM
							waringin_aptwaringin2.vwpembelian
						WHERE
							vwpembelian.tglbeli BETWEEN '$tgl1' AND '$tgl2'
						GROUP BY
							vwpembelian.kode_barang
					) a ON (
						a.kode_barang = waringin_aptwaringin2.barang.kode_barang
					)
				)
				LEFT JOIN (
					SELECT
						vwdetail_jual.kode_barang AS kode_barang,
						sum(vwdetail_jual.jmljual) AS jml_jual
					FROM
						waringin_aptwaringin2.vwdetail_jual
					WHERE
						vwdetail_jual.tgljual BETWEEN '$tgl1' AND '$tgl2'
					GROUP BY
						vwdetail_jual.kode_barang
				) b ON (
					b.kode_barang = waringin_aptwaringin2.barang.kode_barang
				)
			)
			LEFT JOIN (
				SELECT
					waringin_aptwaringin2.tbpenyesuaian_stok.kode_barang AS kode_barang,
					waringin_aptwaringin2.tbpenyesuaian_stok.jml AS jml_stok
				FROM
					waringin_aptwaringin2.tbpenyesuaian_stok
				WHERE
					waringin_aptwaringin2.tbpenyesuaian_stok.tgl_penyesuaian = '$tgl1'
			) c ON (
				c.kode_barang = waringin_aptwaringin2.barang.kode_barang
			)
		)
		LEFT JOIN (
			SELECT
				waringin_aptwaringin2.tbpenyesuaian_stok.kode_barang AS kode_barang,
				waringin_aptwaringin2.tbpenyesuaian_stok.jml AS jml_stok_terbaru
			FROM
				waringin_aptwaringin2.tbpenyesuaian_stok
			WHERE
				waringin_aptwaringin2.tbpenyesuaian_stok.tgl_penyesuaian = '$tgl2'
		) d ON (
			d.kode_barang = waringin_aptwaringin2.barang.kode_barang
		)
	)
ORDER BY
	waringin_aptwaringin2.barang.kode_barang";
			$d['data'] = $this->app_model->manualQuery($text);
			
			$this->load->view('lap_selisih_stok/view',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function cetak()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$tgl = $this->uri->segment(3);
			
			$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 Selisih Stok";
			
			$text = "SELECT *, (selisih * harga_beli) AS kerugian FROM vwstok_opname WHERE ((selisih > 0) AND tgl_penyesuaian='$tgl')";
			$d['data'] = $this->app_model->manualQuery($text);
			
			$this->load->view('lap_selisih_stok/cetak',$d);
		}else{
			header('location:'.base_url());
		}
	}

}

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