<?php

CLASS lbbsp extends RasioProject
{
  protected $set;
  protected $enableCk = false;
  protected $report;
  protected $baseTitle;

	public function __construct($config, $em = false)
	{
		$this->getConfig($config);
		$this->validation();
	
		if($em != false){
			$this->em = $em;
		}
	
		$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 Hutang & Uang Muka';
    $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("Supplier" => 'nm_sub');
    $this->set->groupBy   = "kd_sub";
    $this->set->hitung    = array("saldo");
    $this->set->grandtotal= true;
    $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->mac = $lap->getMinMax($this->set, "acc", "kdac");
		$res->sub = $lap->getMinMax($this->set, "sub", "kd_sub", array("a.jenis = '11'"));
		return $res; 
	}
  
	protected function setForm()
	{
		$this->defVal = $this->defValue();

		$row = 0;
		//$form[] = array('row' =>  $row,  '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")')));

		//$row = $row + 25;
		$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' => '1.0.02.10', 'after' => 'load("macd", "false", "false")')));
		$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' => '1.0.02.10', 'after' => 'load("macd", "false", "false")')));

		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 0,   "label" => array("name" => "Supplier",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'sub1', "size" => 10, 'value' => $this->defVal->sub->min, 'after' => 'load("mastersupp", "false", "false")')));
		$form[] = array('row' =>  $row,  'col' => 200, "label" => array("name" => "s/d",   "width" => 30)  ,   "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'sub2', "size" => 10, 'value' => $this->defVal->sub->max, 'after' => 'load("mastersupp", "false", "false")')));
		
		//$row = $row + 25;
		//$form[] = array('row' =>  $row,  'col' => 0,   "label" => array("name" => "Qty",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'qty', "size" => 2, 'value' => '', 'after' => 'select()', "select" => 'tf')));

		$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' => 'nota',	'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' => 'Supplier',  		'type' => 'text',   'width' => 80,  'size' => 100);
		//$res[] = array('id' => 'nm_sub',	'nama' => 'Nama',  		'type' => 'text',   'width' => 200,  'size' => 100);
		//$res[] = array('id' => 'kd_inv',	'nama' => 'Code',  		'type' => 'text',   'width' => 80,  'size' => 10);

		$res[] = array('id' => 'saldo',		'nama' => 'Hutang',   	'type' => 'numeric', 'width' => 150,  'size' => 15, 'dec' => 2, 'null' => true);
		//$res[] = array('id' => 'credit',	'nama' => 'Credit',   	'type' => 'numeric', 'width' => 150,  'size' => 15, 'dec' => 2, 'null' => true, 'induk' => 'Value');
		$res[] = array('id' => 'saldos',		'nama' => 'Sub Total',   	'type' => 'numeric', 'width' => 150,  'size' => 15, 'dec' => 2, 'null' => true);
		if($_GET['convert'] == 'cetak') $res[] = array('id' => 'spasi',		'nama' => '&nbsp;', 	'type' => 'text',   'width' => 10,  'size' => 60);
		//$res[] = array('id' => 'tgl_jt',		'nama' => 'Tanggal J.T.',   	'type' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 400,  'size' => 60);

		return $res;
	}
  

	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/report";
	}
  
	public function nilai($filter)
	{
	    $filter = $this->set->filter;
	    
	    $sql = $this->em->addSelect("a.nota, max(a.nmac) as nmac
								, sum(COALESCE(a.keluar,0)-COALESCE(a.masuk,0)) as stok
								, sum(COALESCE(a.credit,0)-COALESCE(a.debet,0)) as saldo
								");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->addLeftJoin('sub', 'b', array("a.kd_sub = b.kd_sub"));
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.alias != 'SOG-OBF'");
		$sql = $this->em->andWhere("a.kdac >= '".strtoupper($filter->kdac1->value)."'");
		$sql = $this->em->andWhere("a.kdac <= '".strtoupper($filter->kdac2->value)."'");
		if(!empty($filter->sub1->value)){
			$sql = $this->em->andWhere("a.kd_sub >= '".strtoupper($filter->sub1->value)."'");
		}
		if(!empty($filter->sub2->value)){
			$sql = $this->em->andWhere("a.kd_sub <= '".strtoupper($filter->sub2->value)."'");
		}
		$sql = $this->em->getGroupBy("a.nota");
		$sel = $sql;

		$sql = $this->em->addSelect("a.nota, to_char(a.tgl, 'dd/mm/yyyy') as tgl, to_char(a.tgl, 'mm-yyyy') as periode, to_char(a.nota_jt, 'dd/mm/yyyy') as tgl_jt, a.ket, a.kdac, a.nmac, a.kd_sub, a.nm_sub, a.kd_cabang, a.kd_inv, a.nm_inv, to_char(a.tgl, 'yyyy-mm-dd') as tgls
		                            , b.stok, b.saldo
		                            ");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->addLeftJoin("($sel)", 'b', array("a.nota = b.nota"));
		$sql = $this->em->getWhere("1=1");
		//$sql = $this->em->andWhere("a.alias = 'NBL'");
		$sql = $this->em->andWhere("a.tgl >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
		$sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date2->value))."'");
		$sql = $this->em->andWhere("a.kdac >= '".strtoupper($filter->kdac1->value)."'");
		$sql = $this->em->andWhere("a.kdac <= '".strtoupper($filter->kdac2->value)."'");
		$sql = $this->em->andWhere("b.saldo <> 0");
		if(!empty($filter->sub1->value)){
			$sql = $this->em->andWhere("a.kd_sub >= '".strtoupper($filter->sub1->value)."'");
		}
		if(!empty($filter->sub2->value)){
			$sql = $this->em->andWhere("a.kd_sub <= '".strtoupper($filter->sub2->value)."'");
		}
		$sql = $this->em->getMultiSort("a.kd_sub, a.tgl, a.nota");
		//echo $sql;
		$eks = $this->em->queryEks($sql);

		$res = array();
		while($row = $this->em->fetchObject($eks)){
			$res[$row->kd_sub][$row->periode][] = $row;
		}
		
		return $res;
	}
	public function genData($filter = false, $em = false)
	{
		if($filter == false){
			$filter = $this->set->filter;
		}
		if($em != false){
			$this->em = $em;
		}
		$val = $this->nilai($filter);
		$res = array();
		$ctr = 0;
	    foreach($val as $v){
	        foreach($v as $s){
	            $saldos = 0;
	            foreach($s as $r){
    	            $res[++$ctr] = $r;
    	            $saldos += $r->saldo;
    	        }
    	        $res[$ctr]->saldos = $saldos;
	        }
	    }
	
		return $res;
	}
  
  public function View()
  {
	$this->set->dataLoad = $this->genData();
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}