<?php

CLASS rhp 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 		= 'Register Hutang Piutang';
		$this->set->dBaseH 		= 'acd';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('nota');
		$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.nota as pk, a.kdac, a.nota, sum(a.debet - a.credit) as nilai, min(_no) as no");
		$sql = $this->em->getFrom('acd', 'a');
		$sql = $this->em->getWhere("nota is not null");
		$sql = $this->em->andWhere("nota != ''");
		$sql = $this->em->getGroupBy("a.nota, a.kdac");
		return $sql;
	}
	
	public function queryData($filter = array(), $lunas = false)
	{
		$sel = $this->queryAll();
		$sql = $this->em->addSelect("a.*, b.nm_sub
										, case when b.nota_tgl is not null then b.nota_tgl else b.tgl end as nota_tgl
										, case when b.nota_jt is not null then b.nota_jt else b.tgl end as nota_jt
										, b.bg_no, b.bg_tgl, b.bg_bank, b.ket, b.nojnl, case when debet > 0 then 'D' else 'K' end as dk"); 
										//, case when  a.nilai > 0 then a.nilai else a.nilai*-1 end as nilai");
		$sql = $this->em->getFrom("($sel)", 'a');
		$sql = $this->em->addLeftJoin("acd", 'b', array("a.no = b._no"));
		$sql = $this->em->addLeftJoin("acc", 'c', array("b.kdac = c.kdac"));
		$sql = $this->em->getWhere("1=1");		
		$sql = $this->em->andWhere("c.rhp = 'T'");

		if($_GET['kd_sub']){
			$sql = $this->em->andWhere("replace(b.kd_sub, ' ','') = '".str_ireplace(" ", "", $_GET['kd_sub'])."'");
		}
		if($_GET['kdac']){
			$sql = $this->em->andWhere("a.kdac = '".$_GET['kdac']."'");
		}
		if($lunas == false){
			$sql = $this->em->andWhere("COALESCE(a.nilai, 0) <> 0");
		}
		foreach($filter as $row){
			$sql = $this->em->andWhere("$row");
		}
		
		if($_GET['jpl'] == 'undefined'){
			$sql = $this->em->andWhere("(b.alias = 'SPIU' or b.alias = 'NJL' or b.alias = 'REJ' or b.alias = 'BJM' or b.alias = 'SAW' or b.alias = 'BPH')");
			if(!empty($_GET['kd_sub'])){
				$sql = $this->em->andWhere("b.kd_sub = '".$_GET['kd_sub']."'");
			}
		}
		if($_GET['jph'] == 'undefined'){
			$sql = $this->em->andWhere("(b.alias = 'SHUT' b.alias = 'REB' or b.alias = 'NBL' or b.alias = 'BJM' or b.alias = 'SAW')");
			if(!empty($_GET['kd_sub'])){
				$sql = $this->em->andWhere("b.kd_sub = '".$_GET['kd_sub']."'");
			}
		}
		
		foreach($_POST['nota'] as $k => $nota){
			$nota = trim($nota);
			if(!empty($nota)){
				$sql = $this->em->andWhere("a.nota != '".$nota."'");
			}
		}
		
		$sql = $this->em->getMultiSort("a.nota, b.bg_no");

		if($this->login['kd_user'] == 'ADMIN'){
			// var_dump($sql);
		}

		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" => 'No.Nota', 'attr' => 'a.nota', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Kd. Acc', 'attr' => 'a.kdac', "sort" => 'true', "width" => 60, 'align' => 'left');
		$res->attr[]	= array("label" => 'Tgl. Nota', 'attr' => 'a.nota_tgl', "sort" => 'true', "width" => 80, 'align' => 'left', 'type' => 'date');
		$res->attr[]	= array("label" => 'No.Ref', 'attr' => 'a.nojnl', "sort" => 'true', "width" => 120, 'align' => 'left');
		$res->attr[]	= array("label" => 'Supp/Cust', 'attr' => 'a.nm_sub', "sort" => 'true', "width" => 120, 'align' => 'left');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket', "sort" => 'true', "width" => 150, 'align' => 'left');
		// $res->attr[]	= array("label" => 'Bg. No', 'attr' => 'a.bg_no', "sort" => 'true', "width" => 80, '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("a.nota = '".$_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("a.nota = '".$_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];
		$res->no = $kode[1];
		
		if(!empty($res->asli->kd_sub) and empty($res->asli->nm_sub)){
			$sub = $this->em->getFind("sub", "nm_sub", array("kd_sub = '".$res->asli->kd_sub."'"));
			$res->asli->nm_sub = $sub->nm_sub;
		}
		
		$acc = $this->em->getFind("acc", "nmac", array("kdac = '".$res->asli->kdac."'"));
		$res->asli->nmac = $acc->nmac;

		echo json_encode($res);
		$this->layout = "null";
	}
}