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

<?php   


     $branch=$_SESSION['branch'];
     
     
   $session;

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

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


     /* code for advance Search*/


// $sql = "SELECT receipt_no,branch_id,against_challan_id,received,paid_by,paid_date,paid_time,paid_user_id,student_id FROM  eduportal_challan_fee_receiving  WHERE branch_id='$branch' ";

$sql = "SELECT esd.student_id, esd.first_name, esd.last_name, esd.class_id, esd.section_id, esd.ref_no, esd.candidate_roll_no, ecrf.branch_id, ecrf.received, ecrf.feetypeid, ecrf.paid_date, ecrf.against_challan_id, ecrf.receipt_no, ecrf.paid_by, ecrf.paid_time, ecrf.paid_user_id, ecrf.student_id, ecrf.paid_user_id
FROM edu_student_details AS esd
INNER JOIN eduportal_challan_fee_receiving AS ecrf ON esd.student_id = ecrf.student_id
WHERE ecrf.branch_id = '$branch'";
   
   
    if (isset($_REQUEST['PiadBy'])) { 

     if($_REQUEST['PiadBy']=="All"){
    $sql;
     } else {
     $sql .= " AND ecrf.paid_by = '" .$_REQUEST['PiadBy']. "' ";

     }

     if($_REQUEST['userPaid']=="All"){
    $sql;
    } else {
    $sql .= " AND ecrf.paid_user_id = '" .$_REQUEST['userPaid']. "' ";
    }

} // end isset

    
    
      if($_REQUEST['from']!=" " && $_REQUEST['from']!=NULL ) {
        $from=date("Y-m-d",strtotime($_REQUEST['from']));  

      }else {
        $from=date("Y-m-01");  
      }
     
      if($_REQUEST['to']!=" " && $_REQUEST['to']!=NULL   ) {
        $to=date("Y-m-d",strtotime($_REQUEST['to']));
      }else {
        $to=date("Y-m-t");  
      } 
      
    if ($_REQUEST['searchBy']!="All") {
        $sql .= " AND ecrf.paid_date BETWEEN '" .$from. "' AND '" .$to. "'";
    }    

    $sql .= " GROUP BY ecrf.receipt_no, esd.student_id ";    

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

            $sql .= "order by CAST(ecrf.receipt_no as UNSIGNED) asc ";

            }

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

            $sql .= "order by CAST(ecrf.receipt_no as UNSIGNED) asc ";

            }

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

             $sql .= "order by CAST(ecrf.paid_user_id as UNSIGNED) asc, CAST(ecrf.receipt_no as UNSIGNED) asc ";

            }


            if ($_REQUEST['orderBy'] == 'GradeWise') {
            $sql .= "ORDER BY CAST(esd.class_id AS UNSIGNED), CAST(esd.section_id AS UNSIGNED), CAST(ecrf.receipt_no AS UNSIGNED) ASC";
            }
  
//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 receipt report</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">

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:12px;}
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_fee_receipt_report.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; 
          
      Fee Settlement Report

       <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-1 col-xs-12" style="padding: 8px;width: 150px;">
                <div class="form-group ">
                  <label class="required">Grade</label>

            
                    <select name="class_id" onChange="getsec(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-1 col-xs-12" style="padding:8px;width: 150px;" >

            <div class="form-group ">

            <label class=""> Search By</label>
            <select name="searchBy" id="searchBy" onChange="HideDate(this.value);" class="form-control" >

            <option <?php if($_REQUEST['searchBy']=='dateWise'){echo 'selected';}?>  value="dateWise">Date Wise</option>
            <option <?php if($_REQUEST['searchBy']=='All'){echo 'selected';}?>  value="All">All</option>        

            </select>

            </div>
            </div>
              


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

                       <div class="form-group ">

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

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

                       <div class="form-group ">

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


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

                       <div class="form-group ">

                          <label class=""> Settlement Number</label>
                             <input name="settlement_no" id="settlement_no" value="<?php echo $_REQUEST['settlement_no'];?>" type="number" class="form-control"  >
                       
                       </div>
                  </div> -->

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

            
                    <select name="PiadBy" id="PiadBy" class="form-control" >

                    <option value="All">All</option>

                    <option value="Cash">Cash</option>

                    <option value="JazzCash">JazzCash</option>

                    <option value="Easypaisa">Easypaisa</option>

                    <option value="BankAccount">Bank Account</option>

                    <option value="UblOmni">Ubl Omni</option>

                    </select>

                  
                </div>
              </div>


  <div class="col-md-1 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']=='RecptWise'){echo 'selected';}?>  value="RecptWise">Receipt No Wise</option>
       <option <?php if($_REQUEST['orderBy']=='GradeWise'){echo 'selected';}?>  value="GradeWise">Grade Wise</option>
       <option  <?php if($_REQUEST['orderBy']=='UserWise'){echo 'selected';}?> value="UserWise">User Wise</option>
                              
       </select>
       
       </div>
  </div>


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

                       <div class="form-group ">

                          <label class=""> Received By</label>
                             <select name="userPaid" id="userPaid" class="form-control" >
                       
                       <option  value="All">All</option>              
                        <?php

                    $usrs="SELECT * FROM eduportal_challan_fee_receiving WHERE  branch_id='$branch' group by paid_user_id";
                    $users = $m->query($usrs);

                    foreach($users as $user)

                    { 

                    $m->where("user_id", $user['paid_user_id']);
                    $usrName=$m->get("edu_user_config");
                        ?>
                       <option <?php if($_REQUEST['userPaid']==$usrName[0]['user_id']){echo 'selected';}?> value="<?php echo $usrName[0]['user_id']; ?>"><?php echo $usrName[0]['user_fname']; ?></option>
                    <?php  } ?>
                                              
                       </select>
                       
                       </div>
                  </div>

                        


        <div class="col-md-1" 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>
              
        
        </div>

        </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;"> Receipt Report </div>

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

<div style="width:20%; float:left;font-size:13px; "> <b> Report Date:</b> <?php echo date("d-M-Y",strtotime($from));?> to <?php echo date("d-M-Y",strtotime($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> Order By:</b> 

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

</div>


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

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

</div>


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

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

</div>


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


<?php if ($_REQUEST['userPaid']=="All") {
   echo "All";
} else { 

$sql="SELECT * FROM edu_user_config WHERE user_id=".$_REQUEST['userPaid'];  
  $userName = $m->query($sql);

    echo $userName[0]['user_fname']; }?>

</div>



<?php
// Initialize variables to store class-wise total and overall total
$overallClsRecTotal = 0;
$overallClsFineTotal = 0;
$currentClass = null;
$classRec = 0;
$classFine = 0;
$count=1;
$classSerialNumber = 1;
?>

<table style="width:100%; color:black;" cellspacing="0">
    <thead>
        <tr>
                <th width="3%" style="text-align:center;">S.No</th>
                <th width="3%" style="text-align:center;">Rec#</th>
                <th width="6%" style="text-align:center;">Adm/Roll</th>         
                <th width="6%" style="text-align:center;"> Student Name </th>
                <th width="6%" style="text-align:center;"> Father Name </th>
                <th width="6%" style="text-align:center;"> Grade </th>
                <th width="6%" style="text-align:center;"> Section </th>
                <th width="6%" style="text-align:center;"> Invc# </th>
                <th width="6%" style="text-align:center;"> Invc Month </th>
                <th width="6%" style="text-align:center;"> Fee Recv </th>
                <th width="6%" style="text-align:center;"> Fine Recv </th>
                <th width="6%" style="text-align:center;"> Total Recv</th>
                <th width="6%"> Date/Time</th>                
                <th width="6%"> Recv In</th>
                <th width="6%" style="text-align:center;"> Recv By </th>
                 
            </tr>
    </thead>
    <tbody>
        <?php
        // Loop through the query results
        foreach ($WorkAssignListings as $WorkAssignListVal) {

           
         $r_no=$WorkAssignListVal['receipt_no'];
        $s_ids=$WorkAssignListVal['student_id'];
        $sq = "SELECT SUM(CASE WHEN feetypeid = 4 THEN received ELSE 0 END) AS fine, SUM(CASE WHEN feetypeid != 4 THEN received ELSE 0 END) AS recvd FROM  eduportal_challan_fee_receiving  WHERE branch_id='$branch' and receipt_no='$r_no' and student_id='$s_ids' ";        
        $sumAmt = $m->query($sq);

        $m->where("user_id", $WorkAssignListVal['paid_user_id']);
        $userName=$m->get("edu_user_config");




        // // get Invoice Month
        $c_ids=$WorkAssignListVal['against_challan_id'];
        $getChl = "select from_year_month, challan_created from eduportal_monthly_challan where challan_id='$c_ids' AND branch_id='$branch' "; 
        $getChlMonth = $m->query($getChl);




        // get student data
        $s_ids=$WorkAssignListVal['student_id'];
        $getStd = "select first_name, last_name, section_id,ref_no,candidate_roll_no,class_id from edu_student_details where student_id='$s_ids' AND branch_id='$branch' "; 
        $getStudentData = $m->query($getStd);




        // $toMonthYear=explode("-",$WorkAssignListVal['month']);
        if (isset($_REQUEST['class_id']) && $_REQUEST['class_id'] === 'All') {

        } else {

        if (isset($_REQUEST['class_id']) && $getStudentData[0]['class_id']!=$_REQUEST['class_id']) {
          continue;
        }

        }

        $totalRec+=$sumAmt[0]['recvd'];
        $totalFin+=$sumAmt[0]['fine'];



           if ($_REQUEST['orderBy']=="GradeWise") {
                 $usrCls=$WorkAssignListVal['class_id'];
                 $clsUsrName="Class Name: ". getClassName($m, $WorkAssignListVal['class_id']);
          } elseif ($_REQUEST['orderBy']=="UserWise") {
                 $usrCls=$WorkAssignListVal['paid_user_id'];
                 $clsUsrName="User Name: ". $userName[0]['user_fname'];
          }


            // 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='9' style='text-align:center;'>Total1</td>
                            <td>" . $classRec . "</td>
                            <td>" . $classFine . "</td>
                            <td>" . ($classRec+$classFine) . "</td>
                            <td colspan='3'></td>
                          </tr>";
                    // Update the overall total
                    $overallClsRecTotal += $classRec;
                    $overallClsFineTotal += $classFine;
                }

                // Reset the class-wise total for the new class
                $classRec = 0;
                $classFine = 0;
                $TotalClassFine = 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='15' 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 style='text-align: center;'>" . $classSerialNumber++ . "</td>
                    <td>" . $WorkAssignListVal['receipt_no'] . "</td>
                    <td>" . $getStudentData[0]['ref_no'].'/'.$getStudentData[0]['candidate_roll_no'] . "</td>
                    <td>" . $getStudentData[0]['first_name'] . "</td>
                    <td>" . $getStudentData[0]['last_name'] . "</td>
                    <td>" . getClassName($m,$getStudentData[0]['class_id']) . "</td>
                    <td>" . getSectionName($m,$getStudentData[0]['section_id']) . "</td>
                    <td>" . $WorkAssignListVal['against_challan_id'] . "</td>
                    <td>" . date("M-Y", strtotime($getChlMonth[0]['challan_created'])) . "</td>
                    <td>" . $sumAmt[0]['recvd'] . "</td>
                    <td>" . $sumAmt[0]['fine'] . "</td>
                    <td>" . ($sumAmt[0]['recvd'] + $sumAmt[0]['fine']) . "</td>
                    <td>" . date('d-M-Y', strtotime($WorkAssignListVal['paid_date'])).'/ <small>'.$WorkAssignListVal['paid_time'].'</small>' . "</td>
                    <td>" . $WorkAssignListVal['paid_by'] . "</td>
                    <td>" . $userName[0]['user_fname'] . "</td>
                  </tr>";
                  
            // Accumulate the settlement amount for the current class
            $classRec += $sumAmt[0]['recvd'];
            $classFine += $sumAmt[0]['fine'];
            $TotalClassFine += $sumAmt[0]['recvd']+$sumAmt[0]['fine'];
        }

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

         if ($_REQUEST['orderBy']=="GradeWise" || $_REQUEST['orderBy']=="UserWise") {
               
                  if ($currentClass !== null) {
                    
            echo "<tr style='background-color:lightgray;'>
                    <td colspan='3' style='text-align:center;'>Total Record: $totalRecord</td>
                    <td colspan='6' style='text-align:center;'>Total</td>
                    <td>" . $classRec . "</td>
                    <td>" . $classFine . "</td>
                    <td>" . $TotalClassFine . "</td>
                    <td colspan='3'></td>
                  </tr>";
            // Update the overall total for the last class
            $overallClsRecTotal += $classRec;
            $overallClsFineTotal += $classFine;
            $overallTotal += $TotalClassFine;
        }

          } 
       
         if ($_REQUEST['orderBy']=="RecptWise") {
            $totalSum=$totalSettellmetnAmount;
            $totalR=$totalRec;
            $totalF=$totalFin;
        } else {
            $totalR=$totalRec;
            $totalF=$totalFin;
        }

         // Print the overall total
        echo "<tr style='background-color:lightgray;'>
                <td colspan='9' style='text-align:center;'>Overall Total</td>
                <td>" . $totalR . "</td>
                <td>" . $totalF . "</td>
                <td>" . ($totalR + $totalF) . "</td>
                <td colspan='3'></td>
              </tr></tbody></table>";
        ?>
    </tbody>
</table>

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