<?php

CLASS bg extends RasioProject
{
	protected $set;
	protected $enableCk = true;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
	}

	protected function set()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'Daftar BG Belum Cair';
		$this->set->dBaseH 		= 'acd';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('bg_no');
		$this->set->where 		= array("(tipe = 'BANK')");
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array("ket_induk");
		$this->set->notNull 	= array("kdac", "nmac", "induk", "grup", "detil");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "prinT");
		$this->set->addClear 	= true;
		$this->set->sqlGrid 	= $this->queryData();
		return $this->set;
	}
	
	public function queryAll()
	{
		$sql = $this->em->addSelect("a.bg_no as pk, sum(a.debet - a.credit) as nilai, min(_no) as no");
		$sql = $this->em->getFrom('acd', 'a');
		$sql = $this->em->getWhere("bg_no is not null");
		$sql = $this->em->andWhere("kdac = '0.0.03.30'");
		$sql = $this->em->getGroupBy("a.bg_no");

		return $sql;
	}
	
	public function queryData($filter = array(), $lunas = false)
	{
		$sel = $this->queryAll();
		$sql = $this->em->addSelect("a.*, b.nm_sub, b.bg_no, b.bg_tgl, b.bg_bank, b.nojnl, case when debet > 0 then 'D' else 'K' end as dk");
		$sql = $this->em->getFrom("($sel)", 'a');
		$sql = $this->em->addLeftJoin("acd", 'b', array("a.no = b._no"));
		$sql = $this->em->getWhere("1=1");		
		$sql = $this->em->andWhere("kdac = '0.0.03.30'");

		foreach($filter as $row){
			$sql = $this->em->andWhere("$row");
		}
		
		$sql = $this->em->getMultiSort("b.bg_no");
		
		return $sql;
	}
	
	protected function setForm()
	{
		return array();
	}
	
	protected function setGrid()
	{
		$kode = new Kode;
		$res->page 	= 50;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		
		$res->attr[]	= array("label" => 'DK', 'attr' => 'a.dk', "sort" => 'true', "width" => 30, 'align' => 'left');
		$res->attr[]	= array("label" => 'BG No.', 'attr' => 'a.bg_no', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'No.Ref', 'attr' => 'a.nojnl', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Supp/Cust', 'attr' => 'a.nm_sub', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Bg. JT', 'attr' => 'a.bg_tgl', "sort" => 'true', "width" => 80, 'align' => 'left', 'type' => 'date');
		$res->attr[]	= array("label" => 'Bg. Bank', 'attr' => 'a.bg_bank', "sort" => 'true', "width" => 80, 'align' => 'left');
		$res->attr[]	= array("label" => 'Nilai', 'attr' => 'a.nilai', "sort" => 'true', "width" => 100, 'align' => 'left', 'type' => 'numeric', 'dec' => 2);
		
		return $res;
	}
	
	protected function setPrint()
	{
		return array();
	}
	
	public function Index()
	{
		$this->data = $this->set;
		$this->view = "default/index";
	}
	
	public function data()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayData";
		$this->layout = "view";
	}
	
	public function load()
	{
		$sql = $this->queryData(array("b.bg_no = '".$_GET['idRef']."'"), true);
		$eks = $this->em->queryEks($sql);
		$data = $this->em->fetchObject($eks);
	
		$basic = new Basic;
		$this->data = $basic->load($_GET, $this->set, $data);
		$this->layout = "null";
	}
	
	public function Find()
	{
		$basic = new Basic;
		$this->data = $basic->find($_GET, $this->set);
		$this->view = "default/displayForm";
		$this->layout = "view";
	}
	
	public function account()
	{
		$data = $this->em->getFind("acc", "nmac", array("kdac = '".$_GET['idRef']."'"));
		
		$res->target = $_GET['target'];
		$res->nama 	= $data->nmac;
		echo json_encode($res);
		$this->layout = "null";
	}
	
	public function popUp()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayDataPopUp";
		$this->layout = "view";
	}
	
	public function det()
	{
		$sql = $this->queryData(array("b.bg_no = '".$_GET['idRef']."'"), true);
		$eks = $this->em->queryEks($sql);
		$data = $this->em->fetchObject($eks);
		
		$data->debet  = str_ireplace("-", "", $data->nilai);
		$data->credit = str_ireplace("-", "", $data->nilai);
		
		$kode = explode("_", $_GET['id']);
		
		$res->data = $data;
		$res->asli = $this->em->getFind("acd", "*", array("_no = '".$data->no."'"));
		$res->id = $kode[0].'_'.$kode[1];
		$res->no = $kode[2];
		
		if(!empty($res->data->bg_tgl)){
			$res->data->bg_tgl = date("d/m/Y", strtotime($res->data->bg_tgl));
		}
		
		echo json_encode($res);
		$this->layout = "null";
	}
}