<?php

CLASS lstkon 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 Konsolidasi';
		$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   	= "";
		$this->set->hitung    	= array();
		$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("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" => "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);
		$res[] = array('id' => 'nm_inv',	'nama' => 'Nama',  		'type' => 'text',   'width' => 250,  'size' => 20);
		$res[] = array('id' => 'satuan',	'nama' => 'Sat',  		'type' => 'text',   'width' => 40,  'size' => 5);

		$gdg = $this->em->getFindBy("gudang", "kd_gudang, nm_gudang", array(), "kd_gudang");
		foreach($gdg as $r){
			$id = strtolower("qty_".$r->kd_gudang);
			$id = str_ireplace(".", "_", $id);
			$res[] = array('id' => $id,		'nama' => $r->kd_gudang,   	'type' => 'numeric', 'width' => 100,  'size' => 15, 'dec' => 2, 'link' => array("attr" => array("kd_inv", "kd_gudang"), 'url' => $this->baseUrl.'lstokrt/View/&date1='.$_GET['date1'].'&date2='.$_GET['date1'].'&paper=P&convert=cetak'));
		}
		$res[] = array('id' => 'qty',		'nama' => 'Konsolidasi',   	'type' => 'numeric', 'width' => 100,  'size' => 15, 'dec' => 2);

		return $res;
	}

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

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

	public function query_detail()
	{
		$filter = $this->set->filter;
		
		$akhir 	= $filter->date1->value;
		$stok 	= $this->app($filter, $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($stok as $k => $r){
			foreach($r as $k2 => $r2){
				$id = strtolower("qty_".$r2->kd_gudang);
				$id = str_ireplace(".", "_", $id);
				$res[$r2->kd_inv]->$id = $r2->qty;
				$res[$r2->kd_inv]->qty = $res[$r2->kd_inv]->qty + $r2->qty;
				$res[$r2->kd_inv]->kd_gudang = $r2->kd_gudang;
			}
		}
		
		$gdg = $this->em->getFindBy("gudang", "kd_gudang, nm_gudang", array(), "kd_gudang");
		if($filter->status->value == 'ADA STOK'){
			foreach($res as $k => $r2){
				$dis = false;
				foreach($gdg as $r){
					$id = strtolower("qty_".$r->kd_gudang);
					$id = str_ireplace(".", "_", $id);
					if($r2->$id > 0){
						$dis = true;
					}
				}
				if($dis == true){
					
				}else{
					unset($res[$k]);
				}
			}
		}
		if($filter->status->value == 'TIDAK ADA STOK'){
			foreach($res as $k => $r){
				if($r->qty > 0){
					unset($res[$k]);
				}
			}
		}
		
		foreach($res as $k => $r){
			if(empty($r->skalad)){
				$r->skalad = 0;
			}
			if($r->skalad > 0){
				foreach($gdg as $r2){
					$id = strtolower("qty_".$r2->kd_gudang);
					$id = str_ireplace(".", "_", $id);
					$res[$k]->$id 	= $r->$id / $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()
	{
		
		$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";
	}
}