<!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_once "./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&Act=CARI';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function excel_click(){
		form.action ='./transaksi/akuntansi/export_buku_besar.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function pdf_click(){
		form.action ='./transaksi/akuntansi/pdf_buku_besar.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=1&sa_id='+sa_id;
		form.method = 'post';
		form.submit();		
		return true;
	}
</script>
<?php
if(isset($_REQUEST['Act'])=="CARI"){
	 $fromdate	 = $_REQUEST['fromdate'];
	 $todate     = $_REQUEST['todate']; 
	 $j_acctno   = $_REQUEST['j_acctno']; 
 }else{
	 $fromdate	 = $date;
	 $todate     = $date;	
	 $j_acctno   = "BLANK";
 }
 if($j_acctno != "BLANK"){
	$searchh    = "WHERE j_status=10 AND j_acctno='$j_acctno' ";
}else{
	$searchh    = "WHERE j_status=10";
}
?>
</head>
<body>
<form action="#" method="post" name="form">
<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
	<td colspan="8" align="center"><h1><b>DATA BUKU BESAR</b></h1></td>
  </tr>
  <tr>
	<td colspan="8">
		<img src="images/btn_add.png" height="30" width="30" title="Saldo Awal" border="0" style="cursor:pointer" onclick="add_click(1);" />
	</td>
  </tr>
  <tr>
	<td colspan="7">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:inheri">
		  <select name="j_acctno">
		  		<option value="BLANK" selected="selected">SEMUA</option> 
			<?php
			  $dataSql = mysql_query("SELECT * FROM jurnal_master WHERE j_status=10 ORDER BY j_acctno ASC");
			  while ($dataRow = mysql_fetch_array($dataSql)) {
				if ($dataRow['j_acctno']== $j_acctno) {
					$cek = " selected";
				} else { $cek=""; }
				echo "<option value='$dataRow[j_acctno]' $cek>$dataRow[j_acctno]-$dataRow[j_name1]</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();" />
	</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">Tanggal</th>
	<th rowspan="2">No.Jurnal</th>
	<th rowspan="2">Keterangan</th>
	<th rowspan="2">No Faktur</th>
	<th rowspan="2">Sup./Cust.</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
  	$qcoa = mysql_query("SELECT j_acctno, CONCAT_WS(' - ', j_acctno, j_name1) codename, j_rumusbb FROM jurnal_master $searchh ORDER BY j_acctno ASC");
	while($dcoa=mysql_fetch_array($qcoa)){
	?>
	<tr>
    	<td colspan="13"><strong>ACCOUNT : <?=$dcoa['codename'];?></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>
			<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=10 AND sa_acctno='$dcoa[j_acctno]'
			ORDER BY id DESC LIMIT 0,1"));
		?> 
		<tr>
			<td>&nbsp;</td>
			<td colspan="9">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 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 =$sa['sa_price']; $t_amountin=0; $t_amountout=0; $saldo_usd =$sa['sa_price_usd']; $t_amountin_usd=0; $t_amountout_usd=0;
	$qd=mysql_query("SELECT a.tanggal, a.jurnal_no, a.keterangan, a.jurnal_nofaktur, a.custsup_name, a.amountin, a.amountin_usd, a.amountout, a.amountout_usd, a.flag FROM
			(SELECT a.id, a.jurnal_date tanggal, a.jurnal_no, b.jurnald_desc keterangan, a.jurnal_nofaktur, 
			CASE WHEN a.jurnal_custname<>'' THEN a.jurnal_custname ELSE a.jurnal_suppname END AS custsup_name, b.jurnald_amount amountin, b.jurnald_amount_usd amountin_usd, 
			0 amountout, 0 amountout_usd, 1 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
			WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=10		
			UNION ALL
			SELECT a.id, a.jurnal_date tanggal, a.jurnal_no, b.jurnald_desc keterangan, a.jurnal_nofaktur, 
			CASE WHEN a.jurnal_custname<>'' THEN a.jurnal_custname ELSE a.jurnal_suppname END AS custsup_name, 0 amountin, 0 amountin_usd,
			b.jurnald_amount amountout, b.jurnald_amount_usd amountout_usd, 2 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
			WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=20) a
			ORDER BY a.tanggal ASC");
	$num = mysql_num_rows($qd);		
	if($num==0){
		$t_saldo_usd = $saldo_usd;
		$t_saldo     = $saldo;
	}else{		
	while($dd=mysql_fetch_array($qd)){	
		if($dcoa['j_rumusbb']=='A'){
			$saldo_usd 	= $saldo_usd+$dd['amountin_usd']-$dd['amountout_usd'];
			$saldo 		= $saldo+$dd['amountin']-$dd['amountout'];
		}else{
			$saldo_usd 	= $saldo_usd-$dd['amountin_usd']+$dd['amountout_usd'];
			$saldo 		= $saldo-$dd['amountin']+$dd['amountout'];
		}
		$t_saldo_usd = $saldo_usd;
		$t_saldo	 = $saldo;
		$t_amountin_usd  = $t_amountin_usd+$dd['amountin_usd'];
		$t_amountin  	 = $t_amountin+$dd['amountin'];
		$t_amountout_usd = $t_amountout_usd+$dd['amountout_usd'];
		$t_amountout 	 = $t_amountout+$dd['amountout'];
	?>
	 <tr onmouseover="setTrColor(this, '#FF9900');">
		<td align="center"><?=$no;?>.</td>
		<td align="left"><?=$dd['tanggal'];?></td>
		<td align="left"><?=$dd['jurnal_no'];?></td>
        <td align="left"><?=$dd['keterangan'];?></td>
		<td align="left"><?=$dd['jurnal_nofaktur'];?></td>
		<td align="left"><?=$dd['custsup_name'];?></td>
        <td align="right"><?=number_format($dd['amountin_usd'],2,'.',',');?></td>
		<td align="right"><?=number_format($dd['amountin'],2,'.',',');?></td>
        <td align="right"><?=number_format($dd['amountout_usd'],2,'.',',');?></td>
		<td align="right"><?=number_format($dd['amountout'],2,'.',',');?></td>
        <td align="right"><?=number_format($saldo_usd,2,'.',',');?></td>
		<td align="right"><?=number_format($saldo,2,'.',',');?></td>
		<td>&nbsp;</td>
	  </tr>	  
  <?php $no++; } } ?> 
  	  <tr>
      	<td>&nbsp;</td>
        <td colspan="5"><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 align="right"><b><?=number_format($t_amountin_usd,2,'.',',');?></b></td>
		<td align="right"><b><?=number_format($t_amountin,2,'.',',');?></b></td>
        <td align="right"><b><?=number_format($t_amountout_usd,2,'.',',');?></b></td>
		<td align="right"><b><?=number_format($t_amountout,2,'.',',');?></b></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="#CCFFFF">
	  	<td colspan="13">&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>
		<td style="display:none;"></td>
	  </tr>
  <?php } ?>
</tbody>  
</table>
</div>
</form>
</body>
</html>
