<?php

CLASS lnp 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-004';
    $this->set->date    = date("d/m/Y H:i");
    $this->set->modul     = 'Laporan Neraca Percobaan';
    $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   = "nojnl";
    $this->set->hitung    = array("awal", "debet", "credit", "saldo");
    $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");
    return $res; 
  }
  
  protected function setForm()
  {
    $this->defVal = $this->defValue();
    
    $form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Cabang",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang1', "size" => 5, 'value' => $this->defVal->cabang->min)));
    $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)));
    $form[] = array('row' =>  25,  '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' =>  25,  '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"))));
    $form[] = array('row' =>  50,  'col' => 0,   "label" => array("name" => "Bernilai Saja",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'nilai', "size" => 2, 'value' => 'T', 'after' => 'select()', "select" => 'tf')));
    $form[] = array('row' =>  75,  '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' => 'nmac',		'nama' => 'Account', 	'type' => 'text',   'width' => 400,  'size' => 300);
    $res[] = array('id' => 'awal',		'nama' => 'Awal',   	'type' => 'numeric', 'width' => 130,  'size' => 20, 'dec' => 2, 'null' => true);
    $res[] = array('id' => 'debet',		'nama' => 'Debet',   	'type' => 'numeric', 'width' => 130,  'size' => 20, 'dec' => 2, 'null' => true);
    $res[] = array('id' => 'credit',	'nama' => 'Credit',   	'type' => 'numeric', 'width' => 130,  'size' => 20, 'dec' => 2, 'null' => true);
    $res[] = array('id' => 'saldo',		'nama' => 'Saldo',   	'type' => 'numeric', 'width' => 130,  'size' => 20, 'dec' => 2, 'null' => true, 'set' => 'saldoAcd');
    
    return $res;
  }
  
  public function mutasi()
  {
    $filter = $this->set->filter;
    $sql = $this->em->addSelect("kdac, sum(debet) as debet, sum(credit) as credit");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->getWhere("1=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->cabang1->value)){
      $sql = $this->em->andWhere("a.kd_cabang >= '".strtoupper($filter->cabang1->value)."'");
    }
    if(!empty($filter->cabang2->value)){
      $sql = $this->em->andWhere("a.kd_cabang <= '".strtoupper($filter->cabang2->value)."'");
    }
    $sql = $this->em->getGroupBy("kdac");

    return $sql;
  }
  
  public function awal()
  {
    $filter = $this->set->filter;
    $sql = $this->em->addSelect("kdac, sum(a.debet-a.credit) as awal");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->getWhere("1=1");
     $sql = $this->em->andWhere("a.tgl < '".date('Y-m-d', strtotime($filter->date1->value))."'");
    if(!empty($filter->cabang1->value)){
      $sql = $this->em->andWhere("a.kd_cabang >= '".strtoupper($filter->cabang1->value)."'");
    }
    if(!empty($filter->cabang2->value)){
      $sql = $this->em->andWhere("a.kd_cabang <= '".strtoupper($filter->cabang2->value)."'");
    }
    $sql = $this->em->getGroupBy("kdac");

    return $sql;
  }
  
  public function setQuery()
  {
	$awal = $this->awal();
	$his  = $this->mutasi();

    $sql = $this->em->addSelect("a.kdac, a.induk, a.nmac, b.awal, c.debet, c.credit");
    $sql = $this->em->getFrom("acc", 'a');
    $sql = $this->em->addLeftJoin("($awal)", 'b', array("a.kdac = b.kdac"));
    $sql = $this->em->addLeftJoin("($his)", 'c', array("a.kdac = c.kdac"));
    $sql = $this->em->getWhere("1=1");
    $sql = $this->em->getMultiSort("a.kdac asc");

    return $sql;
  }
  
	public function genData()
	{
	    $filter = $this->set->filter;
		$sql = $this->setQuery();
		$eks = $this->em->queryEks($sql);
		
		$data = array();
		$ci = array();
		$ac = array();
		while($row = $this->em->fetchObject($eks)){
			$row->nmac = $row->kdac.'. '.$row->nmac;
			$data[$row->kdac] = $row;
			
			$ci[$row->induk][$row->kdac] = $row;
			$ac[$row->kdac] = $row;
		}
		
		$fres = array();
        foreach ($data as $k => $r) {
            $dis = true;
            $level = $this->level($r, $ci, $ac, 0);
            if(!empty($filter->nilai->value) == 'T') {
                if($r->debet + $r->credit + $r->awal == 0){
                    $dis = false;   
                }
            }
            $jum = $level * 2;
            $spasi = "";
            for ($i = 1; $i <= $jum; $i++) {
                $spasi .= "-";
            }
            $spasi = "<span style='color:#fff'>$spasi</span>";
            $r->nmac = $spasi.$r->nmac;
            
            if($dis == true){
                $fres[$k] = $r;
            }
        }
        
        return $fres;
	}
	
	public function level($k, $ci, $acc, $no)
	{
		if(!empty($k->induk)){
			$ac = $acc[$k->kdac];
			if(count($ci[$k->induk]) > 0){
				$rr = $acc[$k->induk];
				$no = $no + 1;
				$no = $this->level($rr, $ci, $acc, $no);
			}
		}
		
		return $no;
	}
  
  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";
  }
}