<?php
	include "./include/config_tpb.php";
?>
<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-Pengeluaran-BC41&Act=CANCEL';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function get_click(){
		var sumber = $("#sumber").val();
		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(sumber=="S01"){
			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-Pengeluaran-BC41&Act=GET';
					form.method = 'post';
					form.submit();			
					return true;
				}
			}
		}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-Pengeluaran-BC41&Act=GET';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function temp_click(){
	    var jenis_dokpab = $("#jenis_dokpab").val();
	    var jdp = jenis_dokpab.replace(' ', '%20');
		if(jdp=="BLANK"){
			alert("Jenis dokumen pabean harus dipilih terlebih dahulu");
			form.jenis_dokpab2.focus();
			return false;
		}else{
			form.action ='?page=Proses-Pengeluaran-BC41&Act=TEMP';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function process_click(){
		var jenis_dokpab = $("#jenis_dokpab").val();
	    var jdp = jenis_dokpab.replace(' ', '%20');
		if(jdp=="BLANK"){
			alert("Jenis dokumen pabean harus dipilih terlebih dahulu");
			form.jenis_dokpab2.focus();
			return false;
		}else{
			form.action ='?page=Proses-Pengeluaran-BC41&Act=PROCESS';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function inventory_click(){
		form.action ='?page=Proses-Pengeluaran-BC41&Act=INV';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function reg_click(){
		var jenis_dokpab = $("#jenis_dokpab").val();
	    var jdp = jenis_dokpab.replace(' ', '%20');
		if(jdp=="BLANK"){
			alert("Jenis dokumen pabean harus dipilih terlebih dahulu");
			form.jenis_dokpab2.focus();
			return false;
		}else{
			form.action ='?page=Proses-Pengeluaran-BC41&Act=REG';
			form.method = 'post';
			form.submit();			
			return true;
		}	
	}
	function delete_click(){
		form.action ='?page=Proses-Pengeluaran-BC41&Act=DEL_TEMP';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function deldettemp_click(id, id_header, no_urut){
		form.action ='?page=Proses-Pengeluaran-BC41&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-Pengeluaran-BC41&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_bc41.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">INTERFACE DARI CEISA - BC 4.1</th>
	</tr>
	<tr>
		<td>Masukan Tanggal Dokumen :
			<input type="hidden" id="jenis_dokpab" name="jenis_dokpab" readonly value="BC 4.1" size="10">
			<input type="text" id="start_date" name="start_date" readonly value="<?=$start_date;?>" size="10"> ~
		    <input type="text" id="end_date" name="end_date" readonly value="<?=$end_date;?>" size="10">
		</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="EXCEL TEMP" style="cursor:pointer" onClick="excel_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"){
	$maxid = mysql_fetch_array(mysql_query("SELECT MAX(flag) flag FROM log_outgoing",$inv));
	$flag  = $maxid['flag']+1;
	$qr=mysql_query("SELECT a.id, b.uraian_dokumen_pabean as jenis_dokumen, a.nomor_daftar as no_dokumen, date(a.tanggal_daftar) tgl_dokumen,
							a.nama_penerima_barang as nm_penerima, a.kode_valuta as valuta, c.kode_barang, c.uraian as nm_barang, c.kode_satuan as satuan,
							c.jumlah_satuan jumlah, c.netto as berat, d.nomor_dokumen as no_do, date(d.tanggal_dokumen) as tgl_do,
							e.nomor_dokumen as no_invoice, date(e.tanggal_dokumen) as tgl_invoice, 
							a.nomor_aju as no_aju, date(a.tanggal_aju) tgl_aju, c.harga_penyerahan as nilai,
							f.nomor_dokumen as no_dokpab_asal, date(f.tanggal_dokumen) as tgl_dokpab_asal, c.seri_barang as no_urut, g.tgl_sj,
							h.nomor_kontainer as no_kontainer, h.uraian_tipe_kontainer as tipe_kontainer, h.uraian_ukuran_kontainer as ukuran_kontainer, 
							i.jumlah_kemasan, i.kode_jenis_kemasan as kode_kemasan, i.uraian_kemasan as jenis_kemasan, j.uraian_status, a.id_penerima_barang, 
							CASE WHEN c.harga_penyerahan is null THEN c.cif_rupiah ELSE c.harga_penyerahan END AS cif_rupiah, c.cif, k.uraian_tujuan_pengiriman tujuan,
							l.nomor_dokumen no_sj, l.tanggal_dokumen tgl_sj2, a.ndpbm, c.spesifikasi_lain
					FROM tpb_header a
					INNER JOIN referensi_dokumen_pabean b ON b.kode_dokumen_pabean=a.kode_dokumen_pabean 
					INNER JOIN tpb_barang c ON c.id_header=a.id
					LEFT JOIN (select id_header, nomor_dokumen, tanggal_dokumen from tpb_dokumen where kode_jenis_dokumen=217 group by id_header) AS d ON 
					d.id_header=a.id
					LEFT JOIN (select id_header, nomor_dokumen, tanggal_dokumen from tpb_dokumen where kode_jenis_dokumen=380 group by id_header) AS e ON 
					e.id_header=a.id
					LEFT JOIN (select id_header, nomor_dokumen, tanggal_dokumen from tpb_dokumen where kode_jenis_dokumen=40 group by id_header) AS f ON 
					f.id_header=a.id 	
					LEFT JOIN (SELECT id_header, date(a.waktu_status) as tgl_sj 
							   FROM tpb_detil_status a
							   INNER JOIN referensi_status b ON b.kode_status=a.kode_status AND b.kode_dokumen=25  AND b.kode_status IN (80,85)
							   GROUP BY id_header,date(a.waktu_status))as g ON g.id_header=a.id		
					LEFT JOIN (SELECT a.id_header, a.nomor_kontainer, b.uraian_tipe_kontainer, c.uraian_ukuran_kontainer FROM tpb_kontainer a
						   INNER JOIN referensi_tipe_kontainer b ON b.kode_tipe_kontainer=a.kode_tipe_kontainer
						   INNER JOIN referensi_ukuran_kontainer c ON c.kode_ukuran_kontainer=a.kode_ukuran_kontainer
						   GROUP BY a.id_header) as h ON h.id_header=a.id	
					LEFT JOIN (SELECT a.id_header, a.jumlah_kemasan, a.kode_jenis_kemasan, b.uraian_kemasan FROM tpb_kemasan a 
						   INNER JOIN referensi_kemasan b ON b.kode_kemasan=a.kode_jenis_kemasan
						   GROUP BY a.id_header) as i ON i.id_header=a.id	
					LEFT JOIN referensi_status j ON j.kode_status=a.kode_status AND j.kode_dokumen=a.kode_dokumen_pabean
					LEFT JOIN referensi_tujuan_pengiriman k ON k.kode_dokumen=41 AND k.kode_tujuan_pengiriman=a.kode_tujuan_pengiriman
					LEFT JOIN (select id_header, nomor_dokumen, tanggal_dokumen from tpb_dokumen where kode_jenis_dokumen=640 group by id_header) AS l ON l.id_header=a.id
					WHERE a.kode_dokumen_pabean=41 AND date(a.tanggal_daftar) BETWEEN '$start_date' AND '$end_date'						
					ORDER BY a.id DESC",$tpb);
	while($d=mysql_fetch_array($qr)){
		$qc=mysql_fetch_array(mysql_query("SELECT id FROM log_outgoing WHERE no_urut='$d[no_urut]' AND id_header='$d[id]' AND log_status <> 5",$inv));
		if(empty($qc['id'])){
			#Insert Header & Detil
			mysql_query("INSERT INTO log_outgoing SET no_sj='$d[no_sj]', tgl_sj='$d[tgl_dokumen]', no_invoice='$d[no_invoice]', tgl_invoice='$d[tgl_invoice]', no_do='$d[no_do]', 
						 kode='$d[kode_barang]', jumlah='$d[jumlah]', valuta='$d[valuta]', nilai='$d[nilai]', log_status=1, log_input=now(), flag='$flag', 
						 log_user='$user_id', nm_penerima='".mysql_real_escape_string($d['nm_penerima'])."', jenis_dokpab='$d[jenis_dokumen]', no_dokpab='$d[no_dokumen]', tgl_dokpab='$d[tgl_dokumen]', 						 						 nm_barang='".mysql_real_escape_string($d['nm_barang'])."', satuan='$d[satuan]', berat='$d[berat]', no_aju='$d[no_aju]', tgl_aju='$d[tgl_aju]', 
						 no_urut='$d[no_urut]', no_kontainer='$d[no_kontainer]', tipe_kontainer='$d[tipe_kontainer]',
						 ukuran_kontainer='$d[ukuran_kontainer]', jumlah_kemasan='$d[jumlah_kemasan]', kode_kemasan='$d[kode_kemasan]', 
						 jenis_kemasan='$d[jenis_kemasan]',status_dokpab='$d[uraian_status]', id_header='$d[id]', sumber='CIESA', id_penerima='$d[id_penerima_barang]',cif_rupiah='$d[cif_rupiah]',
						 non_rupiah='$d[cif]',tujuan='$d[tujuan]', outh_rate='$d[ndpbm]', outd_scno='$d[spesifikasi_lain]'",$inv);
		
			#Select&Insert dokumen asal, 
			$qr2=mysql_query("SELECT seri_barang, seri_bahan_baku, kode_barang, uraian, jenis_satuan, jumlah_satuan,
							  CASE WHEN kode_asal_bahan_baku=0 THEN cif ELSE harga_penyerahan END as nilai,
							  substring(nomor_aju_dok_asal,21,6) as no_aju_dokasal, 
							  CASE WHEN kode_jenis_dok_asal=23 THEN 'BC 2.3' WHEN kode_jenis_dok_asal=27 THEN 'BC 2.7' WHEN kode_jenis_dok_asal=40 THEN 
							  'BC 4.0' ELSE 'LAINNYA' END as jenis_dokpab_asal, nomor_daftar_dok_asal, date(tanggal_daftar_dok_asal) as tgl_dokpab_asal,
							  seri_barang_dok_asal, kode_asal_bahan_baku, id_header
							  FROM tpb_bahan_baku WHERE id_header=$d[id] AND seri_barang=$d[no_urut] 
							  ORDER BY seri_barang,kode_asal_bahan_baku, seri_bahan_baku ASC",$tpb);
			while($d2=mysql_fetch_array($qr2)){
				#Cek sudah masuk ke database atau belum
				$cekda =mysql_fetch_array(mysql_query("SELECT id FROM log_outgoing_dokasal WHERE id_header='$d2[id_header]' AND no_urut='$d2[seri_barang]'
					    AND no_urut_dokpab='$d2[seri_bahan_baku]' AND kode_dokasal='$d2[kode_asal_bahan_baku]' AND log_status<>5",$inv));
				if(empty($cekda['id'])){		
					mysql_query("INSERT INTO log_outgoing_dokasal SET no_urut='$d2[seri_barang]', no_urut_dokpab='$d2[seri_bahan_baku]', kode_barang='$d2[kode_barang]',
								 nm_barang='".mysql_real_escape_string($d2['uraian'])."', satuan='$d2[jenis_satuan]', jumlah='$d2[jumlah_satuan]', nilai='$d2[nilai]', 
								 noaju_dokpab_asal='$d2[no_aju_dokasal]', jenis_dokpab_asal='$d2[jenis_dokpab_asal]', no_dokpab_asal='$d2[nomor_daftar_dok_asal]',
								 tgl_dokpab_asal='$d2[tgl_dokpab_asal]', nourut_dokpab_asal='$d2[seri_barang_dok_asal]', kode_dokasal='$d2[kode_asal_bahan_baku]',
								 id_header='$d2[id_header]', log_status=1, jenis_dokpab='BC 4.1'",$inv);
				}else{
					mysql_query("UPDATE log_outgoing_dokasal SET kode_barang='$d2[kode_barang]', nm_barang='".mysql_real_escape_string($d2['uraian'])."', satuan='$d2[jenis_satuan]', 
							 jumlah='$d2[jumlah_satuan]', nilai='$d2[nilai]', noaju_dokpab_asal='$d2[no_aju_dokasal]', jenis_dokpab_asal='$d2[jenis_dokpab_asal]', 
							 no_dokpab_asal='$d2[nomor_daftar_dok_asal]', tgl_dokpab_asal='$d2[tgl_dokpab_asal]', nourut_dokpab_asal='$d2[seri_barang_dok_asal]', 
							 kode_dokasal='$d2[kode_asal_bahan_baku]', jenis_dokpab='BC 4.1'
							 WHERE id_header='$d2[id_header]' AND log_status=1 AND no_urut='$d2[seri_barang]' AND no_urut_dokpab='$d2[seri_bahan_baku]'
							 AND kode_dokasal='$d2[kode_asal_bahan_baku]'",$inv);
				}				 
			}
		}else{
			#Update
			mysql_query("UPDATE log_outgoing SET no_sj='$d[no_sj]', tgl_sj='$d[tgl_dokumen]', no_invoice='$d[no_invoice]', tgl_invoice='$d[tgl_invoice]', no_do='$d[no_do]', 
						 kode='$d[kode_barang]', jumlah='$d[jumlah]', valuta='$d[valuta]', nilai='$d[nilai]', log_user='$user_id', nm_penerima='".mysql_real_escape_string($d['nm_penerima'])."', 
						 nm_barang='".mysql_real_escape_string($d['nm_barang'])."', satuan='$d[satuan]', berat='$d[berat]', no_aju='$d[no_aju]', tgl_aju='$d[tgl_aju]', 
						 no_kontainer='$d[no_kontainer]', tipe_kontainer='$d[tipe_kontainer]',
						 ukuran_kontainer='$d[ukuran_kontainer]', jumlah_kemasan='$d[jumlah_kemasan]', kode_kemasan='$d[kode_kemasan]', 
						 jenis_kemasan='$d[jenis_kemasan]',status_dokpab='$d[uraian_status]',  sumber='CIESA', id_penerima='$d[id_penerima_barang]',cif_rupiah='$d[cif_rupiah]',
						 non_rupiah='$d[cif]',tujuan='$d[tujuan]', outh_rate='$d[ndpbm]', outd_scno='$d[spesifikasi_lain]' WHERE id_header='$d[id]' AND no_urut='$d[no_urut]' AND log_status=1",$inv);
		
			#Select&Update dokumen asal, 
			$qr2=mysql_query("SELECT seri_barang, seri_bahan_baku, kode_barang, uraian, jenis_satuan, jumlah_satuan,
							  CASE WHEN kode_asal_bahan_baku=0 THEN cif ELSE harga_penyerahan END as nilai,
							  substring(nomor_aju_dok_asal,21,6) as no_aju_dokasal, 
							  CASE WHEN kode_jenis_dok_asal=23 THEN 'BC 2.3' WHEN kode_jenis_dok_asal=27 THEN 'BC 2.7' WHEN kode_jenis_dok_asal=40 THEN 
							  'BC 4.0' ELSE 'LAINNYA' END as jenis_dokpab_asal, nomor_daftar_dok_asal, date(tanggal_daftar_dok_asal) as tgl_dokpab_asal,
							  seri_barang_dok_asal, kode_asal_bahan_baku, id_header
							  FROM tpb_bahan_baku WHERE id_header=$d[id] AND seri_barang=$d[no_urut] 
							  ORDER BY seri_barang,kode_asal_bahan_baku, seri_bahan_baku ASC",$tpb);
			while($d2=mysql_fetch_array($qr2)){
				#Cek sudah masuk ke database atau belum
				$cekda =mysql_fetch_array(mysql_query("SELECT id FROM log_outgoing_dokasal WHERE id_header='$d2[id_header]' AND no_urut='$d2[seri_barang]'
					    AND no_urut_dokpab='$d2[seri_bahan_baku]' AND kode_dokasal='$d2[kode_asal_bahan_baku]' AND log_status<>5",$inv));
				if(empty($cekda['id'])){		
					mysql_query("INSERT INTO log_outgoing_dokasal SET no_urut='$d2[seri_barang]', no_urut_dokpab='$d2[seri_bahan_baku]', kode_barang='$d2[kode_barang]',
								 nm_barang='".mysql_real_escape_string($d2['uraian'])."', satuan='$d2[jenis_satuan]', jumlah='$d2[jumlah_satuan]', nilai='$d2[nilai]', 
								 noaju_dokpab_asal='$d2[no_aju_dokasal]', jenis_dokpab_asal='$d2[jenis_dokpab_asal]', no_dokpab_asal='$d2[nomor_daftar_dok_asal]',
								 tgl_dokpab_asal='$d2[tgl_dokpab_asal]', nourut_dokpab_asal='$d2[seri_barang_dok_asal]', kode_dokasal='$d2[kode_asal_bahan_baku]',
								 id_header='$d2[id_header]', log_status=1, jenis_dokpab='BC 4.1'",$inv);
				}else{
					mysql_query("UPDATE log_outgoing_dokasal SET kode_barang='$d2[kode_barang]', nm_barang='".mysql_real_escape_string($d2['uraian'])."', satuan='$d2[jenis_satuan]', 
							 jumlah='$d2[jumlah_satuan]', nilai='$d2[nilai]', noaju_dokpab_asal='$d2[no_aju_dokasal]', jenis_dokpab_asal='$d2[jenis_dokpab_asal]', 
							 no_dokpab_asal='$d2[nomor_daftar_dok_asal]', tgl_dokpab_asal='$d2[tgl_dokpab_asal]', nourut_dokpab_asal='$d2[seri_barang_dok_asal]', 
							 kode_dokasal='$d2[kode_asal_bahan_baku]', jenis_dokpab='BC 4.1'
							 WHERE id_header='$d2[id_header]' AND log_status=1 AND no_urut='$d2[seri_barang]' AND no_urut_dokpab='$d2[seri_bahan_baku]' 
							 AND kode_dokasal='$d2[kode_asal_bahan_baku]'",$inv);
				}
			}	
		}			 
	}
	#Show data 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">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=1 AND jenis_dokpab='$jenis_dokpab'
					AND tgl_dokpab BETWEEN '$start_date' AND '$end_date' ORDER BY no_aju, no_urut ASC",$inv);
    while($d=mysql_fetch_array($q)){
		$tqtyg = $tqtyg + $d['jumlah'];
		$tpriceg = $tpriceg + $d['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><?=$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($d['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="250%">
		<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">Tgl Keluar</td>
			<td align="center">No Aju</td>	
			<td align="center">No Urut</td>
			<td align="center">Kode</td>
			<td align="center">Nama Barang</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">Status Dokumen</td>
			<td align="center">No Keluar</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">Tgl Aju</td>	
			<td align="center">No Kontainer</td>
			<td align="center">Tipe Kontainer</td>
			<td align="center">Ukuran Kontainer</td>
			<td align="center">Kode Kemasan</td>
			<td align="center">Jenis Kemasan</td>
			<td align="center">Jumlah Kemasan</td>
		</tr>
	<?php
	$no=0; $tqty=0; $tprice=0;
	$qr=mysql_query("SELECT * FROM log_outgoing WHERE log_status=1 AND jenis_dokpab='$jenis_dokpab' AND tgl_dokpab BETWEEN '$start_date' AND '$end_date' 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);
		if(empty($vc['prod_code'])){
			$s_kode ="NO";
			$bgcolor0 ="#FF6A6A";
		}else{
			$s_kode ="READY";
			$bgcolor0 ="#000000";
		}		
		/*if($d['status_dokpab']=="SPPD"){
			$bgcolor ="#000000";
		}else{
			$bgcolor ="#FF6A6A";
		}*/
		if($d['jumlah'] > $stock){
			$colorstock ="#FF6A6A";			
		}else{
			$colorstock ="#66FF99";
		}
		if($val_date=="NO"){
			$colord ="#FF0000";
		}else{
			$colord ="#000000";
		}
		$tqty = $tqty + $d['jumlah'];
		$tprice = $tprice + $d['nilai'];
		#validasi item komplit/tidak/no aju
		$val_item = get_valoutgoing($start_date2,$end_date2,$bulan2,$tahun2,$d['id_header'],$inv);
	?>	
	    <tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no;?>.</td>
			<td align="center">
				<?php #if(($s_kode =="NO") && ($d['status_dokpab']!="SPPD") && ($d['jumlah'] > $stock)) {
					  //if(($s_kode =="NO") && ($d['jumlah'] > $stock) && ($colord =="#FF0000")) {
					  if($val_item=="NO"){
				?>
					&nbsp;
				<?php #}elseif(($s_kode =="READY") && ($d['status_dokpab']=="SPPD") && ($d['jumlah'] <= $stock)) {
					  //}elseif(($s_kode =="READY") && ($d['jumlah'] <= $stock) && ($colord =="#000000")) {
					  }else{
				?>
					<input type="checkbox" id="pilih[<?=$d['id'];?>]" name="pilih[<?=$d['id'];?>]" value="<?=$d['id'];?>">
					<input type="hidden" name="id_header[<?=$d['id'];?>]" value="<?=$d['id_header'];?>">
					<input type="hidden" name="no_urut[<?=$d['id'];?>]" value="<?=$d['no_urut'];?>">
					<input type="hidden" name="tgl_dokpab2[<?=$d['id'];?>]" value="<?=$d['tgl_dokpab'];?>">
				<?php }?>
			</td>
			<td align="center"><input type="text" name="tgl_sj2[<?=$d['id'];?>]" value="<?=$d['tgl_sj'];?>" size="10"></td>
			<td align="center"><?=$d['no_aju'];?></td>
			<td align="center"><?=$d['no_urut'];?></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 {?>
					<a href="javascript: detail_dokumen_click('<?=$d['id_header'];?>','<?=$d['no_urut'];?>','1');" 
					title="Detail dokumen asal"><font color="#0000FF"><?=$d['kode'];?></font></a>
				<?php }?>
			</td>
			<td>
				<?php if($s_kode =="NO") {?>
					<input type="text" name="nm_barang[<?=$d['id'];?>]" value="<?=$d['nm_barang'];?>" size="25" style="background-color:<?=$bgcolor0;?>">
				<?php }else {?>
					<?=$d['nm_barang'];?>
				<?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 ORDER BY id ASC",$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 replace(upper(prod_code),' ','')='$kode'",$inv));
					echo $cat['prod_cat'];	   
				} ?>
			</td>
			<td align="right" bgcolor="<?=$colorstock;?>"><?=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($d['nilai'],2,'.',',');?></td>	
			<!--<td align="center"><font color="<?=$bgcolor;?>"><?=$d['status_dokpab'];?></font></td>-->
			<td align="center"><?=$d['status_dokpab'];?></td>
			<td align="left"><?=$d['no_sj'];?></td>
			<td align="center"><?=$d['jenis_dokpab'];?></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 align="center"><?=$d['tgl_aju'];?></td>
			<td align="center"><?=$d['no_kontainer'];?></td>
			<td align="center"><?=$d['tipe_kontainer'];?></td>
			<td align="center"><?=$d['ukuran_kontainer'];?></td>
			<td align="center"><?=$d['kode_kemasan'];?></td>
			<td align="center"><?=$d['jenis_kemasan'];?></td>
			<td align="right"><?=number_format($d['jumlah_kemasan'],2,'.',',');?></td>			
		</tr>	
		<?php } ?>	
		<tr bgcolor="#CCFFFF">
			<td colspan="9">Total</td>
			<td align="right"><?=number_format($tqty,2,'.',',');?></td>
			<td>&nbsp;</td>
			<td align="right"><?=number_format($tprice,2,'.',',');?></td>
			<td colspan="16">&nbsp;</td>
		</tr>
	</table>
	</div>
<?php }elseif(($Act=="PROCESS") || ($Act=="DELDET_TEMP")){ ?>
	<div class="scroll">
	<table width="130%">
		<tr bgcolor="#CCCCCC">
			<td align="center">No</td>
			<td align="center">Tgl Keluar</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=2  AND jenis_dokpab='$jenis_dokpab' ORDER BY no_dokpab 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";
		}
		$tqtyp = $tqtyp + $d['jumlah'];
		$tpricep = $tpricep + $d['nilai'];
	?>	
		<tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no;?>.</td>
			<td align="center"><?=$d['tgl_sj'];?></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>
				<a href="javascript: detail_dokumen_click('<?=$d['id_header'];?>','<?=$d['no_urut'];?>','2');" 
					title="Detail dokumen asal"><font color="#0000FF"><?=$d['kode'];?></font></a>
			</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($d['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> 
