<?php

CLASS lapa extends RasioProject
{
	protected $set;
	protected $enableCk = false;
	protected $report;
	protected $baseTitle;
	protected $min = '0.0.04.10';
	protected $max = '0.0.04.99';

	public function __construct($config, $em = false)
	{
		$this->getConfig($config);
		$this->validation();

		if($em != false){
			$this->em = $em;
		}

		$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 Aging Piutang By Nota';
		$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("Account" => 'kdac');
		$this->set->groupBy   = "kdac";
		$this->set->hitung    = array("debet", "credit", "sisa", "curr", "over", "aging1", "aging2", "aging3", "aging4");
		$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" => "Periode",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "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' => '0.0.04.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' => '0.0.04.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" => "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' => 'tgl',		'nama' => 'Tanggal',   	'type' => 'date', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'kdac',		'nama' => 'Acc', 		'type' => 'text',   'width' => 50,  'size' => 50);
		$res[] = array('id' => 'nota',		'nama' => 'No. Nota', 	'type' => 'text',   'width' => 200,  'size' => 50, 'link' => array("attr" => array("nojnl"), "url" => $this->baseUrl.'oinv/index/operational/'));
		$res[] = array('id' => 'kd_sub',	'nama' => 'Kode',  	'type' => 'text',   'width' => 80,  'size' => 25);
		$res[] = array('id' => 'nm_sub',	'nama' => 'Customer',  	'type' => 'text',   'width' => 200,  'size' => 25);
		$res[] = array('id' => 'tgl_jt',	'nama' => 'Tgl. JT',   	'type' => 'date', 	'width' => 100,  'size' => 10);

		$res[] = array('id' => 'debet',		'nama' => 'Piutang',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true, 'link' => array("attr" => array("nota", "kdac"), "url" => $this->baseUrl.'ldj/View/&date1=01/01/2017&date2='.$_GET['date1'].'&paper=P&convert=cetak'));
		$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' => 'curr',		'nama' => 'Current',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
		$res[] = array('id' => 'over',		'nama' => 'Overdue',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
		$res[] = array('id' => 'day',		'nama' => 'Hari',  		'type' => 'numeric', 'width' => 30,  'size' => 15, 'null' => true);
		$res[] = array('id' => 'aging1',	'nama' => '0-30 Hari',  'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
		$res[] = array('id' => 'aging2',	'nama' => '31-60 Hari', 'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
		$res[] = array('id' => 'aging3',	'nama' => '61-90 Hari', 'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
		$res[] = array('id' => 'aging4',	'nama' => '> 90 Hari',  'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);

		return $res;
	}


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

	public function genData($filter = false)
	{
		if($filter == false){
			$filter = $this->set->filter;
		}
	
		$sql = $this->em->addSelect("a.nota, max(a.kd_sub) as kd_sub
									, min(a.tgl) as tgl
									, min(a.nota_tgl) as nota_tgl
									, max(a.nota_jt) as nota_jt
									, max(kdac) as kdac
									, max(kd_cabang) as kd_cabang
									, sum(debet-credit) as sisa
									, sum(debet) as debet
									, sum(credit) as credit
									");
		$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->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date1->value))."'");
		if(!empty($filter->nota->value)){
			$sql = $this->em->andWhere("a.nota = '".strtoupper($filter->nota->value)."'");
		}
		$sql = $this->em->getGroupBy("a.nota");
		$sel = $sql;

		$sql = $this->em->addSelect("a.*, b.nm_sub
									, a.nota_jt as jt
									, a.tgl as tgla
									, to_char(a.nota_jt, 'dd/mm/yyyy') as tgl_jt
									, to_char(a.tgl, 'dd/mm/yyyy') as tgl
									");
		$sql = $this->em->getFrom("($sel)", 'a');
		$sql = $this->em->addLeftJoin("sub", 'b', array("a.kd_sub = b.kd_sub"));
		$sql = $this->em->getWhere("COALESCE(a.sisa, 0) <> 0");
		if(!empty($filter->kd_sub->value)){
			$sql = $this->em->andWhere("b.kd_sub = '".strtoupper($filter->kd_sub->value)."'");
		}
		if($_GET['cek_det'] == 't' and empty($_GET['kd_sub'])){
			$sql = $this->em->andWhere("(b.kd_sub = '' or b.kd_sub is null)");
		}
		$sql = $this->em->getMultiSort("a.tgl, a.nota");
		$eks = $this->em->queryEks($sql);
		
		$date = new Date;
		$res = array();
		while($r = $this->em->fetchObject($eks)){
			
			if(empty($r->tgl_jt)){
				$r->tgl_jt = date("d/m/Y", strtotime($r->tgla));
				$r->nota_jt = $r->tgla;
			}
			$sel = $date->selisihTanggal($r->nota_jt, $filter->date1->value);
			if($sel < 0){
				$sel = 0;
			}
			$r->day = $sel;
			
			if($filter->date1->value > $r->nota_jt){
				$r->over = $r->sisa;
			}else{
				$r->curr = $r->sisa;
			}
			
			if($r->day > 0 and $r->day <= 30){
				$r->aging1 = $r->sisa;
			}elseif($r->day >= 31 and $r->day <= 60){
				$r->aging2 = $r->sisa;
			}elseif($r->day >= 61 and $r->day <= 90){
				$r->aging3 = $r->sisa;
			}elseif($r->day > 90){
				$r->aging4 = $r->sisa;
			}
			
			$i = '';
			$i = $r;
			
			if(empty($r->nm_sub)){
				$r->nm_sub = 'TANPA SUB';
			}
			
			$res[] = $i;
		}
		
		return $res;
	}

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