<?php

CLASS ltb 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 Jurnal Tidak Ballance';
    $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   = "nojnl";
    $this->set->hitung    = array("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" => "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' => 'kdac',		'nama' => 'Acc',  		'type' => 'text',   'width' => 50,  'size' => 10);
    $res[] = array('id' => 'nmac',		'nama' => 'Nama Account', 'type' => 'text',   'width' => 180,  'size' => 25);
    $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);
    
    return $res;
  }
  
  public function setQuery2()
  {
    $filter = $this->set->filter;
	
    $sql = $this->em->addSelect("
						a.nojnl, to_char(a.tgl, 'dd/mm/yyyy') as tgl, a.ket
						, kd_cabang, debet, credit, (COALESCE(debet, 0) - COALESCE(credit, 0)) as saldo
						");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->addInnerJoin("acc", 'b', array("a.kdac = b.kdac"));
    $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->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->getMultiSort("a.tgl asc, a.nojnl asc");

    return $sql;
  }
  
  public function setQuery1()
  {
	$sel = $this->setQuery2();

    $sql = $this->em->addSelect("nojnl, tgl, kd_cabang, sum(debet) as debet, sum(credit) as credit, sum(debet-credit) as saldo");
    $sql = $this->em->getFrom("($sel)", 'a');
    $sql = $this->em->getWhere("1=1");
    $sql = $this->em->getGroupBy("nojnl, tgl, kd_cabang");

    return $sql;
  }
  
  public function setQuery()
  {
	$sel = $this->setQuery1();

    $sql = $this->em->addSelect("nojnl");
    $sql = $this->em->getFrom("($sel)", 'a');
    $sql = $this->em->getWhere("(saldo > 0.9
								or saldo < -0.9)");

    return $sql;
  }
  
  public function genData()
  {
		$sql = $this->setQuery();
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		$nojnl = '';
		foreach($tmp as $r){
			if(!empty($nojnl)){
				$nojnl.= ' or ';
			}
			$nojnl.= " (a.nojnl = '".$r->nojnl."') ";
		}
		$sql = $this->em->addSelect("a.nojnl, to_char(a.tgl, 'dd/mm/yyyy') as tgl, a.debet, a.credit, a.kdac, b.nmac");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->addLeftJoin("acc", 'b', array("a.kdac = b.kdac"));
		$sql = $this->em->getWhere("($nojnl)");
		$sql = $this->em->getMultiSort("a.nojnl, a.tgl, a.kd_cabang");
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		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";
  }
}