<?php 
if(isset($_POST['aname'])){

extract($_POST);
 $qtytotal=0;
// $qry = "SELECT sum(ifnull(debit,0))-sum(ifnull(credit,0)) as clsBal,ledger.accid,accname from ledger inner join accreg on ledger.accid = accreg.accid where accreg.groupid='$gid' group by ledger.accid";
//         $run_qry = mysqli_query($connect, $qry) or die(mysqli_error($connect));
//         while($row = mysqli_fetch_array($run_qry)){

          // *******************************

require 'connection.php';

$qry2 = "SELECT * FROM groupreg where groupname='$aname'";
   $run2 = mysqli_query($connect, $qry2) or die(mysqli_error($connect));
  $row2 = mysqli_fetch_array($run2);
$groupid=$row2['groupid'];

$flockcounter=0;
$pls=0;
 
 $qryflockno = "SELECT max(flockno) as flockno from monthlyreport where accid = '$groupid'";
        $run_qryflockno = mysqli_query($connect, $qryflockno);
      $rowflockno=mysqli_fetch_array($run_qryflockno);
$flockcounter=$rowflockno['flockno'];



 $qry21 = "SELECT * from monthlyreport where accid = '$groupid'";

        $run_qry21 = mysqli_query($connect, $qry21);

        while ($row21=mysqli_fetch_array($run_qry21)) {
        $pls=$pls+$row21['credit']-$row21['debit'];
        }


$qry28 = "SELECT sum(debit)-sum(credit) as expens FROM ledger inner join accreg on ledger.accid=accreg.accid where groupid='$groupid'";
   $run28 = mysqli_query($connect, $qry28) or die(mysqli_error($connect));
  $row28 = mysqli_fetch_array($run28);
$totlexp=$row28['expens'];
  // $qry = "SELECT * FROM groupreg where groupname='$aname'";
  //  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
  //  while($row = mysqli_fetch_array($run)){?>
<tr>
  <td colspan="4" style=" text-align: center; color: red; font-size: 40px;"><?php echo $aname; ?></td>
  
</tr>

   <tr>
      <th   style=" font-weight: bold;  font-size: 16px; background-color: black; color: white;">Account Name</th>
      <th  style=" font-weight: bold;  font-size: 16px; background-color: black; color: white;">Amount</th>
    <!--   <th>Qty</th>
  <th>Amount</th> -->
                                    
   </tr>
   <?php
   //    $qry1 = "SELECT sum(ifnull(debit,0))-sum(ifnull(credit,0)) as chicks FROM ledger where accid=276";
   // $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
   // $row1 = mysqli_fetch_array($run1);
   $sr=0;
   $clr='';
   $totalexpense=1;
   $check_if_chicks_in_miscellaneous=0;
$qry = "SELECT sum(ifnull(debit,0))-sum(ifnull(credit,0)) as clsBal,ledger.accid,accname,accid2 from ledger inner join accreg on ledger.accid = accreg.accid where accreg.groupid='$groupid' group by accreg.accid2";
        $run_qry = mysqli_query($connect, $qry) or die(mysqli_error($connect));
        while($row = mysqli_fetch_array($run_qry)){
          $acId = $row['accid'];
          $acId2 = $row['accid2'];
          if($acId2==1){
            $check_if_chicks_in_miscellaneous=1;
          }
        if($acId2==7){
          $miscid=$acId;
        }

           $sr=$sr+1;
            if($sr==1 or$sr==3 or $sr==5 or $sr==7 or $sr==9 or $sr==11 ){
    $clr='white';

   }
  
    else{
   $clr='cyan';

   }
     if($acId2==1){
    // CHICKS
      $qrychk = "SELECT max(dated) as dateds from ledger where accid = '$acId' and entrytype='Transfer'";
$run_qrychk = mysqli_query($connect, $qrychk);
 $rowchk=mysqli_fetch_array($run_qrychk);
 $chkdated=$rowchk['dateds'];

      $qry22 = "SELECT sum(qty) as chickqty, min(dated) as mindate from chicksinout where accid = '$acId' and dated > '$chkdated'";
        $run_qry22 = mysqli_query($connect, $qry22);
       $row22=mysqli_fetch_array($run_qry22);
 $totalexpense=$totalexpense+$row['clsBal'];
	?>

	<tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'CHICKS Qty=  '.$row22['chickqty'];?> <br> <?php echo 'dated= '.$row22['mindate']; ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>

 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==2){
 $totalexpense=$totalexpense+$row['clsBal'];

$qryfb = "SELECT max(dated) as dateds from ledger where accid = '$acId' and entrytype='Transfer'";
$run_qryfb = mysqli_query($connect, $qryfb);
 $rowfb=mysqli_fetch_array($run_qryfb);
 $fddated=$rowfb['dateds'];

      $qry21 = "SELECT * from ledger where accid = '$acId' and dated > '$fddated' and itemid>0 and smid=1 or accid = '$acId' and dated > '$fddated' and itemid>0 and smid=2 order by dated,trid ASC";

        $run_qry21 = mysqli_query($connect, $qry21);

        while ($row21=mysqli_fetch_array($run_qry21)) {
   
 if($row21['entrytype']=="Sales-Return"){
   $qtytotal = $qtytotal - $row21['qty'];
}
else{
    $qtytotal = $qtytotal + $row21['qty'];
}

}
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'FEED Bags= '.$qtytotal; ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal']; ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==3){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'MEDICINE' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==4){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'DIESEL' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==5){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'KITCHEN' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==6){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'ELECTRICITY BILL' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==7){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'MISCELLANEOUS' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==8){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'REPAIRING EXPENSE' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
   if($acId2==9){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'SALARY' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
// SUPERVISOR EXPENSE START
   if($acId2==10){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'SUPERVISOR EXPENSE' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
// SUPERVISOR EXPENSE END


// SHED RENT START
   if($acId2==11){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'SHED RENT' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}
// SHED RENT END


   if($acId2==12){
 $totalexpense=$totalexpense+$row['clsBal'];
   
  ?>
  <tr>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo 'BRADA BARDANA' ?> </td>
 <td style=" font-weight: bold; background-color: <?php echo $clr ?>; font-size: 16px;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>

<?php
}

   if($acId2==13){
 $totalexpense=$totalexpense+$row['clsBal'];
 $qtydate=$row22['mindate'];

      $qrybrlqty = "SELECT sum(qty) as brlqty from broilertransfer where supid = '$acId' and dated > '$qtydate'";
        $run_qrybrlqty = mysqli_query($connect, $qrybrlqty);
      $rowbrlqty=mysqli_fetch_array($run_qrybrlqty);

  ?>
  <tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: <?php echo $clr ?>;"> 
  <?php echo 'BROILER SALES';?>
  <br> 
  <?php echo 'Total weight='.$rowbrlqty['brlqty']; ?> 

</td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: <?php echo $clr ?>;"> <?php echo $row['clsBal'] ?> </td>
 <!-- <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td>
 <td style="color: blue; font-weight: bold;"> <?php //echo $row1['chicks']; ?>  </td> -->
</tr>


<?php


?>
<tr>
 <td colspan="3" style=" font-weight: bold;  font-size: 16px; background-color: black;"> </td>

</tr>

 <tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'TOTAL EXPENSE' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $totlexp; ?> </td>

</tr>


<?php 
  
// PHP code to find the number of days 
// between two given dates 
  
// Function to find the difference  
// between two dates. 
function dateDiffInDays($date1, $date2)  
{ 
    // Calculating the difference in timestamps 
    $diff = strtotime($date2) - strtotime($date1); 
      
    // 1 day = 24 hours 
    // 24 * 60 * 60 = 86400 seconds 
    return abs(round($diff / 86400)); 
} 
  
// Start date 
$date1 = $row22['mindate']; 
  
// End date 
$date2 =date("d-m-Y"); 
  
// Function call to find date difference 
$dateDiff = dateDiffInDays($date1, $date2); 
  
// Display the result 
printf("Difference between two dates: "
   . $dateDiff . " Days "); 
?> 

<tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'FLOCK DAYS:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $dateDiff+1; ?> </td>

</tr>


<?php


  if($check_if_chicks_in_miscellaneous==1){
    if($row22['chickqty']>0){
 ?>
 <tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'PER BIRD COST:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo round($totlexp/$row22['chickqty'],2); ?> </td>
</tr>
  <tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'Current Flock#:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $flockcounter+1; ?> </td>
</tr>
  <tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'Total Final Flock#:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $flockcounter; ?> </td>
</tr>
<tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'Total Flock Profit/Loss:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $pls; ?> </td>
</tr>
<?php 
}
}
  if($check_if_chicks_in_miscellaneous==0){
        $qrychk = "SELECT max(dated) as dateds from ledger where accid = '$miscid' and entrytype='Transfer'";
$run_qrychk = mysqli_query($connect, $qrychk);
 $rowchk=mysqli_fetch_array($run_qrychk);
 $chkdated=$rowchk['dateds'];

      $qry22 = "SELECT sum(qty) as chickqty, min(dated) as mindate from chicksinout where accid = '$miscid' and dated > '$chkdated'";
        $run_qry22 = mysqli_query($connect, $qry22);
       $row22=mysqli_fetch_array($run_qry22);
       if($row22['chickqty']>0){
   
  ?>
   <tr>

 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'PER BIRD COST:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo round($totlexp/$row22['chickqty'],2); ?> </td>

</tr>
  <tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'Current Flock#:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $flockcounter+1; ?> </td>
</tr>
  <tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'Total Final Flock#:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $flockcounter; ?> </td>
</tr>
<tr>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo 'Total Flock Profit/Loss:' ?> </td>
 <td style=" font-weight: bold;  font-size: 16px; background-color: yellow;"> <?php echo $pls; ?> </td>
</tr>
<?php
}
}
?>
<?php
}
}
}

 ?>