<?php

CLASS lstkgd extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
	}

	protected function set()
	{
		$this->report       	= new Report;
		$this->set->em      	= $this->em;
		$this->set->kode    	= 'ACC-001';
		$this->set->date      	= date("d/m/Y H:i");
		$this->set->modul     	= 'Laporan Posisi Stok';
		$this->set->form      	= $this->setForm();
		$this->set->filter    	= $this->report->setFilter($_GET, $this->setForm());
		$this->set->notNull   	= array("date1", "date2", "paper");
		$this->set->button    	= array("View", "Cetak", "Pdf", "Excel");
		$this->set->groupDis  	= array();
		$this->set->groupBy   	= "apank";
		$this->set->hitung    	= array("vawal", "vmasuk", "vkeluar", "val");
		$this->set->rowPage   	= 60;
		$this->baseTitle      	= $this->set->modul;
		return $this->set;
	}

	protected function defValue()
	{
		$lap = new Lap;
		$res->cabang  = '';
		return $res; 
	}

	protected function setForm()
	{
		$this->defVal = $this->defValue();
		
		$row = 0;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "size" => 10, 'class' => 'tanggal', 'value' => date("01/m/Y"))));
		$form[] = array('row' =>  $row,  'col' => 250, "label" => array("name" => "s/d",     "width" => 30),   "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date2', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Profit Center", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_cabang', "size" => 9, 'after' => 'load("cabang", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_cabang=>nm_cabang')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Gudang", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_gudang', "size" => 9, 'after' => 'load("mgud", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_gudang=>nm_gudang')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_gudang', "size" => 30, 'readonly' => true)));

		$row = $row + 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Barang", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_inv', "size" => 9, 'after' => 'load("minv", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_inv=>nm_inv')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_inv', "size" => 30, 'readonly' => true)));

		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Status",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'status', "size" => 10, 'after' => 'select()', "select" => 'statusStok')));
		
		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 25,   "label" => array("name" => "Paper",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'paper', "size" => 2, 'value' => 'P', 'after' => 'select()', "select" => 'paper')));

		return $form;
	}

	protected function report_detail()
	{
		$res[] = array('id' => 'kd_inv',	'nama' => 'Kode',  		'type' => 'text',   'width' => 100,  'size' => 20, 'induk' => 'Barang');
		$res[] = array('id' => 'nm_inv',	'nama' => 'Nama',  		'type' => 'text',   'width' => 300,  'size' => 20, 'induk' => 'Barang');
		$res[] = array('id' => 'satuan',	'nama' => 'Sat',  		'type' => 'text',   'width' => 40,  'size' => 5);

		$res[] = array('id' => 'awal',		'nama' => 'Awal',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Qty');
		$res[] = array('id' => 'masuk',		'nama' => 'Masuk',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Qty');
		$res[] = array('id' => 'keluar',	'nama' => 'Keluar',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Qty');
		$res[] = array('id' => 'qty',		'nama' => 'Stok',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Qty', 'link' => array("attr" => array("kd_inv"), 'url' => $this->baseUrl.'lstokrt/View/&date1='.$_GET['date1'].'&date2='.$_GET['date2'].'&kd_gudang='.$_GET['kd_gudang'].'&paper=P&convert=cetak'));

		$res[] = array('id' => 'vawal',		'nama' => 'Awal',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Value');
		$res[] = array('id' => 'vmasuk',	'nama' => 'Masuk',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Value');
		$res[] = array('id' => 'vkeluar',	'nama' => 'Keluar',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Value');
		$res[] = array('id' => 'val',		'nama' => 'Nilai',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Value', 'link' => array("attr" => array("kd_inv"), 'url' => $this->baseUrl.'lstokrt/View/&date1='.$_GET['date1'].'&date2='.$_GET['date2'].'&kd_gudang='.$_GET['kd_gudang'].'&paper=P&convert=cetak'));

		return $res;
	}

	public function app($filter, $awal, $akhir, $dis = false)
	{
		$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			
		$sql = $this->em->addSelect("a.kd_inv
								, sum(a.masuk - a.keluar) as qty
								, sum(a.masuk) as masuk
								, sum(a.keluar) as keluar
								, sum(a.debet - a.credit) as val
								, sum(a.debet) as debet
								, sum(a.credit) as credit
								");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->getWhere("a.kdac = '".$per->kdac."'");
		if(!empty($filter->date1->value)){
			$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($awal))."'");
		}
		if(!empty($filter->date2->value)){
			$sql = $this->em->andWhere("a.tgl < '".date('Y-m-d', strtotime($akhir))."'");
		}
		if(!empty($filter->kd_gudang->value)){
			$sql = $this->em->andWhere("a.kd_gudang = '".$filter->kd_gudang->value."'");
		}
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("a.kd_inv = '".$filter->kd_inv->value."'");
		}

		$sql = $this->em->getGroupBy("a.kd_inv");
		$eks = $this->em->queryEks($sql);
		$res = array();
		while($r = $this->em->fetchObject($eks)){
			$res[$r->kd_inv] = $r;
		}
		
		if($dis == true){
			echo $sql;
		}
		
		return $res;
	}

	public function query_detail()
	{
		$filter = $this->set->filter;
		
		$awal 	= "1970-01-01";
		$akhir 	= $filter->date1->value;
		$sAw = $this->app($filter, $awal, $akhir);

		$date	= new Date;
		$awal 	= $filter->date1->value;
		$akhir 	= $date->getPenjumlahanTanggal($filter->date2->value, 0, 1, 0);
		$mut = $this->app($filter, $awal, $akhir);
		
		$sql = $this->em->addSelect("a.kd_inv, a.nm_inv, a.satuan, a.skalad");
		$sql = $this->em->getFrom("inv", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.jenis = '11'");
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("a.kd_inv = '".$filter->kd_inv->value."'");
		}
		$sql = $this->em->getMultiSort("a.kd_inv, a.nm_inv asc");
		$eks = $this->em->queryEks($sql);

		$res = array();
		while($r = $this->em->fetchObject($eks)){
			$res[$r->kd_inv] = $r;
		}
		foreach($sAw as $k => $r){
			if(!empty($res[$k])){
				$res[$k]->awal 	= $r->qty;
				$res[$k]->vawal = $r->val;
			}else{
				$res[$k]->kd_inv = $k;
				$res[$k]->awal 	 = $r->qty;
				$res[$k]->vawal  = $r->val;
			}
		}
		foreach($mut as $k => $r){
			$res[$k]->masuk  = $r->masuk;
			$res[$k]->keluar = $r->keluar;
		}
		foreach($res as $k => $r){
			$res[$k]->qty = $r->awal + ($r->masuk - $r->keluar);
		}
		
		foreach($res as $k => $r){
			if($filter->status->value == 'ADA STOK'){
				if($r->qty <= 0){
					unset($res[$k]);
				}
			}
			if($filter->status->value == 'ADA PROGRES'){
				$dis = false;
				if($r->masuk > 0 or $r->keluar > 0){
					$dis = true;
				}
				if($dis == false){
					unset($res[$k]);
				}
			}
			if($filter->status->value == 'TIDAK ADA STOK'){
				if($r->qty > 0){
					unset($res[$k]);
				}
			}
		}
		
		foreach($res as $k => $r){
			if(empty($r->skalad)){
				$r->skalad = 0;
			}
			if($r->skalad > 0){
				$res[$k]->awal 	= $r->awal / $r->skalad;
				$res[$k]->masuk = $r->masuk / $r->skalad;
				$res[$k]->keluar= $r->keluar / $r->skalad;
				$res[$k]->qty 	= $r->qty / $r->skalad;
			}
		}
		
		return $res;
	}

	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/report";
	}

	public function View()
	{
		if(empty($_GET['kd_gudang'])){
			echo "<script>alert('Gudang tidak boleh kosong');</script>";
			$this->layout = "null";
			return;
		}
		
		$mode 	= strtolower('detail');
		$query 	= "query_".$mode;
		$report = "report_".$mode;
		
		$this->set->query 	= false;
		$this->set->dataLoad= $this->$query();
		$this->set->display	= $this->$report();

		$this->data = $this->report->getDef($this->set);
		$this->layout = "report";
	}
}