<?php

CLASS jso 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 		= 'Sales Order';
		$this->set->dBaseH 		= 'oj';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('nooj');
		$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_sales", "ppn", "alamat_kirim");
		$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->push 		= true;
		$this->set->addClear 	= true;
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_creatingOj';
		
		if($_GET['njl'] == 'undefined'){
			$this->set->sqlGrid = $this->queryData();
		}
		
		return $this->set;
	}

	protected function queryData()
	{
		$sql = $this->em->addSelect("a.nooj as pk, a.*");
		$sql = $this->em->getFrom("oj", 'a');
		$sql = $this->em->addLeftJoin("njl", 'b', array("b.noref = a.nooj", "b.control = '1'"));
		$sql = $this->em->getWhere("a.control = 1");
		$sql = $this->em->andWhere("a.jenis = '".$this->jenis."'");
		$sql = $this->em->andWhere("b.nonjl is null");
		return $sql;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "No. Transaksi", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nooj', "size" => 25, "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' => 25, "label" => array("name" => "No.Ref#", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noref', "size" => 17)));
		
		$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_sales', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "Sales", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sales', "size" => 9, 'after' => 'select("")', 'select' => 'sales', 'select_width' => 280, 'select_height' => 245, 'select_label' => "Kode=>50;Name=>200", 'select_target' => "name=>nm_sales", 'select_warning' => true, 'display' => 'selectDisplay')));
		
		$form[] = array('row' => 125, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30, 'readonly' => true, 'child' => 'prv')));
		$form[] = array('row' => 125, 'col' => 25, "label" => array("name" => "Customer", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 9, 'after' => 'load("mastercust", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub,alamat_kirim=>alamat,alamat_kirim2=>alamat2,alamat_kirim3=>alamat3')));


		$form[] = array('row' => 25, 'col' => 500, "label" => array("name" => "No.Promo", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noprm', "size" => 15, 'after' => 'load("mprom", "false", "false")', "load" => 'jso')));
		
		$form[] = array('row' => 50, 'col' => 500, "label" => array("name" => "Alamat Kirim", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'alamat_kirim', "size" => 30)));
		$form[] = array('row' => 75, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'alamat_kirim2', "size" => 30)));
		$form[] = array('row' => 100, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'alamat_kirim3', "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" => "Keterangan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', "size" => 30)));
		$form[] = array('row' => 500, 'col' => 25, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket2', "size" => 30)));
		$form[] = array('row' => 525, 'col' => 25, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket3', "size" => 30)));
		
		$form[] = array('row' => 475, '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' => 500, 'col' => 500, "label" => array("name" => "Diskon Cash", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskg', "size" => 4, 'class' => 'decimal _hitungPo')));
		$form[] = array('row' => 500, 'col' => 660, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'diskg_nilai', "size" => 10, 'readonly' => true, 'class' => 'decimal')));

		$form[] = array('row' => 525, 'col' => 500, "label" => array("name" => "Total Diskon", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskt', "size" => 20, 'readonly' => true, 'class' => 'decimal')));
		$form[] = array('row' => 550, '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' => 575, 'col' => 500, "label" => array("name" => "PPN", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ppn', "size" => 4, 'after' => 'select("")', 'select' => 'ppn', 'class' => '_hitungPo', 'select_after' => '_hitungPo')));
		$form[] = array('row' => 575, 'col' => 660, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'ppn_nilai', "size" => 10, 'readonly' => true, 'class' => 'decimal')));
		
		$form[] = array('row' => 600, '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' => 650, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 650, '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 	= 900;
		$res->height	= 250;
		$res->col		= 25;
		$res->row		= 170;
		$res->data		= 'ojd';
		$res->pk		= 'nooj';
		$res->sort		= 'noojd';
		
		$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_jual', '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=>hargaj', '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" => 'DsRp 1',	"attr" => 'diskon_2',	'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'DsRp 2',	"attr" => 'diskon_3',	'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Ds.1(%)',	"attr" => 'diskon_4',	'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Ds.2(%)',	"attr" => 'diskon_5',	'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Ds.2a(%)',	"attr" => 'diskon_6',	'width' => 40, 'class' => 'decimal _hitungPo');
		$res->attr[] = array("label" => 'Dis.Rp',	"attr" => 'diskon',		'width' => 60, 'class' => 'decimal _hitungPo', 'type' => 'hidden');
		$res->attr[] = array("label" => 'Ket', 		"attr" => 'ket_det', 	'width' => 80);
		$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.nooj', "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" => 'Customer', 'attr' => 'a.nm_sub', "sort" => 'true', "width" => 200, 'align' => 'left');
		$res->attr[]	= array("label" => 'Sales', 'attr' => 'a.nm_sales', "sort" => 'true', "width" => 150, '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.nooj', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'nooj');

		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['nooj']);
		return $basic->getError($error);
	}
	
	public function cekStatus($nojnl, $alert = false)
	{
		if(!empty($nojnl)){
			$data = $this->em->getFind("pb", "nopb", array("noref = '$nojnl'", "(a.control = '1' or a.control is null or a.control = '0')"));
			if(!empty($data->nopb)){
				echo "<script>alert('Sales Order Sudah memiliki Pengiriman ".$data->nopb."')</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['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);
			
			// echo "<pre>";
			// var_dump($sql);
			
			$save = $this->em->getFlush($sql);

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

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