<?php

CLASS lsjla 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 Approval Surat Jalan';
		$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();
		
		if(!empty($_GET['nomut'])){
			$form[] = array('row' =>  0,  'col' => 25,   "label" => array("name" => "No.SJ",   "width" => 100),  "input" => array("type" => 'text',  'attr' => array("name" => 'nomut')));
		}
		
		$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 Asal", "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" => "Gudang Tujuan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_gudang2', "size" => 9, 'after' => 'load("mgud", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_gudang2=>nm_gudang')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_gudang2', "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" => 'statusPo')));
		
		$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' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'nomut',		'nama' => 'No.SJ',  	'type' => 'text',   'width' => 130,  'size' => 30, 'noduplicate' => true);
		$res[] = array('id' => 'noref',		'nama' => 'No.Ref#',  	'type' => 'text',   'width' => 130,  'size' => 30);
		$res[] = array('id' => 'kd_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 30,  'size' => 3);
		$res[] = array('id' => 'kd_sub',	'nama' => 'Kode',  		'type' => 'text',   'width' => 40,  'size' => 20, 'induk' => 'Gd. Asal');
		$res[] = array('id' => 'nm_sub',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Gd. Asal');
		$res[] = array('id' => 'kd_gudang',	'nama' => 'Kode',  		'type' => 'text',   'width' => 40,  'size' => 20, 'induk' => 'Gd. Tujuan');
		$res[] = array('id' => 'nm_gudang',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Gd. Tujuan');

		$res[] = array('id' => 'kd_inv',	'nama' => 'Kode',  		'type' => 'text',   'width' => 60,  'size' => 20, 'induk' => 'Barang');
		$res[] = array('id' => 'nm_inv',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Barang');
		$res[] = array('id' => 'satuan',	'nama' => 'Sat',  		'type' => 'text',   'width' => 40,  'size' => 5);

		$res[] = array('id' => 'qty',		'nama' => 'Qty',   		'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2);
		$res[] = array('id' => 'ket_det',	'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);

		return $res;
	}

	public function query_detail()
	{
		$filter = $this->set->filter;
		
		$sql = $this->em->addSelect("
						to_char(a.tgl, 'dd/mm/yyyy') as tgl
						, a.*, b.*
						");
		$sql = $this->em->getFrom("mut", 'a');
		$sql = $this->em->addLeftJoin("mutd", 'b', array("a.nomut = b.nomut"));
		$sql = $this->em->getWhere("a.control = '1'");
		$sql = $this->em->andWhere("a.jenis = '2'");
		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 23:59:59', strtotime($filter->date2->value))."'");
		}
		if(!empty($filter->cabang->value)){
			$sql = $this->em->andWhere("a.kd_cabang = '".$filter->cabang->value."'");
		}
		if(!empty($filter->kd_gudang->value)){
			$sql = $this->em->andWhere("a.kd_sub = '".$filter->kd_gudang->value."'");
		}
		if(!empty($filter->kd_gudang2->value)){
			$sql = $this->em->andWhere("a.kd_gudang = '".$filter->kd_gudang2->value."'");
		}
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("b.kd_inv = '".$filter->kd_inv->value."'");
		}
		if(!empty($_GET['nomut'])){
			$sql = $this->em->andWhere("a.noref = '".$_GET['nomut']."'");
		}

		$sql = $this->em->getMultiSort("a.tgl asc, a.nomut asc");

		return $sql;
	}

	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 	= $this->$query();
		$this->set->display	= $this->$report();

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