  <?php 
   session_start();
   error_reporting(0);
    include('../include/config.php');
    $pname='';
    
    $date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
    $timenow=$date->format('Y-m-d H:i:s');
    $timenow2=$date->format('Y-m-d');
    $month=$date->format('M');
    $cat='';

   if(isset($_POST["pid"]))
   {
    $pid= $_POST["pid"];
    $yr= $_POST["yr"];
    $bala= $_POST["bala"];
    $grade= $_POST["grade"];
    $trm= $_POST["trm"];
    $pdate= $_POST["pdate"];
    $amount= $_POST["amount"];
    $cheq= $_POST["cheq"];
    $bk= $_POST["bk"];
    $cod= $_POST["cod"];
    $usa= $_POST["usa"];
    
     $tu= $_POST["tu"];
      $bo= $_POST["bo"];
       $ad= $_POST["ad"];
        $tr= $_POST["tr"];
         $mot= $_POST["mot"];
          $uni= $_POST["uni"];
           $dia= $_POST["dia"];
            $bkk= $_POST["bkk"];
             $pe= $_POST["pe"];
              $tab= $_POST["tab"];
               $dev= $_POST["dev"];
                $asbk= $_POST["asbk"];
               
   if($grade=='GRADE 1' || $grade=='GRADE 2' || $grade=='GRADE 3' || $grade=='GRADE 4' || $grade=='GRADE 5' || $grade=='GRADE 6' || $grade=='GRADE 7' || $grade=='GRADE 8' || $grade=='GRADE 9')
   {
      $cat='Upper';
   }else
   {
      $cat='Lower'; 
   }
               
    $sqlpupil = "SELECT * FROM pupils where count='$pid'";
    $result = $con->query($sqlpupil);
    while($row = $result->fetch_assoc()) 
    {
    $pname=$row['Names'];
    }

    $yrr=substr($pdate, 0, 4);
    $mm=substr($pdate, 5, 2);
    
    if($mm=='01'){$mm='January';}
    if($mm=='02'){$mm='February';}
    if($mm=='03'){$mm='March';}
    if($mm=='04'){$mm='April';}
    if($mm=='05'){$mm='May';}
    if($mm=='06'){$mm='June';}
    if($mm=='07'){$mm='July';}
    if($mm=='08'){$mm='August';}
    if($mm=='09'){$mm='September';}
    if($mm=='10'){$mm='October';}
    if($mm=='11'){$mm='November';}
    if($mm=='12'){$mm='December';}
    
    $invoice=$yr.$mm;
    $query=mysqli_query($con,"SELECT * FROM Fees2 WHERE Year='$yr' AND Grad='$grade' AND Term='$trm' AND pid='$pid'");

    $num=mysqli_fetch_array($query);
    if($num>0)
    {
    $ba=$tofee-$amount;
    $sql=mysqli_query($con,"INSERT INTO feestatement(Cate,pid,Grad,BankDate,Bank,Month,Term,Year,PaidAmount,User,Voucherid,EntryDate,Invoice,Cod,TuFee,BoFee,AdFee,TrFee,MoFee,UnFee,DiFee,BookFee,PeFee,Tablet,Dev,Assbok)
    VALUES ('$cat','$pid','$grade','$pdate','$bk','$mm','$trm','$yrr','$amount','$usa','$cheq','$timenow2','$invoice','$cod','$tu','$bo','$ad','$tr','$mot','$uni','$dia','$bkk','$pe','$tab','$dev','$asbk')");
    if($sql)
    {
    $msgg='added fee payment for '.$pname.' for '.$trm.' '.$yr;
    $sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$usa','$msgg','$timenow','$timenow2','save','$month')");    
        
    echo 'Successfully saved';
    }else
    {
    echo 'Error occured while submitting data!';  
    }
        
    }else
    {
    echo 'Fees not available';
    }
   }else if(isset($_POST["id"]))
   {
        $pid= $_POST["id"];
        $tu= $_POST["tu"];
        $bo= $_POST["bo"];
        $ad= $_POST["ad"];
        $tr= $_POST["tr"];
        $mot= $_POST["mot"];
        $uni= $_POST["uni"];
        $dia= $_POST["dia"];
        $bkk= $_POST["bkk"];
        $pe= $_POST["pe"];
        $tot= $_POST["tot"];
        $tab= $_POST["tab"];
        $dev= $_POST["dev"];
        
        $trm= $_POST["trm"];
        $yr= $_POST["yr"];
        $grd= $_POST["grd"];
        
        $grade=$grd;
                 
   if($grade=='GRADE 1' || $grade=='GRADE 2' || $grade=='GRADE 3' || $grade=='GRADE 4' || $grade=='GRADE 5' || $grade=='GRADE 6' || $grade=='GRADE 7' || $grade=='GRADE 8' || $grade=='GRADE 9')
   {
      $cat='Upper';
   }else
   {
      $cat='Lower'; 
   }
  
    $sql=mysqli_query($con,"UPDATE feestatement SET Year='$yr',Term='$trm',Grad='$grd',Cate='$cat',Dev='$dev',TuFee='$tu',BoFee='$bo',AdFee='$ad',TrFee='$tr',MoFee='$mot',UnFee='$uni',DiFee='$dia',BookFee='$bkk',PeFee='$pe',PaidAmount='$tot',Tablet='$tab' where count='$pid'");
    if($sql)
    {
    $msgg='updated a fee statement';
    $sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$usa','$msgg','$timenow','$timenow2','save','$month')");    
    echo 'Successfully saved';
    }else
    {
    echo 'Error occured while submitting data!';  
    }
   }
   else{
       echo 'error occured';
   }
?>