<?php
$pgMod="route_wise_report";
$pgAct="view";
$monthsList = array(
    1=>'Apr',
    2=>'May',
    3=>'Jun',
    4=>'Jul ',
    5=>'Aug',
    6=>'Sep',
    7=>'Oct',
    8=>'Nov',
    9=>'Dec',
	10=>'Jan',
    11=>'Feb',
    12=>'Mar'
); 
?>
<style>.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 100% !important;overflow: inherit !important;} 
.dynamic{display:none}
table td {
padding: 6px !important;
}
.data{border-right: 3px solid #04606C;}
legend{line-height: 16px;font-weight:100;}
</style>
<form method="get" action="">
    <input type="hidden" name="src" value="Transport" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<div class="row-fluid">
        <div class="span12">
            <div class="well no-padding">
                <div class="controls controls-row">
                    
                    <div class="control-group span3">
                        
                        <div class="controls span3">
                                <select name="fmonth" id="fmonth">
                                    <option value="">Month</option>
                                 <?php
                                    for($m = 1;$m <= 12; $m++){ 
                                        $newDate=date("Y")."-".$m."-1";
                                        $month =  date("F",strtotime($newDate));//."(".($ij+3).")";
                                        //$month =  date("F", mktime(0, 0, 0, $m)); 
                                        $selected="";if($_GET["fmonth"]==$m)$selected=" selected ";
                                        echo "<option $selected value='$m'>$month</option>";
                                    }
                                 ?> 
                                </select>
                            </div>
                    </div>
                    
                    <div class="control-group span3">
                        
                        <div class="controls span3">
                                <select name="location">
                                    <option value="">Select Route</option>
                                 <?php
										$route=$db->getRows("SELECT * FROM location group by name ");
										foreach($route as $d){
											?>
											<option <?php if($d["id"]==$_GET["location"])echo 'selected';?> value="<?php echo $d["id"] ?>">
												<?php echo $d["name"] ?>
                                            </option>
											<?php
										}
                                 ?> 
                                </select>
                            </div>
                    </div>

                    <div class="control-group span3">
                    <button type="submit" name="vehicle_report" class="btn-classic margin">Get Report</button>
                    <!--<button type="submit" name="bus_detail" class="btn-classic margin">Bus Report</button>-->
                    <!--<button type="submit" name="teacher_detail" class="btn-classic margin">Teacher Report</button>                    -->
                    <!--<button type="submit" name="daily_att" class="btn-classic margin">Bus Attendance</button>-->
                   
                    </div>

                    <div class="control-group span2">
                      <button type="button" onclick="printDiv('printDiv')" class="btn btn-danger">Print</button>
                      </div>

                </div>
            </div>
        </div>
    </div>
</form>
<div id="printDiv">
<?php
if(isset($_REQUEST['vehicle_report'])){

	
	$overAllTotal=0;$overAlltution=0;$overAllTransport=0;$overAllhotel=0;$overAllOther=0;$overAllFine=0;$overAllextra=array();$overAllNewextra=0;
	
	$aryStudentList=$db->getRows("select * from student where location='".$_GET["location"]."' and  school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 $where order by class,name");
	
	$month=$_GET["fmonth"];$m=0;
	
	  if($month<4){
		$m=($month+9);
	  }
	  else{
		$m=$month-3;
	  }

	if(is_array($aryStudentList) && count($aryStudentList)>0){
		?>
		<h5><p style="float:right">Vechicle No : <?php echo $db->getVal("select name from bus_detail where id='".$_GET["old_school"]."' and school_id='".$_SESSION["admin"]["school_id"]."'");?>(
    	<?php	echo ($_SESSION['admin']['session']);?>)</p>
		<p>Transport dues list Vechicle no Wise :</p></h5>

        <button class="btn btn-danger" onclick="exportTableToExcel('tblData', 'Get Report')" style="float:right;margin-top:10px;">Excel</button>
		<table cellpadding="0" cellspacing="0" class="table" border="1" id="tblData">
			<thead>
			<tr>
            	<th>S.No</td>
				<th>Student Name</td>
				<th>Father Name</th>
				<th>Reg.No</th>
				<th>Class</th>
				<th>Roll No.</th>
				<th>Contact no</th>
			   <?php
			   if(is_array($monthsList) && count($monthsList)>0){
				for($i=1;$i<=$m;$i++){
				?>
						<th width="0.5%"><?php echo $monthsList[$i]; ?></th>
						<?php
					}
			   }
			   ?>
			   <th>Total</th>
			</tr>
			</thead>
			<tbody>
				<?php $i=0;$j=0; $columntotal = [];
				foreach($aryStudentList as $fStudent){$i++;$total=0;$j++;
					?>
					<tr >
                    	<td><?php echo $j; ?></td>
						<td><?php echo $fStudent["name"] ?></td>
						<td><?php echo $fStudent["fname"] ?></td>
                        <td><?php echo $fStudent["admissionno"] ?></td>
                        <td><?php echo $fStudent["class"] ?></td>
                        <td><?php echo $fStudent["fincome"] ?></td>
                        <td><?php echo $fStudent["mobile"] ?></td>
						<?php
						   if(is_array($monthsList) && count($monthsList)>0){
                            $rowtotal = 0;
							for($i = 1; $i <= $m; $i++){
							    
							    $installTution=$db->getRow("select b".$i.",bp".$i." from installment where idno='".$fStudent["idno"]."'");

            					if(is_array($installTution) && count($installTution)>0){
            						if($installTution["bp$i"]=="" || $installTution["bp$i"]==0){
            							$trasnport=$installTution["b$i"];
            						}
            						else    {
                    				if($m==$i)
                    				{
                        				$trasnport=$db->getVal("select (b".$m."-COALESCE(bp".$m.",0)) from installment where  idno='".$fStudent["idno"]."' ");
                    				}
                                    else    {
                            	             $trasnport=0;	
                                            }
            							}
            					}   else    {
            						$trasnport="";
            					}
            					
            					$rowtotal+=$trasnport;
                                $columntotal[$i] += $trasnport;
								?>
								<td>
    								<?php
    								if($trasnport=="0")echo "<lable style='color:red'>paid</label>";elseif($trasnport=="") echo "<lable style='color:red'>X</label>";else echo $trasnport;
    								 ?>
								 </td>
								 <?php
							}
						   }
						 ?>
						 <td class="MyTotalLeft"><?php echo $rowtotal; $total_comp+=$rowtotal;?></td>
					</tr>
					<?php
				}
				?>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="<?php echo 7;?>">Gross Total</td>
					<?php for($i=1; $i<=$m; $i++){
                			    echo "<td>$columntotal[$i]</td>";
                			} ?>
				   <td><?php echo $total_comp; ?></td>
				</tr>
				<!--<tr>-->
				<!--   <td colspan="<?php echo $m+7;?>">Driver Salary</td>-->
					
				<!--   <td><input style="width:50px;" value="" type="textbox" name="driver"/> </td>-->
				<!--</tr>-->
			</tfoot>
		</table>

        Print date : <?php echo date("d-M-Y") ?>
		<?php
	}else{
		//echo $db->getErMsg().$db->getLastQuery();
	}
 
}
else{
	echo "Please select Route First";
}
//echo $db->getErMsg()
?>
</div>


<script>

function getpickup(route){

    $.post( "Transport/ajax.php", { type: "route_pickup" ,route: route} , function( data ) {
        $("#pickup").html(data);
    });

}


    function exportTableToExcel(tableID, filename = '',e){
    
    var tableType = document.getElementById('tblData').getAttribute("data-tblname");

    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    e.preventDefault();
}
</script>