<?php

CLASS lbbst2 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 Buku Pembantu Piutang';
    $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', "Sub" => 'kd_sub', ' ' => 'nm_sub');
    $this->set->groupBy   = "grup";
    $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->mac = $lap->getMinMax($this->set, "acc", "kdac");
		$res->sub = $lap->getMinMax($this->set, "sub", "kd_sub");
		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' => '0.0.04.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.04.10', 'after' => 'load("macd", "false", "false")')));

		$row = $row + 25;
		$form[] = array('row' =>  $row,  'col' => 0,   "label" => array("name" => "Sub",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'sub1', "size" => 10, 'value' => $this->defVal->sub->min, 'after' => 'load("sub", "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("sub", "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' => 'Tgl',   	'type' => 'text', 	'width' => 100,  'size' => 10, 'noduplicate' => true);
		$res[] = array('id' => 'nojnl',	'nama' => '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' => 80,  'size' => 100);
		//$res[] = array('id' => 'kd_inv',	'nama' => 'Code',  		'type' => 'text',   'width' => 80,  'size' => 10);

		//if($_GET['qty'] == 'T'){
		//	$res[] = array('id' => 'masuk',		'nama' => 'Masuk',   	'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2, 'null' => true, 'induk' => 'Qty');
		//	$res[] = array('id' => 'keluar',	'nama' => 'Keluar',   	'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2, 'null' => true, 'induk' => 'Qty');
		//	$res[] = array('id' => 'stok',		'nama' => 'Saldo',   	'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2, 'null' => true, 'induk' => 'Qty');
		//	$res[] = array('id' => 'spasi',		'nama' => '&nbsp;', 	'type' => 'text',   'width' => 10,  'size' => 60);
		//}
		$res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 400,  'size' => 60);
		$res[] = array('id' => 'debet',		'nama' => 'Debet',   	'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);
		$res[] = array('id' => 'saldo',		'nama' => 'Saldo',   	'type' => 'numeric', 'width' => 150,  'size' => 15, 'dec' => 2, 'null' => true);
		

		return $res;
	}
  

	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/report";
	}
	public function hist()
	{
		$filter = $this->set->filter;

		$sql = $this->em->addSelect("
								a.nojnl, to_char(a.tgl, 'dd/mm/yyyy') as tgl, a.ket, a.kdac, a.nmac, a.kd_sub
								, a.kd_cabang, a.debet, a.credit, b.nm_sub, a.kd_inv
								, to_char(a.tgl, 'yyyy-mm-dd') as tgls, a.masuk, a.keluar
							");
		$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.alias != 'SOG-OBF2'");
		$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)."' or 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->getMultiSort("a.tgl, a.nojnl, a.kd_sub");
		$eks = $this->em->queryEks($sql);

		$res = array();
		while($row = $this->em->fetchObject($eks)){
			$res[$row->kdac][$row->kd_sub][] = $row;
		}
		// echo $sql;
		return $res;
	}
  
	public function nilai($filter)
	{
		$sql = $this->em->addSelect("a.kdac, a.kd_sub, b.nm_sub, max(a.nmac) as nmac
								, sum(COALESCE(a.masuk,0)-COALESCE(a.keluar,0)) as stok
								, sum(COALESCE(a.debet,0)-COALESCE(a.credit,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.tgl < '".date('Y-m-d', strtotime($filter->date1->value))."'");
		$sql = $this->em->andWhere("(a.kdac = '".strtoupper($filter->kdac1->value)."' or 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.kdac, a.kd_sub, b.nm_sub");
		$eks = $this->em->queryEks($sql);

		$res = array();
		$tgl = date('d/m/Y', strtotime($filter->date1->value));
		while($row = $this->em->fetchObject($eks)){
			$row->nojnl = 'SALDO AWAL Per '.$tgl;
			$row->tgl 	= $tgl;
			$row->sawsd = true;
			$row->grup  = $row->kdac.'_'.$row->kd_sub;
			$res[$row->kdac][$row->kd_sub][] = $row;
		}
		
		$hist = $this->hist();
		foreach($hist as $kdac => $r){
			foreach($r as $sub => $r2){
				$val = 0;
				$stk = 0;
				if(empty($res[$kdac][$sub])){
					$i = (object)'apank';
					$i->kdac 	= $kdac;
					$i->kd_sub 	= $sub;
					$i->nojnl 	= 'SALDO AWAL Per '.$tgl;
					$i->tgl 	= $tgl;
					$i->sawsd 	= true;
					$i->grup  	= $kdac.'_'.$sub;
					$i->stok  	= 0;
					$i->saldo  	= 0;
					$res[$kdac][$sub][] = $i;
				}else{
					$val = $res[$kdac][$sub][0]->saldo;
					$stk = $res[$kdac][$sub][0]->stok;
					$val = number_format($val, 5, '.', '');
					$stk = number_format($stk, 5, '.', '');
				}
				foreach($r2 as $k => $r3){
					$val = $val + ($r3->debet - $r3->credit);
					$stk = $stk + ($r3->masuk - $r3->keluar);
					$val = number_format($val, 5, '.', '');
					$stk = number_format($stk, 5, '.', '');

					$r3->saldo = $val;
					$r3->stok  = $stk;
					$r3->grup  = $r3->kdac.'_'.$r3->kd_sub;
					$res[$kdac][$sub][] = $r3;
				}
			}
		}
		
		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);
		$fil = array();
		$fil[] = "detil = 'T'";
		$fil[] = "(a.kdac = '".strtoupper($filter->kdac1->value)."' or a.kdac = '".strtoupper($filter->kdac2->value)."')";
		$mac = $this->em->getFindBy("acc", "kdac, nmac", $fil, "kdac");
		$res = array();
		foreach($mac as $r){
			$id = $r->kdac;
			if(!empty($val[$id])){
				foreach($val[$id] as $r2){
					foreach($r2 as $r3){
						$res[] = $r3;
					}
				}
			}
		}
	
		return $res;
	}
  
  public function View()
  {
	$this->set->dataLoad = $this->genData();
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}