<?php 
include('config.php');
$autoid 	= $conn->real_escape_string($_POST['autoid']);
$detailid 	= $conn->real_escape_string($_POST['dautoid']);
$coaid 		= $conn->real_escape_string($_POST['coa']);
$debet 		= $conn->real_escape_string($_POST['debet']);
$credit 	= $conn->real_escape_string($_POST['credit']);
$coaid2 		= $conn->real_escape_string($_POST['coa2']);
$debet2 		= $conn->real_escape_string($_POST['debet2']);
$credit2 	= $conn->real_escape_string($_POST['credit2']);
$vcno 		= $conn->real_escape_string($_POST['vcno']);
$notes 		= $conn->real_escape_string($_POST['notes']);
$previd 	= $conn->real_escape_string($_POST['previd']);
$prevcoa 	= $conn->real_escape_string($_POST['prevcoa']);
$vcdate 	= dtosql($_POST['vcdate']);
$previd 	= $conn->real_escape_string($_POST['previd']);
$act 		= $_POST['act'];

if($act=="1"){

	$insert = "insert into jtdetail 
		(jt_autoid,coa_autoid,jtd_debit,jtd_credit,jtd_vcno,jtd_vcdate,jtd_notes,coa_autoid2,jtd_debIt2,jtd_credit2) 
		values ('$autoid','$coaid','$debet','$credit','$vcno','$vcdate','$notes','$coaid2','$debet2','$credit2')";
		if($coaid==""||$coaid2=="")
			{}
		else{
			$conn->query($insert);
			}

}
if($act=="99"){
$conn->query("delete from jtdetail where jtd_autoid = '$detailid'");
}
?>
<style type="text/css">
.tdetail{
	border-collapse:collapse;
}
.tdetail td{
	border:1px gray solid;
}

img{
	cursor:pointer;
}
</style>

<table class="table table-sm table-border w-100">
<thead>
<tr class="thead" style="background-color:#333;color:white">
	<td class="tl" style="width:80px;">VC No</td>
    <td class="tl" style="width:80px;">VC Date</td>
	<td class="tl" style="width:250px;">Chart of Account</td>
    <td class="tr" style="width:120px;">Debet</td>
	<td class="tr" style="width:120px">Credit</td>
	<td class="tc" style="width:130px">Catatan</td>
	<td style="width:20px"></td>
</tr>
</thead>
<?php
$x = 0;
$tdebit = 0;
$tcredit = 0;
$sqld = "select * from jtdetail WHERE jt_autoid = '$autoid' ORDER BY jtd_autoid DESC";
$dcon = $conn->query($sqld) or die($sqld);
while($rs= $dcon->fetch_assoc()){
	
	$jtd_autoid = $rs['jtd_autoid'];
	$coa_autoid_1 = $rs['coa_autoid'];
	$coa_autoid_2 = $rs['coa_autoid2'];
	if($x%2==0){
		$style = "background-color:#FFFDDF";
		}else{$style="background-color:white";}

?>
<tr class="tdetail" style="<?php echo ($style)?>">
	<td><?php echo $rs['jtd_vcno'];?> </td>
	<td><?php echo dtostr($rs['jtd_vcdate']);?></td>
	<td><?php echo $_coa_accno[$coa_autoid_1] . " - " .$_coa_accdesc[$coa_autoid_1]?></td>
	<td class="tr"><?php echo number_format($rs['jtd_debit']);?>&nbsp;</td>
	<td class="tr"><?php echo number_format($rs['jtd_credit']);?>&nbsp;</td>
    <td style="background-color:white" rowspan="2"><?php echo $rs['jtd_notes'];?></td>
	
	<td style="background-color:white" rowspan="2" class="tc">
    <input type="button" class="btn btn-sm btn-danger" value="Hapus" onclick="getdetail('99','<?php echo $rs['jtd_autoid']?>')">

    	<input type="hidden" id="coa<?php echo $jtd_autoid?>" value="<?php echo $rs[coa_autoid]?>"/>
		<input type="hidden" id="debet<?php echo $jtd_autoid?>" value="<?php echo $rs[jtd_debit]?>"/>
        <input type="hidden" id="credit<?php echo $jtd_autoid?>" value="<?php echo $rs[jtd_credit]?>"/>
        <input type="hidden" id="coadesc<?php echo $jtd_autoid?>" value="<?php echo $rs[coa_accdesc]?>"/>
        <input type="hidden" id="notes<?php echo $jtd_autoid?>" value="<?php echo $rs[jtd_notes]?>"/>
        <input type="hidden" id="vcno<?php echo $jtd_autoid?>" value="<?php echo $rs[jtd_vcno]?>"/>
        <input type="hidden" id="vcdate<?php echo $jtd_autoid?>" value="<?php echo dtostr($rs[jtd_vcdate])?>"/>
		<input type="hidden" id="ncoa<?php echo $jtd_autoid?>" value="<?php echo $rs[coa_autoid2]?>"/>
		<input type="hidden" id="ndebet<?php echo $jtd_autoid?>" value="<?php echo $rs['jtd_debit2']?>"/>
        <input type="hidden" id="ncredit<?php echo $jtd_autoid?>" value="<?php echo $rs['jtd_credit2']?>"/>
        <input type="hidden" id="ncoadesc<?php echo $jtd_autoid?>" value="<?php echo $coadesc?>"/>
    </td>
</tr>
<tr class="tdetail" style="<?php echo ($style)?>">
	<td><?php echo $rs['jtd_vcno'];?> </td>
	<td><?php echo dtostr($rs['jtd_vcdate']);?></td>
	<td><span style="margin-left:20px"></span><?php echo $_coa_accno[$coa_autoid_2] . " - " .$_coa_accdesc[$coa_autoid_2]?></td>
	<td class="tr"><?php echo number_format($rs['jtd_debit2']);?>&nbsp;</td>
	<td class="tr"><?php echo number_format($rs['jtd_credit2']);?>&nbsp;</td>
</tr>
<?php 
$tdebit = $tdebit + $rs['jtd_debit'] + $rs['jtd_debit2'];
$tcredit = $tcredit + $rs['jtd_credit']+ $rs['jtd_credit2'];
$x = $x+1;
}
$strdx = "select * from jtdetail where jt_autoid = '$autoid' order by jtd_autoid desc";
$dx_conn = $conn->query($strdx) or die($strdx);
$rdx = $dx_conn->fetch_assoc();
$njtdautoid = $rdx['jtd_autoid'];
$ncoaid 	= $rdx['coa_autoid'];

if($tdebit==$tcredit){
	$njtdautoid = "";
	$ncoaid = "";
	}
?>
<tr class="theader" style="background-color:#CCC;font-weight:bold;font-size:14px;">
	<td colspan="3" class="tr">Total : </td>
	<td class="tr"><?php echo number_format($tdebit)?>&nbsp;</td>
	<td class="tr"><?php echo number_format($tcredit)?>&nbsp;</td>
    <td></td>
	<td class="tr">
		<input type="hidden" id="jmldebet" value="<?php echo $tdebit?>"/>
		<input type="hidden" id="jmlcredit" value="<?php echo $tcredit?>"/>
        <input type="hidden" id="previd" value="<?php echo $njtdautoid?>"/>
        <input type="hidden" id="prevcoa" value="<?php echo $ncoaid?>"/>
	</td>

</tr>
</table>
