<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>
<script type="text/javascript">
 
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

</script>
<script type="text/javascript">

window.addEventListener("keyup", function(e){ if(e.keyCode == 27) history.back(); }, false);

</script>
<style type="text/css">
    
        tr:hover {background-color:#bcd1cd;}

</style>
      <div class="page-wrapper" style="background-color: #eae2c9;">
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body" id="printableArea">
 

<form method="post">
		<div class="panel">
			<div class="panel-heading" style="font-style: bold"><h3>Trial Balance</h3> </div>
				<p>&nbsp;</p>
                
                    <table width="100%" class="table table-bordered">
                     <tr height="32" align="left">
			            <th nowrap="nowrap" width="35%" rowspan="2">Particular</th>
						<th nowrap="nowrap" width="17%" colspan="2" style="text-align: center;">Closing Balance</th>
					</tr>
                    <tr>
                        
                        <th nowrap="nowrap" width="35%" colspan="1">Debit(Amount)</th>
                        <th nowrap="nowrap" width="35%" colspan="1">Credit(Amount)</th>
                    </tr>
<?php
// here we select all debtors sale in debite side 
    $sql_debtor="SELECT sum(dr_amount) as dr_amount from client_details where session_login_id = '".$_SESSION['login_id']."'";
    $res_debtor=mysql_query($sql_debtor);
    $count=mysql_fetch_assoc($res_debtor);
    $d_amount=$count["dr_amount"];
?>
<?php
// here we subtract sale return item from sale
    $sql_sal_r1 = "SELECT sum(Dr_Amt) as  Dr_Amt FROM tbl_account where sales_type='Sales Return' and session_login_id = '".$_SESSION['login_id']."'";
    $res_sal_r1 = mysql_query($sql_sal_r1);
    $rowsal_r1 = mysql_fetch_assoc($res_sal_r1);
    $cashsal_r1 = $rowsal_r1["Dr_Amt"];
//here we select cash in hand from cash to as assets that should be credited
    // $sql_cash="SELECT sum(cr_amount) as cr_amount from cash_detail";
    // $res_cash=mysql_query($sql_cash);
    // $count=mysql_fetch_assoc($res_cash);
    // $cr_amount=$count["cr_amount"];
// here we select cash in bank from bank to as assets that should be credited
// here at first sum those amount that are purchased 
    $sql_pur="SELECT sum(credit) as credit, sum(debit) as debit from tbl_bank where session_login_id = '".$_SESSION['login_id']."'";
    $res_pur=mysql_query($sql_pur);
    $coun=mysql_fetch_assoc($res_pur);
    $credit1=$coun["credit"];  
// after that we subtract those amount from bank


   /*$sql_bank="SELECT sum(debit) as debit from tbl_bank";
    $res_bank=mysql_query($sql_bank);
    $row=mysql_fetch_assoc($res_bank);*/
    $debit_a=$coun["debit"];

    $cre_amt=$debit_a-$credit1; 
    $tot_amt=$cre_amt+$d_amount-$cashsal_r1;
?>


                    <tr height="32" class='clickable-row' data-href='current_trail_ass.php'>
                        <td nowrap="nowrap" width="6%">Current Assets</td>
                        <td nowrap="nowrap" width="6%"><?php echo $tot_amt; ?></td>
                        <td nowrap="nowrap" width="6%"></td>
                    </tr>

<?php
// captial amount from first investement
    $sql_capt="SELECT sum(credit_amount) as credit_amount from captial_ac where session_login_id = '".$_SESSION['login_id']."'";
    $res_capt=mysql_query($sql_capt);
    $rows=mysql_fetch_assoc($res_capt);
    $cd_amt=$rows["credit_amount"];
?>

                    <tr height="32" class='clickable-row' data-href='captial_trail_ass.php'>
                        <td nowrap="nowrap" width="6%">Captial Account</td>
                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"><?php echo $cd_amt; ?></td>
                    </tr>
<?php  
// here we select creditors for sum of total amt that are purchased
 $sql_tri = "SELECT sum(cr_amount) as Cr_amt FROM supplier_details
WHERE session_login_id = '".$_SESSION['login_id']."' and  sales_type='Purchase';
";
$res_tri = mysql_query($sql_tri);
$row = mysql_fetch_assoc($res_tri);
$cash1 = $row["Cr_amt"];

    $sql_pu="SELECT sum(credit) as credit from tbl_bank where head='purchase_payment' and session_login_id = '".$_SESSION['login_id']."'";
    $res_pu=mysql_query($sql_pu);
    $cou=mysql_fetch_assoc($res_pu);
    $credit2=$cou["credit"]; 

$lib_tot=$cash1-$credit2;
?>


                    <tr height="32" class='clickable-row' data-href='liablitie_trail.php'>
                        <td nowrap="nowrap" width="6%">Current Liablities</td>
                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"><?php echo $lib_tot; ?></td>
                    </tr>

<?php  
// here table name Purchase are shown
$sql_tri = "SELECT *,sum(Dr_Amt) as Dr_amt FROM tbl_account WHERE session_login_id = '".$_SESSION['login_id']."' and sales_type='Purchase'";
$res_tri = mysql_query($sql_tri);
$row = mysql_fetch_assoc($res_tri);
$sales_type = $row["sales_type"];
$cash3 = $row["Dr_amt"];
?>

					<tr height="32" class='clickable-row' data-href='purchase_trail_bal.php'>
            			<td nowrap="nowrap" width="6%"><?php echo $sales_type; ?></td>
<?php  
// here sum of all purchased item that are purchaed through supplier
 /*$sql_tri = "SELECT sum(Dr_Amt) as Dr_amt FROM tbl_account
WHERE sales_type IN (SELECT sales_type FROM tbl_account WHERE sales_type='Purchase');
";
$res_tri = mysql_query($sql_tri);
$row = mysql_fetch_assoc($res_tri);
$cash3 = $row["Dr_amt"];*/

// here we select those item that are return to supplier
 $sql_tri1 = "SELECT sum(Cr_Amt) as Cr_amt FROM tbl_account
WHERE session_login_id = '".$_SESSION['login_id']."' and  sales_type='Purchase Return';
";
$res_tri1 = mysql_query($sql_tri1);
$row1 = mysql_fetch_assoc($res_tri1);
$cash32 = $row1["Cr_amt"];

$P_Total=$cash3+$cash32;
?>

            			<td nowrap="nowrap" width="6%"><?php echo $P_Total; ?></td>
                        <td nowrap="nowrap" width="6%"></td>
            		</tr>

 <?php  
 // here sale account shown in table
    $sql_tri = "SELECT *, sum(Cr_Amt) as  Cr_Amt FROM tbl_account WHERE session_login_id = '".$_SESSION['login_id']."' and sales_type='Sales'";
    $res_tri = mysql_query($sql_tri);
    $row = mysql_fetch_assoc($res_tri);
    $sales_type1 = $row["sales_type"];
	$cashsal = $row["Cr_Amt"];
?>
              
                    <tr height="32" class='clickable-row' data-href='sale_trail_bal.php'>
                        <td nowrap="nowrap" width="6%"><?php echo $sales_type1; ?></td>
<?php
//here we select sales iteem that are sale//
    /*$sql_sal = "SELECT sum(Cr_Amt) as  Cr_Amt FROM tbl_account where sales_type='Sales'";
    $res_sal = mysql_query($sql_sal);
    $rowsal = mysql_fetch_assoc($res_sal);
    $cashsal = $rowsal["Cr_Amt"];*/

// here we subtract sale return item from sale
    $sql_sal_r = "SELECT sum(Dr_Amt) as  Dr_Amt FROM tbl_account where session_login_id = '".$_SESSION['login_id']."' and sales_type='Sales Return'";
    $res_sal_r = mysql_query($sql_sal_r);
    $rowsal_r = mysql_fetch_assoc($res_sal_r);
    $cashsal_r = $rowsal_r["Dr_Amt"];

    $sale_ret_tot=$cashsal-$cashsal_r;

?>

                        <td nowrap="nowrap" width="6%"></td>
                        <td nowrap="nowrap" width="6%"><?php echo $sale_ret_tot;  ?></td>
                    </tr>
   <?php $d_total= $tot_amt+$P_Total?>
  <?php $sales_credit = $cd_amt+$lib_tot+$sale_ret_tot; ?>
  <?php $sale_difference=$d_total-$sales_credit ?>
  <?php $after_diff=$sale_difference+$sales_credit ?>
                    <tr height="32">
                        <td colspan="2" width="18%" style="font-weight: bold; text-align: right">Difference in Opening Balance</td>
                        <td nowrap="nowrap" width="6%"><?php echo $sale_difference; ?></td>
                    </tr>
            </table>
            <br><br><br><br>
            <table class="table">
                    <tr height="32">
                        <td nowrap="nowrap" width="6%" style="font-weight: bold">Ground Total</td>
                        <td nowrap="nowrap" width="6%"><?php echo  $d_total; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo  $after_diff; ?></td>
                    </tr>
            	</table>
</form>
</div>
<br />
<br /><br /><br /><br />
<div align="center"><span class="btn btn-info" onClick="printDiv('printableArea')">Print</span></div>

  </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
<?php include 'footer.php' ?>
 <script src="../js/lib/datatables/datatables.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
    <script src="../js/lib/datatables/cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"></script>
    <script src="../js/lib/datatables/datatables-init.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
    $(".clickable-row").click(function() {
        window.location = $(this).data("href");
    });
});
</script>