#!/usr/bin/perl
#  Copyright 2001-2012 Leslie Richardson

#  This file is part of Open Admin for Schools.

#  Open Admin for Schools is free software; you can redistribute it 
#  and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2 of 
#  the License, or (at your option) any later version.

my %lex = ('School Enrollment' => 'School Enrollment',
	   'Main' => 'Main',
	   'Enrollment' => 'Enrollment',
	   'Current Date' => 'Current Date',
	   'Aging Date' => 'Aging Date',
	   'Current Enrollment' => 'Current Enrollment',
	   'Student' => 'Student',
	   'Grade' => 'Grade',
	   'Date' => 'Date',
	   'Type' => 'Type',
	   'Description' => 'Description',
	   'Enrolled' => 'Enrolled',
	   'Not Found' => 'Not Found',
	   'No Enrollment Changes' => 'No Enrollment Changes',
	   'Error' => 'Error',
	   'Continue' => 'Continue',
	   'Blank=Today' => 'Blank=Today',
	   'by' => 'by',

	   );

use DBI;
use CGI;

my $self = 'rptenroldate.pl';

eval require "../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. ": $@<br>\n";
    die $lex{Error}. ": $@\n";
}

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, 
    $iddst) = localtime(time);
$year = $year + 1900;
$mon++;
$wday++;
#my $currsdate = "$year-$mon-$mday";
my $currdate = "$dow[$wday], $month[$mon] $mday, $year";

my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;

print "$doctype\n<html><head><title>$lex{'School Enrollment'}</title>\n";
print "<link rel=\"stylesheet\" href=\"$css\" type=\"text/css\">\n";
print "$chartype\n</head><body style=\"padding:1em 2em;\">\n";
print "[ <a href=\"$homepage\">$lex{Main}</a> ]\n";

print "<center><h1>$lex{Enrollment} $lex{by} $lex{Date}</h1>\n";


if ( not $arr{page} ) {
    showStartPage();
}


my ($mo, $yr, $da);
if ( not $arr{agingdate} ) { # We'll go back 1 month.
    if ( $mon > 1 ){
	$mo = $mon;
	$yr = $year;
    } else { # else we're at 1 - January...
	$mo = 12;
	$yr = $year - 1;
    }
    $da = $mday;
} else {
    ($yr, $mo, $da) = split(/-/, $arr{agingdate} );
}

my $agingdate = "$yr-$mo-$da";


my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;

# First count how many currently enrolled (in student table)
my $sth = $dbh->prepare("select count(*) from student");
$sth->execute;
my $curenrol = $sth->fetchrow;

# Now count backwards through entry records until we reach date of interest
my $sth1 = $dbh->prepare("select * from transfer 
  where to_days(date) >= to_days('$agingdate') order by date desc");
$sth1->execute;
if ($DBI::errstr) {print $DBI::errstr; die $DBI::errstr; }
my $rows = $sth1->rows;

print "<table cellpadding=\"4\" cellspacing=\"0\" border=\"0\">\n";
print "<tr><td class=\"bra\">$lex{'Current Date'}</td><td class=\"la\">$currdate</td></tr>\n";
print "<tr><td class=\"bra\">$lex{'Aging Date'}</td><td class=\"la\">$agingdate</td></tr>\n";
print "<tr><td class=\"bra\">$lex{'Current Enrollment'}</td><td class=\"la\">$curenrol</td></tr>\n";
print "</table>\n";


if ( $rows < 1 ) {
    print "<h3>$lex{'No Enrollment Changes'}</h3>\n";
    print "</body></html>\n";
    exit;
}


print "<table cellpadding=\"4\" cellspacing=\"0\" border=\"1\">\n";
print "<tr><th>$lex{Student}</th><th>$lex{Grade}</th><th>$lex{Date}</th>";
print "<th>$lex{Type}</th><th>$lex{Description}</th><th>$lex{Enrolled}</th></tr>\n";


my $sth2 = $dbh->prepare("select lastname, firstname, grade from studentall 
  where studnum = ?");

for ( 1 .. $rows ) {

    my $ref = $sth1->fetchrow_hashref;
    my %sr = %$ref;

    # Find student name
    my $name;
    $sth2->execute( $sr{studnum} );
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    my ( $lastname, $firstname, $grade ) = $sth2->fetchrow;
    if ( not $lastname ) {
	if ( $sr{lastname} ) { $name = "$sr{firstname} $sr{lastname}"; } else {
	    $name = $lex{'Not Found'};
	}
    } else {
	$name = "$firstname $lastname";
    }

    # print record
    print "<tr><td class=\"bla\">$name ($sr{studnum})</td><td class=\"cn\">$grade</td>\n";
    print "<td>$sr{date}</td><td>$sr{type}</td><td>$sr{description}</td>\n";
    print "<td class=\"cn\">$curenrol</td></tr>\n";

    # Change Enrollment Number
    if ( $sr{type} ne 'withdraw' ) { # then was an enrol
	$curenrol--;
    } else {
	$curenrol++;
    }

}

print "</table></center></body></html>\n";



#----------------
sub showStartPage {
#----------------

    print "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" ";
    print "href=\"/js/calendar-blue.css\" title=\"blue\">\n";
    print "<script type=\"text/javascript\" src=\"/js/calendar.js\"></script>\n";
    print "<script type=\"text/javascript\" src=\"/js/lang/calendar-en.js\"></script>\n";
    print "<script type=\"text/javascript\" src=\"/js/calendar-setup.js\"></script>\n";

    print "<form action=\"$self\" method=\"post\">\n";
    print "<input type=\"hidden\" name=\"page\" value=\"1\">\n";

    print "<table cellspacing=\"0\" border=\"0\" cellpadding=\"3\">\n";

    print "<tr><td class=\"bra\">$lex{'Aging Date'}</td><td class=\"la\">\n";
    print "<input type=\"text\" name=\"agingdate\" size=\"12\" id=\"agingdate\">\n";
    print "<button type=\"reset\" id=\"start_trigger\">...</button> ". $lex{'Blank=Today'};
    print "</td></tr>\n";

    print "<tr><td></td><td class=\"la\"><input type=\"submit\" value=\"$lex{Continue}\">\n";
    print "</td></tr></table></form>\n";

    print "<script type=\"text/javascript\">
     Calendar.setup({
        inputField     :    \"agingdate\", // id of the input field
        ifFormat       :    \"%Y-%m-%d\", // format of the input field
        button         :    \"start_trigger\", // trigger for the calendar (button ID)
        singleClick    :    false,        // double-click mode
        step           :    1             // show all years in drop-down boxes 
    })";

    print "</script>\n";
    print "</body></html>\n";

    exit;

}
