<?php

CLASS lbp 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-002';
    $this->set->date    = date("d/m/Y H:i");
    $this->set->modul     = 'Laporan Buku Besar Pembantu';
    $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();
    $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" => "Date",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "size" => 10, 'class' => 'tanggal', 'value' => date("01/01/2025"))));
    $form[] = array('row' =>  $row,  '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"))));

	$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' => $this->defVal->account->min, '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' => $this->defVal->account->max, 	'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" => 'sub', "size" => 10, 'after' => 'load("sub", "false", "sub")')));
  
	$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' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
    $res[] = array('id' => 'nojnl',	'nama' => 'No. Jurnal', 'type' => 'text',   'width' => 180,  'size' => 25);
    $res[] = array('id' => 'kd_cabang',	'nama' => 'Cab',  		'type' => 'text',   'width' => 50,  'size' => 3);
    $res[] = array('id' => 'kd_sub',	'nama' => 'Sub',  		'type' => 'text',   'width' => 110,  'size' => 20);
    $res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 300,  'size' => 60);
    $res[] = array('id' => 'debet',		'nama' => 'Debet',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2, 'null' => true);
    $res[] = array('id' => 'credit',	'nama' => 'Credit',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2, 'null' => true);
    $res[] = array('id' => 'saldo',		'nama' => 'Saldo',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'dec' => 2, 'null' => true);
    $res[] = array('id' => 'notas',		'nama' => '', 			'type' => 'text',   'width' => 10,  'size' => 60);
    //$res[] = array('id' => 'nota',		'nama' => '#Ref', 'type' => 'text',   'width' => 150,  'size' => 60);
    
    return $res;
  }
  
  public function setQuery()
  {
    $filter = $this->set->filter;
	
    $sql = $this->em->addSelect("
						a.nojnl, to_char(a.tgl, 'dd/mm/yyyy') as tgl, a.ket, a.kdac, a.kd_sub, a.nota
						, a.kd_cabang, debet, credit, (COALESCE(debet, 0) - COALESCE(credit, 0)) as saldo
						");
    $sql = $this->em->getFrom("acd", 'a');
	$sql = $this->em->addLeftJoin('cabang', 'b', array("a.kd_cabang = b.kd_cabang"));
	$sql = $this->em->addLeftJoin('sub_cab', 'c', array("b.kd_sub_cab = c.kd_sub_cab"));
    $sql = $this->em->getWhere("1=1");
    $sql = $this->em->andWhere("a.alias != 'TTS'");
    $sql = $this->em->andWhere("a.alias != 'PCR'");
    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->cabang->value)){
		$sql = $this->em->andWhere("a.kd_cabang = '".$filter->cabang->value."'");
	}
	if(!empty($filter->sub_grup->value)){
		$sql = $this->em->andWhere("b.kd_sub_cab = '".$filter->sub_grup->value."'");
	}
	if(!empty($filter->grup->value)){
		$sql = $this->em->andWhere("c.kd_grp_cab = '".$filter->grup->value."'");
	}
    if(!empty($filter->kdac1->value)){
      $sql = $this->em->andWhere("a.kdac >= '".strtoupper($filter->kdac1->value)."'");
    }
    if(!empty($filter->kdac2->value)){
      $sql = $this->em->andWhere("a.kdac <= '".strtoupper($filter->kdac2->value)."'");
    }
    if(!empty($filter->sub->value)){
      $sql = $this->em->andWhere("a.kd_sub = '".strtoupper($filter->sub->value)."'");
    }
    $sql = $this->em->getMultiSort("a.kdac asc, a.tgl asc, a.nojnl asc");
	$eks = $this->em->queryEks($sql);
	$res = array();
	while($row = $this->em->fetchObject($eks)){
		$res[$row->kdac][] = $row;
	}
	return $res;
  }
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
  public function awal($filter)
  {
    $sql = $this->em->addSelect("kdac, sum(debet-credit) as saldo");
    $sql = $this->em->getFrom("acd", 'a');
	$sql = $this->em->addLeftJoin('cabang', 'b', array("a.kd_cabang = b.kd_cabang"));
	$sql = $this->em->addLeftJoin('sub_cab', 'c', array("b.kd_sub_cab = c.kd_sub_cab"));
    $sql = $this->em->getWhere("1=1");
    $sql = $this->em->andWhere("a.tgl < '".date('Y-m-d', strtotime($filter->date1->value))."'");
    if(!empty($filter->kdac1->value)){
      $sql = $this->em->andWhere("a.kdac >= '".strtoupper($filter->kdac1->value)."'");
    }
    if(!empty($filter->kdac2->value)){
      $sql = $this->em->andWhere("a.kdac <= '".strtoupper($filter->kdac2->value)."'");
    }
    if(!empty($filter->sub->value)){
      $sql = $this->em->andWhere("a.kd_sub = '".strtoupper($filter->sub->value)."'");
    }
	if(!empty($filter->cabang->value)){
		$sql = $this->em->andWhere("a.kd_cabang = '".$filter->cabang->value."'");
	}
	if(!empty($filter->sub_grup->value)){
		$sql = $this->em->andWhere("b.kd_sub_cab = '".$filter->sub_grup->value."'");
	}
	if(!empty($filter->grup->value)){
		$sql = $this->em->andWhere("c.kd_grp_cab = '".$filter->grup->value."'");
	}
    return $this->em->getGroupBy("kdac");
  }
  
  public function cekSaldoAwal()
  {
    $filter = $this->set->filter;
	$awal = $this->awal($filter);
	$tgl = date('d/m/Y', strtotime($filter->date1->value));
    $sql = $this->em->addSelect("a.kdac, 'SALDO AWAL' as nojnl, '$tgl' as tgl, b.saldo");
    $sql = $this->em->getFrom("acc", 'a');
    $sql = $this->em->addLeftJoin("($awal)", 'b', array("a.kdac = b.kdac"));
    $sql = $this->em->getWhere("1=1");
    if(!empty($filter->kdac1->value)){
      $sql = $this->em->andWhere("a.kdac >= '".strtoupper($filter->kdac1->value)."'");
    }
    if(!empty($filter->kdac2->value)){
      $sql = $this->em->andWhere("a.kdac <= '".strtoupper($filter->kdac2->value)."'");
    }
    $sql = $this->em->getMultiSort("kdac asc");
	return $this->em->queryEks($sql);
  }
  
  public function genData()
  {
	$his = $this->setQuery();
	
	$data = $this->cekSaldoAwal();
	$res = array();
	while($row = $this->em->fetchObject($data)){
		$res[] = $row;
		$id = $row->kdac;
		$saldo = $row->saldo;
		if(count($his[$id]) > 0){
			foreach($his[$id] as $r){
				$saldo = $saldo + ($r->debet - $r->credit);
				$r->saldo = $saldo;
				$res[] = $r;
			}
		}
	}
	
	return $res;
  }
  
  public function View()
  {
	$this->set->dataLoad = $this->genData();
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}