<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="function/dataTables/media/css/demo_page.css">
<link rel="stylesheet" href="function/dataTables/media/css/demo_table_jui.css">
<link rel="stylesheet" href="function/dataTables/media/themes/smoothness/jquery-ui-1.8.4.custom.css">
<script type="text/javascript" src="function/dataTables/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript">
	$(document).ready(function(){ 
		$("#fromdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#todate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :10
		});
	});
	function setTrColor(obj, color)
	{
		obj.style.backgroundColor=color;
		obj.onmouseout = function(){
				obj.style.backgroundColor=''; 
		}
	}
	function print_click(id){
		window.open("transaksi/purchasing/print_po_sales.php?po_no="+id,"mywindow","menubar=1,scrollbars=1,resizable=1,width=700,height=1000");
	}
	function add_click(){
		form.action ='?page=Proses-PO-Sales&Act=ADD';
		form.method = 'post';
		form.submit();		
		return true;
	}
	function posting_click(id,code){
		form.action ='?page=Proses-PO-Sales&Act=POSTING&po_id='+id+'&po_no='+code;
		form.method = 'post';
		form.submit();		
		return true;
	}
	function detail_click(id){
		form.action ='?page=Proses-PO-Sales&Act=DETAIL&po_id='+id;
		form.method = 'post';
		form.submit();		
		return true;
	}
	function delete_click(id,code){
		var a = confirm("Apakah anda yakin akan menghapus data?");
		if (a == true){
			form.action ='?page=Proses-PO-Sales&Act=DELETE&po_id='+id+'&po_no='+code;
			form.method = 'post';
			form.submit();		
			return true;
		}	
	}
</script>
<?php
if(isset($_REQUEST['cari'])=="CARI"){
	 $fromdate	 = $_REQUEST['fromdate'];
	 $todate     = $_REQUEST['todate']; 	
 }else{
	 $fromdate	 = $start_date;
	 $todate     = $end_date;	
 }
?>
<form action="?page=List-PO-Sales" method="post" name="form" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
	<td colspan="9" align="center"><h1><b>HISTORY SALES ORDER</b></h1></td>
  </tr>
  <tr>
	  <td colspan="9">Periode :
	      <input type="text" id="fromdate" name="fromdate" size="9" value="<?=$fromdate;?>" style="border-collapse:inherit"  readonly="true">&nbsp;s/d&nbsp;
	      <input type="text" id="todate" name="todate" size="9" value="<?=$todate;?>" style="border-collapse:inheri" readonly="true">
	      <input type="submit" name="cari" value="CARI" style="cursor:pointer">
		</td>
	</tr>
</table>
<table id="myTables" class="display" cellpadding="0" cellspacing="0">
	<thead>  
  <tr>
	<th>No</th>
	<th>No SO</th>
	<th>Tanggal SO</th>
	<th>Nama Customer</th>
	<th>Kode Barang</th>
	<th>Nama Barang</th>
	<th>Satuan</th>
	<th>Qty SO</th>
	<th>Qty Rec./Delv.</th>
	<th>Dokumen</th>
	<th>Status</th>
  </tr>
  </thead>
	<tbody>
  <?php
	$no=1; 
	$qd = mysql_query("SELECT a.id, b.pod_prodid, a.po_no, a.po_date, d.sup_name, c.prod_code, c.prod_name, c.prod_sat, b.pod_qty, 
		  e.cust_name, a.po_type, a.po_status FROM purchase_order a
		  INNER JOIN purchase_order_detail b ON b.po_no=a.po_no AND b.pod_status=10
		  INNER JOIN product c ON c.id=b.pod_prodid
		  LEFT JOIN supplier d ON d.id=a.po_idsupp
		  LEFT JOIN customer e ON e.id=a.po_idcust		  
		  WHERE a.po_status IN(20,30) AND a.po_date BETWEEN '$fromdate' AND '$todate' AND po_type='Sales' ORDER BY a.id DESC");
	while($dd=mysql_fetch_object($qd)){	
		$docbc=""; 
		$po = mysql_query("SELECT a.outh_doctype, a.outh_docno, a.outh_docdate FROM outgoing a
			   INNER JOIN outgoing_detail b ON b.outd_hid=a.id 
			   WHERE a.outh_status=20 AND a.po_no='$dd->po_no' AND b.outd_prodid='$dd->pod_prodid'");	
		while($dpo=mysql_fetch_array($po)){
			$docbc = $docbc."".$dpo['outh_doctype']."-".$dpo['outh_docno']."-".$dpo['outh_docdate'];
		}		
		$rec = mysql_fetch_array(mysql_query("SELECT SUM(b.outd_qty) qty_rec FROM outgoing a
			   INNER JOIN outgoing_detail b ON b.outd_hid=a.id 
			   WHERE a.outh_status=20 AND a.po_no='$dd->po_no' AND b.outd_prodid='$dd->pod_prodid'"));
		
		$rmn = $dd->pod_qty-$rec['qty_rec'];
	    if($dd->po_status=="20"){
			if($rmn > 0){$status = "Proses";}
			else{$status = "Completed";}
		}else{
			$status = "Completed";
		}
	   if($dd->po_type=="Purchasing"){$names = $dd->sup_name;}else{$names = $dd->cust_name;}
	?> 
	  <tr onmouseover="setTrColor(this, '#FF9900');">
		<td align="center"><?=$no;?>.</td>
		<td align="center"><?=$dd->po_no;?></td>
		<td align="center"><?=$dd->po_date;?></td>
		<td align="left"><?=$names;?></td>
		<td><?=$dd->prod_code;?></td>
		<td align="left"><?=$dd->prod_name;?></td>
		<td align="center"><?=$dd->prod_sat;?></td>
		<td align="right"><?=number_format($dd->pod_qty,2,',','.');?></td>
		<td align="right"><?=number_format($rec['qty_rec'],2,',','.');?></td>
		<td align="center"><?=$docbc;?></td>
		<td align="center"><?=$status;?></td>
	  </tr>
  <?php $no++; }  ?>
	</tbody>
</table>
</form>
