<?php

CLASS arout 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-006';
    $this->set->date    = date("d/m/Y H:i");
    $this->set->modul     = 'LAPORAN UMUR PIUTANG USAHA';
    $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");
    $this->set->groupDis  = array();
    $this->set->groupBy   = "";
    $this->set->hitung    = array("debet", "credit", "saldo", "current", "out1", "out2", "out3", "out4", "sisa");
    $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();
    
    // $form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Profit Center",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang1', "size" => 5, 'value' => $this->defVal->cabang->min, 'after' => 'load("cabang", "false", "cabang")')));
    // $form[] = array('row' =>  0,  'col' => 170, "label" => array("name" => "s/d",   "width" => 30)  ,   "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang2', "size" => 5, 'value' => $this->defVal->cabang->max, 'after' => 'load("cabang", "false", "cabang")')));
    $form[] = array('row' =>  25,  'col' => 0,   "label" => array("name" => "Periode S/d",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));
  
    $form[] = array('row' =>  50,  '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' => 'nm_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 50,  'size' => 10);
    $res[] = array('id' => 'unit',		'nama' => 'Unit',   	'type' => 'text', 	'width' => 50,  'size' => 10);
    $res[] = array('id' => 'kd_sub',	'nama' => 'Kd. Cust',  	'type' => 'text',   'width' => 100,  'size' => 20);
    $res[] = array('id' => 'nm_sub',	'nama' => 'Nm. Cust', 	'type' => 'text',   'width' => 180,  'size' => 25);
    $res[] = array('id' => 'nota_jt',	'nama' => 'Jatuh Tempo', 'type' => 'date',   'width' => 80,  'size' => 25);
    // $res[] = array('id' => 'debet',		'nama' => 'Nilai',   	'type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true);
    // $res[] = array('id' => 'credit',	'nama' => 'Bayar',   	'type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true);
    $res[] = array('id' => 'current',	'nama' => 'Current',	'type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true);
    $res[] = array('id' => 'saldo',		'nama' => 'Outstanding','type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true);
    $res[] = array('id' => 'out1',		'nama' => '1-30 Hari','type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true, 'induk' => 'Aging');
    $res[] = array('id' => 'out2',		'nama' => '31-60 Hari','type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true, 'induk' => 'Aging');
    $res[] = array('id' => 'out3',		'nama' => '61-90 Hari','type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true, 'induk' => 'Aging');
    $res[] = array('id' => 'out4',		'nama' => '> 90 Hari','type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true, 'induk' => 'Aging');
    $res[] = array('id' => 'sisa',		'nama' => 'Total','type' => 'numeric', 'width' => 80,  'size' => 15, 'null' => true);
    $res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);
    // $res[] = array('id' => 'sel',		'nama' => 'Sel', 'type' => 'text',   'width' => 50,  'size' => 60);
    // $res[] = array('id' => 'saiki',		'nama' => 'Saiki', 'type' => 'text',   'width' => 100,  'size' => 60);
    
    return $res;
  }
  
  public function bayar()
  {
    $sql = $this->em->addSelect("a.nota, sum(credit) as credit");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->getWhere("a.nota is not null");
    $sql = $this->em->andWhere("a.nota != ''");
    $sql = $this->em->andWhere("a.credit > 0");
    $sql = $this->em->andWhere("(a.kd_cabang = 'GR1' or a.kd_cabang = 'GR2')");
    if(!empty($filter->date1->value)){
      $sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date1->value))."'");
    }
    $sql = $this->em->getGroupBy("a.nota");
	return $sql;
  }
  
  public function setQuery()
  {
    $filter = $this->set->filter;
	$bayar = $this->bayar($filter);
	
    $sql = $this->em->addSelect("
									a.nota, a.unit, a.nm_cabang, a.kd_sub, a.nm_sub, a.ket, a.kdac
									, a.debet, b.credit, (a.debet - COALESCE(b.credit, 0)) as sisa
									, to_char(a.nota_jt, 'dd/mm/yyyy') as nota_jt
									, a.nota_jt as jt
								");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->addLeftJoin("($bayar)", 'b', array("a.nota = b.nota"));
    $sql = $this->em->getWhere("a.nota is not null");
    $sql = $this->em->andWhere("a.nota != ''");
    $sql = $this->em->andWhere("a.debet > 0");
    $sql = $this->em->andWhere("(a.debet - COALESCE(b.credit, 0)) > 0");
    $sql = $this->em->andWhere("(a.kd_cabang = 'GR1' or a.kd_cabang = 'GR2')");
    if(!empty($filter->date1->value)){
      $sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date1->value))."'");
    }
    $sql = $this->em->getMultiSort("a.nota_jt asc, a.unit asc");

    return $sql;
  }
  
	public function genData()
	{
		$filter = $this->set->filter;
	
		$sel = $this->setQuery();
		$sql = $this->em->addSelect("*");
		$sql = $this->em->getFrom("($sel)", 'a');
		$sql = $this->em->getWhere("1=1");
		$eks = $this->em->queryEks("$sql");
		$res = array();
		$no = 0;
		$date = new date;
		$saiki = date('Y-m-d', strtotime($filter->date1->value));
		while($row = $this->em->fetchObject($eks)){
			$sel = $date->selisihTanggal($row->jt, $saiki);
			
			$row->sel = $sel;
			$row->saiki = $saiki;
			
			$row->current = 0;
			$row->saldo = 0;
			$row->out1 = 0;
			$row->out2 = 0;
			$row->out3 = 0;
			$row->out4 = 0;
			
			if($sel <= 0){
				$row->current = $row->sisa;
			}else{
				$row->saldo = $row->sisa;
				
				if($sel >= 1 and $sel <= 30){
					$row->out1 = $row->sisa;
				}else
				if($sel >= 31 and $sel <= 60){
					$row->out2 = $row->sisa;
				}else
				if($sel >= 61 and $sel <= 90){
					$row->out3 = $row->sisa;
				}else
				if($sel > 90){
					$row->out4 = $row->sisa;
				}
				
			}
		
		
			$res[$no] = $row;
			$no++;
		}
		return $res;
	}
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
  public function View()
  {
	$this->set->dataLoad = $this->genData();
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}