<?php 
 echo @$_GET['msg'];
  // session_start();
  // if(isset($_SESSION['userId'])){ 
if(isset($_POST['dated'])){
extract($_POST);


 require 'connection.php';
$qry = "SELECT vno,qty,vrate, srate FROM chicksinout where dated='$dated' order by vno asc";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
$vno=$row['vno'];


  $qry1 = "SELECT supid,accid FROM chicksinout where vno='$vno'";
  $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
  $row1 = mysqli_fetch_array($run1);
$supid=$row1['supid'];
$accid=$row1['accid'];

$qry1 = "SELECT accname FROM accreg where accid='$supid'";
  $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
  $row1 = mysqli_fetch_array($run1);
$supliername=$row1['accname'];

$qry1 = "SELECT accname FROM accreg where accid='$accid'";
  $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
  $row1 = mysqli_fetch_array($run1);
$custname=$row1['accname'];

?>
                
         <tr>
        <td class="center"><?php echo $row['vno']; ?></td>
        <td class="center"><i style="color: green; font-weight: bold;"><?php echo $supliername; ?></i>
        	<br><i style="color: brown; font-weight: bold;"><?php echo $custname; ?></i></td>
        <td class="center"><?php echo $row['qty'];?></td>
        <td style="color: green; font-weight: bold;" class="center"><?php echo $row['vrate']; ?></td>
        <td style="color: brown; font-weight: bold;" class="center"><?php echo $row['srate']; ?></td>
        <td  class="center vdltbtn"><button id="" style="background-color: red" class="btn btn-warning btn-sm">Delete</button></td>

  
    </tr>


   <?php } 



}

if(isset($_POST['minilgr'])){
extract($_POST);
 
require 'connection.php';
    $accid= $_POST['minilgr'];
   $qry1 = "SELECT max(dated) as dated from ledger where accid = '$accid' and entrytype='Transfer'";
        $run_qry1 = mysqli_query($connect, $qry1);
        $row1 = mysqli_fetch_array($run_qry1);
if($row1==1){
    $from= $row1['dated']; 
}
else{
    $qry1 = "SELECT min(dated) as dated from ledger where accid = '$accid' ";
        $run_qry1 = mysqli_query($connect, $qry1);
        $row1 = mysqli_fetch_array($run_qry1);
    $from= $row1['dated'];

}

      $to=date("Y/m/d");
    $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;
    ?>

   
    <tr>
    <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;">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;
$drtotal = 0;
$qtytotal = 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)) {
            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'];
           $selitem = "SELECT itemname from itemreg where itemid = '$itid'";
              $itemrun = mysqli_query($connect, $selitem);
              $itemrow = mysqli_fetch_array($itemrun);
?>

<tr>
<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 echo $row['description']; ?></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 $row['debit']; ?></td>
<td style="font-size: 12px; height: auto; color: black; text-align: right;"><?php echo $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 $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 colspan="2"></td>
  

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

    <td style="text-align: right; font-size: 12px;"> <?php echo $crtotal; ?></td>
    <td style="text-align: right; font-size: 12px;"> <?php echo $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="3" style="text-align: right; border-color: white; color: black;">Balance: </td>
   
    <td style="text-align: right; border-color: white; color: black;"><?php echo $cashOp+$drtotal-$crtotal; ?></td>
</tr>
<?php
}
?>
