<?php
$pgMod="balanceSheet";
$pgAct="view";
?>
<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;
}
legend{line-height: 16px;font-weight:100;}
</style>
<form method="get" action="">
    <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 span5">
                        <label class="control-label" for="class">From Date</label>
                        <div class="controls">
                                <select class="span3" name="fdate" id="date">
                                    <option value="">Date</option>
                                    <?php
                                    for($m = 01;$m <= 31; $m++){ 
                                        $selected="";if($_GET["fdate"]==$m)$selected=" selected ";
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                                <select class="span5" 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>
                                <select class="span4" name="fyear" id="fyear">
                                    <option value="">Year</option>
                                    <?php
                                    for($m = 2013;$m <= 2050; $m++){  
                                        $selected="";if($_GET["fyear"]==$m)$selected=" selected ";
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                            </div>
                    </div>
                    <div class="control-group span5">
                        <label class="control-label" for="medium">To date</label>
                        <div class="controls">
                                <select class="span3" name="tdate" id="tdate">
                                    <option value="">Date</option>
                                    <?php
                                    for($m = 01;$m <= 31; $m++){ 
                                         $selected="";if($_GET["tdate"]==$m)$selected=" selected ";
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                                <select class="span5" name="tmonth" id="tmonth">
                                    <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["tmonth"]==$m)$selected=" selected ";
                                        echo "<option $selected value='$m'>$month</option>";
                                    }
                                 ?> 
                                </select>
                                <select class="span4" name="tyear" id="tyear">
                                    <option value="">Year</option>
                                    <?php
                                    for($m = 2013;$m <= 2050; $m++){  
                                         $selected="";if($_GET["tyear"]==$m)$selected=" selected ";
                                        echo "<option $selected value='$m'>$m</option>";
                                    }
                                    ?> 
                                </select>
                            </div>
                    </div>
                    <div class="control-group span2">
                    <button type="submit" name="monthly" class="btn-classic margin">Profit / Loss</button>
                    <button type="submit" name="complete" class="btn-classic margin">Complete</button>
                    <button type="button" onclick="printDiv('printDiv')" class="btn btn-danger">Print</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<div id="printDiv">
<?php

if(isset($_GET["fyear"]) && trim($_GET["fyear"])!="" && isset($_GET["fmonth"]) && trim($_GET["fmonth"])!="" && isset($_GET["fdate"]) && trim($_GET["fdate"])!="" && isset($_GET["tyear"]) && trim($_GET["tyear"])!="" && isset($_GET["tmonth"]) && trim($_GET["tmonth"])!="" && isset($_GET["tdate"]) && trim($_GET["tdate"])!="" && isset($_GET["monthly"])){
		$fdate=$_GET["fyear"]."/".$_GET["fmonth"]."/".$_GET["fdate"];
		$tdate=$_GET["tyear"]."/".$_GET["tmonth"]."/".$_GET["tdate"];
	
		$GrandTotal=0;$Grandtution=0;$GrandTransport=0;$Grandhotel=0;$GrandOther=0;$GrandFine=0;$Grandextra=array();$GrandNewextra=0;
		$day=date_range($fdate, $tdate);
		$aryFeeType=$db->getRows("select distinct(type) from extrafee_type where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 order by type");
		$tndate = date('Y/m/d', strtotime('-1 days',strtotime($tdate)));
		?>
        <center><h5><p style="float:right">Report Date: <?php if(strtotime($tndate)==strtotime($fdate))echo date("d-M-Y",strtotime($tndate));else echo date("d-M-Y",strtotime($fdate)) ." to ".date("d-M-Y",strtotime($tndate)); ?></p> <p>Montly Profit and Loss Report</p>	</h5></center>
		<table class="table" border="1" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
               <td>Date</td>
               <td>Income</td>
               <td>Expance</td>
               <td>Balance</td>
            </tr>
            </thead>
            <tbody>
		<?php $i=0;
		foreach($day as $thisday){ $i++;
			$overAllTotal=0;$overAlltution=0;$overAllTransport=0;$overAllhotel=0;$overAllOther=0;$overAllFine=0;$overAllextra=array();$overAllNewextra=0;
			if(count($day)==$i){
			}else{
			$fdate=$thisday;
			$tdate = date('Y-m-d', strtotime('+1 days',strtotime($thisday)));
			//$aryStudentList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by class,name");
			//echo $db->getErMsg().$db->getLastQuery();
			//if(is_array($aryStudentList) && count($aryStudentList)>0){
				//foreach($aryStudentList as $fStudent){
					$tution=0;$trasnport=0;$hostel=0;$otherExtra=0;$total=0;$fine=0;
					$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'ip%' and (date between '$fdate' and '$tdate')");
					if(is_array($installTution) && count($installTution)>0){
						foreach($installTution as $installT){
							$tution+=$db->getVal("select ".$installT["ins_name"]." from installment where idno=".$installT["idno"]);
						}
						$overAlltution+=$tution;
						$total+=$tution;
					}
					$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'bp%' and (date between '$fdate' and '$tdate')");
					if(is_array($installTution) && count($installTution)>0){
						foreach($installTution as $installT){
							$trasnport+=$db->getVal("select ".$installT["ins_name"]." from installment where idno=".$installT["idno"]);
						}
						$overAllTransport+=$trasnport;
						$total+=$trasnport;
					}
					$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'hp%' and (date between '$fdate' and '$tdate')");
					if(is_array($installTution) && count($installTution)>0){
						foreach($installTution as $installT){
							$inst="ip".preg_replace("/hp/","",$installT["ins_name"]);
							$hostel+=$db->getVal("select ".$inst." from hostelinstallment where idno=".$installT["idno"]);
						}
						$overAllhotel+=$hostel;
						$total+=$hostel;
					}
					$extrasum=array();
					/*if(is_array($aryFeeType) && count($aryFeeType)>0){
						foreach($aryFeeType as $ftype){  
							$sums=0;
							//$Ftypeid=$db->getVal("select id from extrafee_type where type='".$ftype["type"]."' and class='".$fStudent["class"]."'");
							$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'ext-%' and (date between '$fdate' and '$tdate')");$no=array();
							if(is_array($installTution) && count($installTution)>0){
								foreach($installTution as $installT){
									$Ftypeid=$db->getVal("select id from extrafee_type where type='".$ftype["type"]."' and class=(select class from student where idno='".$installT["idno"]."')");
									$no=explode("-",$installT[0]);
									$sums+=$db->getVal("select pay from extrafee where idno='".$installT["idno"]."' and  pid='p".$no[2]."' and type='".$Ftypeid."'");
									echo $db->getErMsg();
								}
								
							}
							$extrasum[]=$sums;
							$total+=$sums;$tp=$ftype["type"];
							$overAllextra[$tp]+=$sums;
						}
					}*/
					$installOtherExtra=$db->getRows("select ins_name,idno from installment_time where ins_name like 'other%' and (date between '$fdate' and '$tdate')");
					if(is_array($installOtherExtra) && count($installOtherExtra)>0){
						foreach($installOtherExtra as $installT){
							//other1_detail
							$idSdetail=str_replace("_detail","",$installT[0]);
							$idSdetail=str_replace("other","",$idSdetail);
							$otherExtra+=$db->getVal("select fine from installment_fine where ins_name='".$idSdetail."-otherfee' and idno=".$installT["idno"]);
							//echo $db->getLastQuery();
						}
						$overAllOther+=$otherExtra;
						$total+=$otherExtra;
					}
					$fine=(float)$db->getVal("select sum(fine) from installment_fine where (ins_name like '%-class' or ins_name like '%-hostel' or ins_name like '%-bus' or ins_name like '%-extra%') and (udate between '$fdate' and '$tdate')");
					$total+=$fine;
					$overAllFine+=$fine;
					$newExtra=(float)$db->getVal("select sum(fine) from installment_fine where (ins_name like '%-other') and (udate between '$fdate' and '$tdate')");
					$total+=$newExtra;
					$overAllNewextra+=$newExtra;
					if($total>0){
						$overAllTotal+=$total;
					}
				//}
					?>
				<tr>
					<td><?php echo date("dM Y",strtotime($fdate));?></td>
                    <td><?php
					$Grandtution+=$overAlltution; 
					$GrandTransport+=$overAllTransport; 
					$Grandhotel+=$overAllhotel;
					if(is_array($overAllextra) && count($overAllextra)>0){ $gi=0;
						foreach($overAllextra as $ftype){ $gi++;
							 $Grandextra[$gi]+=$ftype; 
						}
					}
					$GrandOther+=$overAllOther;
					$GrandFine+=$overAllFine;
					$GrandNewextra+=$overAllNewextra;
					$tot=(float)$db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '$fdate' and '$tdate') and (tfor='cash' or tfor='cheque')"); 
					echo $overAllTotal+$tot; $overAllTot=$overAllTot+$tot; $GrandTotal+=$overAllTotal+$tot; ?></td>
                    <td><?php 
						echo $exopance=(float)$db->getVal("select sum(amount) from transaction where type='withdraw' and (udate between '$fdate' and '$tdate') and rid=0 and tid=0");
					 	$ovarallexopance+=$exopance; ?></td>
                    <td><?php echo ($overAllTotal+$tot)-$exopance; ?></td>
				</tr>
			<?php
			//}
		}
	}
		?>
		</tbody>
        <tfoot>
            <td>Gross Total</td>
            <?php /*?><td><?php echo $Grandtution ?></td>
            <td><?php echo $GrandTransport ?></td>
            <td><?php echo $Grandhotel ?></td>
            <?php
            if(is_array($Grandextra) && count($Grandextra)>0){
                foreach($Grandextra as $ftype){
                    ?><td><?php echo $ftype ?></td><?php
                }
           }?>
           <td><?php echo $GrandOther ?></td>
           <td><?php echo $GrandFine ?></td>
           <td><?php echo $GrandNewextra ?></td><?php 
           <td><?php echo $overAllTot; ?></td>*/?>
           <td><?php echo $GrandTotal; ?></td>
           <td><?php echo $ovarallexopance;?></td>
           <td><?php echo $GrandTotal-$ovarallexopance ?></td>
        </tfoot>
	</table>
		<?php
		//echo "<pre>";print_r($day);
	
}
elseif(isset($_GET["fyear"]) && trim($_GET["fyear"])!="" && isset($_GET["fmonth"]) && trim($_GET["fmonth"])!="" && isset($_GET["fdate"]) && trim($_GET["fdate"])!="" && isset($_GET["tyear"]) && trim($_GET["tyear"])!="" && isset($_GET["tmonth"]) && trim($_GET["tmonth"])!="" && isset($_GET["tdate"]) && trim($_GET["tdate"])!="" && isset($_GET["complete"])){
		$fdate=$_GET["fyear"]."/".$_GET["fmonth"]."/".$_GET["fdate"];
		$tdate=$_GET["tyear"]."/".$_GET["tmonth"]."/".$_GET["tdate"];
	
		$GrandTotal=0;$Grandtution=0;$GrandTransport=0;$Grandhotel=0;$GrandOther=0;$GrandFine=0;$Grandextra=array();$GrandNewextra=0;$gtot_inc=0;$gtot_exp=0;
		$GrandTotal_t=0;$overAllexpense=0;$overAllexpense_t=0;
		$day=date_range($fdate, $tdate);
		$aryFeeType=$db->getRows("select distinct(type) from extrafee_type where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 order by type");
		$tndate = date('Y/m/d', strtotime('-1 days',strtotime($tdate)));
		?>
        <center><h5><p style="float:right">Report Date: <?php if(strtotime($tndate)==strtotime($fdate))echo date("d-M-Y",strtotime($tndate));else echo date("d-M-Y",strtotime($fdate)) ." to ".date("d-M-Y",strtotime($tndate)); ?></p> <p>Montly Profit and Loss Report</p>	</h5></center>
		<table class="table" border="1" cellpadding="0" cellspacing="0">
            <thead>
            <tr style="text-align:center;">
            	<td></td>
                <td colspan="3">Profit/Loss(Except Transport)</td>
                <td colspan="3">Transport Profit/Loss</td>
                <td colspan="3">Overall Profit/Loss</td>
            </tr>
            <tr>
               <td>Date</td>
               <td>Income</td>
               <td>Expance</td>
               <td>Balance</td>
               <td>Income</td>
               <td>Expance</td>
               <td>Balance</td>
               <td>Income</td>
               <td>Expance</td>
               <td>Balance</td>
            </tr>
            </thead>
            <tbody>
		<?php $i=0;
		foreach($day as $thisday){ $i++;
			$overAllTotal=0;$overAlltution=0;$overAllTransports=0;$overAllhotel=0;$overAllOther=0;$overAllFine=0;$overAllextra=array();$overAllNewextra=0;$tot_expense_tot=0;
			if(count($day)==$i){
			}else{
			$fdate=$thisday;
			$tdate = date('Y-m-d', strtotime('+1 days',strtotime($thisday)));
			//$aryStudentList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by class,name");
			//echo $db->getErMsg().$db->getLastQuery();
			//if(is_array($aryStudentList) && count($aryStudentList)>0){
				//foreach($aryStudentList as $fStudent){
					$tution=0;$trasnport=0;$hostel=0;$otherExtra=0;$total=0;$total_t=$fine=0;
					$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'ip%' and (date between '$fdate' and '$tdate')");
					if(is_array($installTution) && count($installTution)>0){
						foreach($installTution as $installT){
							$tution+=$db->getVal("select ".$installT["ins_name"]." from installment where idno=".$installT["idno"]);
						}
						$overAlltution+=$tution;
						$total+=$tution;
					}
					$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'bp%' and (date between '$fdate' and '$tdate')");
					if(is_array($installTution) && count($installTution)>0){
						foreach($installTution as $installT){
							$trasnport+=$db->getVal("select ".$installT["ins_name"]." from installment where idno=".$installT["idno"]);
						}
						$overAllTransports+=$trasnport;
						$total_t+=$trasnport;
					}
					$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'hp%' and (date between '$fdate' and '$tdate')");
					if(is_array($installTution) && count($installTution)>0){
						foreach($installTution as $installT){
							$inst="ip".preg_replace("/hp/","",$installT["ins_name"]);
							$hostel+=$db->getVal("select ".$inst." from hostelinstallment where idno=".$installT["idno"]);
						}
						$overAllhotel+=$hostel;
						$total+=$hostel;
					}
					$extrasum=array();
					/*if(is_array($aryFeeType) && count($aryFeeType)>0){
						foreach($aryFeeType as $ftype){  
							$sums=0;
							//$Ftypeid=$db->getVal("select id from extrafee_type where type='".$ftype["type"]."' and class='".$fStudent["class"]."'");
							$installTution=$db->getRows("select ins_name,idno from installment_time where ins_name like 'ext-%' and (date between '$fdate' and '$tdate')");$no=array();
							if(is_array($installTution) && count($installTution)>0){
								foreach($installTution as $installT){
									$Ftypeid=$db->getVal("select id from extrafee_type where type='".$ftype["type"]."' and class=(select class from student where idno='".$installT["idno"]."')");
									$no=explode("-",$installT[0]);
									$sums+=$db->getVal("select pay from extrafee where idno='".$installT["idno"]."' and  pid='p".$no[2]."' and type='".$Ftypeid."'");
									echo $db->getErMsg();
								}
								
							}
							$extrasum[]=$sums;
							$total+=$sums;$tp=$ftype["type"];
							$overAllextra[$tp]+=$sums;
						}
					}*/
					$installOtherExtra=$db->getRows("select ins_name,idno from installment_time where ins_name like 'other%' and (date between '$fdate' and '$tdate')");
					if(is_array($installOtherExtra) && count($installOtherExtra)>0){
						foreach($installOtherExtra as $installT){
							//other1_detail
							$idSdetail=str_replace("_detail","",$installT[0]);
							$idSdetail=str_replace("other","",$idSdetail);
							$otherExtra+=$db->getVal("select fine from installment_fine where ins_name='".$idSdetail."-otherfee' and idno=".$installT["idno"]);
							//echo $db->getLastQuery();
						}
						$overAllOther+=$otherExtra;
						$total+=$otherExtra;
					}
					$fine=(float)$db->getVal("select sum(fine) from installment_fine where (ins_name like '%-class' or ins_name like '%-hostel' or ins_name like '%-bus' or ins_name like '%-extra%') and (udate between '$fdate' and '$tdate')");
					$total+=$fine;
					$overAllFine+=$fine;
					$newExtra=(float)$db->getVal("select sum(fine) from installment_fine where (ins_name like '%-other') and (udate between '$fdate' and '$tdate')");
					$total+=$newExtra;
					$overAllNewextra+=$newExtra;
					if($total>0){
						$overAllTotal+=$total;
					}
				//}
					?>
				<tr>
					<td><?php echo date("dM Y",strtotime($fdate));?></td>
                    <td><?php
					$Grandtution+=$overAlltution; 
					//$GrandTransport+=$overAllTransport; 
					$Grandhotel+=$overAllhotel;
					if(is_array($overAllextra) && count($overAllextra)>0){ $gi=0;
						foreach($overAllextra as $ftype){ $gi++;
							 $Grandextra[$gi]+=$ftype; 
						}
					}
					$GrandOther+=$overAllOther;
					$GrandFine+=$overAllFine;
					$GrandNewextra+=$overAllNewextra;
					$tot=(float)$db->getVal("select sum(amount) from transaction where type='deposit' and (udate between '$fdate' and '$tdate') and (tfor='cash' or tfor='cheque')"); 
					echo $overAllTotal+$tot; $overAllTot=$overAllTot+$tot; $GrandTotal+=$overAllTotal+$tot; ?></td>
                    <td><?php 
						$mode=$db->getRows("Select * from mode where type='Expense' and status=1 and mode_type='transport'");
						//echo $db->getLastQuery();
						$tot_expense=0;
						if(is_array($mode) && count($mode)!=""){
							$where="";$expense=0;
							foreach($mode as $m){
								$where1.=" (detail NOT LIKE '%".$m["mode"]."%') AND";
							}
								$where=substr($where1, 0, strrpos($where1, " "));
							$expense=$db->getVal("select sum(amount) from transaction where type='withdraw' and (udate between '$fdate' and '$tdate') and rid=0 and tid=0 and ( ".$where.") and amount!=0");  if($expense!=0)
							$tot_expense=$expense;
							//echo $db->getLastQuery();								
						}echo $tot_expense;
						/*echo $exopance=(float)$db->getVal("select sum(amount) from transaction where type='withdraw' and (udate between '$fdate' and '$tdate') and rid=0 and tid=0");*/
					 	$overAllexpense+=$tot_expense; ?></td>
                    <td><?php echo ($overAllTotal+$tot)-$tot_expense; ?></td>
                    <td><?php echo $total_t;$GrandTotal_t+=$total_t; ?></td>
                    <td><?php $tot_expense_t=0;
						$expense_t=$db->getVal("select sum(amount) from transaction where type='withdraw' and (udate between '$fdate' and '$tdate') and rid=0 and tid=0 and amount!=0");			if($expense_t!=0){
						$tot_expense_tot=$expense_t;
						$tot_expense_t=$tot_expense_tot-$tot_expense;
						} echo $tot_expense_t;$overAllexpense_t+=$tot_expense_t;
						/*$mode=$db->getRows("Select * from mode where type='Expense' and status=1 and mode_type='transport'");
						if(is_array($mode) && count($mode)!=""){
							$tot_expense_t=0;
							foreach($mode as $m){
								$expense=$db->getRows("select amount from transaction where type='withdraw' and (udate between '$fdate' and '$tdate') and rid=0 and tid=0 and detail like '%".$m["mode"]."%' and amount!=0");
								//echo $db->getLastQuery();
								foreach($expense as $exp){
									//echo $exp['amount']."-----";
								  $tot_expense_t+=$exp['amount'];		
								}	
							}
						}echo $tot_expense_t;$overAllexpense_t+=$tot_expense_t;*/
						/*echo $exopance=(float)$db->getVal("select sum(amount) from transaction where type='withdraw' and (udate between '$fdate' and '$tdate') and rid=0 and tid=0");*/
					 	//$ovarallexopance+=$tot_expense_t; ?></td>
                        <td><?php echo $balance_t=$total_t-$tot_expense_t; ?></td>
                        <td><?php echo ($overAllTotal+$tot)+$total_t; $gtot_inc+=($overAllTotal+$tot)+$total_t; ?></td>
                        <td><?php echo $tot_expense_tot; $gtot_exp+=$tot_expense+$tot_expense_t; ?></td>
                        <td><?php echo (($overAllTotal+$tot)+$total_t)-($tot_expense+$tot_expense_t); ?></td>
                        
				</tr>
			<?php
			//}
		}
	}
		?>
		</tbody>
        <tfoot>
            <td>Gross Total</td>
            <?php /*?><td><?php echo $Grandtution ?></td>
            <td><?php echo $GrandTransport ?></td>
            <td><?php echo $Grandhotel ?></td>
            <?php
            if(is_array($Grandextra) && count($Grandextra)>0){
                foreach($Grandextra as $ftype){
                    ?><td><?php echo $ftype ?></td><?php
                }
           }?>
           <td><?php echo $GrandOther ?></td>
           <td><?php echo $GrandFine ?></td>
           <td><?php echo $GrandNewextra ?></td><?php 
           <td><?php echo $overAllTot; ?></td>*/?>
           <td><?php echo $GrandTotal; ?></td>
           <td><?php echo $overAllexpense;?></td>
           <td><?php echo $GrandTotal-$overAllexpense ?></td>
            <td><?php echo $GrandTotal_t; ?></td>
           <td><?php echo $overAllexpense_t;?></td>
           <td><?php echo $GrandTotal_t-$overAllexpense_t ?></td>
            <td><?php echo $gtot_inc; ?></td>
           <td><?php echo $gtot_exp;?></td>
           <td><?php echo $gtot_inc-$gtot_exp ?></td>
        </tfoot>
	</table>
		<?php
		//echo "<pre>";print_r($day);
	
}
else{
	echo "Please select date First";
}

?>
</div>