<?php

CLASS bbm 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->alias 		= 'BMS';
		$this->set->modul 		= 'Bank Masuk';
		$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('selisih');
		$this->set->notNull 	= array("tgl", "kd_cabang");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "prinT", "View");
		$this->set->addClear 	= true;
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_creatingJurnal';
		
		$this->set->where[]		= "tgl >= '".$this->login['date_aw']."'";
		$this->set->where[]		= "tgl <= '".$this->login['date_ak']." 23:59:59'";
		
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "No.Jurnal", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nojnl', "size" => 25, "readonly" => true)));
		$form[] = array('row' => 25, 'col' => 240, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'control', "size" => 10, 'readonly' => true)));
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "Tanggal", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));

		$form[] = array('row' => 50, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Profit Center", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_cabang', "size" => 9, 'after' => 'select("")', 'select' => 'cabang', 'select_width' => 280, 'select_height' => 245, 'select_label' => "Kode=>50;Name=>200", 'select_target' => "name=>nm_cabang", 'select_warning' => true, 'display' => 'selectDisplay')));

		$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "No. Referensi", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'no_ref', "size" => 20)));

		$form[] = array('row' => 0, 'col' => 500, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket_jurnal', "size" => 30)));
		$form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "Debet", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tot_debet', "size" => 15, 'readonly' => true, 'class' => 'decimal', 'alt' => 'decimal-us')));
		$form[] = array('row' => 50, 'col' => 500, "label" => array("name" => "Credit", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tot_credit', "size" => 15, 'readonly' => true, 'class' => 'decimal', 'alt' => 'decimal-us')));
		$form[] = array('row' => 75, 'col' => 500, "label" => array("name" => "Selisih", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'selisih', "size" => 15, 'readonly' => true, 'class' => 'decimal', 'alt' => 'decimal-us')));

		$form[] = array('row' => 475, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 475, 'col' => 500, "label" => array("name" => "Last", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'mode' => 'datetime', 'size' => 20, 'readonly' => true)));
		return $form;
	}
	
	public function setDetail()
	{
		$res->width 	= 800;
		$res->height	= 300;
		$res->col		= 25;
		$res->row		= 105;
		$res->data		= 'umd';
		$res->pk		= 'nojnl';
		$res->sort		= 'noumd';
		
		$res->attr[] = array("label" => 'Sub',	"attr" => 'kd_sub', 'width' => 50, 	'after' => 'load("sub", "false", "sub")', 'display' => 'sub', 'target' => 'nm_sub');
		$res->attr[] = array("label" => 'Nm. Sub',	"attr" => 'nm_sub', 'width' => 150, 'notnull' => true,	'readonly' => true);
		$res->attr[] = array("label" => 'Nota',	"attr" => 'nota', 	'width' => 100, 	'after' => 'load("rhp", "true", "rhp")', 'display' => 'rhp', 'load' => 'kd_sub,kdac');
		$res->attr[] = array("label" => 'Kd. Acc',	"attr" => 'kdac', 	'width' => 70, 	'after' => 'load("macd", "false", "account")', 'display' => 'account', 'target' => 'nmac', 'notnull' => true);
		$res->attr[] = array("label" => 'Nm. Acc',	"attr" => 'nmac', 	'width' => 150, 'notnull' => true,	'readonly' => true);
		$res->attr[] = array("label" => 'nnota',	"attr" => 'kdacf', 	'width' => 100, 'notnull' => true,	'type' => 'hidden');
		$res->attr[] = array("label" => 'PC',	"attr" => 'pc', 	'width' => 30, 	'after' => 'load("cabang", "false", "cabang")', 'display' => 'cabang',	'type' => 'hidden');
		$res->attr[] = array("label" => 'BG.No',	"attr" => 'bg_no', 	'width' => 50, 	'after' => 'load("bg", "true", "giro")', 'display' => 'giro', 'type' => 'hidden');
		$res->attr[] = array("label" => 'BG.Bank',	"attr" => 'bg_bank', 'width' => 60,	'type' => 'hidden');
		$res->attr[] = array("label" => 'BG.Tgl',	"attr" => 'bg_tgl', 'width' => 70, 	'class' => 'tanggal', 'mode' => 'date', 'type' => 'hidden');

		$res->attr[] = array("label" => 'Keterangan', "attr" => 'ket', 	'width' => 150);
		$res->attr[] = array("label" => 'Debet',	"attr" => 'debet',	'width' => 80, 'class' => 'decimal _hitung', 'dec' => 2, 'targetNull' => 'credit');
		$res->attr[] = array("label" => 'Credit',	"attr" => 'credit',	'width' => 80, 'class' => 'decimal _hitung', 'dec' => 2, 'targetNull' => 'debet');
		return $res;
	}
	
	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" => 'Cab', 'attr' => 'a.kd_cabang', "sort" => 'true', "width" => 30, 'align' => 'center');
		$res->attr[]	= array("label" => 'No. Ref', 'attr' => 'a.no_ref', "sort" => 'true', "width" => 70, 'align' => 'left');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket_jurnal', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Nilai', 'attr' => 'a.tot_debet', "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, false);

		//$pc 	= $_POST['pc'];
		$kdac 	= $_POST['kdac'];
		foreach($kdac as $k => $row){
			if(!empty($row)){
				$cek = $this->em->getFind("acc", "kdac", array("kdac = '$row'", "detil = 'T'"));
				if(empty($cek->kdac)){
					echo "<script>$('#_displayErrorPopUp').html('Kode Account $row Tidak terdaftar atau bukan Account Detail')</script>";
					$error[] = false;
				}
				if(!empty($pc[$k])){
					$cpc = $this->em->getFind("cabang", "kd_cabang", array("kd_cabang = '".$pc[$k]."'"));
					if(empty($cpc->kd_cabang)){
						echo "<script>$('#_displayErrorPopUp').html('Profit Center ".$pc[$k]." Tidak terdaftar')</script>";
						$error[] = false;
					}
				}
			}
		}
		
		$debet = 0;
		foreach($_POST['debet'] as $row){
			$debet = $debet + str_ireplace(",", "", $row);
		}
		$credit = 0;
		foreach($_POST['credit'] as $row){
			$credit = $credit + str_ireplace(",", "", $row);
		}
		
		$debet  = number_format($debet, 2, '.', '');
		$credit = number_format($credit, 2, '.', '');
		
		$sisa = $debet - $credit;
		if($sisa <> 0){
			echo "<script>$('#_displayErrorPopUp').html('Debet Credit tidak Ballance')</script>";
			$error[] = false;
		}
		
		$ctrl = $basic->cekControl($this->set, $_POST);
		if((int)$ctrl > 1){
			echo "<script>$('#_displayErrorPopUp').html('Data tidak bisa di edit')</script>";
			$error[] = false;
		}


		$sub = $_POST['kd_sub'];
		$cab = $_POST['kd_counter'];
		foreach($sub as $k => $row){
			$row = trim($row);
			if(!empty($row)){
				$sub = $this->em->getFind("sub", "kd_sub", array("kd_sub = '$row'"));
				if(empty($sub->kd_sub)){
					echo "<script>$('#_displayErrorPopUp').html('Sub $row tidak terdaftar')</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;
		$kode  = new Kode;
		$validasi = $this->validasi();
		if($validasi == true){
			$this->set->setLastKode = $_POST['nojnl'];
			$qb = array();
			$qb[] = $this->deleteAcd($_POST['nojnl']);
			$tambah = array();
			$tambah['alias'] = $this->set->alias;
			$_POST['tgl'] = $date->serialDate($_POST['tgl']);
			$_POST['nojnl'] = $this->nKode($_POST);
			$_POST['control'] = 1;
			
			$no = 1;
			$this->nota = $kode->getLastAutoNota($this->em, $_POST);
			foreach($_POST['kdac'] as $n => $row){
				if(!empty($row)){
					$qb[] = $this->saveDet($n, $_POST['nojnl'], $no++);
				}
			}
			
			$debet = 0;
			foreach($_POST['debet'] as $row){
				$debet = $debet + str_ireplace(",", "", $row);
			}
			$credit = 0;
			foreach($_POST['credit'] as $row){
				$credit = $credit + str_ireplace(",", "", $row);
			}
			$_POST['tot_debet']	= $debet;
			$_POST['tot_credit']= $credit;
		
			$sql = $basic->proses2($_POST, $this->set, $tambah, $qb, array(), true, '.');
			
			$this->em->getBeginTransaction();
			$this->em->getRoolbackFlush($sql);
			
			$posting = new Posting;
			$save = $posting->jurnal($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 setAutoNota($attr, $post, $id)
	{
		if(!empty($attr['kdacf'])){
			return $attr['kdacf'];
		}else
		if(empty($attr['kdacf']) and !empty($attr['kd_sub']) and empty($attr['nota'])){
			$kode = new Kode;
			$noasli = explode(".", $attr['nojnl']);
			$noasli = $noasli[0];
			
			$nota = $noasli.'-'.$kode->getRight($this->nota, 3, 0);
			$this->nota = $this->nota + 1;
			return $nota;
		}
		return;
	}
	
	public function saveDet($id, $pk, $nom)
	{
		$kode = new Kode;
		$attr = array();
		foreach($this->set->detail->attr as $k => $r){
			$no = $r['attr'];
			$attr[$no] = $_POST[$no][$id];
		}
		$attr['noumd'] = $pk.'.'.$kode->getRight($nom, 4, 0);
		$attr['nojnl'] = $pk;
		
		if(empty($attr['pc'])){
			$attr['pc'] = $_POST['kd_cabang'];
		}
		
		$attr['kdacf'] = $this->setAutoNota($attr, $_POST, $id);
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}
	
	public function deleteAcd($id)
	{
		$sql = $this->em->addDelete('acd');
		$sql = $this->em->getWhere("nojnl = '$id'");
		return $sql;
	}
	
	public function Delete()
	{
		$_GET['nojnl'] = str_ireplace("__--__", "/", $_GET['nojnl']);
		$qb = array();
		$qb[] = $this->deleteAcd($_GET['nojnl']);
			
		$basic = new Basic;
		$this->data = $basic->delete2($_GET, $this->set, $qb);
		$this->layout = "null";
	}
	
	public function cetak()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("umh", "*", array("nojnl = '".$_GET['filPrintanojnl']."'"));
		$this->data->pk 	= $this->em->getFind("umd", "*", array("nojnl = '".$_GET['filPrintanojnl']."'"));
		$this->data->det 	= $this->em->getFindBy("umd", "*, debet as jumlah", array("nojnl = '".$_GET['filPrintanojnl']."'", "(pk != '1' or pk is null)"), "noumd asc");
		$this->data->set 	= $this->set;
		$this->data->mode 	= 'Keluar Dari';
		$this->view = "buktiMemo";
		$this->layout = "printFormPdf";
	}
	
	public function load()
	{
		$basic = new Basic;
		$this->data = $basic->load($_GET, $this->set);
		$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 Detail()
	{
		$this->view = "default/detail";
		$this->layout = "view";
	}
}