<?php

CLASS ldj 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-001';
    $this->set->date    = date("d/m/Y H:i");
    $this->set->modul     = 'Laporan Detail Jurnal';
    $this->set->form    = $this->setForm();
    $this->set->display   = $this->setReport();
    $this->set->filter    = $this->report->setFilter($_GET, $this->setForm());
    $this->set->query     = $this->setQuery();
    $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  = '';
    return $res; 
  }
  
  protected function setForm()
  {
    $this->defVal = $this->defValue();
    
	$form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Group",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'grup', "size" => 5, 'after' => 'load("gcab", "false", "gcab")')));

	$form[] = array('row' =>  25,  'col' => 0,   "label" => array("name" => "Sub Group",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'sub_grup', "size" => 5, 'after' => 'load("scab", "false", "scab")')));

	$form[] = array('row' =>  50,  'col' => 0,   "label" => array("name" => "Profit Center",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang', "size" => 5, 'after' => 'load("cabang", "false", "cabang")')));

	$form[] = array('row' =>  75,  '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' =>  75,  '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' =>  100,  'col' => 0, "label" => array("name" => "Prefik",     "width" => 100),   "input" => array("type" => 'text', 'attr' => array("name" => 'prefik', "size" => 30)));
    $form[] = array('row' =>  125,  'col' => 0, "label" => array("name" => "No. Transksi",     "width" => 100),   "input" => array("type" => 'text', 'attr' => array("name" => 'nojnl', "size" => 30)));
  
	if(!empty($_GET['nota'])){
		$form[] = array('row' =>  125,  'col' => 0, "label" => array("name" => "No. Nota",     "width" => 100),   "input" => array("type" => 'text', 'attr' => array("name" => 'nota', "size" => 30)));
	}
  
    $form[] = array('row' =>  150,  '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' => 50, 'noduplicate' => true);
    $res[] = array('id' => 'kd_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 50,  'size' => 3);
    $res[] = array('id' => 'kdac',		'nama' => 'Acc',  		'type' => 'text',   'width' => 60,  'size' => 20);
    $res[] = array('id' => 'nmac',		'nama' => 'NmAcc',  	'type' => 'text',   'width' => 150,  'size' => 20);
    $res[] = array('id' => 'kd_sub',	'nama' => 'Sub',  		'type' => 'text',   'width' => 100,  'size' => 20);
    $res[] = array('id' => 'kd_inv',	'nama' => 'Inv',  		'type' => 'text',   'width' => 100,  'size' => 20);
    $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' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 400,  'size' => 60);
    $res[] = array('id' => 'kd_gudang',	'nama' => 'Gudang',  	'type' => 'text',   'width' => 100,  'size' => 20);
    $res[] = array('id' => 'masuk',		'nama' => 'In',   		'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2, 'null' => true);
    $res[] = array('id' => 'keluar',	'nama' => 'Out',   		'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2, 'null' => true);
    
    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.kd_inv, a.kdacf
						, a.kd_cabang, debet, credit, (COALESCE(debet, 0) - COALESCE(credit, 0)) as saldo
						, b.nmac, a.masuk, a.keluar, a.kd_gudang
						");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->addLeftJoin("acc", 'b', array("a.kdac = b.kdac"));
	$sql = $this->em->addLeftJoin('cabang', 'c', array("a.kd_cabang = c.kd_cabang"));
	$sql = $this->em->addLeftJoin('sub_cab', 'd', array("c.kd_sub_cab = d.kd_sub_cab"));
    $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->cabang->value)){
		$sql = $this->em->andWhere("a.kd_cabang = '".$filter->cabang->value."'");
	}
	if(!empty($filter->sub_grup->value)){
		$sql = $this->em->andWhere("c.kd_sub_cab = '".$filter->sub_grup->value."'");
	}
	if(!empty($filter->grup->value)){
		$sql = $this->em->andWhere("d.kd_grp_cab = '".$filter->grup->value."'");
	}
    if(!empty($filter->nojnl->value)){
		$sql = $this->em->andWhere("a.nojnl = '".strtoupper($filter->nojnl->value)."'");
    }
    if(!empty($filter->nota->value)){
		$sql = $this->em->andWhere("a.nota = '".strtoupper($filter->nota->value)."'");
    }
    if(!empty($filter->prefik->value)){
		$sql = $this->em->andWhere("a.nojnl like '".strtoupper($filter->prefik->value)."%'");
    }
	if($_GET['cek_det'] == 't' and empty($_GET['kd_sub'])){
		$sql = $this->em->andWhere("(a.kd_sub = '' or a.kd_sub is null)");
	}
    $sql = $this->em->getMultiSort("a.tgl asc, a.nojnl asc, a._no asc");

    return $sql;
  }
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
  public function View()
  {
	if(!empty($_GET['nota'])){
		$this->set->groupBy   = "nota";
	}
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}