
<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(isset($_GET['accid'])){
		require 'connection.php';
		$accid= $_GET['accid'];
		$from= $_GET['from'];
		$to= $_GET['to'];

             $descp='';


		$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;
		$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 style="border-color: white;"  class="d-none d-sm-block"></td>
    <td style="border-color: white;"  class="d-none d-sm-block"></td>
    
       <td style="color: black; font-size: 14px; text-align: center; font-family: Arial; border-color: white;" colspan="6">********Opening Balance********</td>
    <td style="border-color: white;" class="d-none d-sm-block"></td>

       <td colspan="2" style="text-align: right; color: black; font-size: 14px; border-right: 1px solid; border-right-color: white; border-color: white; text-align: right;"><?php echo number_format($cashOp); ?></td>

   </tr>
        <tr style="background-color: lightblue;">
    <th style="min-width: 70px; max-width: 80px; font-size: 12px; min-height: 5px; color: black; border-left: 1px solid; border-left-color: #ECE9E8;">Dated</th>
    <th class="d-none d-sm-block" style="font-size: 12px; height: 5px; color: black;">Vno</th>
    
    
     <th colspan="4" style="font-size: 12px; height: 5px; color: black; min-width: 300px;">Description</th>
      <th class="d-none d-sm-block" style="font-size: 12px; height: 5px; color: black;">Ref.No</th> 
      <th class="d-none d-sm-block" style="font-size: 12px; height: 5px; color: black;">Type</th> 
 
    <th style="font-size: 12px; height: 5px; color: black; text-align: right;">Debit</th>
    <th style="font-size: 12px; height: 5px; color: black; text-align: right;">Credit</th>
    <th style="font-size: 12px; height: 5px; color: black; text-align: right; border-right: 1px solid; border-right-color: #ECE9E8;">Balance</th>
</tr>
<?php
$crtotal = 0;
$credit = 0;
$debit = 0;
$qty = 0;
$rowclr = 0;
$rowclrs = 0;
$drtotal = 0;
$qtytotal = 0;
$prvno = 0;
$descqty=0;
$descrate=0;


			$qry = "SELECT * from ledger where accid = '$accid' and dated >= '$from' and dated <= '$to' and itemid<=0 order by dated,trid ASC";
        $run_qry = mysqli_query($connect, $qry);
        while ($row=mysqli_fetch_array($run_qry)) {
                        $prvno=$row['vno'];
            if($row['credit']!==''){
                $credit=$row['credit'];
            }
            $crtotal = $crtotal + $credit;
              if($row['debit']!==''){
                $debit=$row['debit'];
            }
            $drtotal = $drtotal + $debit;
              if($row['qty']!==''){
                $qty=$row['qty'];
            }
            $qtytotal = $qtytotal + $qty;
            $itid = $row['itemid'];

      if($accid==1){
                $selitem = "SELECT accname,qty,vrate from broilertransfer join accreg on broilertransfer.supid=accreg.accid where vno = '$prvno'";
              $itemrun = mysqli_query($connect, $selitem);
              $itemrow = mysqli_fetch_array($itemrun);
              $descp=$itemrow['accname'];
             $descqty=$itemrow['qty'];
              $descrate=$itemrow['vrate'];
            }

               if($accid==2){
                $selitem = "SELECT accname from broilertransfer join accreg on broilertransfer.accid=accreg.accid where vno = '$prvno'";
              $itemrun = mysqli_query($connect, $selitem);
              $itemrow = mysqli_fetch_array($itemrun);
              $descp=$itemrow['accname'];
               
            }



        	 $selitem = "SELECT itemname from itemreg where itemid = '$itid'";
              $itemrun = mysqli_query($connect, $selitem);
              $itemrow = mysqli_fetch_array($itemrun);

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

}

?>

<tr style="background-color: <?php echo $rowclrs; ?>">

<td style="font-size: 12px; max-width: 80px; height: auto; min-width: 70px; 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 class="d-none d-sm-block" style="font-size: 12px; height: auto;  color: black; "><?php echo $row['vno']; ?></td>
 
 <td colspan="4" style="font-size: 12px; height: auto; color: black; min-width: 100px;">




    <?php 


if($descp==''){
    echo $row['description']; 

}
else{

    if($accid==2){
    echo $descp.' ('.$row['description'].')'; 

    }
    else
    {
    echo $descp.' (Weight='.$descqty.' @ '.$descrate.')'; 

    }

}
    
    $descp='';


    ?>
        
        




        
    </td>





<td class="d-none d-sm-block" style="font-size: 12px; height: auto; color: black;"><?php  echo $row['refno']; ?></td> 
<td class="d-none d-sm-block" style="font-size: 12px; height: auto; color: black;"><?php  echo $row['entrytype']; ?></td> 
<td style="font-size: 12px; height: auto; color: black; text-align: right;"><?php echo number_format($row['debit']); ?></td>
<td style="font-size: 12px; height: auto; color: black; text-align: right;"><?php echo number_format($row['credit']); ?></td>
<td style="font-size: 12px; height: auto; color: black; border-bottom: 1px solid #ECE9E8; text-align: right; border-right: 1px solid; border-color: #ECE9E8;"><?php echo number_format($bal=$bal+$debit-$credit); ?></td>
	
</tr>
		<?php
        	}
				?>
	
		
<tr>
    <td  class="d-none d-sm-block"></td>

    <td colspan="3"></td>
    <td  class="d-none d-sm-block"></td>
    <td  class="d-none d-sm-block"></td>
    <td colspan="2"></td>
  

    <td style="text-align: right; font-size: 12px;"> <?php echo number_format($drtotal); ?></td>

    <td style="text-align: right; font-size: 12px;"> <?php echo number_format($crtotal); ?></td>
    <td style="text-align: right; font-size: 12px;"> <?php echo number_format($drtotal-$crtotal); ?></td>


</tr>
<tr>
  
     <td class="d-none d-sm-block" style="border-color: white;"></td>
     <td colspan="4"  style="border-color: white;"></td>
     <td class="d-none d-sm-block" style="border-color: white;"></td>
 
   
    <td colspan="4" style="text-align: right; border-color: white; color: black;">Balance: </td>
   
    <td style="text-align: right; border-color: white; color: black;"><?php echo number_format($cashOp+$drtotal-$crtotal); ?></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>
