<?php

CLASS jret_off extends RasioProject
{
	protected $set;
	protected $jenis = 12;
	protected $off = 'T';
	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 		= 'RJO';
		$this->set->modul 		= 'Nota Retur';
		$this->set->dBaseH 		= 'ret';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('noret');
		$this->set->setKode 	= array("del" => '/', 'length' => 4, 'var' => array("alias", 'cabang', 'tgl'));
		$this->set->where 		= array("offline = '".$this->off."'");
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("tgl", "kd_cabang", "kd_gudang", "kd_sub");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("Save", "First", "Prev", "Next", "Last", "Browse", "prinT", "View");
		$this->set->addClear 	= true;
		$this->set->push 		= true;
		$this->set->posting 	= 'rej';
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_creatingRej';
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "No. Retur", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noret', "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' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_gudang', "size" => 30, 'readonly' => true)));
		$form[] = array('row' => 50, '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' => 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' => 25, "label" => array("name" => "No.Ref#", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'noref', "size" => 20, 'after' => 'load("jnj", "false", "loadDisplay")', 'display' => 'loadDisplay', 'load'=> 'retur, kd_sub', 'loadAfter' => 'detNjl', 'loadAct' => 'simpan', 'loadHide' => 'update', 'target' => 'kd_sub=>kd_sub,nm_sub=>nm_sub,noref=>nonjl,ppn=>ppn,diskg=>diskg')));
		
		
		$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')));



		$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' => 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 Global", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskg', "size" => 4, 'readonly' => true, 'class' => 'decimal')));
		$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 	= 800;
		$res->height	= 250;
		$res->col		= 25;
		$res->row		= 170;
		$res->data		= 'retd';
		$res->pk		= 'noret';
		$res->sort		= 'noretd';
		
		$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,	'readonly' => true);
		$res->attr[] = array("label" => 'Satuan',	"attr" => 'satuan', 	'width' => 40,	'readonly' => true);
		$res->attr[] = array("label" => 'Beli',		"attr" => 'qty_nbl',	'width' => 40, 'class' => 'decimal', 'readonly' => true);
		$res->attr[] = array("label" => 'Ret Sebelumnya',"attr" => 'qty_ret',	'width' => 40, 'class' => 'decimal', 'readonly' => true);
		$res->attr[] = array("label" => 'Stok',		"attr" => 'stok',		'width' => 40, 'class' => 'decimal', 'readonly' => 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" => '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.noret', "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" => 'Customer', 'attr' => 'a.nm_sub', "sort" => 'true', "width" => 200, '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.noret', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'noret');

		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['noret']);
		return $basic->getError($error);
	}
	
	public function cekStatus($nojnl, $alert = false)
	{
		// $data = $this->em->getFind("ret", "noret", array("noref = '$nojnl'", "(a.control = '1' or a.control is null or a.control = '0')"));
		// if(!empty($data->noret)){
			// echo "<script>alert('LPB Sudah dibuatkan Nota Beli ".$data->noret."')</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['qty'] as $n => $row){
				$row = str_ireplace(",", "", $row);
				if($row > 0){
					$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("noret = '".$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['noretd'] = $pk.'.'.date("ymdHis").$kode->getRight($nom, 3, 0);
		$attr['noret'] = $pk;
		
		unset($attr['total']);
		unset($attr['qty_nbl']);
		unset($attr['qty_ret']);
		
		$sql = $this->em->addInsert($this->set->detail->data);
		$sql = $this->em->addAttrIns($attr);

		return $sql;
	}

	public function deleteDet($id)
	{
		$sql = $this->em->addDelete('retd');
		$sql = $this->em->getWhere("noret = '$id'");
		return $sql;
	}
	
	public function Delete()
	{
		$_GET['noret'] = str_ireplace("__--__", "/", $_GET['noret']);
			
		if($this->cekStatus($_GET['noret']) == true){
			$qb = array();
			$basic = new Basic;
			$this->data = $basic->delete2($_GET, $this->set, $qb);
		}
		$this->layout = "null";
	}
	
	public function push()
	{
		$nojnl = $_GET['filPrintanoret'];
		$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['filPrintanoret'];
		$this->layout = "printLabelPdf";
	}
	
	public function pdf()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("ret", "*", array("noret = '".$_GET['filPrintanoret']."'"));
		$this->data->det 	= $this->em->getFindBy("retd", "*", array("noret = '".$_GET['filPrintanoret']."'"), "noretd 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 = "rej";
		$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 retur()
	{
		$sql = $this->em->addSelect("a.noref as nonjl, b.kd_inv, sum(b.qty) as qty_ret");
		$sql = $this->em->getFrom("ret", 'a');
		$sql = $this->em->addLeftJoin("retd", 'b', array("a.noret = b.noret"));
		$sql = $this->em->getWhere("a.noref = '".$_GET['nonjl']."'");
		if(!empty($_GET['noret'])){
			$sql = $this->em->andWhere("a.noret != '".$_GET['noret']."'");
		}
		$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 stok()
	{
		$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));

		$sql = $this->em->addSelect("a.kd_inv, sum(a.masuk-a.keluar) as stok");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->getWhere("a.kdac = '".$per->kdac."'");
		$sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59')."'");
		$sql = $this->em->andWhere("a.kd_gudang = '".strtoupper($_GET['gudang'])."'");
		$sql = $this->em->getGroupBy("a.kd_inv");
		return $sql;
	}
	
	public function Detnjl()
	{
		$ret 	= $this->retur();
		$stok 	= $this->stok();
		
		$sql = $this->em->addSelect("a.*, b.qty_ret, c.stok");
		$sql = $this->em->getFrom("njld", 'a');
		$sql = $this->em->addLeftJoin("($ret)", 'b', array("a.nonjl = b.nonjl", "a.kd_inv = b.kd_inv"));
		$sql = $this->em->addLeftJoin("($stok)", 'c', array("a.kd_inv = c.kd_inv"));
		$sql = $this->em->getWhere("a.nonjl = '".$_GET['nonjl']."'");
		$sql = $this->em->getMultiSort("a.kd_inv asc");
		$eks = $this->em->queryEks($sql);
		
		$res = array();
		while($row = $this->em->fetchObject($eks)){
			$row->qty_nbl 	= $row->qty;
			$row->qty 		= $row->qty - $row->qty_ret;
			
			if($row->qty > 0){
				$res[] = $row;
			}
		}
		
		$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";
	}
}