<?php

CLASS lnbl 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();
		$this->set->groupBy   	= "nonbl";
		$this->set->hitung    	= array("total", "dpp", "ppn_nil");
		$this->set->rowPage   	= 60;
		$this->baseTitle      	= $this->set->modul;
		$this->set->grandtotal= true;
		return $this->set;
	}

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

	protected function setForm()
	{
		$this->defVal = $this->defValue();

		if(!empty($_GET['nonbl'])){
			$form[] = array('row' =>  0,  'col' => 25,   "label" => array("name" => "No.Nota",   "width" => 100),  "input" => array("type" => 'text',  'attr' => array("name" => 'nonbl')));
		}
		
		$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)));

		$form[] = array('row' => $row, 'col' => 430, "label" => array("name" => "S/d", "width" => 30), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub2', "size" => 9, 'after' => 'load("mastersupp", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub2=>nm_sub')));
		$form[] = array('row' => $row, 'col' => 550, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub2', "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" => "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" => "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.Nota',  	'type' => 'text',   'width' => 130,  'size' => 30, 'link' => array("attr" => array("nonbl"), 'url' => $this->baseUrl.'lnbl/View/&date1=01/10/2010&date2='.date("d/m/Y").'&mode=DETAIL&paper=P&convert=cetak'));
		$res[] = array('id' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10, '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' => 60,  'size' => 20, 'induk' => 'Supplier');
		$res[] = array('id' => 'nm_sub',	'nama' => 'Nama',  		'type' => 'text',   'width' => 150,  'size' => 20, 'induk' => 'Supplier');

		$res[] = array('id' => 'kd_inv',	'nama' => 'Kode',  		'type' => 'text',   'width' => 120,  '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' => 'skala',		'nama' => 'Skala',  	'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' => 'diskon_1',	'nama' => 'Diskon(%)',  	'type' => 'text',   'width' => 50,  'size' => 5, 'type' => 'numeric', 'dec' => 2);

		// for($i = 1; $i <= 5; $i++){
			// $iD = $i + 1;
			// $res[] = array('id' => 'diskon_'.$iD,		'nama' => 'D.'.$i.'(%)',  	'type' => 'text',   'width' => 50,  'size' => 5, 'type' => 'numeric', 'dec' => 2, 'induk' => 'Diskon Bertingkat');
		// }
		$res[] = array('id' => 'diskon',	'nama' => '(Rp)',  	'type' => 'text',   'width' => 60,  'size' => 20, 'type' => 'numeric');
		// $res[] = array('id' => 'diskg',		'nama' => 'Dis.G (%)',  	'type' => 'text',   'width' => 40,  'size' => 20, 'type' => 'numeric', 'dec' => 2);
		$res[] = array('id' => 'dpp',		'nama' => 'DPP',  	'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric', 'set' => 'dppBeli');
		$res[] = array('id' => 'ppn',		'nama' => 'Jen',   		'type' => 'text', 'width' => 30,  'size' => 20, 'induk' => 'PPN', 'align' => 'center');
		$res[] = array('id' => 'ppn_nil',	'nama' => 'Nilai',   		'type' => 'numeric', 'width' => 60,  'size' => 20, 'set' => 'ppnBeli', 'induk' => 'PPN');
		$res[] = array('id' => 'total',		'nama' => 'Total',  	'type' => 'text',   'width' => 90,  'size' => 20, 'type' => 'numeric', 'set' => 'totBeli');
		$res[] = array('id' => 'ket',		'nama' => 'Ket Header', 'type' => 'text',   'width' => 600,  'size' => 100);
		$res[] = array('id' => 'ket_det',	'nama' => 'Ket Detail', 'type' => 'text',   'width' => 200,  'size' => 100);

		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.*
						, a.ket||' '||a.ket2||' '||a.ket3 as ket
						");
		$sql = $this->em->getFrom("nbl", 'a');
		$sql = $this->em->addLeftJoin("nbld", 'b', array("a.nonbl = b.nonbl"));
		$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_sub2->value)){
			$sql = $this->em->andWhere("a.kd_sub <= '".$filter->kd_sub2->value."'");
		}
		if(!empty($filter->kd_inv->value)){
			$sql = $this->em->andWhere("b.kd_inv = '".$filter->kd_inv->value."'");
		}
		if(!empty($_GET['nonbl'])){
			$sql = $this->em->andWhere("a.nonbl = '".$_GET['nonbl']."'");
		}

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

		return $sql;
	}

	protected function report_rekap()
	{
		$res[] = array('id' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'nonbl',		'nama' => 'No.Nota',  	'type' => 'text',   'width' => 130,  'size' => 30, 'link' => array("attr" => array("nonbl"), 'url' => $this->baseUrl.'lnbl/View/&date1=01/10/2010&date2='.date("d/m/Y").'&mode=DETAIL&paper=P&convert=cetak'));
		$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' => 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, 'dd/mm/yyyy') as tgl
						, a.*
						");
		$sql = $this->em->getFrom("nbl", 'a');
		$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_sub2->value)){
			$sql = $this->em->andWhere("a.kd_sub <= '".$filter->kd_sub2->value."'");
		}

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

		return $sql;
	}

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

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