<?php

CLASS Up extends RasioProject
{
	protected $set;
	protected $enableCk = true;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
		$this->layout = "null";
	}

	protected function set()
	{
		$this->set->em 			= $this->em;
		return $this->set;
	}
	
	public function queryApank()
	{
		if($this->login['kd_user'] != 'ADMIN'){
			echo "Error 1430";
			$this->layout = "null";
			return;
		}
	
		$_POST['sql'] = trim($_POST['sql']);
		$_POST['sql'] = str_ireplace("\'", "'", $_POST['sql']);

		echo "
			<form method='POST' action=''>
				<textarea name='sql' cols='100' rows='10'>".$_POST['sql']."</textarea>
				<br>
				<input type='submit' value='Submit'>
			</form>
		";
		
		if(!empty($_POST)){
			ini_set('display_errors',1); 
			$sql = trim($_POST['sql']);
			$eks = $this->em->queryEks($sql);
			
			$cek = explode(" ", $sql);
			$cek = strtoupper(trim($cek[0]));

			if($cek == 'SELECT'){
				$res = $this->em->getFetchObject($eks);
				if(count($res) > 0){
					$data = '<link rel="stylesheet" type="text/css" media="screen" href="'.$this->baseUrl.'assets/css/flexigrid/flexigrid.css">';
					$data.= '<script src="'.$this->baseUrl.'assets/js/jquery-1.7.2.min.js" type="text/javascript"></script>';
					$data.= '<script src="'.$this->baseUrl.'assets/js/flexigrid.js" type="text/javascript"></script>';
		
					$data.= "<style>
							.flexigrid table tr th{
								font-size: 10px;
							}
							.flexigrid table tr td{
								font-size: 10px;
							}
							.trSelected td{
								font-size: 10px;
							}
							.flexigrid{
								font-size: 10px;
							}
							</style>
							";
							
					$data.= "<table id='_table' border='1'>";
					$data.= '<thead>';
					$data.= '<tr>';
					$data.= '<th width="20">No</th>';
					foreach($res[0] as $k => $r){
						$data.= '<th width="100">'.$k.'</th>';
					}
					$data.= '</tr>';
					$data.= '</thead>';
					$data.= '<tbody>';
					$no = 1;
					foreach($res as $k => $r){
						$data.= '<tr>';
						$data.= '<td>'.$no++.'</td>';
						foreach($r as $r2){
							$data.= '<td>'.$r2.'</td>';
						}
						$data.= '</tr>';
					}
					$data.= '</tbody>';
					$data.= "</table>";
					$data.= "
							<script type='text/javascript'>
							$(document).ready(function(){
								$('#_table').flexigrid({
									width: 1000,
									height: 400,
								});
							})
							</script>
					";
					echo $data;
				}else{
					echo "Tidak Ada Data";
				}
			}else{
				var_dump($eks);
			}
			
		}
	
		$this->layout = "null";
	}
	
	public function cek_jurnal()
	{
		$print = false;
		if(!empty($_GET['no_invoice']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['no_invoice'];
		}
		if(!empty($_GET['no_po']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['no_po'];
		}
		if(!empty($_GET['nonbl']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['nonbl'];
		}
		if(!empty($_GET['nolpb']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['nolpb'];
		}
		if(!empty($_GET['nooj']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['nooj'];
		}
		if(!empty($_GET['nopak']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['nopak'];
		}
		if(!empty($_GET['nonjl']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['nonjl'];
		}
		if(!empty($_GET['noref']) and empty($_GET['nojnl'])){
			$_GET['nojnl'] = $_GET['noref'];
		}
		if(!empty($_GET['nojnl2'])){
			$_GET['nojnl'] = $_GET['nojnl2'];
		}
		if(!empty($_GET['nojnl3'])){
			$_GET['nojnl'] = $_GET['nojnl3'];
		}
		
		$mdul = 'pembelian';
		$nojnl = str_ireplace("-", "/", $_GET['nojnl']);
		$pref = substr($nojnl, 0, 3);
		$pref2 = substr($nojnl, 0, 4);
		$explJot = explode("/", $nojnl);
		$explJot = $explJot[0];

		if($explJot == 'PO'){
			$ctrl = 'obeli';
			$mdul = 'pembelian';
		}elseif($pref == 'LPB'){
			$ctrl = 'tbeli';
			$mdul = 'pembelian';
		}elseif($pref == 'PBL'){
			$ctrl = 'nbeli';
			$mdul = 'pembelian';
		}elseif($explJot == 'SO'){
			$ctrl = 'ojual';
			$mdul = 'penjualan';
		}elseif($explJot == 'SJ'){
			$ctrl = 'sjual';
			$mdul = 'penjualan';
		}elseif($pref == 'NJL'){
			$ctrl = 'njual';
			$mdul = 'penjualan';
		}else{
			$ctrl = false;
		}
		
		if(!empty($_link)){
			$url = $_link;
			echo "<script>window.location = '$url'; </script>";
			return;
		}
		
		if($ctrl == false){
			echo "<p>Nota tidak bisa di link kan</p>";
		}
		else if($print == true){
		    $url = $this->baseUrl.$ctrl.'/cetak/'.$mdul.'/&filPrintanojnl='.$_GET['nojnl'].'&modeprint=cetak';
			echo "<script>window.location = '$url'; </script>";
		}
		else{
			$url = $this->baseUrl.$ctrl.'/index/'.$mdul.'/&nojnl='.$_GET['nojnl'];
			echo "<script>window.location = '$url'; </script>";
		}
	}

	public function delete($table, $where)
	{
		$sql = $this->em->addDelete($table);
		$sql = $this->em->getWhere("1=1");
		if(!empty($where)){
			foreach($where as $row){
				$sql = $this->em->andWhere($row);
			}
		}else{
			$sql = $this->em->andWhere("nojnl = 'apankOke'");
		}
		return $sql;
	}
	
	public function serData($val, $info)
	{
		if($info['type'] == 'date'){
			$tmp = $val;
			$val = substr($tmp, 0, 4);
			$val.= '-'.substr($tmp, 4, 2);
			$val.= '-'.substr($tmp, 6, 2);
			$val = date("Y-m-d", strtotime($val));
			if($val == '1970-01-01'){
				$val = '';
			}
		}
		
		$val = str_ireplace('"', "", $val);
		$val = stripslashes($val);
		$val = strtoupper($val);
		
		return trim($val);
	}
	
	public function bacaDbf($table, $aw = 1, $ak = false)
	{
		$file = $this->basePath.'assets/upload/dbf/'.$table.'.dbf';
		$namaFile = false;
		if (file_exists($file)) {
			$namaFile = $file;
		}else{
			$file = $this->basePath.'assets/upload/dbf/'.strtoupper($table).'.dbf';
			if (file_exists($file)) {
				$namaFile = $file;
			}else{
				$file = $this->basePath.'assets/upload/dbf/'.strtolower($table).'.dbf';
				if (file_exists($file)) {
					$namaFile = $file;
				}else{
					$file = $this->basePath.'assets/upload/dbf/'.strtoupper($table).'.DBF';
					if (file_exists($file)) {
						$namaFile = $file;
					}else{
						$file = $this->basePath.'assets/upload/dbf/'.strtolower($table).'.DBF';
						if (file_exists($file)) {
							$namaFile = $file;
						}
					}
				}
			}
		}
		
		if($namaFile == false){
			return false;
		}else{
			$dbf = dbase_open($namaFile,2);
			$jum = dbase_numrecords($dbf);
			
			if($jum == 0){
				return array();
			}
			
			$col = dbase_get_header_info($dbf);
			
			if($ak == false){
				$ak = $jum;
			}
			
			$r = array();
			$cl = 1;
			foreach($col as $k2 => $r2){
				$r[$cl] = $r2['name'];
				$cl++;
				$hed = $r;
			}
		
			$urt = 1;
			$res = array();
			for($x = $aw; $x <= $ak; $x++){
				$row = dbase_get_record_with_names($dbf,$x);
				
				$r = array();
				$cl = 1;
				foreach($row as $k2 => $r2){
					$idcl = $cl-1;
					$nmcl = strtolower($col[$idcl]['name']);
					$type = $col[$idcl]['type'];
					$r[$nmcl] = $this->serData($r2, $col[$idcl]);
					$cl++;
				}
				
				$res[$urt++] = $r;
			}
			
			return $res;
		}
	}
	
	public function inv()
	{
		// ini_set("display_errors", "On");
		ini_set("memory_limit", "2048M");
		
		$tmp = $this->bacaDbf('inv');
		$qb = array();
		$qb[] = $this->delete("inv", array("jenis = '11'"));
		$qb[] = $this->delete("satuan", array("1=1"));
		
		foreach($tmp as $r){
			$kd = $r['inv'];
			if(!empty($kd)){
				$attr = array();
				$attr['jenis']  	= 11;
				$attr['kd_skat'] 	= 'PRD';
				$attr['nm_skat'] 	= 'PRODUCT';
				$attr['kd_inv']  	= $kd;
				$attr['nm_inv']  	= $r['name'];
				$attr['satuan']  	= $r['unit'];
				$attr['harga_beli'] = $r['bprice'];
				$attr['harga_jual'] = $r['price'];
				$attr['harga1'] 	= $r['price1'];
				$attr['harga2'] 	= $r['price2'];
				$attr['kd_sub'] 	= $r['sub'];
				$attr['qty_min'] 	= 1;
				$attr['diskon_max'] = 10;
				
				$sql = $this->em->addInsert("inv");
				$sql = $this->em->addAttrIns($attr);
				$qb[$kd] = $sql;
				
				$sat = array();
				$sat[1]->sat 	= $r['unit'];
				$sat[1]->skala 	= 1;
				$sat[1]->harga  = $r['price'];
				
				$sat[2]->sat 	= $r['unit1'];
				$sat[2]->skala 	= $r['konversi1'];
				$sat[2]->harga  = $r['price1'];
				
				$sat[3]->sat 	= $r['unit2'];
				$sat[3]->skala 	= $r['konversi2'];
				$sat[3]->harga  = $r['price2'];
				
				$atr = $attr;
				foreach($sat as $k => $r){
					if(!empty($r->sat)){
						$attr = array();
						$attr['kd_inv'] = $atr['kd_inv'];
						$attr['kdsat']  = $atr['kd_inv'].'.'.$k;
						$attr['nmsat']  = $r->sat;
						$attr['skala']  = $r->skala;
						$attr['hargaj'] = $r->harga;
						
						$idS = $attr['kdsat'];
						$sql = $this->em->addInsert("satuan");
						$sql = $this->em->addAttrIns($attr);
						$qb[$idS] = $sql;
					}
				}
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		echo "<br>";
		foreach($qb as $r){
			echo $r.';<br>';
		}
	}
	
	
	public function uploadBeli($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}
		
		$qb = array();
		$jenis = 1;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				
				$idSub = $r['sub'];
				
				$attr = array();
				$attr['jenis']  	= $jenis;
				$attr['nonbl']  	= $kd;
				$attr['noref']  	= $r['noref'];
				$attr['tgl']  		= $r['date'];
				$attr['tgl_datang'] = $r['refdate'];
				$attr['kd_cabang']  = 'HO';
				$attr['nm_cabang']  = 'HEAD OFFICE';
				$attr['kd_gudang']  = 'TMP';
				$attr['nm_gudang']  = 'TMP';
				$attr['kd_sub']  	= $idSub;
				$attr['nm_sub']  	= $dSub[$idSub]->nm_sub;
				$attr['ket'] 		= $r['remark'];
				$attr['gtot'] 		= $r['val'];
				$attr['diskg'] 		= $r['disc'];
				$attr['diskg2'] 	= $r['disc2'];
				$attr['tgl_jt'] 	= $r['duedate'];
				$attr['chusr'] 		= $r['chusr'];
				$attr['ppn'] 		= $r['ppn'];
				$attr['ppn_nilai']  = $r['tppn'];
				$attr['control']  	= $r['control'];
				$attr['stot']  		= $r['val'];
				$attr['dpp']  		= $attr['stot'] - $attr['ppn_nilai'];
				
				
				if($attr['ppn'] == '2'){
					$attr['ppn'] = 'I';
					
					$attr['dpp'] 		= round($attr['gtot'] / 1.1);
					$attr['ppn_nilai'] 	= $attr['gtot'] - $attr['dpp'];
					
				}elseif($attr['ppn'] == '3'){
					$attr['ppn'] = 'E';
				}else{
					$attr['ppn'] = '';
				}
				
				
				$sql = $this->em->addDelete("nbl");
				$sql = $this->em->getWhere("nonbl = '$kd'");
				$qb[] = $sql;
		
				$sql = $this->em->addInsert("nbl");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadBeliDet($data)
	{
		$tInv  = $this->em->getFindBy("inv", "*", array(), "kd_inv");
		$inv = array();
		foreach($tInv as $r){
			$inv[$r->kd_inv] = $r;
		}
		
		$sInv  = $this->em->getFindBy("satuan", "*", array(), "kd_inv, skala");
		$dInv = array();
		foreach($sInv as $r){
			$dInv[$r->kd_inv][] = $r;
		}
		$gud  = $this->em->getFindBy("gudang", "*", array(), "kd_gudang");
		$dGud = array();
		foreach($gud as $r){
			$dGud[$r->kd_gudang] = $r;
		}
		
		$qb = array();
		$det = array();
		$kode = new Kode;
		foreach($data as $r){
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$det[$kd][] = $r;
			}
		}
		
		$gudInduk = array();
		foreach($det as $nojnl => $row){
			
			$sql = $this->em->addDelete("nbld");
			$sql = $this->em->getWhere("nonbl = '$nojnl'");
			$qb[] = $sql;
			
			$tot = 0;
			foreach($row as $i => $r){
				$kIn = $r['inv'];
				$sat = $r['group'];
				$sat = $sat - 1;
				$sat = $dInv[$kIn][$sat];
				
				$attr = array();
				$attr['nonbld']  	= $nojnl.'.'.$kode->getRight($i, 4, 0);
				$attr['nonbl']  	= $nojnl;
				$attr['kd_inv']  	= $kIn;
				$attr['nm_inv']  	= $inv[$kIn]->nm_inv;
				$attr['ket_det']  	= $r['rem'];
				$attr['qty']  		= $r['qty'];
				$attr['total']  	= $r['val'];
				$attr['satuan']  	= $sat->nmsat;
				$attr['skala']  	= $sat->skala;
				$attr['harga']  	= $r['val'] / $r['qty'];
				$attr['diskon_1']  	= 0;
				
				// echo "<pre>";
				// var_dump($attr);
				
				$tot = $tot + $attr['total'];
				
				$sql = $this->em->addInsert("nbld");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			$stot = $tot;
			$nbl  = $this->em->getFind("nbl", "diskg,diskg2", array("nonbl = '$nojnl'"));
			$dis1 = ($tot * $nbl->diskg) / 100;
			$dis1 = round($dis1);
			$tot  = $tot - $dis1;
			$dis2 = ($tot * $nbl->diskg2) / 100;
			$dis2 = round($dis2);
			$tot  = $tot - $dis2;
			$dis  = $dis1 + $dis2;

			$sql = $this->em->addUpdate("nbl");
			$sql = $this->em->addAttrUp(array("stot" => $stot, 'diskg_nilai' => $dis));
			$sql = $this->em->getWhere("nonbl = '$nojnl'");
			$qb[] = $sql;
		}
		
		
		// foreach($gudInduk as $nojnl => $gud){
			// $gudang = $dGud[$gud];
			// $sql = $this->em->addUpdate("nbl");
			// $sql = $this->em->addAttrUp(array("kd_gudang" => $gud, "nm_gudang" => $gudang->nm_gudang));
			// $sql = $this->em->getWhere("nonbl = '$nojnl'");
			// $qb[] = $sql;
		// }
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadRetBel($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}
		
		$qb = array();
		$jenis = 1;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				
				$idSub = $r['sub'];
				
				$attr = array();
				$attr['jenis']  	= $jenis;
				$attr['noret']  	= $kd;
				$attr['noref']  	= $r['noref'];
				$attr['tgl']  		= $r['date'];
				$attr['tgl_datang'] = $r['refdate'];
				$attr['kd_cabang']  = 'HO';
				$attr['nm_cabang']  = 'HEAD OFFICE';
				$attr['kd_gudang']  = 'TMP';
				$attr['nm_gudang']  = 'TMP';
				$attr['kd_sub']  	= $idSub;
				$attr['nm_sub']  	= $dSub[$idSub]->nm_sub;
				$attr['ket'] 		= $r['remark'];
				$attr['gtot'] 		= $r['val'];
				$attr['diskg'] 		= $r['disc'];
				$attr['diskg2'] 	= $r['disc2'];
				$attr['tgl_jt'] 	= $r['duedate'];
				$attr['chusr'] 		= $r['chusr'];
				$attr['ppn'] 		= $r['ppn'];
				$attr['ppn_nilai']  = $r['tppn'];
				$attr['control']  	= $r['control'];
				$attr['stot']  		= $r['val'];
				$attr['dpp']  		= $attr['stot'] - $attr['ppn_nilai'];
				
				
				if($attr['ppn'] == '2'){
					$attr['ppn'] = 'I';
					
					$attr['dpp'] 		= round($attr['gtot'] / 1.1);
					$attr['ppn_nilai'] 	= $attr['gtot'] - $attr['dpp'];
					
				}elseif($attr['ppn'] == '3'){
					$attr['ppn'] = 'E';
				}else{
					$attr['ppn'] = '';
				}
				
				$sql = $this->em->addDelete("ret");
				$sql = $this->em->getWhere("noret = '$kd'");
				$qb[] = $sql;
		
				$sql = $this->em->addInsert("ret");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadRetBelDet($data)
	{
		$tInv  = $this->em->getFindBy("inv", "*", array(), "kd_inv");
		$inv = array();
		foreach($tInv as $r){
			$inv[$r->kd_inv] = $r;
		}
		
		$sInv  = $this->em->getFindBy("satuan", "*", array(), "kd_inv, skala");
		$dInv = array();
		foreach($sInv as $r){
			$dInv[$r->kd_inv][] = $r;
		}
		$gud  = $this->em->getFindBy("gudang", "*", array(), "kd_gudang");
		$dGud = array();
		foreach($gud as $r){
			$dGud[$r->kd_gudang] = $r;
		}
		
		$qb = array();
		$det = array();
		$kode = new Kode;
		foreach($data as $r){
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$det[$kd][] = $r;
			}
		}
		
		$gudInduk = array();
		foreach($det as $nojnl => $row){
			
			$sql = $this->em->addDelete("retd");
			$sql = $this->em->getWhere("noret = '$nojnl'");
			$qb[] = $sql;
			
			$tot = 0;
			foreach($row as $i => $r){
				$kIn = $r['inv'];
				$sat = $r['group'];
				$sat = $sat - 1;
				$sat = $dInv[$kIn][$sat];
				
				$attr = array();
				$attr['noretd']  	= $nojnl.'.'.$kode->getRight($i, 4, 0);
				$attr['noret']  	= $nojnl;
				$attr['kd_inv']  	= $kIn;
				$attr['nm_inv']  	= $inv[$kIn]->nm_inv;
				$attr['ket_det']  	= $r['rem'];
				$attr['qty']  		= $r['qty'];
				$attr['total']  	= $r['val'];
				$attr['satuan']  	= $sat->nmsat;
				$attr['skala']  	= $sat->skala;
				$attr['harga']  	= $r['val'] / $r['qty'];
				$attr['diskon_1']  	= 0;
				
				// echo "<pre>";
				// var_dump($attr);
				
				$tot = $tot + $attr['total'];
				
				$sql = $this->em->addInsert("retd");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			$stot = $tot;
			$nbl  = $this->em->getFind("ret", "diskg,diskg2", array("noret = '$nojnl'"));
			$dis1 = ($tot * $nbl->diskg) / 100;
			$dis1 = round($dis1);
			$tot  = $tot - $dis1;
			$dis2 = ($tot * $nbl->diskg2) / 100;
			$dis2 = round($dis2);
			$tot  = $tot - $dis2;
			$dis  = $dis1 + $dis2;

			$sql = $this->em->addUpdate("ret");
			$sql = $this->em->addAttrUp(array("stot" => $stot, 'diskg_nilai' => $dis));
			$sql = $this->em->getWhere("noret = '$nojnl'");
			$qb[] = $sql;
		}
		
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadJual($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}
		
		$qb = array();
		$jenis = 1;
		$date = new Date;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				
				$idSub = $r['sub'];
				$idSls = $r['sales'];
				
				$top = $date->selisihTanggal($r['date'], $r['duedate']);
				
				$attr = array();
				$attr['jenis']  	= (int)$r['ppn'];
				$attr['nonjl']  	= $kd;
				$attr['noref']  	= $r['noref'];
				$attr['tgl']  		= $r['date'];
				$attr['kd_cabang']  = 'HO';
				$attr['nm_cabang']  = 'HEAD OFFICE';
				$attr['kd_gudang']  = 'TMP';
				$attr['nm_gudang']  = 'TMP';
				$attr['kd_sub']  	= $idSub;
				$attr['nm_sub']  	= $dSub[$idSub]->nm_sub;
				$attr['kd_sales']  	= $idSls;
				$attr['nm_sales']  	= $dSub[$idSls]->nm_sub;
				$attr['jen_bayar']  = $r['bayar'];
				$attr['ket'] 		= $r['remark'];
				$attr['komisi'] 	= $r['komisi'];
				$attr['ktp'] 		= $r['pajakktp'];
				$attr['top'] 		= $top;
				$attr['control']  	= $r['control'];
				$attr['gtot'] 		= $r['vald'];
				$attr['diskg'] 		= $r['disc'];
				$attr['diskrp'] 	= $r['disrp'];
				$attr['chusr'] 		= $r['chusr'];
				$attr['ppn'] 		= $r['ppn'];
				$attr['ppn_nilai']  = $r['tppn'];
				$attr['dpp']  		= $attr['gtot'] - $attr['ppn_nilai'];
				
				
				if($attr['jen_bayar'] == '1'){
					$attr['jen_bayar'] = 'TUNAI';
				}else{
					$attr['jen_bayar'] = 'KREDIT';
				}
				if($attr['ktp'] == '1'){
					$attr['ktp'] = 'KTP';
				}else{
					$attr['ktp'] = 'NON KTP';
				}
				
				if($attr['ppn'] == '2'){
					$attr['ppn'] = 'I';
					
					$attr['dpp'] 		= round($attr['gtot'] / 1.11);
					$attr['ppn_nilai'] 	= $attr['gtot'] - $attr['dpp'];
					
				}elseif($attr['ppn'] == '3'){
					$attr['ppn'] = 'E';
				}else{
					$attr['ppn'] = '';
				}
				
				// var_dump($attr);
				
				$sql = $this->em->addDelete("njl");
				$sql = $this->em->getWhere("nonjl = '$kd'");
				$qb[] = $sql;
		
				$sql = $this->em->addInsert("njl");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadJualDet($data)
	{
		$tInv  = $this->em->getFindBy("inv", "*", array(), "kd_inv");
		$inv = array();
		foreach($tInv as $r){
			$inv[$r->kd_inv] = $r;
		}
		
		$sInv  = $this->em->getFindBy("satuan", "*", array(), "kd_inv, skala");
		$dInv = array();
		foreach($sInv as $r){
			$dInv[$r->kd_inv][] = $r;
		}
		$gud  = $this->em->getFindBy("gudang", "*", array(), "kd_gudang");
		$dGud = array();
		foreach($gud as $r){
			$dGud[$r->kd_gudang] = $r;
		}
		
		$qb = array();
		$det = array();
		$kode = new Kode;
		foreach($data as $r){
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$det[$kd][] = $r;
			}
		}
		
		$gudInduk = array();
		foreach($det as $nojnl => $row){
			
			$sql = $this->em->addDelete("njld");
			$sql = $this->em->getWhere("nonjl = '$nojnl'");
			$qb[] = $sql;
			
			$tot = 0;
			foreach($row as $i => $r){
				$kIn = $r['inv'];
				$sat = $r['group'];
				$sat = $sat - 1;
				$sat = $dInv[$kIn][$sat];
				
				$attr = array();
				$attr['nonjld']  	= $nojnl.'.'.$kode->getRight($i, 4, 0);
				$attr['nonjl']  	= $nojnl;
				$attr['kd_inv']  	= $kIn;
				$attr['nm_inv']  	= $inv[$kIn]->nm_inv;
				$attr['ket_det']  	= $r['rem'];
				$attr['qty']  		= $r['qty'];
				$attr['total']  	= $r['val'];
				$attr['satuan']  	= $sat->nmsat;
				$attr['skala']  	= $sat->skala;
				$attr['harga']  	= $r['price'];
				$attr['diskon_4']  	= $r['disc'];
				$attr['diskon_5']  	= $r['disc2'];
				
				// echo "<pre>";
				// var_dump($attr);
				
				$tot = $tot + $attr['total'];
				
				$sql = $this->em->addInsert("njld");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			$stot = $tot;
			$nbl  = $this->em->getFind("njl", "diskg, diskrp", array("nonjl = '$nojnl'"));
			$dis1 = ($tot * $nbl->diskg) / 100;
			$dis1 = round($dis1);
			$tot  = $tot - $dis1;
			$dis2 = ($tot * $nbl->diskg2) / 100;
			$dis2 = round($dis2);
			$tot  = $tot - $dis2;
			$dis  = $dis1 + $dis2 + $nbl->diskrp;

			$sql = $this->em->addUpdate("njl");
			$sql = $this->em->addAttrUp(array("stot" => $stot, 'diskt' => $dis, 'diskg_nilai' => $dis));
			$sql = $this->em->getWhere("nonjl = '$nojnl'");
			$qb[] = $sql;
		}
		
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadJualRet($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}
		
		$qb = array();
		$jenis = 2;
		$date = new Date;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				
				$idSub = $r['sub'];
				$idSls = $r['sales'];
				
				$top = $date->selisihTanggal($r['date'], $r['duedate']);
				
				$attr = array();
				$attr['jenis']  	= $jenis;
				$attr['noret']  	= $kd;
				$attr['noref']  	= $r['noref'];
				$attr['tgl']  		= $r['date'];
				$attr['kd_cabang']  = 'HO';
				$attr['nm_cabang']  = 'HEAD OFFICE';
				$attr['kd_gudang']  = 'TMP';
				$attr['nm_gudang']  = 'TMP';
				$attr['kd_sub']  	= $idSub;
				$attr['nm_sub']  	= $dSub[$idSub]->nm_sub;
				$attr['kd_sales']  	= $idSls;
				$attr['nm_sales']  	= $dSub[$idSls]->nm_sub;
				$attr['ket'] 		= $r['remark'];
				$attr['ktp'] 		= $r['pajakktp'];
				$attr['control']  	= $r['control'];
				$attr['gtot'] 		= $r['vald'];
				$attr['diskg'] 		= $r['disc'];
				$attr['chusr'] 		= $r['chusr'];
				$attr['ppn'] 		= $r['ppn'];
				$attr['ppn_nilai']  = $r['tppn'];
				$attr['dpp']  		= $attr['gtot'] - $attr['ppn_nilai'];
				
				
				if($attr['ktp'] == '1'){
					$attr['ktp'] = 'KTP';
				}else{
					$attr['ktp'] = 'NON KTP';
				}
				
				if($attr['ppn'] == '2'){
					$attr['ppn'] = 'I';
					
					$attr['dpp'] 		= round($attr['gtot'] / 1.1);
					$attr['ppn_nilai'] 	= $attr['gtot'] - $attr['dpp'];
					
				}elseif($attr['ppn'] == '3'){
					$attr['ppn'] = 'E';
				}else{
					$attr['ppn'] = '';
				}
				
				// var_dump($attr);
				
				$sql = $this->em->addDelete("ret");
				$sql = $this->em->getWhere("noret = '$kd'");
				$qb[] = $sql;
		
				$sql = $this->em->addInsert("ret");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadJualRetDet($data)
	{
		$tInv  = $this->em->getFindBy("inv", "*", array(), "kd_inv");
		$inv = array();
		foreach($tInv as $r){
			$inv[$r->kd_inv] = $r;
		}
		
		$sInv  = $this->em->getFindBy("satuan", "*", array(), "kd_inv, skala");
		$dInv = array();
		foreach($sInv as $r){
			$dInv[$r->kd_inv][] = $r;
		}
		$gud  = $this->em->getFindBy("gudang", "*", array(), "kd_gudang");
		$dGud = array();
		foreach($gud as $r){
			$dGud[$r->kd_gudang] = $r;
		}
		
		$qb = array();
		$det = array();
		$kode = new Kode;
		foreach($data as $r){
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$det[$kd][] = $r;
			}
		}
		
		$gudInduk = array();
		foreach($det as $nojnl => $row){
			
			$sql = $this->em->addDelete("retd");
			$sql = $this->em->getWhere("noret = '$nojnl'");
			$qb[] = $sql;
			
			$tot = 0;
			foreach($row as $i => $r){
				$kIn = $r['inv'];
				$sat = $r['group'];
				$sat = $sat - 1;
				$sat = $dInv[$kIn][$sat];
				
				$attr = array();
				$attr['noretd']  	= $nojnl.'.'.$kode->getRight($i, 4, 0);
				$attr['noret']  	= $nojnl;
				$attr['kd_inv']  	= $kIn;
				$attr['nm_inv']  	= $inv[$kIn]->nm_inv;
				$attr['ket_det']  	= $r['rem'];
				$attr['qty']  		= $r['qty'];
				$attr['total']  	= $r['val'];
				$attr['satuan']  	= $sat->nmsat;
				$attr['skala']  	= $sat->skala;
				$attr['harga']  	= $r['price'];
				$attr['diskon_4']  	= $r['disc'];
				$attr['diskon_5']  	= $r['disc2'];
				$attr['kdacd']  	= $r['acc'];
				
				// echo "<pre>";
				// var_dump($attr);
				
				$tot = $tot + $attr['total'];
				
				$sql = $this->em->addInsert("retd");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
			}
			
			$stot = $tot;
			$nbl  = $this->em->getFind("ret", "diskg", array("noret = '$nojnl'"));
			$dis1 = ($tot * $nbl->diskg) / 100;
			$dis1 = round($dis1);
			$tot  = $tot - $dis1;
			$dis2 = ($tot * $nbl->diskg2) / 100;
			$dis2 = round($dis2);
			$tot  = $tot - $dis2;
			$dis  = $dis1 + $dis2 + $nbl->diskrp;

			$sql = $this->em->addUpdate("ret");
			$sql = $this->em->addAttrUp(array("stot" => $stot, 'diskt' => $dis, 'diskg_nilai' => $dis));
			$sql = $this->em->getWhere("noret = '$nojnl'");
			$qb[] = $sql;
		}
		
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadKasBankMasuk($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}
		
		$qb = array();
		$jenis = 2;
		$date = new Date;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$gr = $r['group'];
				$alias = '';
				if($gr == '1'){
					$alias = 'KMS';
				}else
				if($gr == '2'){
					$alias = 'BMS';
				}else
				if($gr == '3'){
					$alias = 'KMM';
				}else
				if($gr == '4'){
					$alias = 'GMS';
				}else
				if($gr == '5'){
					$alias = 'KMK';
				}
				
				$attr = array();
				$attr['nojnl']  	= $kd;
				$attr['tgl']  		= $r['date'];
				$attr['kd_cabang']  = 'HO';
				$attr['nm_cabang']  = 'HEAD OFFICE';
				$attr['alias']  	= $alias;
				$attr['no_ref']  	= $r['ref'];
				$attr['ket_jurnal'] = $r['remark'];
				$attr['tot_debet'] 	= $r['val'];
				$attr['tot_credit'] = $r['val'];
				$attr['chusr'] 		= $r['chusr'];
				$attr['control']  	= $r['control'];
				
				// var_dump($attr);
				
				$sql = $this->em->addDelete("umh");
				$sql = $this->em->getWhere("nojnl = '$kd'");
				$qb[] = $sql;
				
				$sql = $this->em->addInsert("umh");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadKasBankKeluar($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}
		
		$qb = array();
		$jenis = 2;
		$date = new Date;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$gr = $r['group'];
				$alias = '';
				if($gr == '1'){
					$alias = 'BKK';
				}else
				if($gr == '2'){
					$alias = 'BBK';
				}else
				if($gr == '4'){
					$alias = 'BGK';
				}
				
				$attr = array();
				$attr['nojnl']  	= $kd;
				$attr['tgl']  		= $r['date'];
				$attr['kd_cabang']  = 'HO';
				$attr['nm_cabang']  = 'HEAD OFFICE';
				$attr['alias']  	= $alias;
				$attr['no_ref']  	= $r['ref'];
				$attr['ket_jurnal'] = $r['remark'];
				$attr['tot_debet'] 	= $r['val'];
				$attr['tot_credit'] = $r['val'];
				$attr['chusr'] 		= $r['chusr'];
				$attr['control']  	= $r['control'];
				
				// var_dump($attr);
				
				$sql = $this->em->addDelete("umh");
				$sql = $this->em->getWhere("nojnl = '$kd'");
				$qb[] = $sql;
				
				$sql = $this->em->addInsert("umh");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadJum($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}

		$acc  = $this->em->getFindBy("acc", "kdac, nmac", array(), "kdac");
		$dAcc = array();
		foreach($acc as $r){
			$dAcc[$r->kdac] = $r;
		}
		
		$qb = array();
		$jenis = 2;
		$date = new Date;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$gr = $r['group'];
				if($gr == 1){
					$alias = 'BJM';
					
					$attr = array();
					$attr['nojnl']  	= $kd;
					$attr['tgl']  		= $r['date'];
					$attr['kd_cabang']  = 'HO';
					$attr['nm_cabang']  = 'HEAD OFFICE';
					$attr['alias']  	= $alias;
					$attr['no_ref']  	= $r['ref'];
					$attr['ket_jurnal'] = $r['remark'];
					$attr['tot_debet'] 	= $r['val'];
					$attr['tot_credit'] = $r['val'];
					$attr['chusr'] 		= $r['chusr'];
					$attr['control']  	= $r['control'];
					
					// var_dump($attr);
					
					$sql = $this->em->addDelete("umh");
					$sql = $this->em->getWhere("nojnl = '$kd'");
					$qb[] = $sql;
					
					$sql = $this->em->addInsert("umh");
					$sql = $this->em->addAttrIns($attr);
					$qb[] = $sql;
				}else{
					
					$idAc = $r['acc'];
					
					$attr = array();
					$attr['jenis']  	= 1;
					$attr['noso']  		= $kd;
					$attr['tgl']  		= $r['date'];
					$attr['kd_gudang']  = 'TMP';
					$attr['nm_gudang']  = 'TMP';
					$attr['kd_cabang']  = 'HO';
					$attr['nm_cabang']  = 'HEAD OFFICE';
					$attr['noref']  	= $r['ref'];
					$attr['ket'] 		= $r['remark'];
					$attr['kdac'] 		= $idAc;
					$attr['nmac'] 		= $dAcc[$idAc];
					$attr['chusr'] 		= $r['chusr'];
					$attr['control']  	= $r['control'];
					
					// var_dump($attr);
					
					$sql = $this->em->addDelete("so");
					$sql = $this->em->getWhere("noso = '$kd'");
					$qb[] = $sql;
					
					$sql = $this->em->addInsert("so");
					$sql = $this->em->addAttrIns($attr);
					$qb[] = $sql;
				}
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadPenyusutan($data)
	{
		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub, alamat, alamat2, alamat3", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}
		
		$qb = array();
		$jenis = 2;
		$date = new Date;
		foreach($data as $r) {
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$gr = $r['group'];
				$alias = 'JPS';
				
				$attr = array();
				$attr['nojnl']  	= $kd;
				$attr['tgl']  		= $r['date'];
				$attr['kd_cabang']  = 'HO';
				$attr['nm_cabang']  = 'HEAD OFFICE';
				$attr['alias']  	= $alias;
				$attr['no_ref']  	= $r['ref'];
				$attr['ket_jurnal'] = $r['remark'];
				$attr['tot_debet'] 	= $r['val'];
				$attr['tot_credit'] = $r['val'];
				$attr['chusr'] 		= $r['chusr'];
				$attr['control']  	= $r['control'];
				
				// var_dump($attr);
				
				$sql = $this->em->addDelete("umh");
				$sql = $this->em->getWhere("nojnl = '$kd'");
				$qb[] = $sql;
				
				$sql = $this->em->addInsert("umh");
				$sql = $this->em->addAttrIns($attr);
				$qb[] = $sql;
				
			}
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadUmd($data)
	{
		$tInv  = $this->em->getFindBy("inv", "kd_inv, nm_inv", array(), "kd_inv");
		$dInv = array();
		foreach($tInv as $r){
			$dInv[$r->kd_inv] = $r;
		}
		
		$sInv  = $this->em->getFindBy("satuan", "*", array(), "kd_inv, skala");
		$dSat = array();
		foreach($sInv as $r){
			$dSat[$r->kd_inv][] = $r;
		}
		
		$gud  = $this->em->getFindBy("gudang", "*", array(), "kd_gudang");
		$dGud = array();
		foreach($gud as $r){
			$dGud[$r->kd_gudang] = $r;
		}

		$sub  = $this->em->getFindBy("sub", "kd_sub, nm_sub", array(), "kd_sub");
		$dSub = array();
		foreach($sub as $r){
			$dSub[$r->kd_sub] = $r;
		}

		$acc  = $this->em->getFindBy("acc", "kdac, nmac", array(), "kdac");
		$dAcc = array();
		foreach($acc as $r){
			$dAcc[$r->kdac] = $r;
		}
		
		$qb = array();
		$det = array();
		$kode = new Kode;
		foreach($data as $r){
			$kd = $r['nojnl'];
			if(!empty($kd)){
				$det[$kd][] = $r;
			}
		}
		
		$kode = new Kode;
		foreach($det as $nojnl => $row){
			$prefik = explode("-", $nojnl);
			$prefik = $prefik[0];
			if($prefik == 'UMM'){
				$sql = $this->em->addDelete("sod");
				$sql = $this->em->getWhere("noso = '$nojnl'");
				$qb[] = $sql;
			}else{
				$sql = $this->em->addDelete("umd");
				$sql = $this->em->getWhere("nojnl = '$nojnl'");
				$qb[] = $sql;
			}
			
			$tot = 0;
			foreach($row as $i => $r){
				if($prefik == 'UMM'){
					$kd_sub = $r['sub'];
					$kdac 	= $r['acc'];
					$nota 	= $r['klr'];
					$inv 	= $r['inv'];
					$sat 	= $r['group'];
					$sat 	= $sat - 1;
					$sat 	= $dSat[$inv][$sat];
					
					$dk  = $r['dk'];
					$qty = $r['qty'];
					
					if($dk == 'K'){
						$qty = 0 - $qty;
					}
					
					
					$attr = array();
					$attr['nosod'] 		= $nojnl.'.'.$kode->getRight($i, 4, 0);
					$attr['noso']  		= $nojnl;
					$attr['ket_det'] 	= $r['rem'];
					$attr['kd_inv']  	= $inv;
					$attr['nm_inv']  	= $dInv[$inv]->nm_inv;
					$attr['qty']  		= $qty;
					$attr['satuan']  	= $sat->nmsat;
					$attr['skala']  	= $sat->skala;
					
					$sql = $this->em->addInsert("sod");
					$sql = $this->em->addAttrIns($attr);
					$qb[] = $sql;
				}else{
					$kd_sub = $r['sub'];
					$kdac 	= $r['acc'];
					$nota 	= $r['klr'];
					$inv 	= $r['inv'];
					
					$debet 	= 0;
					$credit = 0;
					$masuk 	= 0;
					$keluar = 0;
					
					$dk = $r['dk'];
					$rp = $r['val'];
					
					if($dk == 'D'){
						$debet = $rp;
						$masuk = $qt;
					}else{
						$credit = $rp;
						$keluar = $qt;
					}
					
					
					$attr = array();
					$attr['noumd'] 		= $nojnl.'.'.$kode->getRight($i, 4, 0);
					$attr['nojnl']  	= $nojnl;
					$attr['kd_sub']  	= $kd_sub;
					$attr['nm_sub']  	= $dSub[$kd_sub]->nm_sub;
					$attr['kdac']  		= $kdac;
					$attr['nmac']  		= $dAcc[$kdac]->nmac;
					$attr['nota']  		= $nota;
					$attr['kd_inv']  	= $inv;
					$attr['nm_inv']  	= $dInv[$inv]->nm_inv;
					$attr['ket'] 		= $r['rem'];
					$attr['bg_no'] 		= $r['bg'];
					$attr['bg_tgl'] 	= $r['bgdate'];
					$attr['debet']  	= $debet;
					$attr['credit']  	= $credit;
					$attr['masuk']  	= $masuk;
					$attr['keluar']  	= $keluar;
					
					$sql = $this->em->addInsert("umd");
					$sql = $this->em->addAttrIns($attr);
					$qb[] = $sql;
				}
			}
			
		}
		
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		$this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function uploadAcrIn($data, $file = false)
	{
		$gud = array();
		foreach($data as $r){
			$id = $r['acc'];
			$gud[$id] = $id;
		}
		
		$qb = array();
		$gudang = array();
		foreach($gud as $r){
			$acc = $this->em->getFind("acc", "kdac, nmac", array("kdac = '$r'"));

			if(empty($acc->kdac)){
				$acc->kdac = 'NONE';
				$acc->nmac = 'NONE';
			}
			$gudang[$acc->kdac] = $r->nmac;
			
			$qb[] = $this->delete("gudang", array("kd_gudang = '".$acc->kdac."'"));
			
			$attr = array();
			$attr['kd_gudang'] 	= $acc->kdac;
			$attr['nm_gudang'] 	= $acc->nmac;
			$attr['kdac'] 		= $acc->kdac;
			$attr['nmac'] 		= $acc->nmac;
			
			$sql = $this->em->addInsert("gudang");
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
		}
		
		$dte = new Date;
		$exp = explode("_", $file);
		$bln = $exp[1];
		$tgl = '20'.substr($bln, 2, 2).'-'.substr($bln, 0, 2).'-01';
		// $tgl = $dte->getPenguranganTanggal($tgl, 0, 1, 0);
		
		$alias = "RIN-".date("ym", strtotime($tgl));
		
		$atr = array();
		$atr['chtime'] 	= date("Y-m-d H:i:s");
		$atr['tgl'] 	= $tgl;
		$atr['alias'] 	= 'RIN';
		$atr['kd_cabang'] = 'HO';
		$atr['nm_cabang'] = 'HEAD OFFICE';

		foreach($data as $r){
			$gud = $r['acc'];
			
			$nojnl = $alias.'-'.$gud.'-'.$r['inv'];
			
			$attr = $atr;
			$attr['nojnl'] 		= $nojnl;
			$attr['kd_gudang'] 	= $gud;
			$attr['kd_inv'] 	= $r['inv'];
			$attr['awal'] 		= $r['vlast'];
			$attr['qtya'] 		= $r['qlast'];
			
			$qb[] = $this->delete("acr", array("nojnl = '".$nojnl."'"));
		
			$sql = $this->em->addInsert("acr");
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
		}
		
		ini_set("display_errors", "On");
		// $save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		// $this->vardump($qb);
		$this->layout = "null";
		return $save;
	}

	public function uploadAcrSub($data, $file = false)
	{
		$qb = array();
		
		$dte = new Date;
		$exp = explode("_", $file);
		$bln = $exp[1];
		$tgl = '20'.substr($bln, 2, 2).'-'.substr($bln, 0, 2).'-01';
		// $tgl = $dte->getPenguranganTanggal($tgl, 0, 1, 0);
		
		$alias = "RSB-".date("ym", strtotime($tgl));
		
		$atr = array();
		$atr['chtime'] 	= date("Y-m-d H:i:s");
		$atr['tgl'] 	= $tgl;
		$atr['alias'] 	= 'RSB';
		$atr['kd_cabang'] = 'HO';
		$atr['nm_cabang'] = 'HEAD OFFICE';

		foreach($data as $r){
			$nojnl = $alias.'-'.$r['acc'].'-'.$r['sub'];
			$attr = $atr;
			$attr['nojnl'] 		= $nojnl;
			$attr['kdac'] 		= $r['acc'];
			$attr['kd_sub'] 	= $r['sub'];
			$attr['awal'] 		= $r['last'];
			
			$qb[] = $this->delete("acr", array("nojnl = '".$nojnl."'"));
			
			$sql = $this->em->addInsert("acr");
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
		}
		
		ini_set("display_errors", "On");
		// $save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		// $this->vardump($qb);
		$this->layout = "null";
		return $save;
	}
	
	public function startUpload()
	{
		$awl = '2023-01-01';
		$akr = date("Y-m-01");
		// $akr = '2018-03-01';
		
		if(!empty($_GET['awal'])){
			$awl = $_GET['awal'];
		}
		if(!empty($_GET['akhir'])){
			$akr = $_GET['akhir'];
		}
		if(!empty($_GET['tgl'])){
			$tgl = $_GET['tgl'];
		}else{
			$tgl = $awl;
		}
		
		if(empty($_GET['no'])){
			$no = 0;
		}else{
			$no = $_GET['no'];
		}
		
		$db = array();
		$db[] = array('dbf' => 'msk', 'func' => 'uploadBeli');
		$db[] = array('dbf' => 'msd', 'func' => 'uploadBeliDet');
		$db[] = array('dbf' => 'rms', 'func' => 'uploadRetBel');
		$db[] = array('dbf' => 'rmd', 'func' => 'uploadRetBelDet');
		$db[] = array('dbf' => 'klr', 'func' => 'uploadJual');
		$db[] = array('dbf' => 'kld', 'func' => 'uploadJualDet');
		$db[] = array('dbf' => 'rkl', 'func' => 'uploadJualRet');
		$db[] = array('dbf' => 'rkd', 'func' => 'uploadJualRetDet');

		$db[] = array('dbf' => 'kms', 'func' => 'uploadKasBankMasuk');
		$db[] = array('dbf' => 'kmd', 'func' => 'uploadUmd');
		$db[] = array('dbf' => 'kkl', 'func' => 'uploadKasBankKeluar');
		$db[] = array('dbf' => 'kkd', 'func' => 'uploadUmd');
		$db[] = array('dbf' => 'umh', 'func' => 'uploadJum');
		$db[] = array('dbf' => 'umd', 'func' => 'uploadUmd');
		$db[] = array('dbf' => 'dfa', 'func' => 'uploadPenyusutan');
		$db[] = array('dbf' => 'dfd', 'func' => 'uploadUmd');
		$db[] = array('dbf' => 'rin', 'func' => 'uploadAcrIn');
		$db[] = array('dbf' => 'rsb', 'func' => 'uploadAcrSub');

		$max = count($db);
		if($no < $max){

			$file = $db[$no]['dbf'].'_'.date("my", strtotime($tgl));

			echo "<pre>";
			var_dump($file);

			ini_set("memory_limit", "2048M");
			// ini_set("display_errors", "On");
			$tmp = $this->bacaDbf($file);

			$func = $db[$no]['func'];
			$save = $this->$func($tmp, $file);

			$nom  = $no + 1;
			$next = $tgl;
		}else{
			$file = 'none';
			$nom  = 0;
			$date = new Date;
			$next = $date->getPenjumlahanTanggal($tgl, 1, 0, 0);
		}

		$url = $this->baseUrl.'up/startUpload/&no='.$nom.'&tgl='.$next.'&awal='.$awl.'&akhir='.$akr;
		
		
		if($tgl < $akr){
			// echo "<script>window.location = '$url' </script>";
			echo "<script>makeLoad('".$url."');</script>";
		}else{
			// echo "<p>Proses Upload Selesai ".$akr."</p>";
			echo "<script>$('#_prosesDis').html('Proses Upload Selesai ".$akr."');</script>";
		}

		
	}
	
	public function uploadDbf()
	{
		
		$awl = '2023-01-01';
		// $akr = date("Y-m-01");
		$akr = '2023-12-01';
		
		if(!empty($_GET['awal'])){
			$awl = $_GET['awal'];
		}
		if(!empty($_GET['akhir'])){
			$akr = $_GET['akhir'];
		}
		if(!empty($_GET['tgl'])){
			$tgl = $_GET['tgl'];
		}else{
			$tgl = $awl;
		}
		$url = $this->baseUrl.'up/startUpload/&no=0&tgl='.$awl.'&awal='.$awl.'&akhir='.$akr;
		
		$dis = "
			<style type='text/css'>
			#_lokUpload{
				width: 300px;
				height: 300px;
				border: 2px solid #cfcfcf;
				overflow: auto;
				padding: 10px;
			}
			#_prosesAwl{
				width: 300px;
				height: 300px;
				border: 2px solid #cfcfcf;
				overflow: auto;
				padding: 10px;
			}
			p{
				margin: 0px;
			}
			</style>
			<table>
				<tr>
					<td>
						<div id='_lokUpload'>
							<div id='_prosesDis'></div>
						</div>
					</td>
					<td style='width: 300px' id=''>
						<div id='_prosesAwl'>Proses...</div>
					</td>
				</tr>
			</table>
			<script type='text/javascript' src='".$this->baseUrl."/assets/js/jquery-1.7.2.min.js' ></script>
			<script type='text/javascript'>
			makeLoad = function(_url){

				$.ajax({
				type: 'POST',
					url: _url,
					data: $(this).serialize(),
					success: function(data) {
						$('#_prosesAwl').html(data);
					}
				})
			}
			makeLoad('".$url."');
			</script>
			";
		
		echo $dis;
	}
	
	public function postingHpp()
	{
		$tgl = $_GET['tgl'];
		if(empty($_GET['tgl'])){
			$tgl = date("Y-m-d");
		}
		
		echo "<b>Persiapan Posting Tanggal ".$tgl.':</b> <br>';
		
		// ini_set("display_errors", "On");
		$this->postingUlang('nbl', "nbl", 'nonbl', array("tgl >= '2020-01-01'", "control = '1'", "tgl >= '".$tgl." 00:00:00'", "tgl <= '".$tgl." 23:59:59'"));
		$this->postingUlang('njl', "njl", 'nonjl', array("tgl >= '2020-01-01'", "control = '1'", "tgl >= '".$tgl." 00:00:00'", "tgl <= '".$tgl." 23:59:59'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("tgl >= '2020-01-01'", "control = '1'", "tgl >= '".$tgl." 00:00:00'", "tgl <= '".$tgl." 23:59:59'"));
		
		$posh = $this->em->getFindBy("posh", "no_pos", array("tgl >= '2020-01-01'", "tgl >= '".$tgl." 00:00:00'", "tgl <= '".$tgl." 23:59:59'"), "no_pos");
		foreach($posh as $r){
			$save = $this->postingUlang('posh', "hppPos", 'no_pos', array("control = '1'", "no_pos = '".$r->no_pos."'"), false);
			var_dump($save);
			echo "<br>";
		}
		
		
		if(!empty($_GET['akhir'])){
			$saiki = $_GET['akhir'];
		}else{
			$saiki = date("Y-m-d");
		}
		if($tgl < $saiki){
			$date = new Date;
			$next = $date->getPenjumlahanTanggal($tgl, 0, 1, 0);
			$url  = $this->baseUrl.'up/postingHpp/&tgl='.date("Y-m-d", strtotime($next)).'&akhir='.$saiki;
			echo "
				<script>
					window.location = '$url';
				</script>
			";
		}else{
			echo "Proses Selesai Per Tanggal ".$saiki;
		}
	}
	
	public function postingUlang($table, $post, $pk, $fil = array())
	{
		$posting = new Posting;
		
		$filD = array();
		$filD[] = "control = '1'";
		foreach($fil as $r){
			$filD[] = $r;
		}
		
		$data = $this->em->getFindBy($table, $pk, $filD, "tgl, $pk");
		$no = 1;
		foreach($data as $r){
			$posting->$post($this->set, $r->$pk);
			echo $no++.' / '.count($data).' ';
			echo($r->$pk);
			echo "<br>";
		}
	}
	
	public function upAcr()
	{
		$sql = $this->em->addSelect('a.nojnl, a.kd_gudang, b.nm_gudang, c.nmac, d.nm_inv, e.nm_sub');
		$sql = $this->em->getFrom('acr', 'a');
		$sql = $this->em->addLeftJoin('gudang', 'b', array("a.kd_gudang = b.kd_gudang"));
		$sql = $this->em->addLeftJoin('acc', 'c', array("a.kdac = c.kdac"));
		$sql = $this->em->addLeftJoin('inv', 'd', array("a.kd_inv = d.kd_inv"));
		$sql = $this->em->addLeftJoin('sub', 'e', array("a.kd_sub = e.kd_sub"));
		$sql = $this->em->getWhere("e.nm_sub is not null");
		$sql = $this->em->andWhere("e.nm_sub != ''");
		$sql = $this->em->andWhere("(a.nm_sub = '' or a.nm_sub is null)");
		$sql = $this->em->getLimit(0, 10000);
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		
		foreach($tmp as $r){
			$attr = array();
			$attr['nm_gudang'] 	= $r->nm_gudang;
			$attr['nmac'] 		= $r->nmac;
			$attr['nm_inv'] 	= $r->nm_inv;
			$attr['nm_sub'] 	= $r->nm_sub;
			$sql = $this->em->addUpdate("acr");
			$sql = $this->em->addAttrUp($attr);
			$sql = $this->em->getWhere("nojnl = '".$r->nojnl."'");
			$qb[] = $sql;
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		var_dump($qb);
		// echo "<script>window.location = '';</script>";
	}
	
	public function Index()
	{
		$this->postingUlang('umh', "jurnal", 'nojnl', array("(nojnl = 'KK -2304-00016A')"));
		
		$this->layout = "null";
	}
	
	public function Postingulangall()
	{
		$tgl = $_GET['tgl'];
		if(empty($_GET['tgl'])){
			$tgl = date("Y-m-d");
		}
		echo "Proses Posting Per Tanggal ".$tgl.'<br>';


		$date = new Date;
		$next = $date->getPenjumlahanTanggal($tgl, 0, 1, 0);
		

		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'BJM'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'KMS'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'BKK'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'KMM'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'BMS'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'BBK'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'GMS'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'BGK'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('umh', "jurnal", 'nojnl', array("alias = 'JPS'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('nbl', "nbl", 'nonbl', array("jenis = '1'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('ret', "ret", 'noret', array("jenis = '1'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('njl', "njl", 'nonjl', array("jenis = '1'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('ret', "rej", 'noret', array("jenis = '2'", "tgl >= '$tgl'", "tgl < '$next'"));
		$this->postingUlang('so', "so", 'noso', array("jenis = '1'", "tgl >= '$tgl'", "tgl < '$next'"));


		if(!empty($_GET['akhir'])){
			$saiki = $_GET['akhir'];
		}else{
			$saiki = date("Y-m-d");
		}
		if($tgl < $saiki){
			$url  = $this->baseUrl.'up/Postingulangall/&tgl='.date("Y-m-d", strtotime($next)).'&akhir='.$saiki;
			echo "
				<script>
					window.location = '$url';
				</script>
			";
		}else{
			echo "Proses Selesai Per Tanggal ".$saiki;
		}
	}
	
	public function uploadSaw()
	{
		// $this->sawStokInv();
		$this->sawRhp();
		// $this->sawNeraca();
	}
	
	public function sawStokInv()
	{
		$tmp = $this->bacaDbf("rin_1123");

		$qb 	= array();
		$tgl 	= '2023-01-01';
		$date 	= new Date;
		$mun  	= $date->getPenguranganTanggal($tgl, 0, 1, 0);
		$nojnl 	= 'STOK-'.date("dmy", strtotime($tgl));

		$sql = $this->em->addDelete("acd");
		$sql = $this->em->getWhere("nojnl = '$nojnl'");
		$qb[] = $sql;
		
		$attr = array();
		$attr['nojnl'] 		= $nojnl;
		$attr['tgl'] 		= $mun;
		$attr['kd_cabang'] 	= 'HO';
		$attr['alias'] 		= 'SAW';
		
		$gAcc = array();
		foreach($tmp as $r){
			$atr = $attr;
			$atr['kd_gudang'] = $r['acc'];
			$atr['kdac'] 	= $r['acc'];
			$atr['kd_inv'] 	= $r['inv'];
			$atr['masuk'] 	= 0;
			$atr['keluar'] 	= 0;
			$atr['debet'] 	= 0;
			$atr['credit'] 	= 0;
			$nil = $r['vlast'];
			if($nil > 0){
				$atr['debet'] 	= $nil;
			}else{
				$atr['credit'] 	= abs($nil);
			}
			$qty = $r['qlast'];
			if($qty > 0){
				$atr['masuk'] 	= $qty;
			}else{
				$atr['keluar'] 	= abs($qty);
			}
			$sql = $this->em->addInsert("acd");
			$sql = $this->em->addAttrIns($atr);
			$qb[] = $sql;
			
			$acc = $r['acc'];
			$gAcc[$acc] = $gAcc[$acc] + $nil;
		}
		foreach($gAcc as $acc => $nil){
			$atr = $attr;
			$atr['kdac'] 	= $acc;
			$atr['masuk'] 	= 0;
			$atr['keluar'] 	= 0;
			$atr['debet'] 	= 0;
			$atr['credit'] 	= 0;
			$nil = $nil;
			if($nil > 0){
				$atr['credit'] 	= $nil;
			}else{
				$atr['debet'] 	= abs($nil);
			}
			$sql = $this->em->addInsert("acd");
			$sql = $this->em->addAttrIns($atr);
			$qb[] = $sql;
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		var_dump($qb);
	}
	
	public function sawRhp()
	{
		$tmp = $this->bacaDbf("rhp_1123");

		$qb 	= array();
		$tgl 	= '2023-01-01';
		$date 	= new Date;
		$mun  	= $date->getPenguranganTanggal($tgl, 0, 1, 0);
		$nojnl 	= 'RHP-'.date("dmy", strtotime($tgl));

		$sql = $this->em->addDelete("acd");
		$sql = $this->em->getWhere("nojnl = '$nojnl'");
		$qb[] = $sql;
		
		$attr = array();
		$attr['nojnl'] 		= $nojnl;
		$attr['tgl'] 		= $mun;
		$attr['kd_cabang'] 	= 'HO';
		$attr['alias'] 		= 'SAW';
		
		$gAcc = array();
		foreach($tmp as $r){
			if($r['date'] < '2023-01-01'){
				$atr = $attr;
				$atr['nota'] 	= $r['nojnl'];
				$atr['nota_tgl']= $r['date'];
				$atr['nota_jt'] = $r['duedate'];
				$atr['kdac'] 	= $r['acc'];
				$atr['kd_sub'] 	= $r['sub'];
				$atr['ket'] 	= $r['remark'];
				$atr['debet'] 	= 0;
				$atr['credit'] 	= 0;
				$nil = $r['val'];
				if($r['dk'] == 'D'){
					$attr['alias'] 	= 'SPIU';
				}else{
					$attr['alias'] 	= 'SHUT';
				}
				if($nil > 0){
					if($r['dk'] == 'D'){
						$atr['debet'] 	= $nil;
					}else{
						$atr['credit'] 	= abs($nil);
					}
				}else{
					if($r['dk'] == 'K'){
						$atr['credit'] 	= abs($nil);
					}else{
						$atr['debet'] 	= abs($nil);
					}
				}
				$sql = $this->em->addInsert("acd");
				$sql = $this->em->addAttrIns($atr);
				$qb[] = $sql;
				
				$acc = $r['acc'];
				$gAcc[$acc] = $gAcc[$acc] + ($atr['debet'] - $atr['credit']);
			}
		}
		foreach($gAcc as $acc => $nil){
			$atr = $attr;
			$atr['kdac'] 	= $acc;
			$atr['masuk'] 	= 0;
			$atr['keluar'] 	= 0;
			$atr['debet'] 	= 0;
			$atr['credit'] 	= 0;
			$nil = $nil;
			if($nil > 0){
				$atr['credit'] 	= $nil;
			}else{
				$atr['debet'] 	= abs($nil);
			}
			$sql = $this->em->addInsert("acd");
			$sql = $this->em->addAttrIns($atr);
			$qb[] = $sql;
		}
		
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		var_dump($qb);
	}
	
	public function sawNeraca()
	{
		$tmp = $this->bacaDbf("acc_0123");

		$qb 	= array();
		$tgl 	= '2023-01-01';
		$date 	= new Date;
		$mun  	= $date->getPenguranganTanggal($tgl, 0, 1, 0);
		$nojnl 	= 'SAW-'.date("dmy", strtotime($tgl));

		$sql = $this->em->addDelete("acd");
		$sql = $this->em->getWhere("tgl < '$tgl'");
		$qb[] = $sql;

		$sql = $this->em->addDelete("acd");
		$sql = $this->em->getWhere("nojnl = '$nojnl'");
		$qb[] = $sql;
		
		$attr = array();
		$attr['nojnl'] 		= $nojnl;
		$attr['tgl'] 		= $mun;
		$attr['kd_cabang'] 	= 'HO';
		$attr['alias'] 		= 'SAW';
		
		$sel = 0;
		foreach($tmp as $r){
			$nil = $r['awal'];
			if($nil <> 0 and $r['detil'] == true){
				$atr = $attr;
				$atr['kdac'] 	= $r['kdac'];
				$atr['nmac'] 	= $r['nmac'];
				$atr['masuk'] 	= 0;
				$atr['keluar'] 	= 0;
				$atr['debet'] 	= 0;
				$atr['credit'] 	= 0;
				if($nil > 0){
					$atr['debet'] 	= $nil;
				}else{
					$atr['credit'] 	= abs($nil);
				}
				
				$sql = $this->em->addInsert("acd");
				$sql = $this->em->addAttrIns($atr);
				$qb[] = $sql;
				
				$sel = $sel + $nil;
			}
		}
		
		if($sel <> 0){
			$atr = $attr;
			$atr['kdac'] 	= '1.3.09.10';
			$atr['nmac'] 	= '';
			$atr['masuk'] 	= 0;
			$atr['keluar'] 	= 0;
			$atr['debet'] 	= 0;
			$atr['credit'] 	= 0;
			if($sel < 0){
				$atr['debet'] 	= abs($sel);
			}else{
				$atr['credit'] 	= abs($sel);
			}
			
			$sql = $this->em->addInsert("acd");
			$sql = $this->em->addAttrIns($atr);
			$qb[] = $sql;
		}

		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		var_dump($qb);
	}
	
	public function sub()
	{
		$tmp = $this->bacaDbf("SUB");

		$qb 	= array();
		$jenis 	= 12;
		
		$sql = $this->em->addDelete("sub");
		$sql = $this->em->getWhere("jenis = '$jenis'");
		$qb[] = $sql;
		
		$attr = array();
		$attr['jenis'] 		= $jenis;
		
		foreach($tmp as $r){
			if($r['group'] == '2'){
				$atr = $attr;
				$atr['kd_sub'] 	= $r['sub'];
				$atr['nm_sub'] 	= $r['name'];
				$atr['cp'] 		= $r['person'];
				$atr['alamat'] 	= $r['address'];
				$atr['kota'] 	= $r['city'];
				$atr['npwp'] 	= $r['npwp'];
				$atr['credit'] 	= $r['max'];
				$atr['telp'] 	= $r['phone'];
				
				$sql = $this->em->addInsert("sub");
				$sql = $this->em->addAttrIns($atr);
				$qb[] = $sql;
			}
		}
		
		ini_set("display_errors", "On");
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		var_dump($qb);
	}
	
	public function acc()
	{
		$tmp = $this->bacaDbf("acc");

		$qb 	= array();
		$jenis 	= 12;
		
		$sql = $this->em->addDelete("acc");
		$qb[] = $sql;
		
		$attr = array();
		
		foreach($tmp as $r){
				$atr = $attr;
				$atr['kdac'] 	= $r['kdac'];
				$atr['nmac'] 	= $r['nmac'];
				$atr['induk'] 	= $r['induk'];
				$atr['detil'] 	= $r['detil'];
				$atr['level'] 	= $r['level'];
				$atr['grup'] 	= $r['group'];
				
				if($atr['detil'] == TRUE){
					$atr['detil'] = 'T';
				}else{
					$atr['detil'] = 'F';
				}
				
				$sql = $this->em->addInsert("acc");
				$sql = $this->em->addAttrIns($atr);
				$qb[] = $sql;
		}
		
		ini_set("display_errors", "On");
		$save = $this->em->getFlush($qb);
		echo "<pre>";
		var_dump($save);
		var_dump($qb);
	}
}