<?php

CLASS larnbl extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;
	protected $min = '1.0.02.10';
	protected $max = '1.0.03.99';

	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-002';
		$this->set->date    = date("d/m/Y H:i");
		$this->set->modul     = 'Laporan Hutang By Nota Belum Lunas';
		$this->set->form    = $this->setForm();
		$this->set->display   = $this->setReport();
		$this->set->filter    = $this->report->setFilter($_GET, $this->setForm());
		$this->set->query     = false;
		$this->set->notNull   = array("date1", "date2", "paper");
		$this->set->button    = array("View", "Cetak", "Pdf", "Excel", "Csv");
		$this->set->groupDis  = array();
		$this->set->groupBy   = "nm_sub";
		$this->set->hitung    = array();
		$this->set->rowPage   = 60;
		$this->baseTitle      = $this->set->modul;
		return $this->set;
	}

	protected function defValue()
	{
		$lap = new Lap;
		$res->cabang  = $lap->getMinMax($this->set, "cabang", "kd_cabang");
		$res->account = $lap->getMinMax($this->set, "acc", "kdac");
		return $res; 
	}

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

		$row = 0;
		$form[] = array('row' =>  $row,  'col' => 0,   "label" => array("name" => "Date",   "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' => 200, "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' => 0,   "label" => array("name" => "Account",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'kdac1', "size" => 10, 'value' => '1.0.02.10', 'after' => 'load("macd", "false", "account")')));
		$form[] = array('row' =>  $row,  'col' => 200, "label" => array("name" => "s/d",   "width" => 30)  ,   "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'kdac2', "size" => 10, 'value' => '1.0.02.10', 	'after' => 'load("macd", "false", "account")')));

		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 0,   "label" => array("name" => "Sub",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'kd_sub', "size" => 10, 'after' => 'load("sub", "false", "sub")')));

		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 0,   "label" => array("name" => "No. Nota",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'nota', "size" => 20)));

        //$row = $row + 25;
		//$form[] = array('row' =>  $row,  'col' => 0,   "label" => array("name" => "PPN",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'ppn', "size" => 2, 'value' => 'F', 'after' => 'select()', "select" => 'tf')));

		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 0,   "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 setReport()
	{
	    $res[] = array('id' => 'kd_supp',	'nama' => 'Supplier',  	'type' => 'text',   'width' => 60,  'size' => 25, 'noduplicate' => true);
		//$res[] = array('id' => 'nm_sub',	'nama' => 'Customer',  	'type' => 'text',   'width' => 200,  'size' => 25, 'noduplicate' => true);
		$res[] = array('id' => 'tgl',		'nama' => 'Tgl',   	'type' => 'date', 	'width' => 80,  'size' => 10, 'induk' => 'Nota');
		$res[] = array('id' => 'nota',		'nama' => 'No', 	'type' => 'text',   'width' => 120,  'size' => 50, 'induk' => 'Nota', 'link' => array("attr" => array("nota"), "url" => $this->baseUrl.'up/cek_jurnal/'));
		$res[] = array('id' => 'debet',		'nama' => 'Jumlah',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true, 'link' => array("attr" => array("nota"), "url" => $this->baseUrl.'ldj/View/&nm_sub=&paper=P&convert=cetak'));
		$res[] = array('id' => 'ketm',	'nama' => 'Keterangan',  		'type' => 'text',   'width' => 260,  'size' => 50);
		
		//$res[] = array('id' => 'kdac',		'nama' => 'Acc', 		'type' => 'text',   'width' => 50,  'size' => 50);
		//$res[] = array('id' => 'kd_cabang',	'nama' => 'PC', 		'type' => 'text',   'width' => 30,  'size' => 50);
		
		
		//$res[] = array('id' => 'tgl_jt',	'nama' => 'Tgl. JT',   	'type' => 'date', 	'width' => 100,  'size' => 10);

		
		//$res[] = array('id' => 'credit',	'nama' => 'Bayar',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
		//$res[] = array('id' => 'sisa',		'nama' => 'Sisa',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
		//$res[] = array('id' => 'no_ref',	'nama' => '#Ref',  		'type' => 'text',   'width' => 130,  'size' => 25);

		return $res;
	}


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

	public function genData()
	{
		$filter = $this->set->filter;
	
		$sql = $this->em->addSelect("a.nota, max(a.kd_sub) as kd_sub, min(a.tgl) as tgl, max(a.nota_jt) as nota_jt
									, max(kdac) as kdac
									, max(kd_cabang) as kd_cabang
									");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.kdac >= '".$this->min."'");
		$sql = $this->em->andWhere("a.kdac <= '".$this->max."'");
		$sql = $this->em->andWhere("a.kdac >= '".$_GET['kdac1']."'");
		$sql = $this->em->andWhere("a.kdac <= '".$_GET['kdac2']."'");
		$sql = $this->em->getGroupBy("a.nota");
		$sel = $sql;

		$sql = $this->em->addSelect("a.nota
								, sum(COALESCE(credit, 0)) as debet
								, sum(COALESCE(debet, 0)) as credit
								, sum(COALESCE(credit, 0) - COALESCE(debet, 0)) as sisa
								");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.kdac >= '".$this->min."'");
		$sql = $this->em->andWhere("a.kdac <= '".$this->max."'");
		$sql = $this->em->andWhere("a.kdac >= '".$_GET['kdac1']."'");
		$sql = $this->em->andWhere("a.kdac <= '".$_GET['kdac2']."'");
		if(!empty($filter->akhir->value)){
			$sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->akhir->value))."'");
		}
		$sql = $this->em->getGroupBy("a.nota");
		$not = $sql;

		$sql = $this->em->addSelect("a.nota, a.tgl, a.kdac, a.kd_cabang
									, max(to_char(a.nota_jt, 'dd/mm/yyyy')) as tgl_jt
									, max(b.kd_sub) as kd_sub, max(c.nm_sub) as nm_sub
									, max(e.kd_sub) as kd_supp
									, sum(d.debet) as debet
									, sum(d.credit) as credit
									, sum(d.sisa) as sisa
									");
		$sql = $this->em->getFrom("($sel)", 'a');
		$sql = $this->em->addLeftJoin("acd", 'b', array("a.nota = b.nota", "a.tgl = b.tgl"));
		$sql = $this->em->addLeftJoin("sub", 'c', array("b.kd_sub = c.kd_sub"));
		$sql = $this->em->addLeftJoin("nbl", 'e', array("a.nota = e.nonbl"));
		$sql = $this->em->addLeftJoin("($not)", 'd', array("a.nota = d.nota"));
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("b.alias = 'NBL'");
		$sql = $this->em->andWhere("COALESCE(d.sisa, 0) <> 0");
		/*if($filter->ppn->value == 'T'){
			$sql = $this->em->andWhere("a.nota like 'PPN%'");
		}
		else if($filter->ppn->value == 'F'){
			$sql = $this->em->andWhere("a.nota like 'NON%'");
		}*/
		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->kd_sub->value)){
			$sql = $this->em->andWhere("b.kd_sub = '".strtoupper($filter->kd_sub->value)."'");
		}
		if(!empty($filter->nota->value)){
			$sql = $this->em->andWhere("a.nota = '".strtoupper($filter->nota->value)."'");
		}
		$sql = $this->em->getGroupBy("a.nota, a.tgl, a.kdac, a.kd_cabang");
		$sql = $this->em->getMultiSort("7, a.tgl, a.nota");
		//echo $sql;
		$eks = $this->em->queryEks($sql);

		$date = new Date;
		$res = array();
		while($r = $this->em->fetchObject($eks)){
			$i = '';
			$i = $r;
			
			$i->tgl = date("d/m/Y", strtotime($i->tgl));
			
			$res[] = $i;
		}
		
		return $res;
	}

	public function View()
	{
		$this->set->dataLoad = $this->genData();
		$this->data = $this->report->getDef($this->set);
		$this->layout = "report";
	}
}