<!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>
<?php include "./load_plugin.php";?>
<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,
		});
	});
	function setTrColor(obj, color)
	{
		obj.style.backgroundColor=color;
		obj.onmouseout = function(){
				obj.style.backgroundColor=''; 
		}
	}
	function search_click(){
		form.action ='?page=Data-Buku-Besar-Piutang&Act=CARI';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function payment_click(outhid){
		form.action ='?page=Proses-Buku-Besar-Piutang&Act=PAY_ADD&outhid='+outhid;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function excel_click(){
		form.action ='./transaksi/akuntansi/export_buku_besar_piutang.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function exceldet_click(){
		form.action ='./transaksi/akuntansi/export_buku_besar_piutang_detail.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function pdf_click(){
		form.action ='./transaksi/akuntansi/pdf_buku_besar_piutang.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function add_click(flag){
		form.action ='?page=Proses-Stock-Awal-Acct&Act=ADD&flag='+flag;
		form.method = 'post';
		form.submit();		
		return true;
	}
	function detail_click(sa_id){
		form.action ='?page=Proses-Stock-Awal-Acct&Act=DETAIL&flag=3&sa_id='+sa_id;
		form.method = 'post';
		form.submit();		
		return true;
	}
</script>
<?php
if(isset($_REQUEST['Act'])=="CARI"){
	 $npwp	 	= isset($_REQUEST['npwp']) ? $_REQUEST['npwp'] : '';
	 if($npwp=="BLANK"){
		$search    = "";
	 }else{
	 	$search    = "npwp='$npwp' AND";
	 }
	 $fromdate	 = $_REQUEST['fromdate'];
	 $todate     = $_REQUEST['todate'];
 }else{
  	 $search    = "";
	 $fromdate	 = $date;
	 $todate     = $date;
 }
?>
</head>
<body>
<form action="#" method="post" name="form">
<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
	<td colspan="12" align="center"><h1><b>BUKU BESAR PIUTANG (PEMBANTU/DETAIL)</b></h1></td>
  </tr>
  <tr>
	<td colspan="12">
		<img src="images/btn_add.png" height="30" width="30" title="Saldo Awal" border="0" style="cursor:pointer" onclick="add_click(3);" />
	</td>
  </tr>
  <tr>
	<td colspan="11">Periode :
	      <input type="date" id="fromdate" name="fromdate" size="9" value="<?=$fromdate;?>" style="border-collapse:inherit" >&nbsp;s/d&nbsp;
	      <input type="date" id="todate" name="todate" size="9" value="<?=$todate;?>" style="border-collapse:inherit">&nbsp;
		   <select name="npwp">
		  		<option value="BLANK" selected="selected">SEMUA</option> 
			<?php
			  $dataSql = mysql_query("SELECT a.npwp, a.nama FROM
					(SELECT sup_npwp npwp, sup_name nama FROM supplier WHERE sup_status=10
					UNION ALL
					SELECT cust_npwp npwp, cust_name nama FROM customer WHERE cust_status=10) a
					WHERE a.npwp is not null GROUP BY a.npwp ORDER BY a.nama ASC");
			  while ($dataRow = mysql_fetch_array($dataSql)) {
				if ($dataRow['npwp']== $npwp) {
					$cek = " selected";
				} else { $cek=""; }
				echo "<option value='$dataRow[npwp]' $cek>$dataRow[nama]</option>";
			  }
			  $sqlData ="";
			?>
		  </select>
	      <input type="button" name="cari" value="CARI" style="cursor:pointer" onclick="search_click();">
		  <input type="button" name="export" value="EXCEL" style="cursor:pointer" onClick="excel_click();" />		  
		  <!--<input type="button" name="pdf" value="Export to PDF" style="cursor:pointer" onClick="pdf_click();"/>
		  <input type="button" name="det" value="EXCEL DETAIL" style="cursor:pointer" onClick="exceldet_click();"/>-->
	</td>		
  </tr>
</table>  
<div class="scroll">
<table id="datatable" class="table table-bordered" width="100%">
<thead>
  <tr height="25px;">
	<th rowspan="2">No</th>
	<th rowspan="2">No. Jurnal</th>
	<th rowspan="2">Tanggal</th>
	<th rowspan="2">No.Invoice</th>
	<th rowspan="2">Keterangan</th>
	<th colspan="2">Debit</th>
	<th colspan="2">Kredit</th>
    <th colspan="2">Saldo</th>
	<th rowspan="2">Action</th>
  </tr>
   <tr>
	<th>Non IDR</th>
	<th>IDR</th>
	<th>Non IDR</th>
	<th>IDR</th>
	<th>Non IDR</th>
	<th>IDR</th>
  </tr>
</thead>
<tbody>
  <?php
  	$gt_saldo_usd=0; $gt_saldo=0; 
	$qh=mysql_query("SELECT a.npwp, a.nama FROM
					(SELECT sup_npwp npwp, sup_name nama FROM supplier WHERE sup_status=10
					UNION ALL
					SELECT cust_npwp npwp, cust_name nama FROM customer WHERE cust_status=10) a
					WHERE $search a.npwp is not null GROUP BY a.npwp ORDER BY a.nama ASC");
	while($dh=mysql_fetch_array($qh)){
	?> 
    <tr bgcolor="#FFFFCC">
        <td colspan="12"><strong>CUSTOMER : <?=$dh['nama'];?></strong></td>
		<!--hack datatable fixed header-->
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
    </tr>
	<?php
		$sa=mysql_fetch_array(mysql_query("SELECT id, sa_price, sa_price_usd FROM stock_acct WHERE sa_date='$fromdate' AND sa_status=30 AND sa_idpemasok='$dh[npwp]'"));
		?> 
		<tr>
			<td>&nbsp;</td>
			<td colspan="8">Saldo Awal</td><!--hack datatable fixed header-->
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td align="right"><?=number_format($sa['sa_price_usd'],2,'.',',');?></td>
			<td align="right"><?=number_format($sa['sa_price'],2,'.',',');?></td>
			<td align="center">
				<img src="images/btn_edit.png" width="20" height="20" border="0" title="Edit" style="cursor:pointer" onclick="detail_click('<?=$sa['id'];?>');" />
			</td>
		</tr>
    <?php
	$no=1; $saldo_usd = $sa['sa_price_usd']; $saldo = $sa['sa_price']; $t_amountin_usd=0; $t_amountin=0; $t_amountout_usd=0; $t_amountout=0;
	$qd=mysql_query("SELECT a.* FROM (
				SELECT a.id, a.jurnal_no, a.jurnal_idpemasok npwp, a.jurnal_date tanggal, a.jurnal_invno noreferensi, a.jurnal_desc keterangan, b.jurnald_acctno, 0 tampil, b.jurnald_amount debit_idr, 
				b.jurnald_amount_usd debit_nidr, 0 kredit_idr, 0 kredit_nidr
				FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_type='PT' AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND b.jurnald_status=10 AND a.jurnal_menu<>'UMUM'
				UNION ALL
				SELECT a.id, a.jurnal_no, a.jurnal_idpemasok npwp, a.jurnal_date tanggal, b.jurnald_invno_acct noreferensi, b.jurnald_desc keterangan, b.jurnald_acctno, 1 tampil, b.jurnald_amount debit_idr, 
				b.jurnald_amount_usd debit_nidr, 0 kredit_idr, 0 kredit_nidr
				FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_type IN('PT') AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND b.jurnald_status=10 AND a.jurnal_menu IN('UMUM')
				UNION ALL
				SELECT a.id, a.jurnal_no, a.jurnal_idpemasok npwp, a.jurnal_date tanggal, b.jurnald_invno_acct noreferensi, b.jurnald_desc keterangan, b.jurnald_acctno, 1 tampil, 0 debit_idr, 
				0 debit_nidr, b.jurnald_amount kredit_idr, b.jurnald_amount_usd kredit_nidr
				FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_type IN('AR') AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND b.jurnald_status=20 AND a.jurnal_menu IN('BCALL','UMUM')) a 
				WHERE a.npwp='$dh[npwp]' ORDER BY a.id ASC");
	$num = mysql_num_rows($qd);		
	if($num==0){
		$t_saldo_usd=$saldo_usd;
		$t_saldo=$saldo;
	}else{		
	while($dd=mysql_fetch_array($qd)){	
		# cek settingan master GL
		$cekhtpt = mysql_fetch_array(mysql_query("SELECT j_tampilhtpt FROM jurnal_master WHERE j_acctno='$dd[jurnald_acctno]'"));
		if($cekhtpt['j_tampilhtpt']=="Y"){
		#Non IDR
		$saldo_usd = $saldo_usd - $dd['debit_nidr'] + $dd['kredit_nidr'];
		$t_saldo_usd=$saldo_usd;
		$t_amountin_usd=$t_amountin_usd+$dd['debit_nidr'];
		$t_amountout_usd=$t_amountout_usd+$dd['kredit_nidr'];
		#IDR
		$saldo = $saldo - $dd['debit_idr'] + $dd['kredit_idr'];
		$t_saldo=$saldo;
		$t_amountin=$t_amountin+$dd['debit_idr'];
		$t_amountout=$t_amountout+$dd['kredit_idr'];
	?>
	 <tr onmouseover="setTrColor(this, '#FF9900');">
		<td align="center"><?=$no;?>.</td>
		<td align="center"><?=$dd['jurnal_no'];?></td>
		<td align="left"><?=$dd['tanggal'];?></td>
		<td align="left"><?=$dd['noreferensi'];?></td>
        <td align="left"><?=$dd['keterangan'];?></td>
        <td align="right"><?=number_format($dd['debit_nidr'],2,'.',',');?></td>
		<td align="right"><?=number_format($dd['debit_idr'],2,'.',',');?></td>
        <td align="right"><?=number_format($dd['kredit_nidr'],2,'.',',');?></td>
		<td align="right"><?=number_format($dd['kredit_idr'],2,'.',',');?></td>
        <td align="right"><?=number_format($t_saldo_usd,2,'.',',');?></td>
		<td align="right"><?=number_format($t_saldo,2,'.',',');?></td>
		<td>&nbsp;</td>
	  </tr>	  
	<?php $no++; } } }?> 
  	  <tr>
      	<td>&nbsp;</td>
        <td colspan="8"><b>TOTAL</b></td>
		<!--hack datatable fixed header-->
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
        <td align="right"><b><?=number_format($t_saldo_usd,2,'.',',');?></b></td>
		<td align="right"><b><?=number_format($t_saldo,2,'.',',');?></b></td>
		<td>&nbsp;</td>
      </tr>
	  <tr bgcolor="#CCFFCC">
	  	<td colspan="12">&nbsp;</td>
		<!--hack datatable fixed header-->
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
	  </tr>
  <?php $gt_saldo_usd=$gt_saldo_usd+$t_saldo_usd;
  		$gt_saldo=$gt_saldo+$t_saldo;
  } ?> 	 
  	   <tr bgcolor="#CCFF00">
      	<td>&nbsp;</td>
        <td colspan="8"><b>GRAND TOTAL</b></td>
		<!--hack datatable fixed header-->
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
			<td style="display:none;"></td>
        <td align="right"><b><?=number_format($gt_saldo_usd,2,'.',',');?></b></td>
		<td align="right"><b><?=number_format($gt_saldo,2,'.',',');?></b></td>
		<td>&nbsp;</td>
      </tr> 
</tbody>
</table>
</div>
</form>
</body>
</html>
