<?php

CLASS lpb 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 		= 'LPB';
		$this->set->modul 		= 'Lembar Penerimaan Barang';
		$this->set->dBaseH 		= 'lpb';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('nolpb');
		$this->set->setKode 	= array("del" => '/', 'length' => 4, 'var' => array("alias", 'cabang', '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_cabang", "kd_gudang", "tglsj", "nosj");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("Save", "First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "prinT", "View");
		$this->set->addClear 	= true;
		$this->set->push 		= true;
		$this->set->posting 	= 'lpb';
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_creatingLpb';
		
		if($_GET['nota'] == 'undefined'){
			$this->set->sqlGrid = $this->queryData();
		}
		
		return $this->set;
	}
	
	protected function queryData()
	{
		$sql = $this->em->addSelect("a.*, a.nolpb as pk");
		$sql = $this->em->getFrom($this->set->dBaseH, 'a');
		$sql = $this->em->addLeftJoin("nbl", 'b', array("a.nolpb = b.noref", "(b.control = '1' or b.control is null or b.control = '0')"));
		$sql = $this->em->getWhere("b.nonbl is null");
		$sql = $this->em->andWhere("a.control = 1");
		return $sql;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "No. LPB", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nolpb', "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' => 0, 'col' => 690, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'control', "size" => 10, 'readonly' => true)));

		$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("prc", "false", "loadDisplay")', 'display' => 'loadDisplay', 'load'=> 'lpb', 'loadAfter' => 'detPo', 'loadAct' => 'simpan', '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,ket=>ket,ket2=>ket2,ket3=>ket3')));
		
		$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' => 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, 'after' => 'select("")', 'select' => 'gudang', 'select_width' => 280, 'select_height' => 245, 'select_label' => "Kode=>50;Name=>200", 'select_target' => "name=>nm_gudang", 'select_warning' => true, 'display' => 'selectDisplay')));


		$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" => "No & Tgl SJ", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nosj', "size" => 15)));
		$form[] = array('row' => 100, 'col' => 720, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'tglsj', "size" => 10,  'class' => 'tanggal')));
		
		$form[] = array('row' => 450, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 450, '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		= 'lpbd';
		$res->pk		= 'nolpb';
		$res->sort		= 'nolpbd';
		
		$res->attr[] = array("label" => 'Kode',		"attr" => 'kd_inv', 	'width' => 80, 	'after' => 'load("minv", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_inv=>nm_inv,satuan=>satuan,skala=>skala,harga=>harga_beli', 'notnull' => true);
		$res->attr[] = array("label" => 'nama',		"attr" => 'nm_inv', 	'width' => 250, 'notnull' => true,	'readonly' => true);
		$res->attr[] = array("label" => 'Qty PO',	"attr" => 'qty_po',	'width' => 70, 'class' => 'decimal', 'readonly' => true);
		$res->attr[] = array("label" => 'Qty Dtg Sebelumnya',"attr" => 'qty_lpb',	'width' => 70, 'class' => 'decimal', 'readonly' => true);
		$res->attr[] = array("label" => 'Qty Dtg Sekarang',"attr" => 'qty',	'width' => 70, 'class' => 'decimal');
		
		$res->attr[] = array("label" => 'Dis.1(%)',	"attr" => 'diskon_2',	'width' => 40, 'class' => 'decimal _hitungPo', 'type' => 'hidden');
		$res->attr[] = array("label" => 'Dis.2(%)',	"attr" => 'diskon_3',	'width' => 40, 'class' => 'decimal _hitungPo', 'type' => 'hidden');
		$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" => 'Total',	"attr" => 'total',		'width' => 80, 'class' => 'decimal', 'dec' => 2, 'readonly' => true, 'type' => 'hidden');
		
		
		$res->attr[] = array("label" => 'Satuan',	"attr" => 'satuan', 	'width' => 40, 	'after' => 'load("msat", "flase", "loadDisplay")', 'display' => 'loadDisplay', "load" => 'kd_inv', "target" => 'skala=>skala', 'detail' => true);
		$res->attr[] = array("label" => 'Skala',	"attr" => 'skala',	'width' => 50, 'class' => 'decimal', 'readonly' => true);
		$res->attr[] = array("label" => 'Ket', 		"attr" => 'ket_det', 	'width' => 100);
		
		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.nolpb', "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.nolpb', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'nolpb');

		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['nolpb']);
		return $basic->getError($error);
	}
	
	public function cekStatus($nojnl, $alert = false)
	{
		if(!empty($nojnl)){
			$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()
	{
		$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['tglsj'] 	= $date->serialDate($_POST['tglsj']);
			$_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);
			
			if($save != false){
				$nom = $this->em->getFind($this->set->dBaseH, "_no", array("nolpb = '".$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['nolpbd'] = $pk.'.'.date("ymdHis").$kode->getRight($nom, 3, 0);
		$attr['nolpb'] = $pk;
		
		unset($attr['qty_po']);
		unset($attr['qty_lpb']);
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}
	
	public function deleteDet($id)
	{
		$sql = $this->em->addDelete('lpbd');
		$sql = $this->em->getWhere("nolpb = '$id'");
		return $sql;
	}
	
	public function Delete()
	{
		$_GET['nolpb'] = str_ireplace("__--__", "/", $_GET['nolpb']);
		$qb = array();
			
		if($this->cekStatus($_GET['nolpb']) == true){
			$qb = array();
			$basic = new Basic;
			$this->data = $basic->delete2($_GET, $this->set, $qb);
		}
		$this->layout = "null";
	}
	
	public function push()
	{
		$nojnl = $_GET['filPrintanolpb'];
		$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['filPrintanolpb'];
		$this->layout = "printLabelPdf";
	}
	
	public function pdf()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("lpb", "*", array("nolpb = '".$_GET['filPrintanolpb']."'"));
		$this->data->det 	= $this->em->getFindBy("lpbd", "*", array("nolpb = '".$_GET['filPrintanolpb']."'"), "nolpbd asc");
		$this->data->cabang = $this->em->getFind("cabang", "*", array("kd_cabang = '".$this->data->data->kd_cabang."'"));
		$this->data->gudang = $this->em->getFind("gudang", "*", array("kd_gudang = '".$this->data->data->kd_gudang."'"));
		$this->data->sub 	= $this->em->getFind("sub", "*", array("kd_sub = '".$this->data->data->kd_sub."'"));

		$this->view = "lpb";
		$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";
	}
	
	public function lbp()
	{
		$sql = $this->em->addSelect("a.noref as no_po, b.kd_inv, sum(b.qty) as qty_lpb");
		$sql = $this->em->getFrom("lpb", 'a');
		$sql = $this->em->addLeftJoin("lpbd", 'b', array("a.nolpb = b.nolpb"));
		$sql = $this->em->getWhere("a.noref = '".$_GET['nopo']."'");
		if(!empty($_GET['nolpb'])){
			$sql = $this->em->andWhere("a.nolpb != '".$_GET['nolpb']."'");
		}
		$sql = $this->em->andWhere("(a.control = '1' or a.control is null or a.control = '0')");
		$sql = $this->em->getGroupBy("a.noref, b.kd_inv");

		return $sql;
	}
	
	public function getDetLpb($nojnl)
	{
		$data = $this->em->getFindBy("lpbd", "kd_inv, qty", array("nolpb = '$nojnl'"), "kd_inv asc");
		$res  = array();
		foreach($data as $row){
			$res[$row->kd_inv] = $row->qty;
		}
		return $res;
	}
	
	public function detPo()
	{
		$lpb = $this->lbp();
		
		if($lpb == 'apank'){
			$sql = $this->em->addSelect("a.*, b.qty_lpb");
			$sql = $this->em->getFrom("beli", 'a');
			$sql = $this->em->addLeftJoin("($lpb)", 'b', array("a.no_po = b.no_po", "a.kd_inv = b.kd_inv"));
			$sql = $this->em->addLeftJoin("lpbd", 'c', array("c.nolpb = '".$_GET['nolpb']."'", "a.kd_inv = c.kd_inv"));
			$sql = $this->em->getWhere("a.no_po = '".$_GET['nopo']."'");
			$sql = $this->em->andWhere("c.nolpbd is not null");
			$sql = $this->em->getMultiSort("a.kd_inv asc");
			$eks = $this->em->queryEks($sql);
		}else{
			$sql = $this->em->addSelect("a.*, b.qty_lpb");
			$sql = $this->em->getFrom("beli_det", 'a');
			$sql = $this->em->addLeftJoin("($lpb)", 'b', array("a.no_po = b.no_po", "a.kd_inv = b.kd_inv"));
			$sql = $this->em->getWhere("a.no_po = '".$_GET['nopo']."'");
			$sql = $this->em->getMultiSort("a.kd_inv asc");
			$eks = $this->em->queryEks($sql);
		}
		
		$detLpb = $this->getDetLpb($_GET['nolpb']);
	
		$res = array();
		while($row = $this->em->fetchObject($eks)){
			$row->qty_po 	= $row->qty;
			$row->qty 		= $row->qty - $row->qty_lpb;
			
			if(!empty($_GET['nolpb'])){
				$row->qty 	= $detLpb[$row->kd_inv];
				if(empty($row->qty)){
					$row->qty = 0;
				}
			}
			
			if($row->qty > 0){
				$res[] = $row;
			}
		}
		
		$this->sessionDet(__CLASS__, $this->set, $res);
		echo "<script>$('#ket_file').val('".$_GET['file']."')</script>";
		echo "<script>_listLoadDetail('".__CLASS__."', '')</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']."'"));
		$po = $this->em->getFind("beli", '*', array("no_po = '".$data->noref."'"));
		
		$data->diskg = $po->diskg;
		$data->ppn 	 = $po->ppn;
		
		if(!empty($data->tgl)){
			$data->tgl = date("d/m/Y", strtotime($data->tgl));
		}
		
		$basic = new Basic;
		$res = $basic->loadDisplay($data, $_GET);
		echo json_encode($res);
		$this->layout = "null";
	}
}