<?php

CLASS nob extends RasioProject
{
	protected $set;
	protected $jenis = 1;
	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 		= 'PBL';
		$this->set->modul 		= 'Nota Pembelian';
		$this->set->dBaseH 		= 'nbl';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('nonbl');
		$this->set->setKode 	= array("del" => '-', 'length' => 4, 'var' => array("alias", 'tgl'));
		$this->set->where 		= array("jenis = '".$this->jenis."'");
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("tgl", "kd_sub", "kd_gudang");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("Save", "First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "prinT");
		$this->set->addClear 	= true;
		$this->set->push 		= true;
		$this->set->posting 	= 'nbl';
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_creatingNota';
		
		$this->set->where[]		= "tgl >= '".$this->login['date_aw']."'";
		$this->set->where[]		= "tgl <= '".$this->login['date_ak']." 23:59:59'";
		
		if($_GET['retur'] == 'undefined'){
			$this->set->sqlGrid = $this->queryData();
		}
		
		return $this->set;
	}
	
	protected function queryData()
	{
		$sql = $this->em->addSelect("a.*, a.nonbl as pk");
		$sql = $this->em->getFrom($this->set->dBaseH, 'a');
		$sql = $this->em->getWhere("a.control = 1");
		return $sql;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "No. Nota", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nonbl', "size" => 20, "readonly" => true)));
		$form[] = array('row' => 0, 'col' => 500, "label" => array("name" => "Tanggal", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));

		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "No.Ref#", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noref', "size" => 20, 'after' => 'load("lpb", "false", "loadDisplay")', 'display' => 'loadDisplay', 'load'=> 'nota', 'loadAfter' => 'detLpb', 'loadAct' => 'simpan,update', 'loadHide' => 'update', 'target' => 'kd_cabang=>kd_cabang,nm_cabang=>nm_cabang,kd_sub=>kd_sub,nm_sub=>nm_sub,kd_gudang=>kd_gudang,nm_gudang=>nm_gudang,noref=>nolpb,ppn=>ppn,diskg=>diskg,tgl_datang=>tgl,ket=>ket,ket2=>ket2,ket3=>ket3')));
		$form[] = array('row' => 25, 'col' => 285, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'tgl_datang', "size" => 10, 'mode' => 'date', 'readonly' => true)));
		
		$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, 'readonly' => true)));
		
		$form[] = array('row' => 75, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true, 'child' => 'prv')));
		$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "Supplier", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 9, 'after' => 'load("mastersupp", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub')));

		$form[] = array('row' => 100, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_gudang', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "Penerimaan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_gudang', "size" => 9, 'readonly' => true)));


		$form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 30)));
		$form[] = array('row' => 50, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket2', "size" => 30)));
		$form[] = array('row' => 75, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket3', "size" => 30)));
		$form[] = array('row' => 100, 'col' => 500, "label" => array("name" => "Status", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'control', "size" => 10, 'readonly' => true)));
		
		$form[] = array('row' => 425, 'col' => 500, "label" => array("name" => "Sub Total", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'stot', "size" => 20, 'readonly' => true, 'class' => 'decimal')));
		
		$form[] = array('row' => 450, 'col' => 500, "label" => array("name" => "Disc. Global (%)", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskg', "size" => 4, 'class' => 'decimal _hitungPo')));
		$form[] = array('row' => 450, 'col' => 660, "label" => array("name" => " +", "width" => 20), "input" => array("type" => 'text',	'attr' => array("name" => 'diskg2', "size" => 4, 'class' => 'decimal _hitungPo')));

		$form[] = array('row' => 475, 'col' => 500, "label" => array("name" => "Nilai Diskon", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskg_nilai', "size" => 20, 'readonly' => true, 'class' => 'decimal')));
		$form[] = array('row' => 500, 'col' => 500, "label" => array("name" => "DPP", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'dpp', "size" => 20, 'readonly' => true, 'class' => 'decimal')));
		
		$form[] = array('row' => 525, 'col' => 500, "label" => array("name" => "PPN", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ppn', "size" => 4, 'after' => 'select("")', 'select' => 'ppn', 'class' => '_hitungPo')));
		$form[] = array('row' => 525, 'col' => 660, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'ppn_nilai', "size" => 10, 'readonly' => true, 'class' => 'decimal')));
		
		$form[] = array('row' => 550, 'col' => 500, "label" => array("name" => "Grand Total", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'gtot', "size" => 20, 'readonly' => true, 'class' => 'decimal')));
		
		
		$form[] = array('row' => 600, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 600, 'col' => 500, "label" => array("name" => "Last", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'size' => 20, 'readonly' => true, 'mode' => 'datetime')));
		return $form;
	}
	
	public function setDetail()
	{
		$res->width 	= 800;
		$res->height	= 250;
		$res->col		= 25;
		$res->row		= 130;
		$res->data		= 'nbld';
		$res->pk		= 'nonbl';
		$res->sort		= 'nonbld';
		
		$res->attr[] = array("label" => 'Kode',		"attr" => 'kd_inv', 	'width' => 130, 	'after' => 'load("minv", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_inv=>nm_inv,satuan=>satuan,skala=>skala,harga=>harga_beli', 'notnull' => true, 'loadAfter' => '_hitungPo');
		$res->attr[] = array("label" => 'nama',		"attr" => 'nm_inv', 	'width' => 150, 'notnull' => true,	'readonly' => true);
		$res->attr[] = array("label" => 'Satuan',	"attr" => 'satuan', 	'width' => 40, 	'after' => 'load("msat", "flase", "loadDisplay")', 'display' => 'loadDisplay', "load" => 'kd_inv', "target" => 'skala=>skala,harga=>hargab', 'detail' => true);
		$res->attr[] = array("label" => 'Qty',		"attr" => 'qty',		'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Skala',	"attr" => 'skala',		'width' => 20, 'class' => 'decimal', 'readonly' => true);
		$res->attr[] = array("label" => 'Harga',	"attr" => 'harga',		'width' => 50, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Dis.1(%)',	"attr" => 'diskon_2',	'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Dis.2(%)',	"attr" => 'diskon_3',	'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Dis.3(%)',	"attr" => 'diskon_4',	'width' => 40, 'class' => 'decimal _hitungPo', 'type' => 'hidden');
		$res->attr[] = array("label" => 'Dis.4(%)',	"attr" => 'diskon_5',	'width' => 40, 'class' => 'decimal _hitungPo', 'type' => 'hidden');
		$res->attr[] = array("label" => 'Dis.5(%)',	"attr" => 'diskon_6',	'width' => 40, 'class' => 'decimal _hitungPo', 'type' => 'hidden');
		$res->attr[] = array("label" => 'Dis.Rp',	"attr" => 'diskon',		'width' => 60, 'class' => 'decimal _hitungPo', 'type' => 'hidden');
		$res->attr[] = array("label" => 'Ket', 		"attr" => 'ket_det', 	'width' => 150);
		$res->attr[] = array("label" => 'Total',	"attr" => 'total',		'width' => 80, 'class' => 'decimal', 'dec' => 2, 'readonly' => true);
		
		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.nonbl', "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" => 'Kirim', 'attr' => 'a.nm_cabang', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Supplier', 'attr' => 'a.nm_sub', "sort" => 'true', "width" => 200, 'align' => 'left');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket', "sort" => 'true', "width" => 150, 'align' => 'left');
		$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.nonbl', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'nonbl');

		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);

		$kd_inv = $_POST['kd_inv'];
		foreach($kd_inv as $row){
			if(!empty($row)){
				$cek = $this->em->getFind("inv", "kd_inv", array("kd_inv = '$row'"));
				if(empty($cek->kd_inv)){
					echo "<script>$('#_displayErrorPopUp').html('Kode Artikel $row Tidak terdaftar')</script>";
					$error[] = false;
				}
			}
		}
	
		$error[] = $this->cekStatus($_POST['nonbl']);
		return $basic->getError($error);
	}
	
	public function cekStatus($nojnl, $alert = false)
	{
		// $data = $this->em->getFind("nbl", "nonbl", array("noref = '$nojnl'", "(a.control = '1' or a.control is null or a.control = '0')"));
		// if(!empty($data->nonbl)){
			// echo "<script>alert('LPB Sudah dibuatkan Nota Beli ".$data->nonbl."')</script>";
			// return false;
		// }
		return true;
	}
	
	protected function kode($get = array())
	{
		$date = new date;
		$post = array();
		$post['alias'] 	= $this->set->alias;
		$post['tgl'] 	= date("ym", strtotime($date->serialDate($_POST['tgl'])));
		$post['cabang'] = $_POST['kd_cabang'];
		
		$basic = new Basic;
		return $basic->kode($this->set, $post, $_POST);
	}
	
	public function proses()
	{
		$_POST['kd_cabang'] = 'HO';
		$_POST['nm_cabang'] = 'CV. GRAHA BUSANA PRIMA';
		
		$_POST['kd_gudang'] = '0.0.06.10';
		$_POST['nm_gudang'] = 'PERSEDIAAN BARANG DAGANGAN';

		$basic = new Basic;
		$date  = new Date;
		$kode  = new Kode;
		$validasi = $this->validasi();
		if($validasi == true){
			$this->set->nojnl = $this->kode($_POST);
			
			$tambah = array();
			$tambah['jenis'] 	= $this->jenis;
			$_POST['tgl'] 		= $date->serialDate($_POST['tgl']);
			$_POST['control'] 	= 1;
			
			$no = 1;
			$qb = array();
			$qb[] = $this->deleteDet($this->set->nojnl['nojnl']);
			foreach($_POST['kd_inv'] as $n => $row){
				if(!empty($row)){
					$qb[] = $this->saveDet($n, $this->set->nojnl['nojnl'], $no++);
				}
			}

			$sql = $basic->proses3($_POST, $this->set, $tambah, $qb, array(), true);
			$save = $basic->posting($this->set, $sql, $this->set->posting);
			
			// echo "<pre>";
			// var_dump($sql);

			if($save != false){
				$nom = $this->em->getFind($this->set->dBaseH, "_no", array("nonbl = '".$this->set->nojnl['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 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['nonbld'] = $pk.'.'.date("ymdHis").$kode->getRight($nom, 3, 0);
		$attr['nonbl'] = $pk;
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}

	public function deleteDet($id)
	{
		$sql = $this->em->addDelete('nbld');
		$sql = $this->em->getWhere("nonbl = '$id'");
		return $sql;
	}
	
	public function Delete()
	{
		$_GET['nonbl'] = str_ireplace("__--__", "/", $_GET['nonbl']);
			
		if($this->cekStatus($_GET['nonbl']) == true){
			$qb = array();
			$basic = new Basic;
			
			$nbl = $this->em->getFind("nbl", "noref", array("nonbl = '".$_GET['nonbl']."'"));
			if(!empty($nbl->noref)){
				$posting = new Posting;
				$qb = $posting->lpb($this->set, $nbl->noref, true);
			}
			
			$this->data = $basic->delete2($_GET, $this->set, $qb);
		}
		$this->layout = "null";
	}
	
	public function Pending()
	{
		$_GET['nonbl'] = str_ireplace("__--__", "/", $_GET['nonbl']);
			
		if($this->cekStatus($_GET['nonbl']) == true){
			$qb = array();
			$basic = new Basic;
			
			$nbl = $this->em->getFind("nbl", "noref", array("nonbl = '".$_GET['nonbl']."'"));
			if(!empty($nbl->noref)){
				$posting = new Posting;
				$qb = $posting->lpb($this->set, $nbl->noref, true);
			}
			
			$this->data = $basic->pending($_GET, $this->set, $qb);
		}
		$this->layout = "null";
	}
	
	public function push()
	{
		$nojnl = $_GET['filPrintanonbl'];
		$pk = $this->set->dBasePk[0];
		$data = $this->em->getFind($this->set->dBaseH, "control, '".$this->set->alias."' as alias, to_char(tgl, 'yymm') as tgl, kd_cabang as cabang, '$nojnl' as nojnl", array("$pk = '$nojnl'"));
		$this->set->url = $this->baseUrl;
		$this->layout = 'null';
		$basic  = new Basic;
		$basic->cekTmp($this->set, $data);
		$this->layout = "null";
	}
	
	public function cetak()
	{
		$this->data->kode = $_GET['filPrintanonbl'];
		$this->layout = "printLabelPdf";
	}
	
	public function pdf()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("nbl", "*", array("nonbl = '".$_GET['filPrintanonbl']."'"));
		$this->data->det 	= $this->em->getFindBy("nbld", "*", array("nonbl = '".$_GET['filPrintanonbl']."'"), "nonbld asc");
		$this->data->gudang = $this->em->getFind("cabang", "*", array("kd_cabang = '".$this->data->data->kd_cabang."'"));
		$this->data->sub 	= $this->em->getFind("sub", "*", array("kd_sub = '".$this->data->data->kd_sub."'"));

		$this->view = "nbl";
		$this->layout = "printForm";
	}
	
	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";
	}
	
	public function detLpb()
	{
		if(!empty($_GET['nonbl'])){
			$res = $this->em->getFindBy("nlbd", "*", array("nonbl = '".$_GET['nonbl']."'"), "nonbld");
		}else{
			$lpb = $this->em->getFind("lpb", "*", array("nolpb = '".$_GET['nolpb']."'"));
			if(!empty($lpb->noref)){
				$sql = $this->em->addSelect("b.*, a.qty");
				$sql = $this->em->getFrom("lpbd", 'a');
				$sql = $this->em->addLeftJoin("beli_det", 'b', array("a.kd_inv = b.kd_inv", "b.no_po = '".$lpb->noref."'"));
				$sql = $this->em->getWhere("a.nolpb = '".$_GET['nolpb']."'");
				$sql = $this->em->getMultiSort("a.nolpbd asc");
				$eks = $this->em->queryEks($sql);
				$res = $this->em->getFetchObject($eks);
			}else{
				$sql = $this->em->addSelect("a.*, c.hargab as harga");
				$sql = $this->em->getFrom("lpbd", 'a');
				$sql = $this->em->addLeftJoin("inv", 'b', array("a.kd_inv = b.kd_inv"));
				$sql = $this->em->addLeftJoin("satuan", 'c', array("a.kd_inv = c.kd_inv", "a.satuan = c.nmsat"));
				$sql = $this->em->getWhere("a.nolpb = '".$_GET['nolpb']."'");
				$sql = $this->em->getMultiSort("a.nolpbd asc");
				$eks = $this->em->queryEks($sql);
				$tmp = $this->em->getFetchObject($eks);
				$res = array();
				foreach($tmp as $r){
					$res[$r->nolpbd] = $r;
				}
			}
		}
		
				
		$this->sessionDet(__CLASS__, $this->set, $res);
		echo "<script>$('#ket_file').val('".$_GET['file']."')</script>";
		echo "<script>_listLoadDetail('".__CLASS__."', '_hitungPo')</script>";
		echo "<script>$('#apankForm').empty()</script>";
		$this->layout = "null";
	}
	
	public function loadDisplay()
	{
		$pk = $this->set->dBasePk[0];
		$data = $this->em->getFind($this->set->dBaseH, '*', array("$pk = '".$_GET['idRef']."'"));
		$basic = new Basic;
		$res = $basic->loadDisplay($data, $_GET);
		echo json_encode($res);
		$this->layout = "null";
	}
}