
<head>

    <meta charset="utf-8">
    <title>DIGITAL ACCOUNTS MANAGEMENT</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Charisma, a fully featured, responsive, HTML5, Bootstrap admin template.">
    <meta name="author" content="KASHIF ALI">

    <!-- The styles -->
    <link id="bs-css" href="css/bootstrap-cerulean.min.css" rel="stylesheet">

    <link href="css/charisma-app.css" rel="stylesheet">
    <link href='bower_components/fullcalendar/dist/fullcalendar.css' rel='stylesheet'>
    <link href='bower_components/fullcalendar/dist/fullcalendar.print.css' rel='stylesheet' media='print'>
    <link href='bower_components/chosen/chosen.min.css' rel='stylesheet'>
    <link href='bower_components/colorbox/example3/colorbox.css' rel='stylesheet'>
    <link href='bower_components/responsive-tables/responsive-tables.css' rel='stylesheet'>
    <link href='bower_components/bootstrap-tour/build/css/bootstrap-tour.min.css' rel='stylesheet'>
    <link href='css/jquery.noty.css' rel='stylesheet'>
    <link href='css/noty_theme_default.css' rel='stylesheet'>
    <link href='css/elfinder.min.css' rel='stylesheet'>
    <link href='css/elfinder.theme.css' rel='stylesheet'>
    <link href='css/jquery.iphone.toggle.css' rel='stylesheet'>
    <link href='css/uploadify.css' rel='stylesheet'>
    <link href='css/animate.min.css' rel='stylesheet'>


 <link rel="stylesheet" href="css/jquery-ui.css">
  <!-- <link rel="stylesheet" href="/resources/demos/style.css"> -->
  <script src="js/jquery-1.12.4.js"></script>
  <script src="js/jquery-ui.js"></script>
    <link rel="stylesheet" href="css/bootsrap_my.css" />


    <!-- jQuery -->
    <script src="bower_components/jquery/jquery.min.js"></script>

    <link rel="shortcut icon" href="img/favicon.ico">
<!-- ****************************************** -->

  <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"> -->
  <link rel="stylesheet" href="css/font-awesome.min2.css">
  <link rel="stylesheet" href="dist/css/bootstrap-select.css">

<!-- ************************************************ -->
</head>



 <body  style="background-color: white; margin-top: 0%; ">
 <div  class="container"> 



<?php 
 echo @$_GET['msg'];
  session_start();
    if(isset($_SESSION['userId'])){
    if($_SESSION['userId']==52 or $_SESSION['userId']==2532){
    if(isset($_GET['accid'])){
        require 'connection.php';
        $accid= $_GET['accid'];
        $from= $_GET['from'];
        $to= $_GET['to'];
        $qry = "SELECT * from accreg where accid = '$accid'";
        $run = mysqli_query($connect, $qry);
        $row = mysqli_fetch_array($run);
        $accId = $row['accid'];
        $name = $row['accname'];
        // finding opening balance 
        $qry1 = "SELECT sum(debit)-sum(credit) as cashOp from ledger where accid = '$accid' and dated < '$from'";
        $run_qry1 = mysqli_query($connect, $qry1);
        $row1 = mysqli_fetch_array($run_qry1);
        $cashOp = $row1['cashOp'];
        // end of opening balance
        // finding opening balance 
        $qry2 = "SELECT sum(debit)-sum(credit) as close, min(dated) as firstdate, max(dated) as lastdate  from ledger where accid = '$accid' and dated <= '$to'";
        $run_qry2 = mysqli_query($connect, $qry2);
        $row2 = mysqli_fetch_array($run_qry2);
        $closingB = $row2['close'];

        $fdate = $row2['firstdate'];
         $dt1= date_create($fdate);
         $fdt=date_format($dt1,"d-m-Y");

        $ldate = $row2['lastdate'];
         $dt2= date_create($ldate);
         $ldt=date_format($dt2,"d-m-Y");
        $sum = 0;
        $bal = $cashOp;
   $runbal = $cashOp;
        $debitT = 0;
        $creditT = 0;
    $qtyT = 0;
        ?>
         <div style="background-color: white; margin-top: 20px;" class="table-responsive"> 
             <button style="align-self: center; min-width: 50px; min-height: 30px;" class="btn btn-warning btn-sm" id="closebtn"><i class="glyphicon glyphicon-off"></i></button>
               <button style="min-width: 70px;"  class="btn btn-primary btn-sm" id="printbtn"><i class="glyphicon glyphicon-print"></i> Print</button> 
               <br><br>

    <h1> Account Ledger </h1> 
    <br>
    <h3>Account Name: &nbsp;&nbsp;<?php echo $name; ?></h3>

<table style="background-color: white;" class="table  table-stripped col-md-12">    

 <tr>

 
     <td  class="d-none d-sm-block" style="border: none; font-weight: bold; font-size: 12px; height: auto; color: black;"></td> 
    <td  class="d-none d-sm-block" style="border: none; font-weight: bold; font-size: 12px; height: auto; color: black;"></td> 
   
    <td  class="d-none d-sm-block" style="border: none; font-weight: bold; font-size: 12px; height: auto; color: black;">************Opening Balance************</td> 

    <td  class="d-none d-sm-block" style="border: none; font-weight: bold; font-size: 12px; height: auto; color: black;"></td> 
    <td  class="d-none d-sm-block" style="border: none; font-weight: bold; font-size: 12px; height: auto; color: black;"></td> 

    <td  class="d-none d-sm-block" style="text-align: right; border: none; font-weight: bold; font-size: 12px; height: auto; color: black;"><?php  echo $cashOp; ?></td> 

</tr>



 <tr style="background-color: lightblue;">
    <th style="font-weight: bold; min-width: auto; font-size: 12px; min-height: 5px; color: black; border-left: 1px solid; border-left-color: #ECE9E8;">Dated</th>

       <th colspan="2" class="d-none d-sm-block" style="font-size: 12px; height: 5px; color: black;">Description</th>
       

       <th  style=" font-weight: bold; font-size: 12px; height: 5px; color: black;">Debit</th>
       <th  style=" font-weight: bold; font-size: 12px; height: 5px; color: black;">Credit</th>
       <th  style=" font-weight: bold; font-size: 12px; height: 5px; color: black;">
<label class="pull-right">
    Balance
</label>
       </th>

</tr>
<?php
$crtotal = 0;
$crate = 0;
$qty = 0;
$rowclr = 0;
$rowclrs = 0;
$qtytotal = 0;
$balok = 0;
            $qry = "SELECT dated from ledger where accid='$accid' and dated>='$from' and dated<='$to' group by dated order by dated asc";
        $run_qry = mysqli_query($connect, $qry);
        while ($row=mysqli_fetch_array($run_qry)) {
            $qrydated=$row['dated'];
 
         $qry34 = "SELECT sum(qty) as qty, crate, dated from broilertransfer where accid='$accid' and dated='$qrydated' group by crate, dated order by dated asc";
        $run_qry34 = mysqli_query($connect, $qry34);
        while ($row34=mysqli_fetch_array($run_qry34)) {
            $qtytotal = $qtytotal + $row34['qty'];
            $crate = $row34['crate'];
            $qty = $row34['qty'];
            $balok = $crate*$qty;
            $debitT=$debitT+$balok;
            $qtytotal = $qtytotal + $row34['qty'];

       if ($rowclr==1){
$rowclrs='#D8D8D8';
$rowclr=0;
       }
else{
$rowclrs='#FFFFFF';
$rowclr=1;

}


    
?>

<tr style="background-color: <?php echo $rowclrs; ?>">
<td style=" font-weight: bold; font-size: 12px; height: auto; min-width: 100px; color: black; width: auto; border-left: 1px solid; border-left-color: #ECE9E8;">

    <?php 
  $dt1= date_create($row['dated']);
         $fdt=date_format($dt1,"d-m-Y");
         echo $fdt;
     ?>
        

    </td>


 <td colspan="2" class="d-none d-sm-block" style=" font-weight: bold; font-size: 12px; height: auto; color: black;"><?php  echo $row34['qty'].' @ '.$row34['crate']; ?></td> 
 <td style=" font-weight: bold; font-size: 12px; height: auto; color: black; max-width: 100px;"><?php echo $balok; ?></td>
  <td style=" font-weight: bold; font-size: 12px; height: auto; color: black; max-width: 100px;"><?php echo '0'; ?></td>
 <td style=" border-right: 1px; font-weight: bold; font-size: 12px; height: auto; color: black; max-width: 100px;">

<label class="pull-right">
    
 <?php
  $runbal=$runbal+$balok;
 echo $runbal; ?>
</label>
   

</td>
    
</tr>


        <?php

    }




      $qry23 = "SELECT dated, description, debit, credit from ledger where credit>0 and accid='$accid' and dated='$qrydated' or entrytype<>'BRL-SL' and accid='$accid' and dated='$qrydated' order by trid asc";
        $run_qry23 = mysqli_query($connect, $qry23);
        while ($row23=mysqli_fetch_array($run_qry23)) {
      
            $debit = $row23['debit'];
            $credit = $row23['credit'];
            $creditT =$creditT +$row23['credit'];
            $description = $row23['description'];
            $dateds = $row23['dated'];

            if ($rowclr==1){
$rowclrs='#D8D8D8';
$rowclr=0;
       }
else{
$rowclrs='#FFFFFF';
$rowclr=1;

}
  
?>

<tr style="background-color: <?php echo $rowclrs; ?>">
<td style=" font-weight: bold; font-size: 12px; height: auto; min-width: 100px; color: black; width: auto; border-left: 1px solid; border-left-color: #ECE9E8;">

    <?php 
  $dt1= date_create($dateds);
         $fdt=date_format($dt1,"d-m-Y");
         echo $fdt;
     ?>
        

    </td>


 <td colspan="2" class="d-none d-sm-block" style=" font-weight: bold; font-size: 12px; height: auto; color: black;"><?php  echo $description; ?></td> 
 <td style=" font-weight: bold; font-size: 12px; height: auto; color: black; max-width: 100px;"><?php echo $debit; ?></td>
  <td style=" font-weight: bold; font-size: 12px; height: auto; color: black; max-width: 100px;"><?php echo $credit; ?></td>
 <td style="border-right: 1px; border-right-color: #ECE9E8; font-weight: bold; font-size: 12px; height: auto; color: black; max-width: 100px;">
<label class="pull-right">
   <?php 

$runbal=$runbal+$debit-$credit;
 echo $runbal; ?>
</label>


 
</td>
    
</tr>


        <?php

    }

            }
                ?>
    
        
        <?php
            }
        }
        else{
   header('location: ../wb2/index.php');
}
    }
    else{
   header('location: ../wb2/index.php');
}
                ?>

<tr>
<td style=" font-weight: bold; font-size: 12px; height: auto; min-width: 100px; color: black; width: auto;  border-left-color: #ECE9E8;">

    </td>

 <td colspan="2" class="d-none d-sm-block" style=" font-weight: bold; font-size: 12px; height: auto; color: lightgray;"></td> 

 <td style=" font-weight: bold; font-size: 12px; height: auto; color: gray; max-width: 100px;"><?php echo $debitT; ?></td>

  <td style=" font-weight: bold; font-size: 12px; height: auto; color: gray; max-width: 100px;"><?php echo $creditT; ?></td>
 <td style=" border-right-color: #ECE9E8; font-weight: bold; font-size: 12px; height: auto; color: gray; max-width: 100px;">
<label class="pull-right">
     <?php
  $rundiff=$debitT-$creditT;
 echo $rundiff; ?>
</label>
   
</td>
    
</tr>




<tr>
<td style="border: none; font-weight: bold; font-size: 12px; height: auto; min-width: 100px; color: black; width: auto;  border-left-color: #ECE9E8;">

    </td>

 <td colspan="2" class="d-none d-sm-block" style="border: none; font-weight: bold; font-size: 12px; height: auto; color: lightgray;"></td> 

 <td style="border: none; font-weight: bold; font-size: 12px; height: auto; color: gray; max-width: 100px;"></td>

  <td style="border: none; font-weight: bold; font-size: 16px; height: auto; color: gray; max-width: 100px;"></td>
 <td style=" border: none; font-weight: bold; font-size: 12px; height: auto; color: gray; max-width: 100px;">

</td>
    
</tr>




<tr>

 <td colspan="6" style=": right; border: none;  font-weight: bold; font-size: 16px; height: auto; color: black; max-width: 100px;">

<label class="pull-right">
         <?php
  
 echo 'Balance: '.$closingB; ?> 


</label>
  
</td>
    
</tr>
</table>

 </div>

</div>
<script src="plugins/common/common.min.js"></script>
    <script src="js/custom.min.js"></script>
    <script src="js/settings.js"></script>
    <script src="js/gleek.js"></script>
    <script src="js/styleSwitcher.js"></script>

    <!-- Chartjs -->
    <script src="./plugins/chart.js/Chart.bundle.min.js"></script>
    <!-- Circle progress -->
    <script src="./plugins/circle-progress/circle-progress.min.js"></script>
    <!-- Datamap -->
    <script src="./plugins/d3v3/index.js"></script>
    <script src="./plugins/topojson/topojson.min.js"></script>
    <script src="./plugins/datamaps/datamaps.world.min.js"></script>
    <!-- Morrisjs -->
    <script src="./plugins/raphael/raphael.min.js"></script>
    <script src="./plugins/morris/morris.min.js"></script>
    <!-- Pignose Calender -->
    <script src="./plugins/moment/moment.min.js"></script>
    <script src="./plugins/pg-calendar/js/pignose.calendar.min.js"></script>
    <!-- ChartistJS -->
    <script src="./plugins/chartist/js/chartist.min.js"></script>
    <script src="./plugins/chartist-plugin-tooltips/js/chartist-plugin-tooltip.min.js"></script>
    <script src="./js/dashboard/dashboard-1.js"></script>


<!-- Group Reg Table Script -->
    <script src="./plugins/tables/js/jquery.dataTables.min.js"></script>
    <script src="./plugins/tables/js/datatable/dataTables.bootstrap4.min.js"></script>
    <script src="./plugins/tables/js/datatable-init/datatable-basic.min.js"></script>

<script type="text/javascript">
    $(document).ready(function(e){
        
        $('#printbtn').click(function(){
            $('#printbtn').hide();
            $('#closebtn').hide();
              window.print();
        })

        $('#closebtn').click(function(){
       
              window.close();
        })

    });
</script>
</body>
