<?php

CLASS llr_xx 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 Laba Rugi';
    $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("Account" => 'kdac');
    $this->set->groupBy   = "";
    $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();
    
    $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" => "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' => 'ket',		'nama' => 'Nama Account', 	'type' => 'text',   'width' => 500,  'size' => 200);
    $res[] = array('id' => 'saldo',		'nama' => 'Saldo',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2, 'null' => true);
    
    return $res;
  }
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
  public function getNilai()
  {
    $filter = $this->set->filter;

    $sql = $this->em->addSelect("kdac, sum(debet-credit) as saldo");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->getWhere("kdac >= '2'");
    $sql = $this->em->ANDWhere("alias != 'JPN'");
    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");
	
    $sql = $this->em->getMultiSort("kdac asc");
	$eks = $this->em->queryEks($sql);
	$res = array();
	while($row = $this->em->fetchObject($eks)){
		$res[$row->kdac] = $row->saldo;
	}
	
	return $res;
  }
  
  public function cekTotal($data, $temp, $nilai, $tot = 0)
  {
		foreach($data as $row){
			if(!empty($nilai[$row->kdac])){
				$tot = $tot + $nilai[$row->kdac];
			}
			if(count($temp[$row->kdac])){
				$tot = $this->cekTotal($temp[$row->kdac], $temp, $nilai, $tot);
			}
		}
		return $tot;
  }
  
  public function cekDet($res, $temp, $nilai, $row, $jum = 0)
  {
		$kode =  new kode;
		$jrk = $jum * 5;
		$spasi = $kode->getRight('', $jrk, '.');
		$spasi = "<span style='color: #fff'>$spasi</span>";
		$res[$row->kdac]->ket = $spasi.$row->kdac.'. '.$row->nmac;
		if(!empty($nilai[$row->kdac])){
			$res[$row->kdac]->saldo = $nilai[$row->kdac];
		}
		if(count($temp[$row->kdac])){
			$jum = $jum + 1;
			foreach($temp[$row->kdac] as $row2){
				$res = $this->cekDet($res, $temp, $nilai, $row2, $jum);
			}
			$total = $this->cekTotal($temp[$row->kdac], $temp, $nilai);
			$jrk = ($jum * 5) - 2;
			$spasi = $kode->getRight('', $jrk, '.');
			$spasi = "<span style='color: #fff'>$spasi</span>";
			$res[$row->kdac.'A']->ket = $spasi.'Total '.$row->kdac.'. '.$row->nmac;
			$res[$row->kdac.'A']->saldo = $total;
		}
		
		return $res;
  }
  
  public function genData()
  {
	$nilai = $this->getNilai();
  
	$kode = new Kode;
	$data = $this->em->getFindBy("acc", "kdac, nmac, induk", array("kdac >= '2'"), "kdac asc");
	$temp = array();
	foreach($data as $row){
		$temp[$row->induk][$row->kdac] = $row;
	}
	
	$data = $this->em->getFindBy("acc", "kdac, nmac", array("kdac >= '2'", "(induk is null or induk = '')"), "kdac asc");
	$res = array();
	$total = 0;
	foreach($data as $row){
		$res = $this->cekDet($res, $temp, $nilai, $row, 0);
		$tot = 0;
		if(!empty($nilai[$row->kdac])){
			$tot = $nilai[$row->kdac];
		}
		$total = $total + $tot;
		if(count($temp[$row->kdac])){
			$tot = $this->cekTotal($temp[$row->kdac], $temp, $nilai, 0);
		}
		$total = $total + $tot;
	}
	$res['z']->ket = 'Total Laba Rugi';
	$res['z']->saldo = $total;
	
	return $res;
  }
  
  public function View()
  {
	$this->set->dataLoad = $this->genData();
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}