<?php

CLASS obeli 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 		= 'PO';
		$this->set->modul 		= 'Order Beli';
		$this->set->dBaseH 		= 'beli';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('no_po');
		$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_po", "kd_sub");
		$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->funct 		= '_creatingOrderB';
		
		if($_GET['lpb'] == 'undefined'){
			$this->set->sqlGrid = $this->queryData();
		}
		
		return $this->set;
	}

	protected function queryData()
	{
		
		$sql = $this->em->addSelect("a.*, a.no_po as pk");
		$sql = $this->em->getFrom('beli', 'a');
		$sql = $this->em->addLeftJoin('lpb', 'b', array("a.no_po = b.noref"));
		$sql = $this->em->getWhere("a.control = '1'");
		$sql = $this->em->andWhere("(b.nolpb is null or b.nolpb = '' or b.nolpb = '".$_GET['nolpb']."')");
		$sql = $this->em->getMultiSort("a.no_po");
		
		return $sql;
	}
	
	protected function setForm()
	{
	    $row = 0;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "No. Order", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'no_po', "size" => 25, "readonly" => true)));
		
		$row += 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Tanggal", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl_po', "size" => 10, 'class' => 'tanggal')));
        $form[] = array('row' => $row, 'col' => 240, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'control', "size" => 10, 'readonly' => true)));
        
        $row += 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "No.Ref#", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noref', "size" => 17)));
		
		//$row += 25;
		//$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));
		//$form[] = array('row' => $row, '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')));
		
        //$row += 25;
		//$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_gudang', "size" => 30, 'readonly' => true)));
		//$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Gudang", "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')));

        $row += 25;
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true, 'child' => 'prv')));
		$form[] = array('row' => $row, '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')));

        //$row += 25;
		//$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Sopir", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30)));

        //$row += 25;
		//$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Nopol", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nopol', "size" => 10)));

        //$row += 25;
		//$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_kat', "size" => 30, 'readonly' => true, 'child' => 'prv')));
		//$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Jenis Kendaraan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_kat', "size" => 9, 'after' => 'load("mkendaraan", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_kat=>nm_kat')));
        
        $row += 25;
		$form[] = array('row' => $row, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_inv', "size" => 30, 'readonly' => true, 'child' => 'prv')));
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Material", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_inv', "size" => 9, 'after' => 'load("mmaterial", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_inv=>nm_inv')));

        $row += 25;
        $form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Berat", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'qty', "size" => 10, 'class' => 'decimal')));
        $form[] = array('row' => $row+3, 'col' => 220, "label" => array("name" => "Kg", "width" => 100), "input" => false);
        
        $row += 25;
        $form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Refraksi", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'humid', "size" => 5, 'class' => 'decimal')));
        $form[] = array('row' => $row+3, 'col' => 190, "label" => array("name" => "%", "width" => 100), "input" => false);
        
        $row += 25;
        $form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Harga/Kg Rp.", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'harga', "size" => 15, 'class' => 'decimal')));
        
        $row += 25;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 30)));
	
	    $row += 50;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => $row, 'col' => 500, "label" => array("name" => "Last", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'size' => 20, 'readonly' => true, 'mode' => 'datetime')));
		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.no_po', "sort" => 'true', "width" => 150, 'align' => 'left', 'focus' => 'true');
		$res->attr[]	= array("label" => 'Tanggal', 'attr' => 'a.tgl_po', "sort" => 'true', "width" => 70, 'align' => 'center', 'type' => 'date');
		$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.no_po', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'no_po');

		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);
	
		$error[] = $this->cekStatus($_POST['no_po']);
		return $basic->getError($error);
	}
	
	public function cekStatus($nojnl, $alert = false)
	{
		if(!empty($nojnl)){
			$data = $this->em->getFind("lpb", "nolpb", array("noref = '$nojnl'", "(a.control = '1' or a.control is null or a.control = '0')"));
			if(!empty($data->nolpb)){
				echo "<script>alert('PO Sudah memiliki LPB ".$data->nolpb."')</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_po'])));
		//$post['cabang'] = $_POST['kd_cabang'];
		//$post['cabang'] = "HO";
		
		$basic = new Basic;
		return $basic->kode($this->set, $post, $_POST);
	}
	
	public function proses()
	{
	    ini_set("display_errors","On");
		$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;
			$tambah['kd_cabang'] 	= "HO";
			$tambah['nm_cabang'] 	= "HEAD OFFICE";
			$_POST['tgl_po'] 	= $date->serialDate($_POST['tgl_po']);
			$_POST['control'] 	= 1;
			
			$qb = array();

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

			if($save != false){
				$nom = $this->em->getFind($this->set->dBaseH, "_no", array("no_po = '".$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 Delete()
	{
		$_GET['no_po'] = str_ireplace("__--__", "/", $_GET['no_po']);
		if($this->cekStatus($_GET['no_po']) == true){
			$qb = array();
			$basic = new Basic;
			$this->data = $basic->delete2($_GET, $this->set, $qb);
		}
		$this->layout = "null";
	}
	
	public function cetak()
	{
		$this->data->kode = $_GET['filPrintano_po'];
		$this->layout = "printLabelPdf";
	}
	
	public function pdf()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("beli", "*", array("no_po = '".$_GET['filPrintano_po']."'"));
		$this->data->det 	= $this->em->getFindBy("beli_det", "*", array("no_po = '".$_GET['filPrintano_po']."'"), "no_po_det 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 = "obeli";
		$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 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";
	}
}