<?php

CLASS njual 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 		= 'NJL';
		$this->set->modul 		= 'Nota Penjualan';
		$this->set->dBaseH 		= 'njl';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('nonjl');
		$this->set->setKode 	= array("del" => '-', 'length' => 5, '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", "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->push 		= true;
		$this->set->addClear 	= true;
		$this->set->posting 	= 'njual';
		$this->set->detail 		= $this->setDetail();
		$this->set->funct 		= '_creatingNjlJ';
		
		//$this->set->where[]		= "tgl >= '".$this->login['date_aw']."'";
		//$this->set->where[]		= "tgl <= '".$this->login['date_ak']." 23:59:59'";
		
		if($_GET['lpb'] == 'undefined'){
			$this->set->sqlGrid = $this->queryData();
		}
		if($_GET['retur'] == 'undefined'){
			$this->set->where 	= array("jenis = '".$this->jenis."'", "control = '1'");
		}

		return $this->set;
	}

	protected function lpb()
	{
		$sql = $this->em->addSelect("a.noref as nonjl, b.kd_inv, sum(b.qty) as qty");
		$sql = $this->em->getFrom("pb", 'a');
		$sql = $this->em->addLeftJoin("pbd", 'b', array("a.nopb = b.nopb"));
		$sql = $this->em->getWhere("a.jenis = '".$this->jenis."'");
		$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;
	}

	protected function queryData()
	{
		$lpb = $this->lpb();
		
		$sql = $this->em->addSelect("a.nonjl as pk, a.nonjl, a.tgl, a.nm_gudang, a.nm_sub
									, a.ket||' '||a.ket2||' '||a.ket3 as ket, a.control");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("njl_det", 'b', array("a.nonjl = b.nonjl"));
		$sql = $this->em->addLeftJoin("($lpb)", 'c', array("a.nonjl = c.nonjl", "b.kd_inv = c.kd_inv"));
		$sql = $this->em->getWhere("a.control = 1");
		$sql = $this->em->andWhere("a.jenis = '".$this->jenis."'");
		$sql = $this->em->andWhere("COALESCE(b.qty, 0) > COALESCE(c.qty, 0)");
		$sql = $this->em->getGroupBy("a.nonjl, a.tgl, a.nm_gudang, a.nm_sub, a.ket||' '||a.ket2||' '||a.ket3, a.control");
		return $sql;
	}
	
	protected function setForm()
	{
	    $row = 0;
		$form[] = array('row' => $row, 'col' => 25, "label" => array("name" => "No. Transaksi", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nonjl', "size" => 25, "readonly" => true)));
		$form[] = array('row' => $row, 'col' => 500, "label" => array("name" => "Tanggal", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'tgl', "size" => 10, 'class' => 'tanggal')));
		$form[] = array('row' => $row, 'col' => 720, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'control', "size" => 10, 'readonly' => true)));

		//$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, 'readonly' => true)));

		//$form[] = array('row' => 75, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sales', "size" => 30, 'readonly' => true)));
		//$form[] = array('row' => 75, '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' => 330, 'select_height' => 245, 'select_label' => "Kode=>100;Name=>200", 'select_target' => "name=>nm_sales", '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" => "Lokasi", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_gudang', "size" => 9, 'readonly' => true)));

		//$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" => "Persediaan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kdac', "size" => 9, 'readonly' => true)));
		
		//$form[] = array('row' => 150, 'col' => 25, "label" => array("name" => "Diskon Cash", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'diskg', "size" => 9, 'class' => 'decimal _hitungPo')));
		//$form[] = array('row' => 150, 'col' => 220, "label" => false, "input" => array("type" => 'text',	'attr' => array("name" => 'diskg_nilai', "size" => 10, 'readonly' => true, 'class' => 'decimal')));
		
		$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" => "Customer", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 9, 'after' => 'load("mastercust", "false", "loadDisplay")', 'loadAct' => 'simpan', 'display' => 'loadDisplay', 'target' => 'nm_sub=>nm_sub,alamat_kirim=>alamat,alamat_kirim2=>alamat2,alamat_kirim3=>alamat3,top=>jt')));
        $form[] = array('row' => $row, 'col' => 500, "label" => array("name" => "Alamat Kirim", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'alamat_kirim', "size" => 30)));
        
        $row += 25;
        $form[] = array('row' => $row+5, 'col' => 500, "label" => array("name" => "<input type='button' id='_loadTimbang' value='Load Timbangan'>", "width" => 100), "input" => false);
		//$form[] = array('row' => 50, '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' => 100, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'alamat_kirim2', "size" => 30)));
		//$form[] = array('row' => 125, 'col' => 500, "label" => array("name" => "&nbsp;", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'alamat_kirim3', "size" => 30)));
		//$form[] = array('row' => 150, 'col' => 500, "label" => array("name" => "Pembayaran", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'jen_bayar', "size" => 10, 'after' => 'select("")', "select" => 'jen_bayar')));
		//$form[] = array('row' => 150, 'col' => 700, "label" => array("name" => "TOP", "width" => 40), "input" => array("type" => 'text',	'attr' => array("name" => 'top', "size" => 4, 'class' => 'decimal')));
		//$form[] = array('row' => 150, 'col' => 800, "label" => array("name" => "Hari", "width" => 40), "input" => false);

        $row += 300;
		$form[] = array('row' => $row, 'col' => 460, "label" => array("name" => "Total", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'gtot', "size" => 20, 'readonly' => true, 'class' => 'decimal')));
		$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;
	}
	
	public function setDetail()
	{
		$res->width 	= 700;
		$res->height	= 225;
		$res->col		= 25;
		$res->row		= 80;
		$res->data		= 'njld';
		$res->pk		= 'nonjl';
		$res->sort		= 'nonjld';
		$res->autoNo	= true;
		
		$res->attr[] = array("label" => 'No. SJ',	"attr" => 'noref', 	'width' => 130, 	'after' => 'load("sjualt", "false", "loadDisplay")', 'display' => 'loadDisplay', 'target' => 'kd_inv=>kd_inv,nm_inv=>nm_inv,qty=>qtya,humid=>humida,harga=>harga,total=>total', 'load' => 'njl,nonjl,kd_sub', 'notnull' => true, 'loadAfter' => '_hitungPoJ');
		$res->attr[] = array("label" => 'Material',		"attr" => 'kd_inv', 	'width' => 80, 'notnull' => true,	'readonly' => true);
		$res->attr[] = array("label" => 'Nama',		"attr" => 'nm_inv', 	'width' => 150, 'notnull' => true,	'readonly' => true);
		$res->attr[] = array("label" => 'Berat',		"attr" => 'qty',		'width' => 40, 'class' => 'decimal _hitungPoJ');
		$res->attr[] = array("label" => 'Refraksi (%)',		"attr" => 'humid',		'width' => 80, 'class' => 'decimal _hitungPoJ');
		$res->attr[] = array("label" => 'Harga',	"attr" => 'harga',		'width' => 50, 'class' => 'decimal _hitungPoJ');
		
		//$res->attr[] = array("label" => 'Dis.1(%)',	"attr" => 'diskon_2',	'width' => 40, 'class' => 'decimal _hitungPo');
		//$res->attr[] = array("label" => 'Dis.2(%)',	"attr" => 'diskon_3',	'width' => 40, 'class' => 'decimal _hitungPo');
		//$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" => 'Ket', 		"attr" => 'ket_det', 	'width' => 150);
		
		$res->attr[] = array("label" => 'Total',	"attr" => 'total',		'width' => 80, 'class' => 'decimal', 'dec' => 2, 'readonly' => true);
		
		return $res;
	}
	
	public function detTimbang()
	{
    	$this->layout = "null";
    	$noref = "";
    	if(!empty($_POST['timbang'])){
    		foreach($_POST['timbang'] as $k){
    			if(!empty($noref)){
    				$noref.= ' or ';
    			}
    			$noref.= "(a.nopak = '$k')";
    		}
    	}
    	
		$sql = $this->em->addSelect("a.*, a.qtya as qty, a.humida as humid, a.nopak as noref");
		$sql = $this->em->getFrom("jpak", 'a');
		$sql = $this->em->addLeftJoin("njld", 'b', array("a.nopak = b.noref"));
		$sql = $this->em->addLeftJoin("njl", 'c', array("b.nonjl = c.nonjl"));
		$sql = $this->em->getWhere("(b.noref is null or b.noref = '')");
		$sql = $this->em->andWhere("a.control = 1");
		if(!empty($_GET['kd_sub'])){
		    $sql = $this->em->andWhere("a.kd_sub = '".$_GET['kd_sub']."'");
		}
		else{
		    echo "<script>alert('Mohon isi customer dulu...')</script>";
		    return;
		}
		if(!empty($noref)){
    		$sql = $this->em->andWhere("($noref)");
    	}
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		//var_dump($sql);
		$tot = 0;
		$data = array();
		foreach($tmp as $r){
        	$r->total = ($r->qty - ($r->qty * ($r->humid / 100))) * $r->harga;
        	$tot += $r->total;
        	$data[] = $r;
		}
		
		$file = __CLASS__.'_'.date("ymdhis").rand(100, 999);
		$this->sessionDet($file, $this->set, $data);
		echo "<script>_listLoadDetail('".$file."', '')</script>";
		echo "<script>$('#apankForm').empty();</script>";
		echo "<script>$('#gtot').val(".$tot.");</script>";
	}
	
	public function loadTimbang()
    {
    	if(empty($_GET['kd_sub'])){
    		echo "<script>alert('Mohon isi customer dulu...')</script>";
    		return;
    	}
    	$kd_sub = $_GET['kd_sub'];
    
    	// same dataset as detTimbang(): unlinked jpak rows for this customer
    	$sql = $this->em->addSelect("a.*, a.qtya as qty, a.humida as humid, a.nopak as noref");
    	$sql = $this->em->getFrom("jpak", 'a');
    	$sql = $this->em->addLeftJoin("njld", 'b', array("a.nopak = b.noref"));
    	$sql = $this->em->addLeftJoin("njl", 'c', array("b.nonjl = c.nonjl"));
    	$sql = $this->em->getWhere("(b.noref is null or b.noref = '')");
    	$sql = $this->em->andWhere("a.control = 1");
    	$sql = $this->em->andWhere("a.kd_sub = '".$kd_sub."'");
    	$sql = $this->em->getMultiSort("a.nopak");
    
    	$eks = $this->em->queryEks($sql);
    	$tmp = $this->em->getFetchObject($eks);
    
    	// same columns/widths as setDetail()'s attr definitions
    	$data = "<form method='POST' id='_pilihTimbang' action=''>";
    	$data.= "<table id='_tabBonus'>";
    	$data.= "<thead>";
    	$data.= "<tr>";
    	$data.= "<th width='30' align='center'><input type='checkbox' id='cekAll'></th>";
    	$data.= "<th width='130'>No. SJ</th>";
    	$data.= "<th width='80'>Material</th>";
    	$data.= "<th width='150'>Nama</th>";
    	$data.= "<th width='40' align='right'>Berat</th>";
    	$data.= "<th width='80' align='right'>Refraksi (%)</th>";
    	$data.= "<th width='50' align='right'>Harga</th>";
    	$data.= "<th width='80' align='right'>Total</th>";
    	$data.= "</tr>";
    	$data.= "</thead>";
    	$data.= "<tbody>";
    	foreach($tmp as $r){
    		$id = str_ireplace("/", "_", $r->noref);
    		$id = str_ireplace(".", "_", $id);
    		$r->total = ($r->qty - ($r->qty * ($r->humid / 100))) * $r->harga;
    
    		$data.= "<tr>";
    		$data.= "<td><input type='checkbox' class='_cekTimbang' name='timbang[".$r->noref."]' id='cek_$id' value='".$r->noref."'></td>";
    		$data.= "<td>".$r->noref."</td>";
    		$data.= "<td>".$r->kd_inv."</td>";
    		$data.= "<td>".$r->nm_inv."</td>";
    		$data.= "<td>".$r->qty."</td>";
    		$data.= "<td>".$r->humid."</td>";
    		$data.= "<td>".$r->harga."</td>";
    		$data.= "<td>".number_format($r->total, 2)."</td>";
    		$data.= "</tr>";
    	}
    	$data.= "</tbody>";
    	$data.= "</table>";
    	$data.= "<p style='text-align: center'><input type='submit' value='Pilih'></p>";
    	$data.= "</form>";
    
    	echo $data;
    	echo "<script type='text/javascript'>
    		$(document).ready(function(){
    			$('#_tabBonus').flexigrid({
    				width: 700,
    				height: 225,
    				title: 'Daftar Timbangan Belum Diproses',
    			});
    		})
    	</script>";
    
    	$this->layout = "null";
    }
	
	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.nonjl', "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" => 'Jumlah Rp', 'attr' => 'a.gtot', "sort" => 'true', "width" => 150, 'align' => 'left', 'align' => 'right', 'type' => 'number');
		$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.nonjl', "sort" => 'true', "width" => 200, 'align' => 'left', 'filter' => true, 'default' => 'nonjl');

		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";
	}
	
	public function getStok($kd_inv)
	{
		$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			
		$sql = $this->em->addSelect("a.kd_inv, sum(a.masuk - a.keluar) as qty");
		$sql = $this->em->getFrom("acd", 'a');
		$sql = $this->em->getWhere("a.kdac = '".$per->kdac."'");
		if(!empty($_POST['nonjl']) && $_POST['nonjl'] != ''){
		    $sql = $this->em->andWhere("a.nojnl != '".$_POST['nonjl']."'");
		}
		$sql = $this->em->getGroupBy("a.kd_inv");
		$stok = $sql;
		
		$sql = $this->em->addSelect("b.qty");
		$sql = $this->em->getFrom('inv', 'a');
		$sql = $this->em->addLeftJoin("($stok)", 'b', array("a.kd_inv = b.kd_inv"));
		$sql = $this->em->getWhere("a.kd_inv = '".$kd_inv."'");
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		
		return $res[0];
	}
	
	protected function validasi()
	{
		$basic = new Basic;
		$error[] = $basic->validasiBasic($_POST, $this->set, false);

		$kd_inv = $_POST['kd_inv'];
		$inv = array();
		foreach($kd_inv as $n => $row){
			if(!empty(trim($row)) && trim($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;
				}
				//if (!empty($inv[$row])) {
                //    echo "<script>$('#_displayErrorPopUp').html('Kode Artikel $row duplikat pada baris ".($seen[$row]+1)." dan ".($n+1)."')</script>";
                //    $error[] = false;
                //} else {
                //    $inv[$row] = $n;
                //}
				$qty = str_ireplace(',','',$_POST['qty'][$n]);
				if($qty <= 0 || empty($qty) || $qty == ''){
				    echo "<script>$('#_displayErrorPopUp').html('Kode Artikel $row Belum Diisi QTY')</script>";
					$error[] = false;
				}
				//$qty2 = $this->getStok($row)->qty;
				//if($qty2 < $qty){
				//    echo "<script>$('#_displayErrorPopUp').html('QTY Kode Artikel $row Kelebihan (Max $qty2)')</script>";
				//	$error[] = false;
				//}
			}
		}
	
		$error[] = $this->cekStatus($_POST['nonjl']);
		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'] = "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';
    		$tambah['kd_gudang'] = 'GD';
    		$tambah['nm_gudang'] = 'GUDANG';
			$_POST['tgl'] 		= $date->serialDate($_POST['tgl']);
			$_POST['control'] 	= 1;
			
			$no = 1;
			$qb = array();
			$qb[] = $this->deleteDet($this->set->nojnl['nojnl']);
			foreach($_POST['noref'] 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);
			
			// echo "<pre>";
			// var_dump($sql);

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

		return $sql;
	}
	
	public function deleteDet($id)
	{
		$sql = $this->em->addDelete('njld');
		$sql = $this->em->getWhere("nonjl = '$id'");
		return $sql;
	}
	
	public function Delete()
	{
		$_GET['nonjl'] = str_ireplace("__--__", "/", $_GET['nonjl']);
		if($this->cekStatus($_GET['nonjl']) == true){
			$qb = array();
			$basic = new Basic;
			$this->data = $basic->delete2($_GET, $this->set, $qb);
		}
		$this->layout = "null";
	}
	
	public function push()
	{
		$nojnl = $_GET['filPrintanonjl'];
		$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()
	{
		$basic = new Basic;
		$this->data->data 	= $this->em->getFind("njl", "*", array("nonjl = '".$_GET['filPrintanonjl']."'"));
		$this->data->det 	= $this->em->getFindBy("njld", "*", array("nonjl = '".$_GET['filPrintanonjl']."'"), "nonjld 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 = "invj";
		$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";
	}
	public function detJo()
	{
		if(!empty($_GET['nonjl'])){
			$res = $this->em->getFindBy("njld", "*", array("nonjl = '".$_GET['nonjl']."'"), "nonjld");
		}else{
			$res = $this->em->getFindBy("jpakd", "*", array("nopak = '".$_GET['nooj']."'"), "nopakd");
		}
		
		$this->sessionDet(__CLASS__, $this->set, $res);
		echo "<script>$('#ket_file').val('".$_GET['file']."')</script>";
		echo "<script>_listLoadDetail('".__CLASS__."', '_hitungPoJ')</script>";
		echo "<script>$('#apankForm').empty()</script>";
		$this->layout = "null";
	}
}