<?php

CLASS tts 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 		= 'Tanda Terima';
		$this->set->alias 		= 'BTT';
		$this->set->dBaseH 		= 'umh';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('nojnl');
		$this->set->where 		= array("alias = '".$this->set->alias."'");
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("tgl", "kd_cabang", "kdsub", "ket_jurnal", "pembayaran", "tgl_kembali", "kd_kurs");
		$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->funct 		= '_creatingTts';
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "No.Jurnal", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'nojnl', "size" => 25, "readonly" => true)));
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Tanggal", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));

		$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "No. Ref", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'no_ref', "size" => 20, 'after' => 'load("po", "false", "tts_po")', 'display' => 'tts_po', 'target' => 'ket_jurnal')));
		
		$form[] = array('row' => 100, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "Profit Center", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_cabang', "size" => 6, 'after' => 'load("cabang", "false", "cabang")', 'display' => 'cabang', 'target' => 'nm_cabang')));
		
		$form[] = array('row' => 125, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nmsub', "size" => 40, 'readonly' => true)));
		$form[] = array('row' => 125, 'col' => 25, "label" => array("name" => "Diberikan kepada", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'kdsub', "size" => 6, 'after' => 'load("mastersupp", "false", "sub")', 'display' => 'sub', 'target' => 'nmsub')));
		
		$form[] = array('row' => 150, 'col' => 25, "label" => array("name" => "Untuk Pembayaran", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'ket_jurnal', "size" => 70)));
		$form[] = array('row' => 175, 'col' => 25, "label" => array("name" => "Bentuk Pembayaran", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'pembayaran', "size" => 20, 'after' => 'select("")', 'select' => 'jenis_bayar')));
		$form[] = array('row' => 200, 'col' => 25, "label" => array("name" => "Tgl. Kembali", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl_kembali', "size" => 10, 'class' => 'tanggal')));

		$form[] = array('row' => 225, 'col' => 210, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'kurs', "size" => 4, 'readonly' => true, 'class' => 'decimal')));
		$form[] = array('row' => 225, 'col' => 25, "label" => array("name" => "Currency", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_kurs', "size" => 4, 'after' => 'load("kurs", "false", "kurs")', 'display' => 'kurs', 'target' => 'kurs')));
		
		$form[] = array('row' => 275, 'col' => 25, "label" => array("name" => "Nilai", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'nilai', "size" => 24, 'class' => 'decimal _hitung')));
		$form[] = array('row' => 300, 'col' => 25, "label" => array("name" => "+ PPN", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'ppn', "size" => 4, 'class' => 'decimal  _hitung')));
		$form[] = array('row' => 300, 'col' => 210, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'ppn_nilai', "size" => 15, 'class' => 'decimal', 'readonly' => true)));
		$form[] = array('row' => 325, 'col' => 25, "label" => array("name" => "- PPh", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'pph', "size" => 4, 'class' => 'decimal  _hitung')));
		$form[] = array('row' => 325, 'col' => 210, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'pph_nilai', "size" => 15, 'class' => 'decimal', 'readonly' => true)));
		$form[] = array('row' => 350, 'col' => 25, "label" => array("name" => "----------------------------------------------------------------------------", "width" => 500), "input" => false);
		$form[] = array('row' => 375, 'col' => 25, "label" => array("name" => "Total", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'total', "size" => 24, 'class' => 'decimal', 'readonly' => true)));
		
		
		
		
		
		$form[] = array('row' => 425, 'col' => 25, "label" => array("name" => "By", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 425, 'col' => 500, "label" => array("name" => "Last", "width" => 130), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'size' => 20, 'readonly' => true)));
		return $form;
	}
	
	protected function setGrid()
	{
		$kode = new Kode;
		$res->page 	= 50;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		
		$res->attr[]	= array("label" => 'No.Jurnal', 'attr' => 'a.nojnl', "sort" => 'true', "width" => 150, 'align' => 'left', 'focus' => 'true');
		$res->attr[]	= array("label" => 'Tanggal', 'attr' => 'a.tgl', "sort" => 'true', "width" => 70, 'align' => 'center', 'type' => 'date');
		$res->attr[]	= array("label" => 'PC', 'attr' => 'a.kd_cabang', "sort" => 'true', "width" => 30, 'align' => 'center');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket_jurnal', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Nilai', 'attr' => 'a.total', "sort" => 'true', "width" => 100, 'align' => 'right', 'type' => 'numeric', 'dec' => 2);
		$res->attr[]	= array("label" => 'Status', 'attr' => 'a.control', "sort" => 'true', "width" => 40, 'align' => 'left', "DisplayFunc" => 'setControl');
		
		return $res;
	}
	
	protected function setPrint()
	{
		$kode = new Kode;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		$res->width = '600px';
		
		$res->attr[]	= array("label" => 'No. Jurnal', 'attr' => 'a.nojnl', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'nojnl');
		
		return $res;
	}
	
	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 Find()
	{
		$basic = new Basic;
		$this->data = $basic->find($_GET, $this->set);
		$this->view = "default/displayForm";
		$this->layout = "view";
	}
	
	protected function validasi()
	{
		$basic = new Basic;
		$error[] = $basic->validasiBasic($_POST, $this->set);
		
		$ctrl = $basic->cekControl($this->set, $_POST);
		if((int)$ctrl > 1){
			echo "<script>$('#_displayErrorPopUp').html('Data tidak bisa di edit')</script>";
			$error[] = false;
		}
		
		$total = str_ireplace(",", ".", $_POST['nilai']);
		if($total <= 0){
			echo "<script>$('#_displayErrorPopUp').html('Nilai tidak boleh kosong')</script>";
			$error[] = false;
		}
		
		return $basic->getError($error);
	}
	
	protected function kode($post)
	{
		$kode  = new Kode;
		$basic = new Basic;
		$prefik = $this->set->alias.'/'.$_POST['kd_cabang'].'/'.date("ym", strtotime($post['tgl'])).'/';
		$lastU  = $basic->getLastNum($this->set->dBaseH, 'nojnl', $prefik, '', 5, $this->set);
		return $prefik.$kode->getRight($lastU, 5, 0);
	}
	
	protected function nKode($post)
	{
		if($post['_prosesAction'] == 'simpan'){
			return $this->kode($post);
		}else{
			$kode = new Kode;
			$ex = explode(".", $post['nojnl']);
			$asli = $ex[0];
			$tamb = $ex[1];
			$next = $kode->setAbj($tamb, 1);
			return $asli.'.'.$next;
		}
	}
	
	public function proses()
	{
		$basic = new Basic;
		$date = new Date;
		$validasi = $this->validasi();
		if($validasi == true){
			$posting = new Posting;
			
			$this->set->setLastKode = $_POST['nojnl'];
			$qb = array();
			$qb[] = $posting->deleteAcd($this->set, $_POST['nojnl']);
			$tambah = array();
			$tambah['alias'] = $this->set->alias;
			$_POST['tgl'] = $date->serialDate($_POST['tgl']);
			$_POST['nojnl'] = $this->nKode($_POST);
			$tambah['control'] = 1;
		
			$sql = $basic->proses2($_POST, $this->set, $tambah, $qb, array(), true, '.');
			
			$this->em->getBeginTransaction();
			$this->em->getRoolbackFlush($sql);
			
			$save = $posting->tts($this->set, $_POST['nojnl']);
			
			if (!$this->em->getRollCek($save) ) { 
				$this->em->getRollBack();
			}else{
				$this->em->getCommit();
			}

			if($save != false){
				$nojnl = $_POST['nojnl'];
				$nom = $this->em->getFind($this->set->dBaseH, "_no", array("nojnl = '".$_POST['nojnl']."'"));
				$nom = $nom->_no;
				$_SESSION['findNo'.$nom] = $this->set;
				echo "<script>makeSuccesNewForm('$nom')</script>";
			}else{
				echo "<script>$('#_displayErrorPopUp').html('Proses Gagal!')</script>";
			}
		}
		$this->layout = "null";
	}
	
	public function Delete()
	{
		$basic = new Basic;
		$this->data = $basic->delete($_GET, $this->set);
		$this->layout = "null";
	}
	
	public function cetak()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("umh", "*", array("nojnl = '".$_GET['filPrintanojnl']."'"));
		$this->data->set 	= $this->set;
		$this->view = "tts";
		$this->layout = "printFormPdf";
	}
	
	public function load()
	{
		$basic = new Basic;
		$this->data = $basic->load($_GET, $this->set);
		$this->layout = "null";
	}
	
	public function po()
	{
		$data = $this->em->getFind("acd", "*", array("nota = '".$_GET['idRef']."'"));
		
		$res->target = $_GET['target'];
		$res->data 	= $data;
		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";
	}
}