<?php

CLASS lbrs3 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 Posisi Sub Account';
    $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', " " => 'nmac');
    $this->set->groupBy   = "kdac";
    $this->set->hitung    = array("awal", "debet", "credit", "saldo", "mulai", "masuk", "keluar", "stok");
    $this->set->rowPage   = 60;
    $this->set->grandtotal= true;
    $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, '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, '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' => 'kd_sub',	'nama' => 'Nomor',  		'type' => 'text',   'width' => 100,  'size' => 100);
		$res[] = array('id' => 'nm_sub',	'nama' => 'Nama',  		'type' => 'text',   'width' => 250,  'size' => 100);

		if($_GET['qty'] == 'T'){
			$res[] = array('id' => 'mulai',		'nama' => 'Awal',   	'type' => 'numeric', 'width' => 50,  'size' => 15, 'dec' => 2, 'null' => true, 'induk' => 'Qty');
			$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' => 'awal',		'nama' => 'Saldo Awal',   	'type' => 'numeric', 'width' => 150,  'size' => 15, 'dec' => 2, 'null' => true);
		$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 Akhir',   	'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 genData()
	{
		$filter = $this->set->filter;
		$lap = new Lbbs($this->config, $this->em);
		$tmp = $lap->genData($filter, $this->em);
	
		$res = array();
		foreach($tmp as $r){
			$id = $r->grup;
			
			if($r->sawsd == true){
				$r->mulai = $r->stok;
				$r->awal  = $r->saldo;
				$res[$id] = $r;
			}else{
				$res[$id]->masuk 	= $res[$id]->masuk + $r->masuk;
				$res[$id]->keluar 	= $res[$id]->keluar + $r->keluar;
				$res[$id]->debet 	= $res[$id]->debet + $r->debet;
				$res[$id]->credit 	= $res[$id]->credit + $r->credit;
				
				if(!empty($r->kd_sub)){
					$res[$id]->kd_sub = $r->kd_sub;
					$res[$id]->nm_sub = $r->nm_sub;
				}
			}
			
		}
		foreach($res as $k => $r){
			$res[$k]->stok 	= $r->mulai + ($r->masuk - $r->keluar);
			$res[$k]->saldo = $r->awal + ($r->debet - $r->credit);
		}
	
		$sort = array();
		foreach($res as $k => $r){
			$sort[$k] = $r->kd_sub;
		}
		array_multisort($sort, SORT_ASC, $res);
	
		return $res;
	}
  
  public function View()
  {
	$this->set->dataLoad = $this->genData();
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}