<?php include('parent/callMe.php');?>
<?php include('Sessioncheck.php');?>
<?php


     $branch=$_SESSION['branch'];
     
     if (isset($_REQUEST['from'])) {
         
     $from1=date("Y-m-d",strtotime($_REQUEST['from'])); 
     $to1=date("Y-m-d",strtotime($_REQUEST['to']));

     } else {

     $from1=date("Y-m-d"); 
     $to1=date("Y-m-t");

     }
     


   $session;

   if($_REQUEST['session']!=NULL){

      $session=$_REQUEST['session'];
      
    }else {
      
      $session=$_SESSION['current_session'];
    }


     /* code for advance Search*/

$sql = "SELECT esd.student_id, esd.first_name, esd.status, esd.last_name, esd.ref_no, esd.candidate_roll_no, esd.class_id, esd.section_id,esd.date_of_adms, emc.challan_id, emc.total_challan_amt
FROM edu_student_details AS esd
INNER JOIN eduportal_monthly_challan AS emc
ON esd.student_id = emc.student_id
WHERE emc.branch_id='$branch' ";

   
    if (isset($_REQUEST['class_id'])) {
        
    if($_REQUEST['class_id']=="All"){
      $sql;
    } else {
    $sql .= " AND esd.class_id = '" .$_REQUEST['class_id']. "' ";

    }       

     if($_REQUEST['section']=="All"){
      $sql;
    } else {
    $sql .= " AND esd.section_id = '" .$_REQUEST['section']. "' ";

    }


    if($_REQUEST['std_status']=="All"){
      $sql;
    } else {
    $sql .= " AND esd.status = '" .$_REQUEST['std_status']. "' ";

    }

}

    
    if ($_REQUEST['from'] != "" && $_REQUEST['from'] != NULL) {
    $from = date("Y-m-d H:i:s", strtotime($_REQUEST['from']));
} else {
    $from = date("Y-m-01 00:00:00");
}

if ($_REQUEST['to'] != "" && $_REQUEST['to'] != NULL) {
    $to = date("Y-m-d H:i:s", strtotime($_REQUEST['to']));
} else {
    $to = date("Y-m-t 23:59:59");
}
      
    
    $sql .= " AND challan_created BETWEEN '" .$from. "' AND '" .$to. "'";


            if(!isset($_REQUEST['orderBy'])){

            $sql .= " ORDER BY esd.class_id ASC, esd.section_id ASC, CAST(esd.ref_no AS UNSIGNED) ASC";

            }

     
            if($_REQUEST['orderBy']=='GradeWise'){

            $sql .= "order by CAST(esd.class_id as UNSIGNED) asc";

            }



            if($_REQUEST['orderBy']=='AdmNoWise'){

            $sql .= "order by CAST(esd.ref_no as UNSIGNED) desc";

            }

 

  //echo $sql;

  $WorkAssignListings = $m->query($sql);

 
?>

<!DOCTYPE HTML>
<html>
<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Print Invoice Recipt Summary</title>
<link rel="icon" type="image/png" href="img/favicon.png" sizes="32x32" />
<?php include('RootMe/css_links.php');?> 
<script type="text/javascript">
// window.print();
</script>
<style type="text/css">

@media print {
.print-link {
display:none;
}
}

tr th {border-top:1px solid gray;border-left:1px solid gray;border-bottom:1px solid gray;text-align:center;background-color:lightgray;font-size:14px;}
tr td {border-bottom:1px solid gray;border-left:1px solid gray;font-size:14px;}
  
.border{border:1px solid black;}
.center{text-align:center;}
.alignment{padding-left:1%;text-align:left;}

/* CSS for print */
@media print {
  .hidden-on-print {
    display: none;
  }
  #naving{display: none;}
}

</style>

</head>


<body style="font-size:12px;color: black;">



    <!-- ================================== search code ================================================ -->
<!-- 
<form class="" action="print_invoice_receipt_summary.php" enctype="multipart/form-data" method="get">
        <div class="panel panel-default hidden-on-print">
          <div class="panel-heading"><a href="<?php echo $_SESSION['dashboard'];?>">Dashboard </a> &nbsp; <i style="" class="fa fa-angle-double-right"></i>  &nbsp; 
    
     <a href="multi_fee_reports.php">Fee Reports </a> &nbsp; <i style="" class="fa fa-angle-double-right"></i>  &nbsp; 
          
      Invoice Receipt Summary

       <span style="float:right;margin-top: -7px;">

          <input type="hidden" value="<?php echo session_id().session_id().session_id().session_id().session_id(); ?>" name="session" />



          </span><div class="clearfix"></div> </div>
          <div class="panel-body">
          
            <div class="col-md-12" id="" style="">
            
              <div class="col-md-2 col-xs-12" style="padding: 8px;width: 150px;">
                <div class="form-group ">
                  <label class="required">Grade</label>

            
                    <select name="class_id" onChange="getsecall(this.value)" class="form-control" >
                     <option value="All">All</option> 
                    <?php
                    $m->where("branch_id", $_SESSION['branch']);
                    $WorkAssignList = $m->get("edu_class_config");
                    foreach($WorkAssignList as $WorkAssignListVal)
                    { ?>

                       <option <?php if($_REQUEST['class_id']==$WorkAssignListVal['class_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['class_id']; ?>"><?php echo $WorkAssignListVal['Class_Name']; ?></option>


                            <?php
                    } ?>

                    </select>

                  
                </div>
              </div>
              


                           <div class="col-md-2 col-xs-12" style="padding: 8px;width: 150px;">
                <div class="form-group ">
                  <label class="required">Section</label>

            
                    <select name="section" id="txt_section" class="form-control">
                      
                        <option value="All">All</option> 
                   <?php

                              if($_REQUEST['section']!='')

                              { 
                              $m->where("class_id", $_REQUEST['class_id']);

                              $secondLevel = $m->get("edu_adm_section_detail");

                              foreach($secondLevel as $secondLevel_val)

                              {

                               ?>

                                <option <?php if($_REQUEST['section']==$secondLevel_val['section_id']){echo "selected";} ?> value="<?php echo $secondLevel_val['section_id']; ?>"><?php echo trim(utf8_encode_all($secondLevel_val['section_name'])); ?></option>

                              <?php }   ?>

                                

                          <?php } ?>



                    </select>

                  
                </div>
              </div>
               


                <div class="col-md-2 col-xs-12" style="padding:8px;width: 150px;">

                       <div class="form-group ">

                          <label class="required"> From Date</label>
                              <input name="from"  id="from" value="<?php echo $from1;?>" type="date" class="form-control"  >
                       
                          </div>
                  </div>                     
             

                  <div class="col-md-2 col-xs-12" style="padding:8px;width: 150px;" >

                       <div class="form-group ">

                          <label class="required"> To Date</label>
                             <input name="to" id="to" value="<?php echo $to1;?>" type="date" class="form-control"  >
                       
                       </div>
                  </div>




                    <div class="col-md-2 col-xs-12" style="padding:8px;width: 150px;" >

                    <div class="form-group ">

                    <label class=""> Status</label>
                    <select name="std_status" id="std_status" class="form-control" >

                    <option <?php if($_REQUEST['std_status']=='All'){echo 'selected';}?>  value="All">All</option>
                    <option <?php if($_REQUEST['std_status']=='0'){echo 'selected';}?>  value="0">Active</option>
                    <option  <?php if($_REQUEST['std_status']=='1'){echo 'selected';}?> value="1">InActive</option>

                    </select>

                    </div>
                    </div>

  <div class="col-md-2 col-xs-12" style="padding:8px;width: 150px;" >

       <div class="form-group ">

          <label class=""> Order By</label>
        <select name="orderBy" id="orderBy" class="form-control" >
       
       <option <?php if($_REQUEST['orderBy']=='SettlWise'){echo 'selected';}?>  value="SettlWise">Settlement No Wise</option>
       <option <?php if($_REQUEST['orderBy']=='GradeWise'){echo 'selected';}?>  value="GradeWise">Grade Wise</option>
       <option  <?php if($_REQUEST['orderBy']=='Adm'){echo 'selected';}?> value="Adm">Admission No Wise</option>
                              
       </select>
       
       </div>
  </div>


                  
        <div class="col-md-2 col-xs-12" style="padding:8px;width: 150px;" >

        <div class="form-group ">

        <label class=""> Fine</label>
        <select name="fine" id="fine" class="form-control" >

        <option value="All">All </option>
        <option <?php if($_REQUEST['fine']=='Inclusive'){echo 'selected';}?>  value="Inclusive">Inclusive</option>
        <option  <?php if($_REQUEST['fine']=='Exclusive'){echo 'selected';}?> value="Exclusive">Exclusive</option>

        </select>

        </div>
        </div>

                        


        <div class="col-md-2" style="padding:2px;margin-top: 29px;float: right;">
        <div class="form-group ">
        <input type="hidden" name="addaccountAdmin" value="1">
        <button type="submit" class="btn btn-primary">Search</button>
        <button id="printButton" class="btn btn-primary">Print</button>

        </div>
        <script>
        // Add an event listener to the print button
        document.getElementById("printButton").addEventListener("click", function() {
        window.print(); // This opens the print dialog
        });
        </script>
        </div>

             
            </div>
            <div class="clearfix"></div>
          </div>
        </div>
      </form> -->

    <!-- =================================== end search code ===========================================-->

<div style="width:100%; text-align:center; height:80px;float:left;">
<div style="width:20%;font-size:13px;text-align:center;float:left;height:80px;">

<img src="<?php echo DOMAIN;?>Production/img/<?php echo $brid[0]['logo']; ?>" style="height:100px;width:100px;"  />

</div>

<div style="width:60%; float:left;">

         <span style="font-size:25px; text-align:center;font-family:times new roman;font-weight:bold;color: black;">  <?php echo $brid[0]['Branch_Name'];?> </span>

         <br>
         <span style="font-size:14px; text-align:center;color: black;">  <?php echo $brid[0]['Branch_Location']."<br>"." Cell: ".$brid[0]['Owner_no'];?> </span>



      </div>
</div>
<br><br>
<div style="text-align:center;font-size:22px;font-weight:bold;"> Invoice Receipt Summary </div>

<hr style="width:100%;">

<div style="width:25%; float:left;font-size:14px; "> <b> Report Date:</b> <?php echo date("d-M-Y",strtotime($_REQUEST['from']));?> to <?php echo date("d-M-Y",strtotime($_REQUEST['to']));?> </div>

<div style="width:15%;float:left;font-size:14px;">  <b> Grade:</b> 

<?php if ($_REQUEST['class_id']=="All") {
   echo "All";
} else { echo getClassName($m,$_REQUEST['class_id']); }?>
 

</div>

<div style="width:15%;float:left;font-size:14px;">  <b> Section:</b> 

<?php if ($_REQUEST['section']=="All") {
   echo "All";
} else { echo getSectionName($m,$_REQUEST['section']); }?>
 

</div>


<div style="width:15%;float:left;font-size:14px;">  <b> Std Status:</b> 

<?php echo $_REQUEST['std_status'];?> 

</div>


<div style="width:15%;float:left;font-size:14px;">  <b> Order By:</b> 

<?php echo $_REQUEST['orderBy'];?> 

</div>


<div style="width:15%;float:left;font-size:14px;">  <b> Fine:</b> 

<?php echo $_REQUEST['fine'];?> 

</div>





<?php
// Initialize variables to store class-wise total and overall total
$overallTotal = 0;
$currentClass = null;
$Arrear = 0;
$InvAmount = 0;
$RecAmount = 0;
$SettlementAmount = 0;
$NetAmount = 0;
$Balance = 0;


$count=1;
$classSerialNumber = 1;
?>

<table style="width:100%; color:black;" cellspacing="0">
    <thead>
        <tr>

              
                <th width="3%">S.No</th>         
                <th width="8%" style="text-align:center;">Adm/Roll</th>              
                <th width="8%" style="text-align:center;"> Student Name </th>
                <th width="8%" style="text-align:center;"> Father Name </th>
                <th width="8%" style="text-align:center;"> Grade </th>
                <th width="8%" style="text-align:center;"> Section </th>
                <th width="8%" style="text-align:center;"> Arrear </th>
                <th width="8%" style="text-align:center;"> Invc Amt </th>
                <th width="8%" style="text-align:center;"> Recvd Amt </th>
                <th width="8%" style="text-align:center;"> Settl Amt </th>
                <th width="8%" style="text-align:center;"> Net </th>     
                <th width="8%" style="text-align:center;"> Balance </th>
          
                                
            </tr>
    </thead>
    <tbody>
        <?php
        // Loop through the query results
        foreach ($WorkAssignListings as $WorkAssignListVal) {

           $c_id=$WorkAssignListVal['challan_id'];
        

        if ($_REQUEST['fine']=="Exclusive") {
            
            $c_id=$WorkAssignListVal['challan_id'];
        
        $sq = "SELECT SUM(arrears_amount) AS arrears FROM eduportal_monthly_challan_arrears   WHERE branch_id='$branch' AND against_challan_id='$c_id' AND feetype_id != 4";    
        $sumAmt = $m->query($sq);


        $sq = "SELECT SUM(received) AS rec FROM  eduportal_challan_fee_receiving  WHERE branch_id='$branch' and against_challan_id='$c_id' AND feetypeid != 4 ";        
        $sumAmtRec = $m->query($sq);


        $sq = "SELECT SUM(settellment_amount) AS sttle FROM  eduportal_challan_fee_settlement  WHERE branch_id='$branch' and against_challan_id='$c_id' AND feetypeid != 4 ";        
        $sumAmtSettl = $m->query($sq);

        } else {


        $sq = "SELECT SUM(arrears_amount) AS arrears FROM  eduportal_monthly_challan_arrears  WHERE branch_id='$branch' and against_challan_id='$c_id' ";    
        //echo $sq;    
        $sumAmt = $m->query($sq);


        $sq = "SELECT SUM(received) AS rec FROM  eduportal_challan_fee_receiving  WHERE branch_id='$branch' and against_challan_id='$c_id' ";        
        $sumAmtRec = $m->query($sq);


        $sq = "SELECT SUM(settellment_amount) AS sttle FROM  eduportal_challan_fee_settlement  WHERE branch_id='$branch' and against_challan_id='$c_id' ";        
        $sumAmtSettl = $m->query($sq);

        }

     
        $t_arrear+=$sumAmt[0]['arrears'];
        $t_amt+=$WorkAssignListVal['total_challan_amt'];
        $t_rec+=$sumAmtRec[0]['rec'];
        $t_sttle+=$sumAmtSettl[0]['sttle'];
        $t_net+=($WorkAssignListVal['total_challan_amt'])-($sumAmtRec[0]['rec']-$sumAmtSettl[0]['sttle']);
        $t_bal+=($sumAmt[0]['arrears']+$WorkAssignListVal['total_challan_amt'])-($sumAmtRec[0]['rec']-$sumAmtSettl[0]['sttle']);




                if ($_REQUEST['orderBy']=="GradeWise") {
                    $usrCls=$WorkAssignListVal['class_id'];
                 $clsUsrName="Class Name: ". getClassName($m, $WorkAssignListVal['class_id']);
                }
                 
         


            // Check if the class has changed, print the class-wise total, and update the overall total
            if ($currentClass !== $usrCls) {
                if ($currentClass !== null) {
                    // Print the class-wise total
                    echo "<tr style='background-color:lightgray;'>
                            <td colspan='6' style='text-align:center;'>Total</td>
                            <td>" . $Arrear . "</td>
                            <td>" . $InvAmount . "</td>
                            <td>" . $RecAmount . "</td>
                            <td>" . $SettlementAmount . "</td>
                            <td>" . $NetAmount . "</td>
                            <td>" . $Balance . "</td>
                          
                          </tr>";
                    // Update the overall total
                    $overallArrearTotal += $Arrear;
                    $overallInvAmountTotal += $InvAmount;
                    $overallRecAmountTotal += $RecAmount;
                    $overallSettlementAmountTotal += $SettlementAmount;
                    $overallNetAmountTotal += $NetAmount;
                    $overallBalanceTotal += $Balance;
                 
                }

                // Reset the class-wise total for the new class
                $Arrear = 0;
                $InvAmount = 0;
                $RecAmount = 0;
                $SettlementAmount = 0;
                $NetAmount = 0;
                $Balance = 0;
                $classSerialNumber = 1; // Reset the serial number for the new class

                // Print the heading for the new class
                echo "<tr style='background-color:lightblue;'>
                        <td colspan='12' style='text-align:center;'><b> " . $clsUsrName . "</b></td>
                      </tr>";
                $currentClass = $usrCls;
            }
          $totalRecord=$count++;
$monthYear=date("M-y", strtotime($getChlMonth[0]['from_year_month']));
            // Process the current record, you can display the data as needed
            echo "<tr>
                <td>" . $classSerialNumber++ . "</td>
                <td>" . $WorkAssignListVal['ref_no'] . "/" . $WorkAssignListVal['candidate_roll_no'] . "</td>
                <td>" . $WorkAssignListVal['first_name'] . "</td>
                <td>" . $WorkAssignListVal['last_name'] . "</td>                
                <td>" . getClassName($m,$WorkAssignListVal['class_id']) . "</td>
                <td>" . getSectionName($m,$WorkAssignListVal['section_id']) . "</td>
                <td>" . $sumAmt[0]['arrears'] . "</td>
                <td>" . $WorkAssignListVal['total_challan_amt'] . "</td>
                <td>" . $sumAmtRec[0]['rec'] . "</td>
                <td>" . $sumAmtSettl[0]['sttle'] . "</td>
                <td>" . ($WorkAssignListVal['total_challan_amt'] - ($sumAmtRec[0]['rec'] - $sumAmtSettl[0]['sttle'])) . "</td>
                <td>" . ($sumAmt[0]['arrears'] + ($WorkAssignListVal['total_challan_amt'] - ($sumAmtRec[0]['rec'] - $sumAmtSettl[0]['sttle'])) ) . "</td>
               
            </tr>";
                  
            // Accumulate the settlement amount for the current class
            $Arrear += $sumAmt[0]['arrears'];
            $InvAmount += $WorkAssignListVal['total_challan_amt'];
            $RecAmount += $sumAmtRec[0]['rec'];
            $SettlementAmount += $sumAmtSettl[0]['sttle'];
            $NetAmount += ($WorkAssignListVal['total_challan_amt'] - ($sumAmtRec[0]['rec'] - $sumAmtSettl[0]['sttle']));
            $Balance += ($sumAmt[0]['arrears'] + ($WorkAssignListVal['total_challan_amt'] - ($sumAmtRec[0]['rec'] - $sumAmtSettl[0]['sttle'])) );
            
        }

        // Print the total settlement for the last class in the result set

                       
                  if ($currentClass !== null) {
            echo "<tr style='background-color:lightgray;'>
                    <td colspan='3' style='text-align:center;'>Total Record: $totalRecord</td>
                    <td colspan='3' style='text-align:center;'>Total</td>
                    <td>" . $Arrear . "</td>
                    <td>" . $InvAmount . "</td>
                    <td>" . $RecAmount . "</td>
                    <td>" . $SettlementAmount . "</td>
                    <td>" . $NetAmount . "</td>
                    <td>" . $Balance . "</td>
                   
                  </tr>";
            // Update the overall total for the last class
            $overallArrearTotal += $Arrear;
            $overallInvAmountTotal += $InvAmount;
            $overallRecAmountTotal += $RecAmount;
            $overallSettlementAmountTotal += $SettlementAmount;
            $overallNetAmountTotal += $NetAmount;
            $overallBalanceTotal += $Balance;            
        }

         
       
         if ($_REQUEST['orderBy']=="Adm") {
             $totalSumArrear=$t_arrear;
            $totalSumInvAmount=$t_amt;
            $totalSumRecAmount=$t_rec;
            $totalSumSettlementAmount=$t_sttle;
            $totalSumNetAmount=$t_net;
            $totalSumBalance=$t_bal;
        } else {
            $totalSumArrear=$overallArrearTotal;
            $totalSumInvAmount=$overallInvAmountTotal;
            $totalSumRecAmount=$overallRecAmountTotal;
            $totalSumSettlementAmount=$overallSettlementAmountTotal;
            $totalSumNetAmount=$overallNetAmountTotal;
            $totalSumBalance=$overallBalanceTotal;
           

        }

         // Print the overall total
        echo "<tr style='background-color:lightgray;'>
                <td colspan='6' style='text-align:center;'>Overall Total</td>
                <td>" . $totalSumArrear . "</td>
                <td>" . $totalSumInvAmount . "</td>
                <td>" . $totalSumRecAmount . "</td>
                <td>" . $totalSumSettlementAmount . "</td>
                <td>" . $totalSumNetAmount . "</td>
                <td>" . $totalSumBalance . "</td>
               
              </tr></tbody></table>";
        ?>
    </tbody>
</table>

<?php include('RootMe/js_linksscript.php');?>
</body>
</html>