<html>
<head>
<script type="text/javascript">
	function setTrColor(obj, color)
	{
		obj.style.backgroundColor=color;
		obj.onmouseout = function(){
				obj.style.backgroundColor=''; 
		}
	}
	$(document).ready(function(){
		$("#start_date").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#end_date").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});	
	function cancel_click(){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=CANCEL';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function get_click(){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=GET';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function temp_click(){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=TEMP';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function process_click(){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=PROCESS';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function inventory_click(){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=INV';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function reg_click(){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=REG';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function delete_click(){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=DEL_TEMP';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function deldettemp_click(id, id_header, no_urut){
		form.action ='?page=Proses-Upload-Pengeluaran&Act=DELDET_TEMP&id_temp='+id+'&tmp_id_header='+id_header+'&tmp_no_urut='+no_urut;
		form.method = 'post';
		form.submit();			
		return true;
	}
	$(document).ready(function(){
		var inc = {
			source: "json_inc.php",					
			select: function(event, ui){
				$("#kd_barang").val(ui.item.kd_barang); 
				$("#nm_barang").val(ui.item.nm_barang); 
				$("#satuan").val(ui.item.satuan); 
			},
			minLength: 1
		};
		$("#kd_barang").autocomplete(inc);		
	});
	function checkAll()
	{
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i< inputs.length; i++) {
			if (inputs[i].type == 'checkbox') {
				inputs[i].checked =true;
			}
		}
	}
	function uncheckAll()
	{
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i< inputs.length; i++) {
			if (inputs[i].type == 'checkbox') {
				inputs[i].checked =false;
			}
		}
	}
	function search_click(){
	    var jenis_dokpab = $("#jenis_dokpab").val();
	    var jdp = jenis_dokpab.replace(' ', '%20');
		var start = document.getElementById("start_date").value;
		var end = document.getElementById("end_date").value;
		if(jdp=="BLANK"){
			alert("Jenis dokumen pabean harus dipilih terlebih dahulu");
			form.jenis_dokpab2.focus();
			return false;
		}else if(start==""){
			alert("Tanggal Awal tidak boleh kosong");
			form.start_date.focus();
			return false;
		}else if(end==""){
			alert("Tanggal akhir tidak boleh kosong");
			form.end_date.focus();
			return false;
		}else{
			form.action ='?page=Proses-Upload-Pengeluaran&Act=SEARCH';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function detail_dokumen_click(id_header,no_urut,status){
		window.open("popup/detail_dokumen_asal.php?id_header="+id_header+"&no_urut="+no_urut+"&status="+status,"mywindow","menubar=1,scrollbars=1,resizable=1,width=1050,height=1000");
	}
	function excel_click(){
		form.action ='./transaksi/interface/excel_temp_bc25.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function template_click(){
		form.action ='./transaksi/interface/template_upload_pengeluaran.php';
		form.method = 'post';
		form.submit();		
		return true;
	}
</script>
</head>
<form action="#" method="post" name="form" target="_self" enctype="multipart/form-data">
<table>
	<tr>
		<th colspan="2">UPLOAD DATA PENGELUARAN</th>
	</tr>
	<tr>
		<td>Masukan Dokumen :
			<input name="userfile" type="file"> 
		</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="button" name="get" value="GET" style="cursor:pointer" onClick="get_click();">
			<input type="button" name="temp" value="TEMP" style="cursor:pointer" onClick="temp_click();">
			<input type="button" name="process" value="PROCESS" style="cursor:pointer" onClick="process_click();">
			<input type="button" name="inventory" value="SEND TO INVENTORY" style="cursor:pointer" onClick="inventory_click();">
			<input type="button" name="register" value="REG. NEW CODE" style="cursor:pointer" onClick="reg_click();">
			<input type="button" name="delete" value="DELETE" style="cursor:pointer" onClick="delete_click();">
			<input type="button" name="batal" value="CLOSE" style="cursor:pointer" onClick="cancel_click();" />
			<input type="button" name="excel" value="TEMPLATE" style="cursor:pointer" onClick="template_click();" />
		</td>
	</tr>
</table>
<!--GET-->
<?php
#Cek stock
$start_date2 = get_startdate2($inv);
$end_date2   = get_enddate2($inv);
$bulan2    	 = substr($start_date2,5,2);
$tahun2  	 = substr($start_date2,0,4);
if ($Act=="GET"){ ?>
	<div class="scroll">
	<table width="100%">
		<tr bgcolor="#CCCCCC">
			<td align="center">No</td>
			<td align="center">No Aju</td>
			<td align="center">No Urut</td>
			<td align="center">Jenis</td>
			<td align="center">Nomor</td>
			<td align="center">Tanggal</td>
			<td align="center">Penerima</td>
			<td align="center">Kode</td>
			<td align="center">Nama Barang</td>
			<td align="center">Satuan</td>
			<td align="center">Jumlah</td>
			<td align="center">Valuta</td>
			<td align="center">Nilai</td>
		</tr>
	<?php
	$no=1; $tqtyg=0; $tpriceg=0;
	$q=mysql_query("SELECT * FROM log_outgoing WHERE log_status=1000 AND log_user='$user_id' ORDER BY no_aju, no_urut ASC",$inv);
    while($d=mysql_fetch_array($q)){
		if($d['valuta']=="IDR"){$nilai = $d['nilai'];}else{$nilai = $d['non_rupiah'];}
		$tqtyg = $tqtyg + $d['jumlah'];
		$tpriceg = $tpriceg + $nilai;
	?>	
		<tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no;?>.</td>
			<td><?=$d['no_aju'];?></td>
			<td><?=$d['no_urut'];?></td>
			<td><?=$d['jenis_dokpab'];?></td>
			<td><?=$d['no_dokpab'];?></td>
			<td><?=$d['tgl_dokpab'];?></td>
			<td><?=$d['nm_penerima'];?></td>
			<td><?=$d['kode'];?></td>
			<td><?=$d['nm_barang'];?></td>
			<td><?=$d['satuan'];?></td>
			<td align="right"><?=number_format($d['jumlah'],2,'.',',');?></td>
			<td align="center"><?=$d['valuta'];?></td>
			<td align="right"><?=number_format($nilai,2,'.',',');?></td>
		</tr>		
	<?php $no++; } ?>
	<tr bgcolor="#CCFFFF">
		<td colspan="10">Total</td>
		<td align="right"><?=number_format($tqtyg,2,'.',',');?></td>
		<td>&nbsp;</td>
		<td align="right"><?=number_format($tpriceg,2,'.',',');?></td>
	</tr>
	</table>
	</div>
<?php }elseif(($Act=="TEMP") || ($Act=="REG") || ($Act=="SEARCH")){ ?>
	<div class="scroll">
	<table width="260%">
		<tr bgcolor="#CCCCCC">
			<td align="center">No</td>
			<td align="center">Pilih<br>
				<input type="radio" name="check" onClick="checkAll();">C
			    <input type="radio" name="check" onClick="uncheckAll();">U
			</td>			
			<td align="center">No Aju</td>	
			<td align="center">Tgl Aju</td>	
			<td align="center">No Urut</td>	
			<td align="center">Jenis</td>
			<td align="center">Nomor</td>
			<td align="center">Tanggal</td>				
			<td align="center">Nama Penerima</td>
			<td align="center">Kode Barang</td>
			<td align="center">Nama Barang</td>
			<td align="center">Tipe</td>	
			<td align="center">Ukuran</td>
			<td align="center">Satuan</td>	
			<td align="center">Kategori</td>			
			<td align="center" bgcolor="#66FF99">Stock</td>
			<td align="center">Jumlah</td>
			<td align="center">Valuta</td>
			<td align="center">Nilai</td>
			<td align="center">No Keluar</td>	
			<td align="center">Tgl Keluar</td>	
			<td align="center">No Invoice</td>	
			<td align="center">Tgl Invoice</td>	
			<td align="center">No Faktur</td>	
			<td align="center">Tipe Dokumen</td>
		</tr>
	<?php
	$no=0; $tqty=0; $tprice=0;
	$qr=mysql_query("SELECT * FROM log_outgoing WHERE log_status=1000 AND log_user='$user_id' ORDER BY no_aju, no_urut ASC",$inv);
    while($d=mysql_fetch_array($qr)){
		$no++;
		$kode = str_replace(' ', '',strtoupper($d['kode']));
		$vc = mysql_fetch_array(mysql_query("SELECT id,prod_code,prod_cat FROM product WHERE prod_code='$d[kode]' AND prod_cat<>'100'",$inv));
		$stock = get_stock($start_date2,$end_date2,$bulan2,$tahun2,$vc['id'],$vc['prod_cat'],$inv);		
		$val_date = get_validationdate2($d['tgl_dokpab'],$inv);
		$val_jns = mysql_fetch_array(mysql_query("SELECT doc_type FROM document_type WHERE doc_type='$d[jenis_dokpab]'"));
		if(!empty($val_jns['doc_type'])){
			$bgcolorjns ="#000000";
		}else{
			$bgcolorjns ="#FF6A6A";
		}
		if(empty($vc['prod_code'])){
			$s_kode ="NO";
			$bgcolor0 ="#FF6A6A";
		}else{
			$s_kode ="READY";
			$bgcolor0 ="#000000";
		}		
		/*if($d['status_dokpab']=="GATE OUT TPB" || $d['status_dokpab']=="SELESAI PROSES"){
			$bgcolor ="#000000";
		}else{
			$bgcolor ="#FF6A6A";
		}*/
		if($d['jumlah'] > $stock){
			$colorstock ="#FF6A6A";			
		}else{
			$colorstock ="#66FF99";
		}
		if($val_date=="NO"){
			$colord ="#FF0000";
		}else{
			$colord ="#000000";
		}
		if($d['jumlah'] <=0){
			$bgcolorjml ="#FF6A6A";
		}else{
			$bgcolorjml ="#000000";
		}
		$tqty = $tqty + $d['jumlah'];		
		#validasi item komplit/tidak/no aju
		$val_item = get_valoutgoing_upl($start_date2,$end_date2,$bulan2,$tahun2,$d['no_dokpab'],$d['jenis_dokpab'],$inv);
		if($d['valuta']=="IDR"){$nilai = $d['nilai'];}else{$nilai = $d['non_rupiah'];}
		$tprice = $tprice + $nilai;
	?>	
	    <tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no;?>.</td>
			<td align="center">
				<?php if($val_item =="NO") { ?>
					&nbsp;
				<?php }else{ ?>
					<input type="checkbox" id="pilih[<?=$d['id'];?>]" name="pilih[<?=$d['id'];?>]" value="<?=$d['id'];?>">
				<?php }?>
			</td>
			<td align="center"><?=$d['no_aju'];?></td>
			<td align="center"><?=$d['tgl_aju'];?></td>
			<td align="center"><?=$d['no_urut'];?></td>
			<td align="center"><font color="<?=$bgcolorjns;?>"><?=$d['jenis_dokpab'];?></font></td>
			<td align="center"><?=$d['no_dokpab'];?></td>
			<td align="center"><font color="<?=$colord;?>"><?=$d['tgl_dokpab'];?></font></td>		
			<td align="left"><?=$d['nm_penerima'];?></td>
			<td>
				<?php if($s_kode =="NO"){ ?>
				<input type="hidden" name="pilih_reg[<?=$d['id'];?>]" value="<?=$d['id'];?>">
				<input type="text" name="kd_barang[<?=$d['id'];?>]" value="<?=$d['kode'];?>" size="15" style="background-color:<?=$bgcolor0;?>">
				<?php }else{ ?>
					<?=$d['kode'];?>
				<?php } ?>
			</td>
			<td>
				<?php if($s_kode =="NO"){ ?>
				<input type="text" name="nm_barang[<?=$d['id'];?>]" value="<?=$d['nm_barang'];?>" size="35" style="background-color:<?=$bgcolor0;?>">
				<?php }else{ ?>
					<?=$d['nm_barang'];?>
				<?php } ?>
			</td>
			<td>
				<?php if($s_kode =="NO"){ ?>
				<input type="text" name="tipe[<?=$d['id'];?>]" value="<?=$d['tipe'];?>" size="15" style="background-color:<?=$bgcolor0;?>">
				<?php }else{ ?>
					<?=$d['tipe'];?>
				<?php } ?>
			</td>
			<td>
				<?php if($s_kode =="NO"){ ?>
				<input type="text" name="ukuran[<?=$d['id'];?>]" value="<?=$d['ukuran'];?>" size="15" style="background-color:<?=$bgcolor0;?>">
				<?php }else{ ?>
					<?=$d['ukuran'];?>
				<?php } ?>
			</td>
			<td>
				<?php if($s_kode =="NO"){ ?>
				<input type="text" name="satuan[<?=$d['id'];?>]" value="<?=$d['satuan'];?>" size="4" style="background-color:<?=$bgcolor0;?>">
				<?php }else{ ?>
					<?=$d['satuan'];?>
				<?php } ?>
			</td>
			<td align="left">
				<?php if($s_kode =="NO"){ ?>
					<select name="kd_kategori[<?=$d['id'];?>]" style="background-color:<?=$bgcolor0;?>">	
						<option value="BLANK" selected="selected">PILIH</option>			
						<?php
						  $sql = mysql_query("SELECT * FROM product_category",$inv);
						  while ($data = mysql_fetch_array($sql)) {
							if ($data['id']== $kd_kategori) {
								$cek = " selected";
							} else { $cek=""; }
							echo "<option value='$data[id]' $cek>$data[prod_cat]</option>";
						  }
						  $sqlData ="";
						?>
					</select>
				<?php }else{ 
					$cat = mysql_fetch_array(mysql_query("SELECT b.prod_cat FROM product a INNER JOIN product_category b ON b.id=a.prod_cat 
					       WHERE prod_code='$d[kode]'",$inv));
					echo $cat['prod_cat'];	   
				} ?>
			</td>		
			<td align="right" bgcolor="<?=$colorstock;?>"><?=number_format($stock,2,'.',',');?></td>
			<td align="right"><font color="<?=$bgcolorjml;?>"><?=number_format($d['jumlah'],2,'.',',');?></font></td>
			<td align="center"><?=$d['valuta'];?></td>
			<td align="right"><?=number_format($nilai,2,'.',',');?></td>	
			<td align="center"><?=$d['no_sj'];?></td>
			<td align="center"><?=$d['tgl_sj'];?></td>
			<td align="center"><?=$d['no_invoice'];?></td>
			<td align="center"><?=$d['tgl_invoice'];?></td>
			<td align="center"><?=$d['no_do'];?></td>
			<td align="right"><?=$d['tipe_dokumen'];?></td>	
		</tr>	
		<?php } ?>	
		<tr bgcolor="#CCFFFF">
			<td colspan="16">Total</td>
			<td align="right"><?=number_format($tqty,2,'.',',');?></td>
			<td>&nbsp;</td>
			<td align="right"><?=number_format($tprice,2,'.',',');?></td>
			<td colspan="6">&nbsp;</td>
		</tr>
	</table>
	</div>
<?php }elseif(($Act=="PROCESS") || ($Act=="DELDET_TEMP")){ ?>
	<div class="scroll">
	<table width="100%">
		<tr bgcolor="#CCCCCC">
			<td align="center">No</td>
			<td align="center">No Aju</td>
			<td align="center">No Urut</td>
			<td align="center">Jenis</td>
			<td align="center">Nomor</td>
			<td align="center">Tanggal</td>
			<td align="center">Penerima</td>
			<td align="center">Kode</td>
			<td align="center">Nama Barang</td>
			<td align="center">Satuan</td>
			<td align="center" bgcolor="#66FF99">Stock</td>
			<td align="center">Jumlah</td>
			<td align="center">Valuta</td>
			<td align="center">Nilai</td>
			<td align="center">Action</td>
		</tr>
	<?php
	$no=1; $tqtyp=0; $tpricep=0;	
	$qr=mysql_query("SELECT * FROM log_outgoing WHERE log_status=2000 AND log_user='$user_id' ORDER BY no_aju, no_urut ASC",$inv);
    while($d=mysql_fetch_array($qr)){
		$prd = mysql_fetch_array(mysql_query("SELECT id,prod_cat FROM product WHERE prod_code='$d[kode]' AND prod_cat<>100",$inv));
		$vc  = mysql_fetch_array(mysql_query("SELECT a.outh_sjno FROM outgoing a INNER JOIN outgoing_detail b ON b.outd_hid=a.id 
											 WHERE a.id_header='$d[id_header]' AND b.no_urut='$d[no_urut]' AND a.outh_status<>100",$inv));
		$stock = get_stock($start_date2,$end_date2,$bulan2,$tahun2,$prd['id'],$prd['prod_cat'],$inv);										 
		if(empty($vc['outh_sjno'])){
			$color ="#FF0000";
		}else{
			$color ="#000000";
		}
		if($d['valuta']=="IDR"){$nilai = $d['nilai'];}else{$nilai = $d['non_rupiah'];}
		$tqtyp = $tqtyp + $d['jumlah'];
		$tpricep = $tpricep + $nilai;
	?>	
		<tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no;?>.</td>
			<td align="center"><font color="<?=$color;?>"><?=$d['no_aju'];?></font></td>
			<td align="center"><font color="<?=$color;?>"><?=$d['no_urut'];?></font></td>
			<td><font color="<?=$color;?>"><?=$d['jenis_dokpab'];?></font></td>
			<td><font color="<?=$color;?>"><?=$d['no_dokpab'];?></font></td>
			<td><font color="<?=$color;?>"><?=$d['tgl_dokpab'];?></font></td>
			<td><?=$d['nm_penerima'];?></td>
			<td><?=$d['kode'];?></td>
			<td><?=$d['nm_barang'];?></td>
			<td><?=$d['satuan'];?></td>
			<td align="right" bgcolor="#66FF99"><?=number_format($stock,2,'.',',');?></td>
			<td align="right"><?=number_format($d['jumlah'],2,'.',',');?></td>
			<td align="center"><?=$d['valuta'];?></td>
			<td align="right"><?=number_format($nilai,2,'.',',');?></td>
			<td align="center">
				<img src="images/delete.png" title="BATAL" style="cursor:pointer" onClick="deldettemp_click('<?=$d['id'];?>','<?=$d['id_header'];?>',
				'<?=$d['no_urut'];?>');">	
			</td>
		</tr>		
	<?php $no++; } ?>
	<tr bgcolor="#CCFFFF">
		<td colspan="11">Total</td>
		<td align="right"><?=number_format($tqtyp,2,'.',',');?></td>
		<td>&nbsp;</td>
		<td align="right"><?=number_format($tpricep,2,'.',',');?></td>
		<td>&nbsp;</td>
	</tr>
	</table>
	</div>
<?php } ?>
</form>
</html> 
