<?php 
include('config.php');
$coa = $_POST['coa'];
$fdate = dtosql($_POST['fdate']);
$tdate = dtosql($_POST['tdate']);
$tahun 			= substr($tdate,0,4);
$outlet = $_POST['outlet'];

$strq = "select * from mst_coa a, balance b where a.coa_autoid = b.coa_autoid 
		and a.coa_autoid = '$coa' and b.bb_year = '$tahun' AND outlet_autoid = '$outlet' ";
$ccon = $conn->query($strq);
$rsx = $ccon->fetch_assoc();
	$saldoawal 	= $rsx['bb_balance'];
	$coaaccno 	= $rsx['coa_accno'];
	$coadesc 	= $rsx['coa_accdesc'];
	$ngroup		= $rsx['coa_accgroup'];
	
$sqlsaldo_a = "select sum(b.jtd_debit) as debet1, sum(b.jtd_credit) as credit1
			 from jtheader a, jtdetail b where a.jt_autoid = b.jt_autoid and 
			a.jt_flagpost <> 'X' and (b.jtd_vcdate < '$fdate') and
			(b.coa_autoid = '$coa') AND a.outlet_autoid = '$outlet'";
$con_saldo_a = $conn->query($sqlsaldo_a) or die($sqlsaldo_a);
$rssaldo_a = $con_saldo_a->fetch_assoc();

$debet_a = $rssaldo_a['debet1'];
$credit_a = $rssaldo_a['credit1'];

$sqlsaldo_b = "select sum(b.jtd_debit2) as debet2, sum(b.jtd_credit2) as credit2
			 from jtheader a, jtdetail b where a.jt_autoid = b.jt_autoid and 
			a.jt_flagpost <> 'X' and (b.jtd_vcdate < '$fdate') and
			(b.coa_autoid2 = '$coa')  AND a.outlet_autoid = '$outlet'";
$con_saldo_b = $conn->query($sqlsaldo_b) or die($sqlsaldo_b);
$rssaldo_b = $con_saldo_b->fetch_assoc();

$debet_b = $rssaldo_b['debet2'];
$credit_b = $rssaldo_b['credit2'];

$sdebet = $debet_a + $debet_b;
$scredit = $credit_a + $credit_b;

if($ngroup==6){
$saldoawal = $saldoawal - ($sdebet -$scredit);
	}else{
$saldoawal = $saldoawal + ($sdebet -$scredit);
		}
?>
<table class="table" style="font-size:12px;">
<thead>

<tr>
	<td colspan="8" style="font-weight:bold;border:none;height:30px;font-size:12px">
	BUKU BESAR - <?php echo strtoupper($_outlet_name[$outlet])?><br/>
	PERIODE : <?php echo iddate($fdate)?> - <?php echo iddate($tdate)?>
	</td>
</tr>
<?php
$gdebit = 0;
$gcredit = 0;
?>
<tr>
<td colspan="8" style="text-align:left;padding-left:5px;font-weight:bold;border:none">
<?php echo $_coa_accno[$coa]?> | <?php echo $_coa_accdesc[$coa]?>
</td>
</tr>
<tr class="thead bold">
	<td style="width:80px">VC No</td>
	<td style="width:80px">VC Date</td>
	<td style="width:60px">COA No</td>
	<td style="width:200px">COA Description</td>
	<td style="width:90px" class="tr">Debet</td>
	<td style="width:90px" class="tr">Credit</td>
	<td style="width:100px" class="tr">Balance</td>
    <td style="width:230px">Notes</td>
    <td style="width:100px"></td>
</tr>
</thead>
<tr class="alert alert-success">
	<td colspan="2"></td>
	<td colspan="3" class="tl">Saldo Awal :</td>
	<td class="tr" colspan="2" style="font-weight:bold"><?php echo number_format($saldoawal);?>&nbsp;</td>
    <td colspan="2"></td>
</tr>

<?php
$xsaldo = $saldoawal;

$sqld = "select * from jtheader a, jtdetail b where a.jt_autoid = b.jt_autoid and 
		a.jt_flagpost <> 'X' and (b.jtd_vcdate between '$fdate' and '$tdate') and
		(b.coa_autoid = '$coa' or b.coa_autoid2 = '$coa')  AND a.outlet_autoid = '$outlet' order by b.jtd_vcdate";
$dcon = $conn->query($sqld);
while($rs = $dcon->fetch_assoc()){
	$lvl	 	= $rs['coa_acclvl'];
	$accno 		= $rs['coa_accno'];
	$jno 		= $rs['jt_no'];
	$jdate 		= $rs['jt_date'];
	
	if($coa==$rs['coa_autoid']){
		$debet = $rs[jtd_debit];
		$credit = $rs[jtd_credit];
		$coaid = $rs['coa_autoid2'];
	}
		
	if($coa==$rs['coa_autoid2']){
		$debet = $rs[jtd_debit2];
		$credit = $rs[jtd_credit2];
		$coaid = $rs['coa_autoid'];
	}

if($ngroup==6){
	$saldo = $xsaldo - $debet + $credit;
	}else{
$saldo = $xsaldo + $debet-$credit;		
		}



?>
<tr class="trb" style="font-size:11px">
	<td><?php echo $rs['jtd_vcno'];?></td>
	<td><?php echo iddate2($rs['jtd_vcdate']);?></td>
	<td><?php echo $_coa_accno[$coaid];?></td>
	<td style="<?php echo $class?>"><?php echo $_coa_accdesc[$coaid];?></td>
	<td class="tr"><?php echo number_format($debet);?></td>
	<td class="tr"><?php echo number_format($credit);?></td>
	<td class="tr"><?php echo number_format($saldo);?>&nbsp;</td>
    <td>&nbsp;<?php echo $rs['jtd_notes'];?></td>
</tr>

<?php 
$xsaldo = $saldo;
$gdebit 	= $gdebit + $debet;
$gcredit 	= $gcredit + $credit;
}

$saldoakhir = $xsaldo;

?>

<tr style="font-weight:bold;" class="alert alert-info">
	<td colspan="4" style="text-align:right">Saldo Akhir .: </td>
	<td class="tr"><?php echo number_format($gdebit)?></td>
	<td class="tr"><?php echo number_format($gcredit)?></td>
	<td class="tr"><?php echo number_format($saldoakhir)?>&nbsp;</td>
    <td colspan="2"></td>
    
</tr>
</table>