package Textbook::SchoolYear;

require Exporter;
@ISA       = qw(Exporter);
# Symbols to be exported by default
#@EXPORT    = qw(
#    opl_
#);
# Symbols to be exported on request
@EXPORT_OK = qw(
    schoolyear_getCurrent
    schoolyear_add
    schoolyear_delete
    schoolyear_update
    schoolyear_list
);
# Version number
$VERSION   = 0.01;      

#use utf8;
use strict;

############################################################
#
# function schoolyear_getCurrent
#   
#
############################################################
sub schoolyear_getCurrent{
    my ($dbh)=@_;
    my $sql="select * from tbk_schoolYear
             where firstDate <=now() && lastDate >=now() 
             order by id desc limit 1 ";
    
    my $sth = $dbh->prepare($sql);
    $sth->execute();
    my $rec ;
    if($rec = $sth->fetchrow_hashref ){
        return $rec;
    }
    else{
        $sql="select * from tbk_schoolYear
             order by id desc limit 1 " ;
        $sth = $dbh->prepare($sql);
        $sth->execute();
        if( $rec = $sth->fetchrow_hashref ){
            return $rec;
        }
    }
    return undef;
}


############################################################
#
# function schoolyear_add
#   
#
############################################################
sub schoolyear_add{
    my ($dbh,$params)=@_;
    
    return -1 if($params->{'schoolYear'} eq '');
    my @fVals=();
    my $sql="insert into tbk_schoolYear set schoolYear= ? ";
    push @fVals,$params->{'schoolYear'};

    if($params->{'firstDate'} ne '' && $params->{'lastDate'} ne '' ){
        $sql .=" , firstDate = ? ";
        $sql .=" , lastDate= ? ";
        push @fVals,$params->{'firstDate'};
        push @fVals,$params->{'lastDate'};
    }



    my $sth = $dbh->prepare($sql);
    $sth->execute(@fVals);
    my $id =  $dbh->{'mysql_insertid'};
    $sth->finish;
    return $id;
}
############################################################
#
# function schoolyear_update
#   
#
############################################################
sub schoolyear_update{
    my ($dbh,$params)=@_;
    return -1 if($params->{'id'} eq '');

    my $sql="update  tbk_schoolYear ";
    my @fields=('schoolYear','firstDate','lastDate');
    my $sql_fields="";
    my @fVals=();
    
    foreach my $f(@fields){
        if($params->{$f} ne ''){
            $sql_fields .=", " if ($sql_fields ne '');
            $sql_fields .=" $f=? ";
            push @fVals,$params->{$f};
        }
    }

    return if($sql_fields eq '');
    $sql .="set $sql_fields where id= ?";

    push @fVals,$params->{'id'};

    my $sth = $dbh->prepare($sql);
    $sth->execute(@fVals);
    $sth->finish;
}
############################################################
#
# function schoolyear_delete
#   
#
############################################################
sub schoolyear_delete{
    my ($dbh,$id)=@_;
    return -1 if($id eq '');

    my $sql="delete from tbk_schoolYear where id=?";
    my $sth = $dbh->prepare($sql);
    $sth->execute($id);
    $sth->finish;
}

1;
