<?php

CLASS Posting extends RasioProject
{
	protected $set;
	protected $em;

	public function deleteAcd($set, $nojnl)
	{
		$sql = $set->em->addDelete('acd');
		$sql = $set->em->getWhere("nojnl = '$nojnl'");
		return $sql;
	}
	
	public function deleteAcdG($set, $nojnl)
	{
		$nojnl = trim($nojnl);
		$sql = $set->em->addDelete('acd');
		if(!empty($nojnl)){
			$sql = $set->em->getWhere("nojnl like '$nojnl%'");
		}else{
			$sql = $set->em->getWhere("nojnl = 'apankOke'");
		}
		return $sql;
	}
	
	protected function nullData()
	{
		$data = array();
		$data['id_record'] 	= date('Ymd/His').'.'.rand(1000, 9999);
		$data['modul'] 		= 'TMP';
		$data['no_induk'] 	= 'APANK';
		$data['waktu'] 		= date('Y-m-d H:i:s');

		$sql = $this->em->addInsert('record');
		$sql = $this->em->addAttrIns($data);
		$qb[] = $sql;
		return $sql;
	}
	
	public function nblData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*, a.ket");
		$sql = $this->em->getFrom("nbl", 'a');
		$sql = $this->em->addLeftJoin("nbld", 'b', array("a.nonbl = b.nonbl"));
		$sql = $this->em->getWhere("a.nonbl = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function nbeli($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->nblData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$hut = $this->em->getFind("acc_set", "*", array("nm_set = 'NBL-HUTANG'"));
			
			//head
			$attr = array();
			$attr['alias'] 		= 'NBL';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['kd_sub'] 	= $data[0]->kd_sub;
			$attr['nm_sub'] 	= $data[0]->nm_sub;
			$attr['kdac'] 	= $hut->kdac;
			$attr['nmac'] 	= $hut->nmac;
			$attr['masuk'] 	= 0;
			$attr['keluar'] = 0;
			$attr['debet'] 	= 0;
			$attr['credit'] = $data[0]->gtot;
			$attr['ket'] 	= $data[0]->ket;
			$attr['nota'] 		= $nojnl;
			$attr['nota_jt'] 	= $data[0]->tgl;
			$attr['nota_tgl'] 	= $data[0]->tgl;
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
			foreach($data as $r){
				$attr = array();
    			$attr['alias'] 		= 'NBL';
    			$attr['nojnl'] 		= $nojnl;
    			$attr['tgl'] 		= $data[0]->tgl;
    			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
    			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
    			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
    			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
    			$attr['kd_sub'] 	= $data[0]->kd_sub;
    			$attr['nm_sub'] 	= $data[0]->nm_sub;
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['no_ref'] = $r->noref;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $r->qty;
				$attr['keluar'] = 0;
				$attr['humid'] 	= $r->humid;
				$attr['debet'] 	= $r->total;
				$attr['credit'] = 0;
				$attr['ket'] 	= $r->nm_inv.' '.$r->qty.' Kg, refraksi '.$r->humid.'%';
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}
	
	public function njlData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*, a.ket");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("njld", 'b', array("a.nonjl = b.nonjl"));
		$sql = $this->em->getWhere("a.nonjl = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function njual($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->njlData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$pit = $this->em->getFind("acc_set", "*", array("nm_set = 'NJL-PIUTANG'"));
			
			//head
			$attr = array();
			$attr['alias'] 		= 'NJL';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['kd_sub'] 	= $data[0]->kd_sub;
			$attr['nm_sub'] 	= $data[0]->nm_sub;
			$attr['kdac'] 	= $pit->kdac;
			$attr['nmac'] 	= $pit->nmac;
			$attr['masuk'] 	= 0;
			$attr['keluar'] = 0;
			$attr['debet'] 	= $data[0]->gtot;;
			$attr['credit'] = 0;
			$attr['ket'] 	= $data[0]->ket;
			$attr['nota'] 		= $nojnl;
			$attr['nota_jt'] 	= $data[0]->tgl;
			$attr['nota_tgl'] 	= $data[0]->tgl;
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
			foreach($data as $r){
				$attr = array();
    			$attr['alias'] 		= 'NJL';
    			$attr['nojnl'] 		= $nojnl;
    			$attr['tgl'] 		= $data[0]->tgl;
    			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
    			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
    			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
    			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
    			$attr['kd_sub'] 	= $data[0]->kd_sub;
    			$attr['nm_sub'] 	= $data[0]->nm_sub;
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['no_ref'] = $r->noref;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = $r->qty;
				$attr['humid'] 	= $r->humid;
				$attr['debet'] 	= 0;
				$attr['credit'] = $r->total;
				$attr['ket'] 	= $r->nm_inv.' '.$r->qty.' Kg, refraksi '.$r->humid.'%';
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}
	
	//sampai sini
	
	protected function umdData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*, c.kd_cabang, c.nm_cabang");
		$sql = $this->em->getFrom("umh", 'a');
		$sql = $this->em->addLeftJoin("umd", 'b', array("a.nojnl = b.nojnl"));
		$sql = $this->em->addLeftJoin("cabang", 'c', array("b.pc = c.kd_cabang"));
		$sql = $this->em->getWhere("a.nojnl = '$nojnl'");
		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function jurnal($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->umdData($nojnl);
		$qb = array();
		$qb[] = $this->deleteAcdG($set, $nojnl);
		$pc = array();
		foreach($data as $row){
			if(empty($row->ket)){
				$row->ket = $row->ket_jurnal;
			}
			
			if($row->debet < 0){
				$row->credit 	= str_ireplace("-", "", $row->debet);
				$row->debet		= 0;
			}
			if($row->credit < 0){
				$row->debet 	= str_ireplace("-", "", $row->credit);
				$row->credit	= 0;
			}
		
			if(!empty($row->satuan)){
				$sat = $this->em->getFind("satuan", "skala", array("kd_inv = '".$row->kd_inv."'", "nmsat = '".$row->satuan."'"));
				$satDef = $this->em->getFind("satuan", "nmsat", array("kd_inv = '".$row->kd_inv."'", "skala = '1'"));
				$row->masuk = $row->masuk * $sat->skala;
				$row->satuan = $satDef->nmsat;
			}
		
			if(!empty($row->kdgud)){
				$row->kd_gudang = $row->kdgud;
			}
		
			if(empty($row->kd_cabang)){
				$row->kd_cabang = 'HO';
			}
		
			$attr = array();
			$attr['nojnl'] 		= $nojnl;
			$attr['alias'] 		= $row->alias;
			$attr['tgl'] 		= $row->tgl;
			$attr['kd_cabang'] 	= $row->kd_cabang;
			$attr['nm_cabang'] 	= $row->nm_cabang;
			$attr['kd_gudang'] 	= $row->kd_gudang;
			$attr['kd_sub'] 	= $row->kd_sub;
			$attr['nm_sub'] 	= $row->nm_sub;
			$attr['kdac'] 		= $row->kdac;
			$attr['nmac'] 		= $row->nmac;
			$attr['debet'] 		= $row->debet;
			$attr['credit'] 	= $row->credit;
			$attr['kd_inv'] 	= $row->kd_inv;
			$attr['nm_inv'] 	= $row->nm_inv;
			$attr['keluar'] 	= $row->keluar;
			$attr['masuk'] 		= $row->masuk;
			$attr['satuan'] 	= $row->satuan;
			$attr['bg_no'] 		= $row->bg_no;
			$attr['bg_tgl'] 	= $row->bg_tgl;
			$attr['bg_bank'] 	= $row->bg_bank;
			$attr['nota'] 		= trim($row->nota);
			$attr['ket'] 		= $row->ket;
			$attr['fix'] 		= $row->fix;
			$attr['kd_counter'] = $row->kd_counter;
			$attr['nm_counter'] = $row->nm_counter;
			
			// if(empty($attr['nota']) and !empty($row->kdacf)){
				// $attr['nota'] 	= trim($row->kdacf);
				// $attr['nota_tgl'] 	= $attr['tgl'];
				// $attr['nota_jt'] 	= $attr['tgl'];
			// }
			// if(empty($attr['nota']) and !empty($attr['kd_sub']) and empty($row->pk)){
				// $nonota = explode(".", $nojnl);
				// $nonota = $nonota[0];
				// $attr['nota'] 	= trim($nonota);
				// $attr['nota_tgl'] 	= $attr['tgl'];
				// $attr['nota_jt'] 	= $attr['tgl'];
			// }
			
			if($attr['kdac'] == '1.0.04.10' and empty($attr['nota'])){
				$nota = explode(".", $nojnl);
				$nota = $nota[0];
				if(!empty($nota)){
					$attr['nota'] 		= $nota;
					$attr['nota_tgl'] 	= $attr['tgl'];
					$attr['nota_jt'] 	= $attr['tgl'];
				}
			}
			if($attr['kdac'] == '0.0.04.30' and empty($attr['nota'])){
				$nota = explode(".", $nojnl);
				$nota = $nota[0];
				if(!empty($nota)){
					$attr['nota'] 		= $nota;
					$attr['nota_tgl'] 	= $attr['tgl'];
					$attr['nota_jt'] 	= $attr['tgl'];
				}
			}

			if($attr['kdac'] == '0.0.03.30'){
				$attr['nota'] 		= $attr['kd_sub'].'-'.$attr['bg_no'];
				$attr['nota_tgl'] 	= $attr['tgl'];
				$attr['nota_jt'] 	= $attr['bg_tgl'];
			}
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
			$saldo = ($row->debet - $row->credit);
			$saldo = number_format($saldo, 3, '.', '');
			$pc[$row->kd_cabang] = $pc[$row->kd_cabang] + $saldo;
			$pc[$row->kd_cabang] = number_format($pc[$row->kd_cabang], 3, '.', '');
		}
		
		$rac = $this->em->getFind("acc_set", "*", array("nm_set = 'RAC'"));
		
		foreach($pc as $k => $r){
			if($r <> 0){
				if($r > 0){
					$row->credit 	= str_ireplace("-", "", $r);
					$row->debet		= 0;
				}else{
					$row->debet 	= str_ireplace("-", "", $r);
					$row->credit	= 0;
				}
				
				$profit = $this->em->getFind("cabang", "kd_cabang, nm_cabang", array("kd_cabang = '$k'"));
				
				$attr = array();
				$attr['nojnl'] 		= $nojnl;
				$attr['alias'] 		= $row->alias;
				$attr['tgl'] 		= $row->tgl;
				$attr['kd_cabang'] 	= $profit->kd_cabang;
				$attr['nm_cabang'] 	= $profit->nm_cabang;
				$attr['kdac'] 		= $rac->kdac;
				$attr['nmac'] 		= $rac->nmac;
				$attr['debet'] 		= $row->debet;
				$attr['credit'] 	= $row->credit;
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
		}

		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}
	
	public function hpp($set, $inv, $tgl, $nojnl, $kdac = false, $po = false)
	{
		$hrg = 0;
		if(!empty($po) and $po != false){
			$po = $this->em->getFind("beli_det", "harga", array("no_po = '$po'", "kd_inv = '$inv'"));
			$hrg = $po->harga;
		}
		if(empty($kdac) or $kdac == false){
			$acc = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$kdac = $acc->kdac;
		}
		
		if($hrg <= 0){
			$acd = $this->em->getFind("acd", "sum(debet-credit) as hrg, sum(masuk-keluar) as qty", array("kd_inv = '$inv'", "kdac = '$kdac'", "tgl <= '".date("Y-m-d 23:59:59", strtotime($tgl))."'", "nojnl != '$nojnl'"));
			
			if($acd->hrg > 0){
				if($acd->qty <> 0){
					$acdHpp = $acd->hrg / $acd->qty;
				}else{
					$acdHpp = 0;
				}
				if($acdHpp > 0){
					$hrg = $acdHpp;
				}
			}
		}
		
		if($hrg <= 0){
			$inv = $this->em->getFind("inv", "harga_beli", array("kd_inv = '$inv'"));
			$hrg = (int)$inv->harga_beli;
		}
		
		return $hrg;
	}
	
	public function lpbData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*");
		$sql = $this->em->getFrom("lpb", 'a');
		$sql = $this->em->addLeftJoin("lpbd", 'b', array("a.nolpb = b.nolpb"));
		$sql = $this->em->getWhere("a.nolpb = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function lpb($set, $nojnl, $noEks = false)
	{
		$this->em = $set->em;
		$data = $this->lpbData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$hut = $this->em->getFind("acc_set", "*", array("nm_set = 'LPB-HUTANG'"));
			
			$attr = array();
			$attr['alias'] 		= 'LPB';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['kd_sub'] 	= $data[0]->kd_sub;
			$attr['nm_sub'] 	= $data[0]->nm_sub;
			$attr['no_ref'] 	= $data[0]->noref;
				
			$tot = 0;
			foreach($data as $r){
				if((int)$r->skala == 0){
					$r->skala = 1;
				}
				$hpp = $this->hpp($set, $r->kd_inv, $r->tgl, $nojnl, $per->kdac, $r->noref);
				$qty = $r->qty * $r->skala;
				$hrg = $hpp * $r->qty;
				$tot = $tot + $hrg;
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $qty;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $hrg;
				$attr['credit'] = 0;
				$attr['ket'] 	= $r->nm_inv." : ".$r->qty.' '.$r->satuan;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			$attr['kdac'] 	= $hut->kdac;
			$attr['nmac'] 	= $hut->nmac;
			$attr['masuk'] 	= 0;
			$attr['keluar'] = 0;
			$attr['debet'] 	= 0;
			$attr['credit'] = $tot;
			$attr['ket'] 	= $r->nm_sub;
			$attr['nota'] 		= $nojnl;
			$attr['nota_jt'] 	= $data[0]->tgl;
			$attr['nota_tgl'] 	= $data[0]->tgl;
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		if($noEks == true){
			return $qb;
		}else{
			return $this->em->getRoolbackFlush($qb);
		}
	}
	
	public function jurnalBalik($qb, $r, $nojnl, $alias, $tgl)
	{
		$acd = $this->em->getFindBy("acd", "*, debet as credit, credit as debet, masuk as keluar, keluar as masuk", array("nojnl = '".$r->noref."'"), "_no");
		foreach($acd as $row){
			$attr = array();
			foreach($row as $k2 => $r2){
				$attr[$k2] = $r2;
			}
			$attr['nojnl'] 	= $nojnl;
			$attr['alias'] 	= $alias;
			$attr['tgl'] 	= $tgl;
			unset($attr['_no']);
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
		}
		return $qb;
	}
	
	public function nbl($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->nblData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			if(!empty($data[0]->noref)){
				$lpb = $this->em->getFind("lpb", "*", array("nolpb = '".$data[0]->noref."'"));
				if(!empty($lpb->nolpb)){
					$noG = explode(".", $lpb->nolpb);
					$noG = $noG[0];
					$qb[] = $this->deleteAcdG($set, $noG);
					// $qb = $this->jurnalBalik($qb, $data[0], $nojnl, 'NBL', $data[0]->tgl);
					
					$data[0]->tgl = $lpb->tgl;
					
				}
			}
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$hut = $this->em->getFind("acc_set", "*", array("nm_set = 'NBL-HUTANG'"));
			$ppn = $this->em->getFind("acc_set", "*", array("nm_set = 'PPN-MASUKAN'"));
			$sel = $this->em->getFind("acc_set", "*", array("nm_set = 'SELISIH'"));
			
			$attr = array();
			$attr['alias'] 		= 'NBL';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['kd_sub'] 	= $data[0]->kd_sub;
			$attr['nm_sub'] 	= $data[0]->nm_sub;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$tot = 0;
			$kode = new Kode;
			
			$d = 0;
			$k = 0;
			
			foreach($data as $r){
				if((int)$r->skala == 0){
					$r->skala = 1;
				}
				$nil = $kode->totBertingkatPosting($r);
				$qty = $r->qty * $r->skala;
				$hrg = $nil['dpp'];
				$tot = $tot + $hrg;
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $qty;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $hrg;
				$attr['credit'] = 0;
				$attr['ket'] 	= $r->nm_inv.' '.$r->qty.' '.$r->satuan;
				
				$d = $d + $attr['debet'];
				$k = $k + $attr['credit'];
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			if($data[0]->ppn_nilai > 0){
				$attr['kdac'] 	= $ppn->kdac;
				$attr['nmac'] 	= $ppn->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $r->ppn_nilai;
				$attr['credit'] = 0;
				$attr['ket'] 	= $r->nm_sub;
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$d = $d + $attr['debet'];
				$k = $k + $attr['credit'];
			}
			
			$attr['kdac'] 	= $hut->kdac;
			$attr['nmac'] 	= $hut->nmac;
			$attr['masuk'] 	= 0;
			$attr['keluar'] = 0;
			$attr['debet'] 	= 0;
			$attr['credit'] = $r->gtot;
			$attr['ket'] 	= $r->ket;
			$attr['nota'] 		= $nojnl;
			$attr['nota_jt'] 	= $data[0]->tgl;
			$attr['nota_tgl'] 	= $data[0]->tgl;
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
			$d = $d + $attr['debet'];
			$k = $k + $attr['credit'];
			
			$nSel = $d - $k;
			if($nSel <> 0){
				$attr['kdac'] 	= $sel->kdac;
				$attr['nmac'] 	= $sel->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = 0;
				$attr['ket'] 	= "SELISIH PEMBELIAN";

				unset($attr['nota']);
				unset($attr['nota_tgl']);
				unset($attr['nota_jt']);
				
				$valSel = str_ireplace("-", "", $nSel);
				
				if($nSel < 0){
					$attr['debet'] 	= $valSel;
					$attr['credit'] = 0;
				}else{
					$attr['debet'] 	= 0;
					$attr['credit'] = $valSel;
				}
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}

	public function retData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*, a.ket, a.kdac, a.nmac");
		$sql = $this->em->getFrom("ret", 'a');
		$sql = $this->em->addLeftJoin("retd", 'b', array("a.noret = b.noret"));
		$sql = $this->em->getWhere("a.noret = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function ret($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->retData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$ppn = $this->em->getFind("acc_set", "*", array("nm_set = 'PPN-MASUKAN'"));
			$sel = $this->em->getFind("acc_set", "*", array("nm_set = 'SELISIH'"));
			if(!empty($data[0]->kdac)){
				$hut = $this->em->getFind("acc", "*", array("kdac = '".$data[0]->kdac."'"));
			}else{
				$hut = $this->em->getFind("acc_set", "*", array("nm_set = 'RET-HUTANG'"));
			}
			
			$attr = array();
			$attr['alias'] 		= 'REB';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['kd_sub'] 	= $data[0]->kd_sub;
			$attr['nm_sub'] 	= $data[0]->nm_sub;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$tot = 0;
			$kode = new Kode;
			
			foreach($data as $r){
				if((int)$r->skala == 0){
					$r->skala = 1;
				}
				$nil = $kode->totBertingkatPosting($r);
				$qty = $r->qty * $r->skala;
				$hrg = $nil['dpp'];
				$tot = $tot + $hrg;
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = $qty;
				$attr['debet'] 	= 0;
				$attr['credit'] = $hrg;
				$attr['ket'] 	= $r->nm_inv." : ".$r->qty.' '.$r->satuan;
				
				$d = $d + $attr['debet'];
				$k = $k + $attr['credit'];
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			if($data[0]->ppn_nilai > 0){
				$attr['kdac'] 	= $ppn->kdac;
				$attr['nmac'] 	= $ppn->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = 0;
				$attr['debet'] 	= 0;
				$attr['credit'] = $r->ppn_nilai;
				$attr['ket'] 	= $r->nm_sub;
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$tot = $tot + $r->ppn_nilai;
			}
			
			$nSel = $tot - $r->gtot;
			if($nSel <> 0){
				$deb = 0;
				$cre = 0;

				if($nSel > 0){
					$deb = str_ireplace("-", "", $nSel);
				}else{
					$cre = str_ireplace("-", "", $nSel);
				}
				
				$attr['kdac'] 	= $sel->kdac;
				$attr['nmac'] 	= $sel->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $deb;
				$attr['credit'] = $cre;
				$attr['ket'] 	= $r->ket;
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			
			}
			
			$attr['kdac'] 	= $hut->kdac;
			$attr['nmac'] 	= $hut->nmac;
			$attr['masuk'] 	= 0;
			$attr['keluar'] = 0;
			$attr['debet'] 	= $r->gtot;
			$attr['credit'] = 0;
			$attr['ket'] 	= $r->ket;
			$attr['nota'] 		= $nojnl;
			$attr['nota_jt'] 	= $data[0]->tgl;
			$attr['nota_tgl'] 	= $data[0]->tgl;
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}

	public function mukData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*");
		$sql = $this->em->getFrom("mut", 'a');
		$sql = $this->em->addLeftJoin("mutd", 'b', array("a.nomut = b.nomut"));
		$sql = $this->em->getWhere("a.nomut = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");
		$sql = $this->em->andWhere("a.jenis = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function muk($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->mukData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$gin = $this->em->getFind("gudang", "*", array("kd_gudang = 'G.IN'"));
			
			$attr = array();
			$attr['alias'] 		= 'MK';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$kode = new Kode;
			foreach($data as $r){
				
				if((int)$r->skala == 0){
					$r->skala = 1;
				}
				
				$hpp = $this->hpp($set, $r->kd_inv, $r->tgl, $nojnl, $per->kdac);
				$hpp = round($hpp);
				$qty = $r->qty * $r->skala;
				$hrg = $hpp * $r->qty;
				
				$attr['kd_gudang'] 	= $data[0]->kd_sub;
				$attr['nm_gudang'] 	= $data[0]->nm_sub;
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = $qty;
				$attr['debet'] 	= 0;
				$attr['credit'] = $hrg;
				$attr['ket'] 	= $r->nm_inv." : ".$r->qty.' '.$r->satuan;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$attr['kd_gudang'] 	= $gin->kd_gudang;
				$attr['nm_gudang'] 	= $gin->nm_gudang;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $qty;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $hrg;
				$attr['credit'] = 0;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}

	public function muaData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*");
		$sql = $this->em->getFrom("mut", 'a');
		$sql = $this->em->addLeftJoin("mutd", 'b', array("a.nomut = b.nomut"));
		$sql = $this->em->getWhere("a.nomut = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");
		$sql = $this->em->andWhere("a.jenis = '2'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function mua($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->muaData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$gin = $this->em->getFind("gudang", "*", array("kd_gudang = 'G.IN'"));
			
			$attr = array();
			$attr['alias'] 		= 'MM';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$kode = new Kode;
			foreach($data as $r){
				if((int)$r->skala == 0){
					$r->skala = 1;
				}
				$muk = $this->em->getFind("acd", "credit, keluar", array("nojnl = '".$r->noref."'", "kd_inv = '".$r->kd_inv."'", "kdac = '".$per->kdac."'"));
				$hpp = 0;
				if($muk->credit > 0){
					$hpp = $muk->credit / $muk->keluar;
				}
				$hpp = round($hpp);
				$qty = $r->qty * $r->skala;
				$hrg = $hpp * $r->qty;
				
				$attr['kd_gudang'] 	= $gin->kd_gudang;
				$attr['nm_gudang'] 	= $gin->nm_gudang;
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = $qty;
				$attr['debet'] 	= 0;
				$attr['credit'] = $hrg;
				$attr['ket'] 	= $r->nm_inv." : ".$r->qty.' '.$r->satuan;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$attr['kd_gudang'] 	= $data[0]->kd_gudang;
				$attr['nm_gudang'] 	= $data[0]->nm_gudang;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $qty;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $hrg;
				$attr['credit'] = 0;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}

	public function soData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*");
		$sql = $this->em->getFrom("so", 'a');
		$sql = $this->em->addLeftJoin("sod", 'b', array("a.noso = b.noso"));
		$sql = $this->em->getWhere("a.noso = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function stok($inv, $gudang, $tgl, $nojnl = false)
	{
		$kd_inv = '';
		foreach($inv as $r){
			if(!empty($kd_inv)){
				$kd_inv.= " or ";
			}
			$kd_inv.= "(a.kd_inv = '".$r->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 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', strtotime($tgl))."'");
		$sql = $this->em->andWhere("a.kd_gudang = '$gudang'");
		$sql = $this->em->andWhere("($kd_inv)");
		if($nojnl != false){
			$sql = $this->em->andWhere("a.nojnl != '$nojnl'");
		}
		$sql = $this->em->getGroupBy("a.kd_inv");
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		
		$data = array();
		foreach($res as $r){
			$data[$r->kd_inv] = $r->stok;
		}
		
		return $data;
	}
	
	public function so($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->soData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$acs = $this->em->getFind("acc_set", "*", array("nm_set = 'SELISIH SO'"));
			
			$stok = $this->stok($data, $data[0]->kd_gudang, $data[0]->tgl, $nojnl);
			
			$attr = array();
			$attr['alias'] 		= 'SO';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$kode = new Kode;
			foreach($data as $r){
				
				if(empty($r->ket_det)){
					$r->ket_det = $r->ket;
				}
				
				$stk = $stok[$r->kd_inv];
				$sel = $stk - $r->qty;
				$hpp = $this->hpp($set, $r->kd_inv, $r->tgl, $nojnl, $per->kdac);
				
				$masuk  = 0;
				$keluar = 0;
				
				if($sel > 0){
					$keluar = str_ireplace("-", "", $sel);
				}else{
					$masuk  = str_ireplace("-", "", $sel);
				}
				
				$debet  = $masuk * $hpp;
				$credit = $keluar * $hpp;
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $masuk;
				$attr['keluar'] = $keluar;
				$attr['debet'] 	= $debet;
				$attr['credit'] = $credit;
				$attr['ket'] 	= $r->ket_det;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$attr['kdac'] 	= $acs->kdac;
				$attr['nmac'] 	= $acs->nmac;
				$attr['masuk'] 	= $keluar;
				$attr['keluar'] = $masuk;
				$attr['debet'] 	= $credit;
				$attr['credit'] = $debet;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}

	public function adjData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*");
		$sql = $this->em->getFrom("adj", 'a');
		$sql = $this->em->addLeftJoin("adjd", 'b', array("a.noadj = b.noadj"));
		$sql = $this->em->getWhere("a.noadj = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function adj($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->adjData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$acs = $this->em->getFind("acc_set", "*", array("nm_set = 'SELISIH SO'"));
			
			$attr = array();
			$attr['alias'] 		= 'ADJ';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$kode = new Kode;
			foreach($data as $r){
				
				if(empty($r->ket_det)){
					$r->ket_det = $r->ket;
				}
				
				$hpp = $this->hpp($set, $r->kd_inv, $r->tgl, $nojnl, $per->kdac);
				
				$masuk  = 0;
				$keluar = 0;
				
				if($r->qty > 0){
					$masuk = str_ireplace("-", "", $r->qty);
				}else{
					$keluar  = str_ireplace("-", "", $r->qty);
				}
				
				$debet  = $masuk * $hpp;
				$credit = $keluar * $hpp;
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $masuk;
				$attr['keluar'] = $keluar;
				$attr['debet'] 	= $debet;
				$attr['credit'] = $credit;
				$attr['ket'] 	= $r->ket_det;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$attr['kdac'] 	= $acs->kdac;
				$attr['nmac'] 	= $acs->nmac;
				$attr['masuk'] 	= $keluar;
				$attr['keluar'] = $masuk;
				$attr['debet'] 	= $credit;
				$attr['credit'] = $debet;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}

	public function njlDatax($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*");
		$sql = $this->em->getFrom("njl", 'a');
		$sql = $this->em->addLeftJoin("njld", 'b', array("a.nonjl = b.nonjl"));
		$sql = $this->em->getWhere("a.nonjl = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function njl($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->njlData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$inv = $data[0];
			$tot = $inv->stot - $inv->diskg_nilai;
			$ppn = 0;
			if($inv->ppn == 'I'){
				$ppn = round($tot / 11);
				$dpp = $tot - $ppn;
			}
			if($inv->ppn == 'E'){
				$dpp = $tot;
				$ppn = round(($tot * 10) / 100);
				$tot = $dpp + $ppn;
			}
			
			$attr = array();
			$attr['ppn_nilai'] 	= $ppn;
			$attr['dpp'] 		= $dpp;
			$attr['gtot'] 		= $tot;
			$sql = $this->em->addUpdate("njl");
			$sql = $this->em->addAttrUp($attr);
			$sql = $this->em->getWhere("nonjl = '".$nojnl."'");
			$qb[] = $sql;
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$pit = $this->em->getFind("acc_set", "*", array("nm_set = 'NJL-PIUTANG'"));
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$njl = $this->em->getFind("acc_set", "*", array("nm_set = 'NJL-PENJUALAN'"));
			$dis = $this->em->getFind("acc_set", "*", array("nm_set = 'NJL-DISKON'"));
			$ppn = $this->em->getFind("acc_set", "*", array("nm_set = 'PPN-KELUARAN'"));
			$hpp = $this->em->getFind("acc_set", "*", array("nm_set = 'NJL-HPP'"));
			
			$attr = array();
			$attr['alias'] 		= 'NJL';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_sub'] 	= $data[0]->kd_sub;
			$attr['nm_sub'] 	= $data[0]->nm_sub;
			$attr['kd_sales'] 	= $data[0]->kd_sales;
			$attr['nm_sales'] 	= $data[0]->nm_sales;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$kode = new Kode;
			$harga = 0;
			foreach($data as $r){
				
				$r->ket_det = $r->nm_sub;
				if((int)$r->skala == 0){
					$r->skala = 1;
				}
				$qty = $r->qty * $r->skala;
				$nhpp = $this->hpp($set, $r->kd_inv, $r->tgl, $nojnl, $per->kdac);
				$nhpp = $nhpp * $qty;
				
				$harga = $harga + ($r->qty * $r->harga);
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = $qty;
				$attr['debet'] 	= 0;
				$attr['credit'] = $nhpp;
				$attr['ket'] 	= $nojnl.' '.$r->kd_sales.' '.$r->nm_sales;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $hpp->kdac;
				$attr['nmac'] 	= $hpp->nmac;
				$attr['masuk'] 	= $qty;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $nhpp;
				$attr['credit'] = 0;
				$attr['ket'] 	= $nojnl.' '.$r->kd_sales.' '.$r->nm_sales;
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
			
			unset($attr['kd_inv']);
			unset($attr['nm_inv']);
			unset($attr['keluar']);
			unset($attr['masuk']);
				
			$attr['kdac'] 	= $njl->kdac;
			$attr['nmac'] 	= $njl->nmac;
			$attr['debet'] 	= 0;
			$attr['credit'] = $harga;
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
			$ndis = $harga - $r->gtot;
			
			if($ndis > 0){
				$attr['kdac'] 	= $dis->kdac;
				$attr['nmac'] 	= $dis->nmac;
				$attr['debet'] 	= $ndis;
				$attr['credit'] = 0;

				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			// if($r->jen_bayar == 'TUNAI'){
				// $pit = $this->em->getFind("acc_set", "*", array("nm_set = 'PIUTANG-TUNAI'"));
			// }
			
			// var_dump($r->gtot);
			
			$attr['kdac'] 	= $pit->kdac;
			$attr['nmac'] 	= $pit->nmac;
			$attr['debet'] 	= $r->gtot;
			$attr['credit'] = 0;
			$attr['nota'] 		= $nojnl;
			$attr['nota_jt'] 	= $data[0]->tgl;
			$attr['nota_tgl'] 	= $data[0]->tgl;

			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}

	public function rejData($nojnl)
	{
		$sql = $this->em->addSelect("a.*, b.*, a.kdac, a.nmac");
		$sql = $this->em->getFrom("ret", 'a');
		$sql = $this->em->addLeftJoin("retd", 'b', array("a.noret = b.noret"));
		$sql = $this->em->getWhere("a.noret = '$nojnl'");
		$sql = $this->em->andWhere("a.control = '1'");

		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function rej($set, $nojnl)
	{
		$this->em = $set->em;
		$data = $this->rejData($nojnl);
		$qb = array();
		
		if(count($data) > 0){
			
			$noG = explode(".", $nojnl);
			$noG = $noG[0];
			$qb[] = $this->deleteAcdG($set, $noG);
			
			$per = $this->em->getFind("acc_set", "*", array("nm_set = 'PERSEDIAAN'"));
			$hpp = $this->em->getFind("acc_set", "*", array("nm_set = 'NJL-HPP'"));
			$ret = $this->em->getFind("acc_set", "*", array("nm_set = 'RET-JUAL'"));
			$ppn = $this->em->getFind("acc_set", "*", array("nm_set = 'PPN-MASUKAN'"));
			$sel = $this->em->getFind("acc_set", "*", array("nm_set = 'SELISIH'"));
			
			if(!empty($data[0]->kdac)){
				$hut = $this->em->getFind("acc", "*", array("kdac = '".$data[0]->kdac."'"));
			}else{
				$hut = $this->em->getFind("acc_set", "*", array("nm_set = 'NJL-PIUTANG'"));
			}
			
			
			$attr = array();
			$attr['alias'] 		= 'REJ';
			$attr['nojnl'] 		= $nojnl;
			$attr['tgl'] 		= $data[0]->tgl;
			$attr['kd_cabang'] 	= $data[0]->kd_cabang;
			$attr['nm_cabang'] 	= $data[0]->nm_cabang;
			$attr['kd_gudang'] 	= $data[0]->kd_gudang;
			$attr['nm_gudang'] 	= $data[0]->nm_gudang;
			$attr['kd_sub'] 	= $data[0]->kd_sub;
			$attr['nm_sub'] 	= $data[0]->nm_sub;
			$attr['kd_sales'] 	= $data[0]->kd_sales;
			$attr['nm_sales'] 	= $data[0]->nm_sales;
			$attr['no_ref'] 	= $data[0]->noref;
			
			$tot = 0;
			$kode = new Kode;
			
			foreach($data as $r){
				if((int)$r->skala == 0){
					$r->skala = 1;
				}
				$qty = $r->qty * $r->skala;
				$nhpp = $this->hpp($set, $r->kd_inv, $r->tgl, $nojnl, $per->kdac);
				$nhpp = $nhpp * $qty;
				
				$tot = $tot + ($r->total);
				
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $per->kdac;
				$attr['nmac'] 	= $per->nmac;
				$attr['masuk'] 	= $qty;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $nhpp;
				$attr['credit'] = 0;
				$attr['ket'] 	= $r->nm_inv." : ".$r->qty.' '.$r->satuan;
				
				$d = $d + $attr['debet'];
				$k = $k + $attr['credit'];
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$attr['kd_inv'] = $r->kd_inv;
				$attr['nm_inv'] = $r->nm_inv;
				$attr['kdac'] 	= $hpp->kdac;
				$attr['nmac'] 	= $hpp->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = $qty;
				$attr['debet'] 	= 0;
				$attr['credit'] = $nhpp;
				$attr['ket'] 	= $r->nm_inv." : ".$r->qty.' '.$r->satuan;
				
				$d = $d + $attr['debet'];
				$k = $k + $attr['credit'];
				
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
			
			if(empty($r->gtot)){
				
				
				if($r->ppn == 'E'){
					$ppn = (int)($tot / 10);
					$dpp = $tot;
					$tot = $dpp + $ppn;
					
					$r->dpp = $dpp;
					$r->gtot = $tot;
					$r->ppn_nilai = $ppn;
				}else
				if($r->ppn == 'I'){
					
					$dpp = $tot / 1.1;
					$ppn = $tot - $dpp;
					
					$r->dpp = $dpp;
					$r->gtot = $tot;
					$r->ppn_nilai = $ppn;
				}else{
					$r->dpp = $tot;
					$r->gtot = $tot;
					$r->ppn_nilai = 0;
				}
			}
			
			$attr['kdac'] 	= $ret->kdac;
			$attr['nmac'] 	= $ret->nmac;
			$attr['masuk'] 	= 0;
			$attr['keluar'] = 0;
			$attr['debet'] 	= $r->dpp;
			$attr['credit'] = 0;
			$attr['ket'] 	= $r->nm_sub;
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
				
			if($r->ppn_nilai > 0){
				$attr['kdac'] 	= $ppn->kdac;
				$attr['nmac'] 	= $ppn->nmac;
				$attr['masuk'] 	= 0;
				$attr['keluar'] = 0;
				$attr['debet'] 	= $r->ppn_nilai;
				$attr['credit'] = 0;
				$attr['ket'] 	= $r->nm_sub;
				$sql = $this->em->addInsert('acd');
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
				$tot = $tot - $r->ppn_nilai;
			}
			
			$attr['kdac'] 	= $hut->kdac;
			$attr['nmac'] 	= $hut->nmac;
			$attr['masuk'] 	= 0;
			$attr['keluar'] = 0;
			$attr['debet'] 	= 0;
			$attr['credit'] = $r->gtot;
			$attr['ket'] 	= $r->nm_sub;
			$attr['nota'] 		= $nojnl;
			$attr['nota_jt'] 	= $data[0]->tgl;
			$attr['nota_tgl'] 	= $data[0]->tgl;
			
			$sql = $this->em->addInsert('acd');
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			
		}else{
			$qb[] = $this->nullData($nojnl);
		}
		
		// echo "<pre>";
		// var_dump($qb);
		
		return $this->em->getRoolbackFlush($qb);
	}
}