<?php

CLASS lstokrt 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 Kartu 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("nm_gudang");
		$this->set->groupBy   	= "kd_gudang";
		$this->set->hitung    	= array("masuk", "keluar", "masukd", "keluard");
		$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" => "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()
	{
		$sat = $this->em->getFind("satuan", "nmsat as satuan", array("kd_inv = '".$_GET['kd_inv']."'", "skala = '1'"));
		$inv = $this->em->getFind("inv", "satuan, skalad", array("kd_inv = '".$_GET['kd_inv']."'"));
		
		$res[] = array('id' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'kd_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 40,  'size' => 5);
		//$res[] = array('id' => 'alias',		'nama' => ' ',  		'type' => 'text',   'width' => 40,  'size' => 5);
		$res[] = array('id' => 'nojnl',		'nama' => 'No.Transaksi','type' => 'text',   'width' => 130,  'size' => 30);
		$res[] = array('id' => 'satuan',	'nama' => 'Sat',  		'type' => 'text',   'width' => 40,  'size' => 5);

		
		$res[] = array('id' => 'masuk',		'nama' => 'Masuk',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Mutasi '.$sat->satuan);
		$res[] = array('id' => 'keluar',	'nama' => 'Keluar',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Mutasi '.$sat->satuan);
		$res[] = array('id' => 'qty',		'nama' => 'Stok',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Mutasi '.$sat->satuan);

		//$res[] = array('id' => 'satdef',	'nama' => 'Skala',  	'type' => 'date',   'width' => 100,  'size' => 100, 'align' => 'center');

		$res[] = array('id' => 'masukd',	'nama' => 'Masuk',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Mutasi '.$inv->satuan);
		$res[] = array('id' => 'keluard',	'nama' => 'Keluar',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Mutasi '.$inv->satuan);
		$res[] = array('id' => 'qtyd',		'nama' => 'Stok',   	'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2, 'induk' => 'Mutasi '.$inv->satuan);

		$res[] = array('id' => 'nm_sub',	'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);
		return $res;
	}
	
	public function filter($filter, $per)
	{
		$sql = $this->em->andWhere("a.kdac = '".$per->kdac."'");
		$sql = $this->em->andWhere("a.kd_inv = '".$filter->kd_inv->value."'");
		if(!empty($filter->kd_gudang->value)){
			$sql = $this->em->andWhere("a.kd_gudang = '".$filter->kd_gudang->value."'");
		}
		return $sql;
	}

	public function app($filter, $per, $dis = false)
	{
		$sql = $this->em->addSelect("a.kd_gudang
								, sum(a.masuk - a.keluar) as qty
								");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->getWhere("a.kdac = '".$per->kdac."'");
		$sql = $this->em->andWhere("a.tgl < '".date('Y-m-d', strtotime($filter->date1->value))."'");
		$sql = $this->filter($filter, $per);
		$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;
		}
		
		if($dis == true){
			echo $sql;
		}
		
		return $res;
	}

	public function mut($filter, $per, $dis = false)
	{
		$sql = $this->em->addSelect("a.nojnl, a.masuk, a.keluar, a.alias, a.kd_cabang
									, a.kd_gudang, b.nm_gudang, a.nm_sub
									, to_char(a.tgl, 'dd/mm/yyyy') as tgl
									");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->addLeftJoin("gudang", 'b', array("a.kd_gudang = b.kd_gudang"));
		$sql = $this->em->getWhere("1=1");
		if(!empty($filter->date1->value)){
			$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		}
		if(!empty($filter->date2->value)){
			$sql = $this->em->andWhere("a.tgl < '".date('Y-m-d', strtotime($filter->date2->value))."'");
		}
		$sql = $this->filter($filter, $per);
		$sql = $this->em->getMultiSort("a.tgl, a._no, a.nojnl");
		$eks = $this->em->queryEks($sql);
		
		$res = array();
		while($r = $this->em->fetchObject($eks)){
			$res[$r->kd_gudang][] = $r;
		}
		
		return $res;
	}

	public function query_detail()
	{
		$filter = $this->set->filter;
		
		$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
		$sAw = $this->app($filter, $per);
		
		$date = new Date;
		$fil = $filter;
		$fil->date2->value = $date->getPenjumlahanTanggal($fil->date2->value, 0, 1, 0);
		$mut = $this->mut($fil, $per);
		
		$fil = array();
		if(!empty($filter->kd_gudang->value)){
			$fil[] = "a.kd_gudang = '".$filter->kd_gudang->value."'";
		}
		
		$gdg = $this->em->getFindBy("gudang", "kd_gudang, nm_gudang", $fil, "kd_gudang");
		$sat = $this->em->getFind("satuan", "nmsat as satuan", array("kd_inv = '".$_GET['kd_inv']."'", "skala = '1'"));
		$inv = $this->em->getFind("inv", "satuan, skalad", array("kd_inv = '".$_GET['kd_inv']."'"));
		$res = array();
		$kde = new Kode;
		foreach($gdg as $r){
			
			if(empty($inv->skalad) or $inv->skalad == 0){
				$inv->skalad = 1;
			}
			
			$no = 1;
			$id = $r->kd_gudang.'.'.$kde->getRight($no++, 4, 0);
			$r->tgl 	= date('d/m/Y', strtotime($filter->date1->value));
			$r->nojnl 	= 'Saldo Awal';
			$r->qty 	= $sAw[$r->kd_gudang]->qty;
			$r->satuan 	= $sat->satuan;
			$r->satdef 	= $inv->skalad.'/'.$inv->satuan;
			$r->qtyd  	= $r->qty / $inv->skalad;
			$res[$id] 	= $r;
			
			$stok = $r->qty;
			if(!empty($mut[$r->kd_gudang])){
				foreach($mut[$r->kd_gudang] as $r2){
					$id = $r->kd_gudang.'.'.$kde->getRight($no++, 4, 0);
					$stok = $stok + ($r2->masuk - $r2->keluar);
					$r2->qty = $stok;
					$r2->satuan = $sat->satuan;
					$r2->satdef = $inv->skalad.'/'.$inv->satuan;
					$r2->masukd = $r2->masuk / $inv->skalad;
					$r2->keluard= $r2->keluar / $inv->skalad;
					$r2->qtyd  	= $r2->qty / $inv->skalad;
					$res[$id] = $r2;
				}
			}
		}
		
		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";
	}
}