<?php

CLASS bso 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 		= 'SO';
		$this->set->modul 		= 'Stok Opname';
		$this->set->dBaseH 		= 'so';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('noso');
		$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_cabang", "kd_gudang", "ket");
		$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 	= 'so';
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_creatingSo';
		
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "No. so", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noso', "size" => 20, "readonly" => true)));
		$form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "Tanggal", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));

		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "No.Ref#", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noref', "size" => 20)));
		
		$form[] = array('row' => 75, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_cabang', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 75, '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' => 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" => "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')));

		$form[] = array('row' => 125, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nmac', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 125, 'col' => 25, "label" => array("name" => "Acc", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kdac', "size" => 9, 'after' => 'load("macd", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => "nmac=>nmac")));


		$form[] = array('row' => 50, 'col' => 500, "label" => array("name" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 30)));
		$form[] = array('row' => 75, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket2', "size" => 30)));
		$form[] = array('row' => 100, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket3', "size" => 30)));
		$form[] = array('row' => 125, 'col' => 500, "label" => array("name" => "Status", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'control', "size" => 10, 'readonly' => true)));
		
		$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', 'size' => 20, 'readonly' => true, 'mode' => 'datetime')));
		return $form;
	}
	
	public function setDetail()
	{
		$res->width 	= 800;
		$res->height	= 250;
		$res->col		= 25;
		$res->row		= 155;
		$res->data		= 'sod';
		$res->pk		= 'noso';
		$res->sort		= 'nosod';
		
		$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,stok=>stok', 'load' =>'kd_gudang,tgl', 'notnull' => true);
		$res->attr[] = array("label" => 'nama',		"attr" => 'nm_inv', 	'width' => 250, 'notnull' => true,	'readonly' => true);
		$res->attr[] = array("label" => 'Stok',"attr" => 'stok',	'width' => 50, 'class' => 'decimal', 'readonly' => true);
		$res->attr[] = array("label" => 'Qty',"attr" => 'qty',	'width' => 50, 'class' => 'decimal');
		$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' => 250);
		
		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.noso', "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" => 'Gudang', 'attr' => 'a.nm_gudang', "sort" => 'true', "width" => 150, 'align' => 'left');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket_jurnal', "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.noso', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'noso');

		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['noso']);
		return $basic->getError($error);
	}
	
	public function cekStatus($nojnl, $alert = 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['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("noso = '".$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['nosod'] = $pk.'.'.date("ymdHis").$kode->getRight($nom, 3, 0);
		$attr['noso'] = $pk;
		
		unset($attr['qty_po']);
		unset($attr['qty_so']);
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}
	
	public function deleteDet($id)
	{
		$sql = $this->em->addDelete('sod');
		$sql = $this->em->getWhere("noso = '$id'");
		return $sql;
	}
	
	public function Delete()
	{
		$_GET['noso'] = str_ireplace("__--__", "/", $_GET['noso']);
		$qb = array();
			
		if($this->cekStatus($_GET['noso']) == true){
			$qb = array();
			$basic = new Basic;
			$this->data = $basic->delete2($_GET, $this->set, $qb);
		}
		$this->layout = "null";
	}
	
	public function Push()
	{
		$nojnl = $_GET['filPrintanoso'];
		$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['filPrintanoso'];
		$this->layout = "printLabelPdf";
	}
	
	public function pdf()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("so", "*", array("noso = '".$_GET['filPrintanoso']."'"));
		$this->data->det 	= $this->em->getFindBy("sod", "*", array("noso = '".$_GET['filPrintanoso']."'"), "nosod 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 = "so";
		$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";
	}
}