<html>
<head>
<script type="text/javascript"  language="javascript">
	$(document).ready(function(){
		$("#bpbdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#invdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#duedate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#docdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#tglaju").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	function openwindow()
	{
		window.open("popup/popup_supp.php?","mywindow","menubar=1,scrollbars=1,resizable=1,width=600,height=250");
	}
	function openwindow1()
	{
		window.open("popup/popup_barang.php?","mywindow","menubar=1,scrollbars=1,resizable=1,width=600,height=250");
	}
	 function cal_click(currency){
		var qty		 = $("#podqty").val();
		var price	 = $("#podprice").val();
		var total 	 = eval(qty)*eval(price);
		$("#podamount").val(total);
	 }
	 function save_click(){
		if(form.sup_name.value==""){
			alert("Silakan isi nama supplier.");
			form.sup_name.focus();
			return false;
		}else if((form.po_pic.value=="") ){
			alert("Silakan isi pic.");
			form.po_pic.focus();
			return false;
		}else if((form.po_shipto.value=="") ){
			alert("Silakan isi dikirim kemana.");
			form.po_shipto.focus();
			return false;
		}else if((form.po_ptype.value=="BLANK") ){
			alert("Silakan pilih tipe pembayaran.");
			form.po_ptype.focus();
			return false;
		}else if((form.po_desc.value=="") ){
			alert("Silakan isi keterangan.");
			form.po_desc.focus();
			return false;
		}else if((form.po_grandtotals.value==0) ){
			alert("Silakan isi detail barang.");
			form.po_grandtotals.focus();
			return false;
		}else{
			var a = confirm("Apakah anda yakin akan menyimpan data?");
			if (a == true){
				form.action ='?page=Proses-Purchase-Order&Act=SAVE';
				form.method = 'post';
				form.submit();			
				return true;
			}
		}
	 }
	 
	function add_det_click(){
		if(form.prodid.value==""){
			alert("Silakan isi nama barang.");
			form.prodname.focus();
			return false;
		}else if((form.podqty.value=="") ){
			alert("Silakan isi jumlah barang.");
			form.podqty.focus();
			return false; 
		}else if((form.podprice.value=="") ){
			alert("Silakan isi harga barang.");
			form.podprice.focus();
			return false;
		}else if((form.podamount.value=="") ){
			alert("Silakan nilai barang.");
			form.podamount.focus();
			return false;
		}else if((form.poddiscount.value=="") ){
			alert("Silakan nilai diskon.");
			form.poddiscount.focus();
			return false;
		}else{
			form.action ='?page=Proses-Purchase-Order&Act_Det=ADD_DET';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function delete_detail_click(id){
		form.action ='?page=Proses-Purchase-Order&Act_Det=DELETE_DET&id_deth='+id;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function close_click(){
		form.action ='?page=Proses-Purchase-Order&Act=CLOSE'
		form.method = 'post';
		form.submit();		
		return true;
	}
	function popup_item(){
		window.open("popup/popup_itempop.php?","mywindow","menubar=1,scrollbars=1,resizable=1,width=650,height=250");
	}
	$(document).ready(function(){
		var inc = {
			source: "transaksi/manual/json_pop.php",					
			select: function(event, ui){
				$("#prodcode").val(ui.item.prod_code); 
				$("#prodid").val(ui.item.prod_id); 
				$("#prodsat").val(ui.item.prod_sat); 
			},
			minLength: 1
		};
		$("#prodcode").autocomplete(inc);		
	});	
	function format_number(angka) {
		 if (typeof(angka) != 'string') angka = angka.toString();
		 var reg = new RegExp('([0-9]+)([0-9]{3})');
		 while(reg.test(angka)) angka = angka.replace(reg, '$1,$2');
		 return angka;
	 }
	 function number_click(objek) {
	   a = objek.value;
	   b = a.replace(/[^\d]/g,"");
	   c = "";
	   panjang = b.length;
	   j = 0;
	   for (i = panjang; i > 0; i--) {
		 j = j + 1;
		 if (((j % 3) == 1) && (j != 1)) {
		   c = b.substr(i-1,1) + "," + c;
		 } else {
		   c = b.substr(i-1,1) + c;
		 }
	   }
	   objek.value = c;
	}
	function to_number(rp){
	 	return parseFloat(rp.replace(/,.*|\D/g,''),10);
	}
	function getNum(val){
	   if (isNaN(val)) 
		 return 0;
	   else
		 return val;
	}
	/*---PPN Klik---*/
	function format_number(angka) {
		 if (typeof(angka) != 'string') angka = angka.toString();
		 var reg = new RegExp('([0-9]+)([0-9]{3})');
		 while(reg.test(angka)) angka = angka.replace(reg, '$1,$2');
		 return angka;
	 }
	 function number_click(objek) {
	   a = objek.value;
	   b = a.replace(/[^\d]/g,"");
	   c = "";
	   panjang = b.length;
	   j = 0;
	   for (i = panjang; i > 0; i--) {
		 j = j + 1;
		 if (((j % 3) == 1) && (j != 1)) {
		   c = b.substr(i-1,1) + "," + c;
		 } else {
		   c = b.substr(i-1,1) + c;
		 }
	   }
	   objek.value = c;
	}
	function to_number(rp){
	 	return parseFloat(rp.replace(/,.*|\D/g,''),10);
	}
	function getNum(val){
	   if (isNaN(val)) 
		 return 0;
	   else
		 return val;
	}	
	function ppn_yes_click(){
		var subtotal = $("#tsubtotal").val();
		var dp       = $("#po_downpayment").val();
		$("#po_downpayment").val(dp);
		var tppn = eval(subtotal)*0.1;	
		$("#po_totalppn").val(tppn);
		var gtotal = (eval(subtotal)+eval(tppn))-eval(dp);
		$("#po_grandtotal").val(gtotal);
		$("#po_grandtotals").val(format_number(gtotal));  		
	 }
	 function ppn_no_click(){
	 	var subtotal = $("#tsubtotal").val();
		var dp       = $("#po_downpayment").val();
		$("#po_downpayment").val(dp);
		var tppn = 0;	
		$("#po_totalppn").val(tppn);
		var gtotal = (eval(subtotal)+eval(tppn))-eval(dp);
		$("#po_grandtotal").val(gtotal);
		$("#po_grandtotals").val(format_number(gtotal)); 
	}
	function dp_click(){
	 	var subtotal = $("#tsubtotal").val();
		var tppn 	 = $("#po_totalppn").val();	
		var dp       = $("#po_downpayment").val();
		var gtotal = (eval(subtotal)+eval(tppn))-eval(dp);
		$("#po_grandtotal").val(gtotal);
		$("#po_grandtotals").val(format_number(gtotal));
	 }	
	function cal(){
		var a  = $("#rows").val();
		var ppn= $("#fjh_ppn").val(); 
		a=parseInt(a);  q=0; p=0; am=0; total=0; d=0; dv=0; dis=0; 
		for(b=1; b<a; b++){		
			q  = document.getElementById("fjd_qty["+b+"]").value;
			p  = document.getElementById("fjd_price["+b+"]").value;
			am = (q*p);			
			document.getElementById("fjd_total["+b+"]").value=am;
			total += parseFloat(am);						
			d  = document.getElementById("fjd_disc["+b+"]").value;
			/*dv = (d/100)*am;*/
			dis += parseFloat(d);
		}
		ppn = parseFloat(ppn);
		$("#fjh_subtotal").val(total);
		$("#fjh_totdisc").val(dis);
		$("#t_subtotal").val(total-dis);	
		$("#fjh_total").val((total-dis)+ppn);
	} 

function cal_tax_yes(){
		var t_subtotal= $("#t_subtotal").val(); 
		var ppn=0; t_subtotal=parseInt(t_subtotal);
		ppn = t_subtotal*0.1;
		ppn = parseFloat(ppn);
		$("#fjh_ppn").val(ppn);	
		$("#fjh_total").val(t_subtotal+ppn);
	}
	function cal_tax_no(){
		var t_subtotal= $("#t_subtotal").val(); 
		var ppn=0; t_subtotal=parseInt(t_subtotal);
		ppn = parseFloat(ppn);
		$("#fjh_ppn").val(ppn);	
		$("#fjh_total").val(t_subtotal+ppn);
	}	
	function potype_click(po_type){
		form.action ='?page=Proses-Purchase-Order&Act=POTYPE&po_type='+po_type;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function openwindowcust(){
		window.open("popup/popup_cust.php?","mywindow","menubar=1,scrollbars=1,resizable=1,width=600,height=250");
	}
	
</script>
</head>
<form action="#" method="post" name="form" target="_self" enctype="multipart/form-data">
<table width="100%" cellpadding="2" cellspacing="1" class="table-list" style="margin-top:0px;">
	<tr>
	  <th colspan="4">PURCHASE ORDER PURCHASING</th>
	</tr>
	<!--<tr>
	  <td width="15%">Tipe PO</td>
	  <td width="30%">: <input type="hidden" name="po_id" value="<?php echo $po_id; ?>" size="50" />
					  <?php if(empty($po_id)){ ?>
						<select name="po_type" onChange="potype_click(this.value);">
					  <?php }else{ ?>
						<input type="hidden" name="po_type" value='<?=$po_type;?>'>
						<select name="po_types" disabled>
					  <?php } ?>
							<?php if($po_type=="Purchasing"){ ?>
							<option value="0">Pilih</option>
							<option value="Purchasing" selected>Purchasing</option>
							<option value="Sales">Sales</option>
							<?php }elseif($po_type=="Sales"){ ?>
							<option value="0">Pilih</option>
							<option value="Purchasing">Purchasing</option>
							<option value="Sales" selected>Sales</option>
							<?php }else{ ?>
							<option value="0" selected>Pilih</option>
							<option value="Purchasing">Purchasing</option>
							<option value="Sales">Sales</option>
							<?php } ?>
						</select>
	  </td>
	  <td width="15%">&nbsp;</td>
	  <td width="40%">&nbsp;</td>
	</tr>-->
	<tr>
	  <td width="15%">No PO</td>
	  <td width="30%">: <input type="hidden" name="po_id" value="<?php echo $po_id; ?>" size="50" />
	  <input type="text" name="po_no" value="<?php echo $po_no; ?>" size="20" style="background-color:#CCFFFF " /></td>
	  <td width="15%">Tipe Pembayaran</td>
	  <td width="40%">: <select name="po_ptype">
			  <option value="BLANK">PILIH</option> 
				<?php
				  $dataSql = mysql_query("SELECT id, bbt_tipe FROM buku_besar_tipe WHERE bbt_status=20 ORDER BY id ASC");
				  while ($dataRow = mysql_fetch_array($dataSql)) {
					if ($dataRow['id']== $po_ptype) {
						$cek = " selected";
					} else { $cek=""; }
					echo "<option value='$dataRow[id]' $cek>$dataRow[bbt_tipe]</option>";
				  }
				  $sqlData =""; 
				?>
			 </select>
	  </td>
	</tr>
	<tr>
		<td>Tanggal PO</td>
		<td>: <input type="text" name="po_date" value="<?php if(empty($po_date)){$po_date=$date;} echo $po_date; ?>" size="12" id="bpbdate" 
				  readonly="true" style="background-color:#CCFFFF " /> </td>
		<td>Mata Uang</td>
		<td>: <select name="po_currency">
				<?php
				  $dataSql = mysql_query("SELECT currency FROM currency ORDER BY id ASC");
				  while ($dataRow = mysql_fetch_array($dataSql)) {
					if ($dataRow['currency']== $po_currency) {
						$cek = " selected";
					} else { $cek=""; }
					echo "<option value='$dataRow[currency]' $cek>$dataRow[currency]</option>";
				  }
				  $sqlData ="";
				?>
			 </select>
		</td>
	</tr>
	<tr>
		<td>Nama Supplier</td>
		<td>: <input type="hidden" name="sup_id" value="<?php echo $sup_id; ?>" size="10" />
				 <input type="text" name="sup_name" value="<?php echo $sup_name; ?>" size="30" readonly style="background-color:#CCFFFF " />		
				 <a href="javascript: openwindow('');"><img src="images/popup.png" height="15" width="15" title="Tambah" border="0" />
		</td>
		<td>Tanggal Kirim</td>
		<td>: <input type="text" name="po_deldate" value="<?php if(empty($po_deldate)){$po_deldate=$date;} echo $po_deldate; ?>" size="12" id="invdate" 
				  readonly="true" style="background-color:#CCFFFF " /></td>
	</tr>
	<tr>
		<td>Dept.</td>
		<td colspan="3">: <select name="po_dept">
							<?php
							  $dataSql = mysql_query("SELECT id,dept_name FROM departement ORDER BY id ASC");
							  while ($dataRow = mysql_fetch_array($dataSql)) {
								if ($dataRow['dept_name']== $po_dept) {
									$cek = " selected";
								} else { $cek=""; }
								echo "<option value='$dataRow[dept_name]' $cek>$dataRow[dept_name]</option>";
							  }
							  $sqlData =""; 
							?>
						 </select></td>
	</tr>
	<tr>
		<td>PIC</td>
		<td>: <input type="text" name="po_pic" value="<?=$po_pic;?>"></td>
		<td>Tanggal Jatuh Tempo</td>
		<td>: <input type="text" name="po_duedate" value="<?php if(empty($po_duedate)){$po_duedate=$date;} echo $po_duedate; ?>" size="12" id="duedate" 
				  readonly="true" style="background-color:#CCFFFF " /></td>
	</tr>
	<tr>
		<td>Style</td>
		<td>: <input type="text" name="po_style" size="25" value="<?=$po_style;?>"></td>
		<td>Revisi</td>
		<td>: <input type="text" name="po_revisi" size="25" value="<?=$po_revisi;?>"></td>
	</tr>
	<tr>
		<td>Requisitioner</td>
		<td>: <input type="text" name="po_requisitioner" size="25" value="<?=$po_requisitioner;?>"></td>
		<td>Ship. VIA</td>
		<td>: <input type="text" name="po_ship_via" size="25" value="<?=$po_ship_via;?>"></td>
	</tr>
	<tr>
		<td>F.O.B</td>
		<td>: <input type="text" name="po_fob" size="25" value="<?=$po_fob;?>"></td>
		<td>Ship. Terms</td>
		<td>: <input type="text" name="po_ship_terms" size="25" value="<?=$po_ship_terms;?>"></td>
	</tr>
	<tr>
		<td>Keterangan</td>
		<td>: <textarea name="po_desc" cols="30"><?php echo $po_desc; ?></textarea></td>
		<td>Kirim Ke</td>
		<td>: <textarea name="po_shipto" cols="30"><?php echo $po_shipto; ?></textarea></td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<th colspan="4">DETAIL ITEM</th>
	</tr>
	<tr>
		<td colspan="4">
			<table width="100%" border="0">
				<tr>
					<td>Kode/Nama Barang</td>
					<td>Satuan</td>
					<td>Jumlah</td>
					<td>Harga</td>
					<td>Nilai</td>
					<td>Diskon</td>
					<td>Keterangan</td>
					<td>Action</td>
				</tr>
				<tr>
					<td><input type="hidden" name="prodid" id="prodid" style="width:300px;">
					    <input type="text" name="prodcode" id="prodcode" style="width:200px; background-color:#CCFFFF" >
						<img src="images/popup.png" height="15" width="15" title="Popup Item Master" style="cursor:pointer" border="0" onClick="popup_item();" />
					</td>
					<td>
						<input type="text" name="prod_sat" id="prodsat" style="width:50px; text-align:left; background-color:#CCFFFF" readonly>
						<input type="hidden" name="prodstock" id="prodstock" style="width:50px; text-align:left; background-color:#CCFFFF" readonly>
					</td>
					<td><input type="text" id="podqty" name="podqty" style="width:60px; text-align:right" onBlur="cal_click();"></td>
					<td><input type="text" id="podprice" name="podprice" style="width:100px; text-align:right" onBlur="cal_click();"></td>
					<td>
						<input type="text" id="podamount" name="podamount" style="width:100px; text-align:right">
					</td>
					<td>
						<input type="text" id="poddiscount" name="poddiscount" style="width:60px; text-align:right" onBlur="cal_click();">
					</td>
					<td>
						<input type="text" id="poddesc" name="poddesc" style="width:200px; text-align:left">
					</td>
					<td><input type="button" name="Add_Detail" value="Tambah" style="cursor:pointer" onClick="add_det_click();"></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4">
			<table width="100%">
				<tr>
					<td align="center">No.</td>
					<td align="center">Nama Barang</td>
					<td align="center">Unit</td>
					<td align="center">Jumlah</td>
					<td align="center">Harga</td>
					<td align="center">Diskon</td>
					<td align="center">Ket.</td>
					<td align="center">Total</td>
					<td align="center">Action</td>
				</tr>
				<?php
					$no=0; $tqty=0; $po_subtotal=0; $po_discount=0; $tsubtotal=0;
					if(empty($po_id)){
						$qd = mysql_query("SELECT a.*, b.prod_code,b.prod_name,b.prod_sat
										   FROM purchase_order_temp a
										   INNER JOIN product b ON b.id=a.pod_prodid
										   WHERE a.pod_inputby='$user_id' AND pod_status=10");
					}else{
						$qd = mysql_query("SELECT a.*, b.prod_code,b.prod_name,b.prod_sat
									   FROM purchase_order_detail a
									   INNER JOIN product b ON b.id=a.pod_prodid
						   			   WHERE a.po_no='$po_no'");	
					}
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     	   = $tqty + $dt->pod_qty; 
					$po_subtotal   = $po_subtotal + $dt->pod_amount;
					$po_discount   = $po_discount + $dt->pod_discount;
					$tsubtotal	   = $po_subtotal - $po_discount;
					if($po_ppn=="Ya"){
						$po_totalppn= $tsubtotal * 0.1;
					}else{
						$po_totalppn= 0;	
					}
					$po_grandtotal = ($tsubtotal + $po_totalppn)- $po_downpayment;
				?> 
				  <tr>
					<td style="width:20px;" align="center"><?=$no;?></td>
					<td style="width:300px;"><?php echo "$dt->prod_code - $dt->prod_name";?></td>
					<td style="width:150px;" align="center"><?=$dt->prod_sat;?></td>
					<td style="width:150px;" align="right"><?=number_format($dt->pod_qty,2,',','.');?></td>
					<td style="width:150px;" align="right"><?=number_format($dt->pod_price,2,',','.');?></td>
					<td style="width:150px;" align="right"><?=number_format($dt->pod_discount,2,',','.');?></td>					
					<td style="width:150px;" align="left"><?=$dt->pod_desc;?></td>
					<td style="width:150px;" align="right"><?=number_format($dt->pod_amount,2,',','.');?></td>
					<td style="width:170px;" align="center">
						<img src="images/btn_delete.png" width="20" height="20"  border="0"  title="Hapus" style="cursor:pointer" onClick="delete_detail_click('<?=$dt->id;?>');"/>
					</td>
				</tr>
				<?php }	?>
				 <tr>
					<td colspan="7" align="right">Total</td>
					<td align="right">
						<?php echo number_format($po_subtotal,2,'.',',');?>
                    	<input type="hidden" id="po_subtotal" name="po_subtotal" value="<?php echo $po_subtotal;?>">
                    </td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td colspan="6" align="left">Terbilang# :</td>
					<td align="right">Discount</td>
					<td align="right"><?php echo number_format($po_discount,2,'.',',');?>
						<input type="hidden" name="po_discount" value="<?php echo $po_discount;?>">
					</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td colspan="5" rowspan="2" align="left"><textarea name="po_terbilang" class="textarea-field2" rows="2" cols="60"><?=$po_terbilang;?></textarea></td>
					<td colspan="2" align="right">Sub Total</td>
					<td align="right"><?php echo number_format($tsubtotal,2,'.',',');?>
						<input type="hidden" id="tsubtotal" name="tsubtotal" value="<?php echo $tsubtotal; ?>" size="20" style="text-align:right " />
					</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td colspan="2" align="right">PPN &nbsp; : &nbsp; 
						 <?php if($po_ppn=="Ya"){?>
							<input type="radio" name="po_ppn" value="Ya" checked onClick="ppn_yes_click();">Ya &nbsp;
							<input type="radio" name="po_ppn" value="Tidak" onClick="ppn_no_click();">Tidak
						  <?php }elseif($po_ppn=="Tidak"){?>
							<input type="radio" name="po_ppn" value="Ya" onClick="ppn_yes_click();">Ya &nbsp;
							<input type="radio" name="po_ppn" value="Tidak" checked onClick="ppn_no_click();">Tidak
						  <?php }else{?>
							<input type="radio" name="po_ppn" value="Ya" onClick="ppn_yes_click();">Ya &nbsp;
							<input type="radio" name="po_ppn" value="Tidak" onClick="ppn_no_click();">Tidak
						  <?php }?>
					</td>
					<td align="right">
						<input type="text" id="po_totalppn" name="po_totalppn" value="<?php echo $po_totalppn; ?>" size="20" style="text-align:right "  readonly="true"/>
					</td>
					<td>&nbsp;</td>
				</tr>				
				<tr>
					<td colspan="7" align="right">Down Payment </td>
					<td align="right">
						<input type="text" id="po_downpayment" name="po_downpayment" value="<?php echo number_format($po_downpayment,0,'.',','); ?>" size="20" style="text-align:right" 
                        onBlur="number_click(this);" onKeyUp="dp_click();" autocomplete="off"/>
					</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td colspan="7" align="right"><b>Grand Total</b></td>
					<td align="right"><b>
						<input type="text" id="po_grandtotals" name="po_grandtotals" value="<?php echo number_format($po_grandtotal,2,'.',',');?>" size="20" style="text-align:right " readonly>
						<input type="hidden" id="po_grandtotal" name="po_grandtotal" value="<?php echo $po_grandtotal;?>" size="20" style="text-align:right " readonly>
						</b>
					</td>
					<td>&nbsp;</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>				
	<tr>
		<td colspan="4" align="center">
			<input type="button" name="btnSave" value="SIMPAN" style="cursor:pointer;" onClick="save_click();">
			<input type="button" value="BATAL" name="batal" style="cursor:pointer"  onClick="close_click();" />
		</td>
    </tr>			
</table>
</form>
</html> 
