<?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-weight: bold;"><h3>Balance Sheet</h3></div>
                    <p>&nbsp;</p>
<?php
    $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"];
?>
             <table width="100%" class="table table-bordered">
             <tr height="32" align="left">
             <th nowrap="nowrap" width="17%" colspan="2" style="font-weight: bold;">Liabilities </th>  
             
             <th nowrap="nowrap" width="17%" colspan="2" style="font-weight: bold;">Assets </th> 
            
             </tr>
                     <tr height="32">
             <td class='clickable-row' data-href='bal_sheet_captil.php' nowrap="nowrap" width="6%">Captial Account</td>  
             <td class='clickable-row' data-href='bal_sheet_captil.php' nowrap="nowrap" width="6%" align="right"><?php echo $cd_amt; ?></td>
             <td nowrap="nowrap" width="6%"><a href="#"> Fixed Assets</a></td> 
             <td nowrap="nowrap" width="6%"></td> 
                     </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 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"];
// 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 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"];
// after returing product from debtors decrease ammount in debors account
   $sql_ret="SELECT sum(Dr_Amt) as Dr_Amt from tbl_account where sales_type='Sales Return' and session_login_id = '".$_SESSION['login_id']."'";
    $res_ret=mysql_query($sql_ret);
    $row=mysql_fetch_assoc($res_ret);
    $dr_Amt=$row["Dr_Amt"];

    $cre_amt=$debit_a-$credit1; 
    $tot_amt=$cre_amt+$cr_amount+$d_amount-$dr_Amt;
?> -->
<?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_t1=($pur_price*$cgst)/100;$c_tot1+=$cgst_t1;
    $sgst_t1=($pur_price*$sgst)/100;$s_tot1+=$sgst_t1;
    $igst_t1=($pur_price*$igst)/100;$i_tot1+=$igst_t1;
    $p_tot_gst1=$pur_price+$c_tot1+$s_tot1+$i_tot1; $p_r_tot1+=$p_tot_gst1;
                 $remaing1=$pur_remain-$sel_qty;$remain1+=$remaing1;    
                 $remaing_tot1=$remaing1*$p_r_tot1;$remain_tot1+=$remaing_tot1;              
           
     }    
?>
<?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 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"];

    $cre_amt=$debit_a-$credit1; 
    $tot_amt=$remain_tot1+$cre_amt+$d_amount-$cashsal_r1;
?>
             <tr height="32">
                         <td nowrap="nowrap" width="6%">Loans(Liability)</td>
                         <td nowrap="nowrap" width="6%"></td>
                          <td class='clickable-row' data-href='bal_sheet_current_asset.php' nowrap="nowrap" width="6%">Current Assets</td>  
                         <td class='clickable-row' data-href='bal_sheet_current_asset.php' nowrap="nowrap" width="6%"><?php echo $tot_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 IN (SELECT sales_type FROM tbl_account WHERE sales_type='Purchase' and session_login_id = '".$_SESSION['login_id']."');
";
$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">
                         <td class='clickable-row' data-href='bal_sheet_labilities.php' nowrap="nowrap" width="6%">Current Liabilities</td>
                         <td class='clickable-row' data-href='bal_sheet_labilities.php' nowrap="nowrap" width="6%" align="right"><?php echo $lib_tot; ?></td>
                          <td nowrap="nowrap" width="6%"></td>  
                         <td nowrap="nowrap" width="6%"></td>
             </tr>
<?php
// net profit  .....
  
 $sql_sal = "SELECT sum(dr_amount) as  dr_amount FROM cash_detail where sales_type='sale' and session_login_id = '".$_SESSION['login_id']."'";
$res_sal = mysql_query($sql_sal);
$rowsal = mysql_fetch_assoc($res_sal);
$cashsal = $rowsal["dr_amount"];

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



$sal_tot=$cashsal-$cashsal_r;
?>
<?php
$opening=0;
?>

<?php  
 $sql_tri = "SELECT sum(Dr_Amt) as Dr_amt FROM tbl_account
WHERE sales_type='Purchase' and session_login_id = '".$_SESSION['login_id']."'
";
$res_tri = mysql_query($sql_tri);
$row = mysql_fetch_assoc($res_tri);
$cash3 = $row["Dr_amt"];

 $sql_tri1 = "SELECT sum(Cr_Amt) as Cr_amt FROM tbl_account
WHERE  sales_type='Purchase Return' and session_login_id = '".$_SESSION['login_id']."'";
$res_tri1 = mysql_query($sql_tri1);
$row1 = mysql_fetch_assoc($res_tri1);
$cash31 = $row1["Cr_amt"];

$P_Total=$cash3-$cash31;
?>
<?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
    $dr_expenses=0;
    $sql_dx="SELECT sum(amount) as dx_amount from expense_details where session_login_id = '".$_SESSION['login_id']."'";
    $res_dx=mysql_query($sql_dx);
    $row_dx=mysql_fetch_assoc($res_dx);
    $dr_expenses=$row_dx["dx_amount"];
?>

             <?php
           $gr_profit=0;
           $loss=0;
           $gr_debit=$opening+$P_Total;
           $gr_credit=$sal_tot+round($remain_tot);
           $gr_profit=$gr_credit-$gr_debit;
           
          
            ?>

<?php 
$d_side_total=$opening+$P_Total+$gr_profit;
$c_side_total=$sal_tot+$remain_tot;
 ?>

<?php
$indirect_amount=0;
$sql_in="SELECT sum(amount) as amount  from indirect_expenses where session_login_id = '".$_SESSION['login_id']."'";
$res_in=mysql_query($sql_in);
$ind=mysql_fetch_assoc($res_in);
$indirect_amount=$ind["amount"];
?>

<?php
$sql_acc="SELECT sum(discount) as discount from tbl_account  where sales_type='Purchase' and session_login_id = '".$_SESSION['login_id']."'";
$res_acc=mysql_query($sql_acc);
$row_acc=mysql_fetch_assoc($res_acc);
$discount_acc=$row_acc["discount"];
?>
<?php
$net_d=$d_side_total+$dr_expenses+$indirect_amount;
$net_cr=$c_side_total+$gr_profit;
$net_profit=$net_cr-$net_d;
$net_total_dr=$net_d+$net_profit;




//end net profit...
?>
             <tr height="32" class='clickable-row' data-href='p_l_account.php'>
                    
                         <td nowrap="nowrap" width="6%" style="font-weight: bold">Profit & Loss A/c</td>
                         <td nowrap="nowrap" width="6%" align="right" style="font-weight: bold"><?php echo $net_profit; ?></td>
                          <td nowrap="nowrap" width="6%"></td>  
                         <td nowrap="nowrap" width="6%"></td>
             </tr>
 
<?php
$opening=0;
$cur_per_amt=$net_profit-$opening;
?>
             <tr height="32">
                         <td nowrap="nowrap" width="6%">Opening Balance</td>
                         <td nowrap="nowrap" width="6%" align="right"><?php echo $opening; ?></td>
                          <td nowrap="nowrap" width="6%"></td>  
                         <td nowrap="nowrap" width="6%"></td>
             </tr>
             <tr height="20">
                         <td nowrap="nowrap" width="6%">Current Period</td>
                         <td nowrap="nowrap" width="6%" ><?php echo $cur_per_amt; ?></td>
                          <td nowrap="nowrap" width="6%"></td>  
                         <td nowrap="nowrap" width="6%"></td>
             </tr>
           <tr height="20">
                         <td nowrap="nowrap" width="6%">Difference in Opening Balances</td>
                         <td nowrap="nowrap" width="6%" align="right"><?php echo $differ_amt=$cd_amt+$cash1+$cur_per_amt+$Opening-$tot_amt;?></td>
                          <td nowrap="nowrap" width="6%">Gross Profit b/f</td>  
                         <td nowrap="nowrap" width="6%"></td>
             </tr>
            </table>
<?php
$t_r_side=$cd_amt+$cash1+$net_profit+$opening;
$t_l_side=$tot_amt+$differ_amt;
?>             <br><br><br><br><br>
             <table class="table">
             <tr height="20" >
                         <td nowrap="nowrap" width="6%" style="font-weight: bold">Total</td>
                         <td nowrap="nowrap" width="6%" align="right" style="font-weight: bold"><?php echo $t_r_side; ?></td>&nbsp;&nbsp;&nbsp;&nbsp;
                         <td nowrap="nowrap" width="4%" style="font-weight: bold">Total</td>  
                         <td nowrap="nowrap" width="6%" style="font-weight: bold"><?php echo $t_l_side; ?></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>