<?php
$message 	 	= array();
$Act         	= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$pilih			= isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
$pilih_reg		= isset($_REQUEST['pilih_reg']) ? $_REQUEST['pilih_reg'] : '';
$tbalance		= isset($_REQUEST['tbalance']) ? $_REQUEST['tbalance'] : 0;
#Proses
if($Act=="GET"){
	$data 	= new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
	$baris 	= $data->rowcount($sheet_index=0);
	for ($i=2; $i<=$baris; $i++){
	  $message = array();
	  $no 		 		= $data->val($i, 1);
	  $jurnal_no 		= $data->val($i, 2);
	  $Jurnal_date 		= date("Y-m-d",strtotime($data->val($i,3)));
	  $jurnal_invno 	= $data->val($i, 4);
	  $jurnal_type 		= $data->val($i, 5);
	  $jurnal_suppcode  = $data->val($i, 6);
	  $jurnal_suppname 	= $data->val($i, 7);
	  $jurnal_ptype  	= $data->val($i, 8);
	  $jurnal_currency  = $data->val($i, 9);
	  $jurnal_rate		= $data->val($i, 10);
	  $jurnal_duedate	= date("Y-m-d",strtotime($data->val($i,11)));
	  $jurnal_post		= $data->val($i, 12);
	  $jurnald_acctno 	= $data->val($i, 13);
	  $jurnald_acctname = $data->val($i, 14);
	  $jurnald_desc 	= $data->val($i, 15);	 
	  $jurnald_amount 	= $data->val($i, 16); 
	  
	  if(!empty($no)){
		  mysql_query("INSERT INTO jurnal_temp SET jurnal_no='$jurnal_no', Jurnal_date='$Jurnal_date', jurnald_status=50, jurnald_inputdate=now(), jurnald_inputby='$user_id', 
		  			   jurnal_suppcode='$jurnal_suppcode', jurnal_suppname='$jurnal_suppname', jurnal_ptype='$jurnal_ptype', jurnal_currency='$jurnal_currency', 	
					   jurnal_invno='$jurnal_invno', jurnal_type='$jurnal_type', jurnal_post='$jurnal_post', jurnald_acctno='$jurnald_acctno', jurnal_rate='$jurnal_rate', 
					   jurnal_duedate='$jurnal_duedate', jurnald_acctname='$jurnald_acctname', jurnald_desc='".mysql_real_escape_string($jurnald_desc)."', jurnald_amount='$jurnald_amount'"); 
	  }
	}
}
if($Act=="PROCESS"){
	if(!empty($pilih)){	
		#cek balance jurnal
		$tdebit =0; $tkredit=0; $tbal=0;
		foreach ( $pilih as $i => $val){
			$debit    = $_REQUEST['debit'][$val];
			$kredit   = $_REQUEST['kredit'][$val];
			$tdebit   = $tdebit + $debit;
			$tkredit  = $tkredit + $kredit;
		}
		$tbal = number_format($tkredit,2,'.','')-number_format($tdebit,2,'.','');
		#echo "test:$tdebit : $tkredit = $tbal";exit;		
		if($tbal==0){
			foreach ( $pilih as $i => $val){
				mysql_query("UPDATE jurnal_temp SET jurnald_status=500 WHERE id='$val' ");
			}
			require "add_upload_jurnal.php";
		}else{
			$message[] = "Jurnal tidak balance ($tdebit : $tkredit)";	
			$status    = "NG";
		}
	}else{
		require "add_upload_jurnal.php";
	}	
}elseif($Act=="INV"){
	if($tbalance ==0){
		#Insert header jurnal
		$qh=mysql_query("SELECT * FROM jurnal_temp WHERE jurnald_status =500 AND jurnald_inputby='$user_id' GROUP BY jurnal_no ORDER BY jurnal_no ASC");		
		while($dh=mysql_fetch_array($qh)){
			$supid        = mysql_fetch_array(mysql_query("SELECT a.id, a.kode, a.nama, snpwp, CONCAT_WS(' - ', a.kode, a.nama) codename, a.tipe FROM
				(SELECT id, sup_code kode, sup_name nama, sup_npwp snpwp, 'SUP' tipe FROM supplier WHERE sup_status=10
				UNION
				SELECT id, cust_code kode, cust_name nama, cust_npwp snpwp, 'CUS' tipe FROM customer WHERE cust_status=10) a
				WHERE a.kode='$dh[jurnal_suppcode]' ORDER BY a.nama ASC"));
			$jamt     = mysql_fetch_array(mysql_query("SELECT sum(jurnald_amount) amt FROM jurnal_temp WHERE jurnald_status =500 AND jurnal_no='$dh[jurnal_no]'
							AND upper(jurnal_post)='DEBIT'"));
			if($dh['jurnal_currency']=="IDR"){
				$j_amount_usd = $jamt['amt']/$dh['jurnal_rate'];
				$j_amount     = $jamt['amt'];
			}else{
				$j_amount_usd = $jamt['amt'];
				$j_amount     = $jamt['amt']*$dh['jurnal_rate'];
			}		
			#Insert jurnal Headar
			mysql_query("INSERT INTO jurnal SET jurnal_type='$dh[jurnal_type]', jurnal_no='$dh[jurnal_no]', jurnal_date='$dh[jurnal_date]', jurnal_idsupp='$supid[id]', 
					  jurnal_ptype='$dh[jurnal_ptype]', jurnal_invno='$dh[jurnal_invno]', jurnal_currency='$dh[jurnal_currency]', jurnal_duedate='$dh[jurnal_duedate]', 
					  jurnal_rate='$dh[jurnal_rate]', jurnal_amount='$j_amount', jurnal_amount_usd='$j_amount_usd', jurnal_idpemasok='$supid[snpwp]',
					  jurnal_suppname='$supid[nama]', jurnal_inputby='$user_id', jurnal_inputdate='$datetime', jurnal_status=17, jurnal_menu='UMUM'");
			#Aktivitas User
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi Jurnal No. $dh[jurnal_no]', user_date='$date', user_time='$time'");
			$status = 1;
		}	
		//exit;
		if($status == 1){
			$qd = mysql_query("SELECT * FROM jurnal_temp WHERE jurnald_status =500 AND jurnald_inputby='$user_id' ORDER BY jurnal_no ASC");
			while($dd = mysql_fetch_array($qd)){
				if(strtoupper($dd['jurnal_post'])=="DEBIT"){
					$jurnald_status =10;
				}else{ $jurnald_status =20; }
				if($dd['jurnal_currency']=="IDR"){
					$jurnald_amount_usd = $dd['jurnald_amount']/$dd['jurnal_rate'];
					$jurnald_amount     = $dd['jurnald_amount'];
				}else{
					$jurnald_amount_usd = $dd['jurnald_amount'];
					$jurnald_amount     = $dd['jurnald_amount']*$dd['jurnal_rate'];
				}
				#Insert Detail
				mysql_query("INSERT INTO jurnal_detail SET jurnal_no='$dd[jurnal_no]', jurnald_acctno='$dd[jurnald_acctno]', jurnald_status='$jurnald_status',
							 jurnald_desc='".mysql_real_escape_string($dd['jurnald_desc'])."', jurnald_amount='$jurnald_amount', jurnald_amount_usd='$jurnald_amount_usd', 
							 jurnald_inputby='$user_id', jurnald_inputdate='$datetime'");
				
			}
			$status = 2;
		}
		//exit;
		if($status == 2){	
			#update status log
			mysql_query("DELETE FROM jurnal_temp WHERE jurnald_status =500 AND jurnald_inputby='$user_id'");
			#update status log
			mysql_query("UPDATE jurnal SET jurnal_status=20 WHERE jurnal_status=17 AND jurnal_inputby='$user_id'");
			$message[] = "Data Sukses dikirim.";	
			$status    = "OK";
		}	
	}else{
		$message[] = "Jurnal tidak balance : $tbalance";	
		$status    = "NG";
	}
}elseif($Act=="REG"){
	if(!empty($pilih_reg)){	
		foreach ( $pilih_reg as $i => $val)
		{
			$kd_barang   = $_REQUEST['kd_barang'][$val];
			$nm_barang   = $_REQUEST['nm_barang'][$val];
			$satuan      = $_REQUEST['satuan'][$val];
			$kd_kategori = $_REQUEST['kd_kategori'][$val];
			#Insert master barang
			if($kd_kategori != "BLANK"){
				#Update po_duedate branag ditemp
				mysql_query("UPDATE jurnal_temp SET jurnal_type='$kd_barang', jurnal_suppcode='$nm_barang', jurnal_suppname='$satuan' WHERE id='$val'");
				#Insert ke master barang				
				mysql_query("INSERT INTO product SET prod_code='$kd_barang', prod_name='$nm_barang', prod_sat='$satuan', prod_cat='$kd_kategori'");
				#Aktivitas user					
				mysql_query("INSERT INTO user_activity SET user_id='".$_SESSION['USER_ID']."', user_act='Menambahkan data barang - $kd_barang', 
				             user_date='$date', user_time='$time'");
			}
		}
		require "add_upload_jurnal.php";
	}else{
		require "add_upload_jurnal.php";
	}	
}elseif($Act=="DEL_TEMP"){
	#update status
	mysql_query("UPDATE jurnal_temp SET jurnald_status=100 WHERE jurnald_status=50 AND jurnald_inputby='$user_id'");
	require "add_upload_jurnal.php";
}elseif($Act=="DELDET_TEMP"){
	#update status
	mysql_query("UPDATE jurnal_temp SET jurnald_status=50 WHERE id='".$_REQUEST['id_temp']."' ");
	require "add_upload_jurnal.php";
}elseif(($Act=="ADD") || ($Act=="GET") || ($Act=="TEMP") ){
	require "add_upload_jurnal.php";
}elseif($Act=="CANCEL" || $Act=="CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Transaksi-Jurnal'>";	
	exit;
}
#Messages
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil) { 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			alert(pesan);
		</script>
	<?php  
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Transaksi-Jurnal'>";	
		exit;
	}else{
		echo "<meta http-equiv='refresh' content='0; url=?page=Proses-Upload-Jurnal&Act=TEMP'>";
	}
}
?>