<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>
<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">
            <!-- 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>Group Summary</h3></div>
                <p>&nbsp;</p>
                
                    <table width="100%" class="table table-bordered">
                     <tr height="32" align="left">
                        <th nowrap="nowrap" width="50%" rowspan="2">Particular</th>
                        <th nowrap="nowrap" width="17%" colspan="2" style="text-align: center;">Closing Balance</th>
                    </tr>
                    <tr>
                        
                        <th nowrap="nowrap" width="25%" colspan="1">Debit(Amount)</th>
                        <th nowrap="nowrap" width="50%" colspan="1">Credit(Amount)</th>
                    </tr>
<?php


 $sql_viewtmp="SELECT * from view_purchase_det where session_login_id = '".$_SESSION['login_id']."' and purmas_id in(select id from tbl_purchase_master where session_login_id = '".$_SESSION['login_id']."') group by item_id,pur_price,sell_price ";
 $res_viewtmp=mysql_query($sql_viewtmp);

 $ret_qty=0;
$remaing=0;
$remaing_tot=0;
    while($rows=mysql_fetch_assoc($res_viewtmp))
                    {

                    $item_id=$rows["item_id"];  
                    $unit_id=$rows["unit_id"];
                    $client_id=$rows["client_id"];  
                    $pur_qty=$rows["pur_qty"];    
                    $pur_price=$rows["pur_price"];  
                    $cgst=$rows["cgst"];
                    $sgst=$rows["sgst"];
                    $igst=$rowss["igst"];
                    $sell_price=$rows["sell_price"];   

 $sql_viewret="SELECT sum(ret_qty) as ret_qty from view_purchasereturn_det where session_login_id = '".$_SESSION['login_id']."' and item_id='$item_id' and pur_price='$pur_price' and sell_price='$sell_price' and purretmas_id in(select purretmas_id from view_purret_mstrdet where session_login_id = '".$_SESSION['login_id']."' and purchasemstr_id in(select purmas_id from view_purchase_det where session_login_id = '".$_SESSION['login_id']."')) group by item_id,pur_price,sell_price ";
 $res_viewret=mysql_query($sql_viewret);  
 $ret_view=mysql_fetch_assoc($res_viewret);
                    $ret_qty=$ret_view["ret_qty"];     
                    $pur_remain=$pur_qty-$ret_qty;
                    $ret_qty;

 $sql_sl="SELECT * from view_sales_detail where session_login_id = '".$_SESSION['login_id']."' and item_id='$item_id' and unit_price ='$sell_price' and salesmas_id in (select id from tbl_sales where session_login_id = '".$_SESSION['login_id']."') group by item_id,unit_price";
                $res_sl=mysql_query($sql_sl);
                $row_sl=mysql_fetch_assoc($res_sl);
                   
                 $sel_qty=$row_sl["qty"];

  $sql_sl_ret="SELECT sum(ret_qty) as ret_qty from tbl_sales_return_details where session_login_id = '".$_SESSION['login_id']."' and item_id='$item_id' and sell_price ='$sell_price' and salesretmas_id in (select id from tbl_sales_return_mstr  where session_login_id = '".$_SESSION['login_id']."' and salesmas_id in(select id from tbl_sales where session_login_id = '".$_SESSION['login_id']."')) group by item_id,sell_price";
                $res_sl_ret=mysql_query($sql_sl_ret);
                $row_sl_ret=mysql_fetch_assoc($res_sl_ret);
                   
                 $sel_ret_qty=$row_sl_ret["ret_qty"];  

    $cgst_t=($pur_price*$cgst)/100;$c_tot+=$cgst_t;
    $sgst_t=($pur_price*$sgst)/100;$s_tot+=$sgst_t;
    $igst_t=($pur_price*$igst)/100;$i_tot+=$igst_t;
    $p_tot_gst=$pur_price+$c_tot+$s_tot+$i_tot; $p_r_tot+=$p_tot_gst;
                 $remaing=$pur_remain-$sel_qty;$remain+=$remaing;    
                 $remaing_tot=$remaing*$p_r_tot;$remain_tot+=$remaing_tot;              
           
     }    
?>
<?php 
    $sql_pur="SELECT sum(credit) as credit 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 where session_login_id = '".$_SESSION['login_id']."'";
    $res_bank=mysql_query($sql_bank);
    $row=mysql_fetch_assoc($res_bank);
    $debit_a=$row["debit"];

    ?>
    <?php 
//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 where session_login_id = '".$_SESSION['login_id']."'";
    $res_cash=mysql_query($sql_cash);
    $count=mysql_fetch_assoc($res_cash);
    $cr_amount=$count["cr_amount"];
// after that we subtract those amount from bank
   $sql_cashs="SELECT sum(dr_amount) as dr_amount from cash_detail  where session_login_id = '".$_SESSION['login_id']."'";
    $res_cashs=mysql_query($sql_cashs);
    $counts=mysql_fetch_assoc($res_cashs);
    $dr_amounts=$counts["dr_amount"];

    $cassh_tot=$dr_amounts-$cr_amount;

    ?>
                    <tr height="32" class='clickable-row' data-href='Profit_l_closing_acc.php'>
                        <td nowrap="nowrap" width="6%">Closing Stock</td>
                        <td nowrap="nowrap" width="6%"><?php echo $remain_tot; ?></td>
                        <td nowrap="nowrap" width="6%"></td>
                    </tr>
                    <tr height="32" class='clickable-row' data-href='tri_cuass_c_bank.php'>
                        <td nowrap="nowrap" width="6%">Bank</td>
                        <td nowrap="nowrap" width="6%"><?php echo $debit_a; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $credit1; ?></td>
                    </tr>
                    <tr height="32" class='clickable-row' data-href='tri_cur_a_cashdet.php'>
                        <td nowrap="nowrap" width="6%">Cash</td>
                        <td nowrap="nowrap" width="6%"><?php echo $dr_amounts; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $cr_amount; ?></td>
                    </tr>
<?php $d_side_tot=$remain_tot+$debit_a+$dr_amounts;
      $s_side_tot=$credit1+$cr_amount;
 ?>
</table>
<br /><br /><br /><br />
            <table class="table">
                    <tr height="32">
                        <td nowrap="nowrap" width="8%" style="font-weight: bold">Grand Total</td>
                        <td nowrap="nowrap" width="1%"><?php echo $d_side_tot; ?></td>
                        <td nowrap="nowrap" width="6%"><?php echo $s_side_tot; ?></td>
                    </tr>
                </table
></div>
</form>
                                         </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
      </div>
      
 <?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>