<?php

CLASS larc 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 Aging Piutang By Customer';
		$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' => '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" => "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' => 'kd_sub',	'nama' => 'Kode',  	'type' => 'text',   'width' => 80,  'size' => 25);
		$res[] = array('id' => 'nm_sub',	'nama' => 'Supplier',  	'type' => 'text',   'width' => 200,  'size' => 25);

		$res[] = array('id' => 'sisa',		'nama' => 'Hutang',   		'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true, 'link' => array("url" => $this->baseUrl.'lara/View/&date1='.$_GET['date1'].'&paper=P&convert=cetak&cek_det=t', 'attr' => array("kdac1", "kdac2", "kd_sub", "nm_sub")));
		$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' => '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()
	{
		// ini_set("display_errors", "On");
		$filter = $this->set->filter;
		$lapa = new lara($this->config, $this->em);
		$tmp = $lapa->genData($filter);
		$res = array();
		$hit = array("sisa", "curr", "over", "aging1", "aging2", "aging3", "aging4");
		foreach($tmp as $r){
			$id = $r->kdac.$r->kd_sub;
			if(empty($res[$id])){
				$r->kdac1 = $r->kdac;
				$r->kdac2 = $r->kdac;
				$res[$id] = $r;
			}else{
				foreach($hit as $rH){
					$res[$id]->$rH = $res[$id]->$rH + $r->$rH;
				}
			}
		}
		
		$sort1 = array();
		$sort2 = array();
		foreach($res as $k => $r){
			$sort1[$k] = $r->kdac;
			$sort2[$k] = $r->kd_sub;
		}
		array_multisort($sort1, SORT_ASC, $sort2, SORT_ASC, $res);
		
		return $res;
	}

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