<?php

if(isset($_POST["submit600"])){

$selectyear=$_POST['schoolyear'];

$ya = "and schoolyear=$selectyear";

$tid= $_POST['term'];

}

else

{

$selectyear=date('y');

$ya = "and schoolyear=$selectyear";

$cm = date("m");



if ($cm>='1' and $cm<='4'){$tid="1";}

elseif ($cm>='5' and $cm<='8'){$tid="2";}

elseif ($cm>='9' and $cm<='12'){$tid="3";}

}

//Unpaid fees and requirements primary 



$pri_expected = $dbh->query

("select sum(ownfee)+sum(uniform)+sum(contribution)+sum(registration)+sum(holiday)+sum(admission)+sum(transport)+sum(mockfa)+sum(other1)+sum(other2)+sum(other3) AS pri_totalexpected from t".$tid." where schoolclass<20 $ya and active='1'");

$row_expected1 = $pri_expected->fetchObject(); 



$sec_expected = $dbh->query

("select sum(ownfee)+sum(uniform)+sum(contribution)+sum(registration)+sum(holiday)+sum(admission)+sum(transport)+sum(mockfa)+sum(other1)+sum(other2)+sum(other3) AS sec_totalexpected from t".$tid." where schoolclass>20 $ya and active='1'");

$row_expected2 = $sec_expected->fetchObject();



$pri_income = $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS pri_totalincome from received where schoolclass<20 and term=".$tid." $ya");

$row_income1 = $pri_income->fetchObject(); 



$sec_income = $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS sec_totalincome from received where schoolclass>20 and term=".$tid." $ya");

$row_income2 = $sec_income->fetchObject();





if ($row_expected1->pri_totalexpected>'0'){

$sum_pri_expected=$row_expected1->pri_totalexpected;

$sum_pri_income=$row_income1->pri_totalincome;

$pri_variance=$sum_pri_income-$sum_pri_expected;

$pri_variance_p=($sum_pri_income/$sum_pri_expected)*100;

}

else

{

$sum_pri_expected=0;

$sum_pri_income=0;

$pri_variance=0;

$pri_variance_p=0;

}



if ($row_expected2->sec_totalexpected>'0'){

$sum_sec_expected=$row_expected2->sec_totalexpected;

$sum_sec_income=$row_income2->sec_totalincome;

$sec_variance=$sum_sec_income-$sum_sec_expected;

$sec_variance_p=($sum_sec_income/$sum_sec_expected)*100;

}

else

{

$sum_sec_expected=0;

$sum_sec_income=0;

$sec_variance=0;

$sec_variance_p=0;

}



$all_expected=$sum_pri_expected+$sum_sec_expected;

$all_income=$sum_pri_income+$sum_sec_income;



if($all_expected>0)

{

$all_variance=$all_income-$all_expected;

$all_variance_p=($all_income/$all_expected)*100;

}

else

{

$all_variance=0;

$all_variance_p=0;

}





/*above: Unpaid fees and requirements secondary */





$cmtouse=date("m", strtotime("this month"));

$datecmfull   = DateTime::createFromFormat('!m', $cmtouse);

$cmfull = $datecmfull->format('F'); 







$cm1=date("m", strtotime("-1 months"));

$datecm1full   = DateTime::createFromFormat('!m', $cm1);

$cm1full = $datecm1full->format('F'); 



$cm2=date("m", strtotime("-2 months"));

$datecm2full   = DateTime::createFromFormat('!m', $cm2);

$cm2full = $datecm2full->format('F'); 



$cm3=date("m", strtotime("-3 months"));

$datecm3full   = DateTime::createFromFormat('!m', $cm3);

$cm3full = $datecm3full->format('F'); 



$cm4=date("m", strtotime("-4 months"));

$datecm4full   = DateTime::createFromFormat('!m', $cm4);

$cm4full = $datecm4full->format('F'); 



$cm5=date("m", strtotime("-5 months"));

$datecm5full   = DateTime::createFromFormat('!m', $cm5);

$cm5full = $datecm5full->format('F'); 





$cmexpenses= $dbh->query

("select sum(cost) from expenses2 where datez like '%-".$cmtouse."-%'");

$rowcmexpenses = $cmexpenses->fetch(); 



$cm1expenses= $dbh->query

("select sum(cost) from expenses2 where datez like '%-".$cm1."-%'");

$rowcm1expenses = $cm1expenses->fetch(); 



$cm2expenses= $dbh->query

("select sum(cost) from expenses2 where datez like '%-".$cm2."-%'");

$rowcm2expenses = $cm2expenses->fetch(); 



$cm3expenses= $dbh->query

("select sum(cost) from expenses2 where datez like '%-".$cm3."-%'");

$rowcm3expenses = $cm3expenses->fetch(); 



$cm4expenses= $dbh->query

("select sum(cost) from expenses2 where datez like '%-".$cm4."-%'");

$rowcm4expenses = $cm4expenses->fetch(); 



$cm5expenses= $dbh->query

("select sum(cost) from expenses2 where datez like '%-".$cm5."-%'");

$rowcm5expenses = $cm5expenses->fetch(); 





//incomes per month

$cmincome= $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS cmincome from received where datez like '%-".$cmtouse."-%'");

$rowcmincome = $cmincome->fetch(); 



$cm1income= $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS cm1income from received where datez like '%-".$cm1."-%'");

$rowcm1income = $cm1income->fetch(); 



$cm2income= $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS cm2income from received where datez like '%-".$cm2."-%'");

$rowcm2income = $cm2income->fetch(); 



$cm3income= $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS cm3income from received where datez like '%-".$cm3."-%'");

$rowcm3income = $cm3income->fetch(); 



$cm4income= $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS cm4income from received where datez like '%-".$cm4."-%'");

$rowcm4income = $cm4income->fetch(); 



$cm5income= $dbh->query

("select sum(p_fees)+sum(p_uniform)+sum(p_contribution)+sum(p_registration)+sum(p_holiday)+sum(p_admission)+sum(p_transport)+sum(p_mockfa)+sum(p_other1)+sum(p_other2)+sum(p_other3

	) AS cm5income from received where datez like '%-".$cm5."-%'");

$rowcm5income = $cm5income->fetch(); 

?>