<?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>Profit And Loss Account</h3> </div>
                    <p>&nbsp;</p>
                     <table width="100%" class="table table-bordered">
                     <tr height="32" align="left">
             <th nowrap="nowrap" width="17%">Particulars </th>  
             <th nowrap="nowrap" width="17%"></th>
             <th nowrap="nowrap" width="17%">Particulars </th> 
             <th nowrap="nowrap" width="17%"> </th> 
             </tr>
  
 <?php  
 $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;
?>
           
             <tr height="32">
                 <td nowrap="nowrap" width="6%">Opening Stock</td>
                 <td nowrap="nowrap" width="6%" align="right"><?php echo $opening; ?></td>
                  <td class='clickable-row' data-href='Profit_l_sale_acc.php' nowrap="nowrap" width="6%">Sales Account</a></td>  
                 <td class='clickable-row' data-href='Profit_l_sale_acc.php' nowrap="nowrap" width="6%"><?php echo $sal_tot;?></td>
             </tr>
<?php  
 $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);
$cash3 = $row["Dr_amt"];

 $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);
$cash31 = $row1["Cr_amt"];

$P_Total=$cash3-$cash31;
?>
<?php
// .......................................Closing Stock Summary.............................................//

 $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 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 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+$cgst_t+$sgst_t+$igst_t; $p_r_tot+=$p_tot_gst;
                 $remaing=$pur_remain-$sel_qty;$remain+=$remaing;    
                 $remaing_tot=$remaing*$p_tot_gst;$remain_tot+=$remaing_tot;              
           
     }    
?>
             <tr height="32">
                 <td class='clickable-row' data-href='Profit_l_purchase_acc.php' nowrap="nowrap" width="6%">Purchase Account</td>
                 <td class='clickable-row' data-href='Profit_l_purchase_acc.php' nowrap="nowrap" width="6%" align="right"><?php echo $P_Total; ?></td>
                  <td class='clickable-row' data-href='Profit_l_closing_acc.php' nowrap="nowrap" width="6%">Closing Stock</td>  
                 <td class='clickable-row' data-href='Profit_l_closing_acc.php' nowrap="nowrap" width="6%"><?php echo round($remain_tot); ?></td>
             </tr>
<?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"];
?>
             <tr height="32">
                 <td nowrap="nowrap" width="6%">Direct Expenses</td>
                 <td nowrap="nowrap" width="6%" align="right"><?php echo $dr_expenses; ?></td>
                  <td nowrap="nowrap" width="6%"></td>  
                 <td nowrap="nowrap" width="6%"></td>
             </tr>

             <tr height="32">
             <?php
           $gr_profit=0;
           $loss=0;
           $gr_debit=$opening+$P_Total;
           $gr_credit=$sal_tot+round($remain_tot);
           $gr_profit=$gr_credit-$gr_debit;
           
          
            ?>
                 <td nowrap="nowrap" width="6%" style="font-weight: bold">Gross Profit c/o</td>
                 <td nowrap="nowrap" width="6%" align="right" style="font-weight: bold"><?php echo round($gr_profit); ?></td>
                  <td nowrap="nowrap" width="6%"></td>  
                 <td nowrap="nowrap" width="6%"></td>
             </tr>
<?php 
$d_side_total=$opening+$P_Total+$gr_profit;
$c_side_total=$sal_tot+$remain_tot;
 ?>
             <tr height="20">
                 <td nowrap="nowrap" width="6%"></td>
                 <td nowrap="nowrap" width="6%" align="right" style="font-weight: bold"><?php echo $d_side_total; ?></td>
                  <td nowrap="nowrap" width="6%"></td>  
                 <td nowrap="nowrap" width="6%" style="font-weight: bold"><?php echo round($c_side_total); ?></td>
             </tr>
<?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"];
?>
             <tr height="20">
                 <td nowrap="nowrap" width="6%">Indirect Expenses</td>
                 <td nowrap="nowrap" width="6%" align="right"><?php echo $indirect_amount; ?></td>
                  <td nowrap="nowrap" width="6%">Gross Profit b/f</td>  
                 <td nowrap="nowrap" width="6%"><?php echo $gr_profit; ?></td>
             </tr>
<?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;
?>
             <tr height="20">
                 <td nowrap="nowrap" width="6%" style="font-weight: bold;">Nett Profit</td>
                 <td nowrap="nowrap" width="6%" align="right"><?php echo $net_profit; ?></td>
                  <td nowrap="nowrap" width="6%">Indirect Incomes</td>  
                 <td nowrap="nowrap" width="6%"><?php echo $discount_acc; ?></td>
             </tr>
                    </table>
                    <br><br><br><br>
            <table class="table">
                <tr height="30" >
                 <td nowrap="nowrap" width="6%">Total</td>
                 <td nowrap="nowrap" width="6%" align="right"><?php echo $net_total_dr; ?></td>
                  <td nowrap="nowrap" width="6%">Total</td>  
                 <td nowrap="nowrap" width="6%"><?php echo $net_cr; ?></td>
             </tr>
            </table>
            <br><br>
            <div align="center">
                <input type="submit" name="Save" value="Save" class="btn btn-success">
            </div>
    </div>
    </form>
    <?php
    if(isset($_POST["Save"])){

   $sql_insert="INSERT into tbl_p_l (opening_stock, closing_stock, netprofit,session_login_id) values('$opening', '$remain_tot', '$net_profit','".$_SESSION['login_id']."')";
    $res_insert=mysql_query($sql_insert);
    }
    ?>


                            </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>