<?php

CLASS lnbeli 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 Nota Beli';
		$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("nonbl");
		$this->set->groupBy   	= "nonbl";
		$this->set->grandtotal  = true;
		$this->set->hitung    	= array("total", "qty");
		$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['no_po'])){
			$form[] = array('row' =>  0,  'col' => 25,   "label" => array("name" => "No.PO",   "width" => 100),  "input" => array("type" => 'text',  'attr' => array("name" => 'no_po')));
		}
		
		$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" => "Supplier", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 9, 'after' => 'load("mastersupp", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub')));
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true)));

        //$row += 25;
		//$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_kat', "size" => 30, 'readonly' => true, 'child' => 'prv')));
		//$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Jenis Kendaraan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_kat', "size" => 9, 'after' => 'load("mkendaraan", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_kat=>nm_kat')));

		//$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" => "Mode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'mode', "size" => 10, 'value' => 'DETAIL', 'after' => 'select()', "select" => 'mode_report')));

		//$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' => 'nonbl',		'nama' => 'No. Beli',   		'type' => 'text', 'width' => 100,  'size' => 15, 'link' => array("attr" => array("nonbl"), 'url' => $this->baseUrl.'up/cek_jurnal/'));
	    $res[] = array('id' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10);
	    
	    $res[] = array('id' => 'kd_sub',	'nama' => 'Kode',  		'type' => 'text',   'width' => 60,  'size' => 20, 'induk' => 'Supplier');
		$res[] = array('id' => 'nm_sub',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Supplier');
	    $res[] = array('id' => 'ket',	'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);
	    
	    $res[] = array('id' => 'nolpb',		'nama' => 'No. LPB',   		'type' => 'text', 'width' => 100,  'size' => 20, 'link' => array("attr" => array("nolpb"), 'url' => $this->baseUrl.'up/cek_jurnal/'));
		
		//$res[] = array('id' => 'kd_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 30,  'size' => 3);
		
		$res[] = array('id' => 'kd_inv',	'nama' => 'Kode',  		'type' => 'text',   'width' => 60,  'size' => 20, 'induk' => 'Material');
		$res[] = array('id' => 'nm_inv',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Material');

		$res[] = array('id' => 'qty',		'nama' => 'Berat Bersih',   		'type' => 'numeric', 'width' => 60,  'size' => 15, 'dec' => 2);
		$res[] = array('id' => 'humid',		'nama' => 'Refraksi (%)',  	'type' => 'text',   'width' => 40,  'size' => 5, 'type' => 'numeric', 'dec' => 2);
		$res[] = array('id' => 'harga',		'nama' => 'Harga',  	'type' => 'text',   'width' => 80,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'total',		'nama' => 'Total',  	'type' => 'text',   'width' => 80,  'size' => 20, 'type' => 'numeric');
		
        

		return $res;
	}
	
	public function query_detail()
	{
		$filter = $this->set->filter;
		
		$sql = $this->em->addSelect("a.*, b.*, to_char(a.tgl, 'dd/mm/yyyy') as tgl, b.noref as nolpb");
		$sql = $this->em->getFrom("nbl", 'a');
		$sql = $this->em->addLeftJoin("nbld", 'b', array("a.nonbl = b.nonbl"));
		//$sql = $this->em->addLeftJoin("beli_det", 'b', array("a.no_po = b.no_po"));
		
		$sql = $this->em->getWhere("a.control = '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 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_sub->value)){
			$sql = $this->em->andWhere("a.kd_sub = '".$filter->kd_sub->value."'");
		}
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("a.kd_inv = '".$filter->kd_inv->value."'");
		}
		if(!empty($filter->kd_kat->value)){
			$sql = $this->em->andWhere("a.kd_kat = '".$filter->kd_kat->value."'");
		}
		if(strtoupper($_GET['status']) == 'OUTSTANDING'){
			$sql = $this->em->andWhere("COALESCE(b.qty, 0) > COALESCE(c.qty, 0)");
		}
		if(strtoupper($_GET['status']) == 'SUDAH DATANG'){
			$sql = $this->em->andWhere("COALESCE(b.qty, 0) <= COALESCE(c.qty, 0)");
		}

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

		return $sql; 
	}

	protected function report_rekap()
	{
		$res[] = array('id' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'no_po',		'nama' => 'No.PO',  	'type' => 'text',   'width' => 130,  'size' => 30, 'link' => array("attr" => array("no_po"), 'url' => $this->baseUrl.'lprc/View/&date1=01/10/2010&date2='.date("d/m/Y").'&mode=DETAIL&paper=P&convert=cetak'));
		$res[] = array('id' => 'kd_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 30,  'size' => 3);
		$res[] = array('id' => 'kd_sub',	'nama' => 'Kode',  		'type' => 'text',   'width' => 60,  'size' => 20, 'induk' => 'Supplier');
		$res[] = array('id' => 'nm_sub',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Supplier');

		$res[] = array('id' => 'dpp',		'nama' => 'DPP',  		'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'ppn',		'nama' => 'Jen',   		'type' => 'text', 'width' => 30,  'size' => 20, 'induk' => 'PPN', 'align' => 'center', 'induk' => 'PPN');
		$res[] = array('id' => 'ppn_nilai',	'nama' => 'Nilai',   	'type' => 'numeric', 'width' => 80,  'size' => 20, 'induk' => 'PPN');
		$res[] = array('id' => 'gtot',		'nama' => 'Total',  	'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric');
		$res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);

		return $res;
	}

	public function query_rekap()
	{
		$this->set->groupDis  	= array();
		$this->set->groupBy   	= "";
		
		$filter = $this->set->filter;

		$sql = $this->em->addSelect("
						to_char(a.tgl_po, 'dd/mm/yyyy') as tgl
						, a.*
						");
		$sql = $this->em->getFrom("beli", 'a');
		$sql = $this->em->getWhere("a.control = '1'");
		if(!empty($filter->date1->value)){
			$sql = $this->em->andWhere("a.tgl_po >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		}
		if(!empty($filter->date2->value)){
			$sql = $this->em->andWhere("a.tgl_po <= '".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_sub->value)){
			$sql = $this->em->andWhere("a.kd_sub = '".$filter->kd_sub->value."'");
		}

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

		return $sql;
	}

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

	public function View()
	{
		$mode 	= strtolower($_GET['mode']);
		$mode = '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";
	}
}