#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Opals::Circulation qw(
    circ_infoRecord
);

use Opals::Search qw(
    srch_searchRecord
);

use Time::localtime;

use Opals::Date qw(
    date_parse
    date_today
    date_text
);

use Opals::Template qw(
    tmpl_read
    tmpl_write
);


my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();

my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'report/ancreserve.tmpl',
            reqPermission   => 'report',
        }
);

#if ($permission && $permission->{'user_delete'}) 
#{
#    my $input = $cgi->Vars();
#    if ($input->{'op'} eq 'delete') 
#    {
#        if (user_delete($dbh, $input)) 
#        {
#            $template->param(success => 1);
#        }
#        else 
#        {
#            $template->param(error => 1);
#        }
#    }
#    $template->param(userDel => 1);
#}

    if ( !$input->{'username'} )
    {
        $template->param(EnterIdUser => 1);
    }
    else
    {
        my $username = $input->{'username'};
        GetLoan($dbh, $template, $username);

        my $tm = localtime;
        my $szToday = sprintf("%02d-%02d-%04d", ($tm->mon)+1, $tm->mday, $tm->year+1900);
        $template->param(today => $szToday);
    }

   tmpl_write($dbh, $cgi, $cookie, $template);

#--------------------------------------------------------
sub GetLoan
{
    my ($dbh, $template, $username) = @_;

# Get borrower information
    my $szSQL = "
select  teacher, grade, homeroom, buildingcode, email,
        firstname, lastname
from    opl_user
where   username = ?";        

    my $query = $dbh->prepare($szSQL);
    my $bResult = $query->execute($username);
    my $brwr = $query->fetchrow_hashref();
    $query->finish;

    $template->param(firstname => $brwr->{'firstname'});
    $template->param(lastname => $brwr->{'lastname'});
    $template->param(grade => $brwr->{'grade'});
    $template->param(email => $brwr->{'email'});

# Get reserve information
    $szSQL = "
select      idReserve, rid, dateReserve
from        opl_reserve
where       username = ? && dateCencel is null
groupby     rid";

# Get item information        
    $szSQL = "        
select  barcode, dateLoan, dateDue,
        to_days(now()) - to_days(dateDue) as deltaDueDate 
from    opl_hold
where   username = ? && dateReturn is null";

    my $query = $dbh->prepare($szSQL);
    my $bResult = $query->execute($username);
    my $bZebraServerDown = 0;
    
    my $loan;
    my @loanRecs = ();
    
    while ( $loan = $query->fetchrow_hashref() )
    {
        if ( $loan->{'deltaDueDate'} < 0 )
        {
            next;
        }
        
        $loan->{'dateLoan'} = date_text($loan->{'dateLoan'}, 0);
        $loan->{'dateDue'}  = date_text($loan->{'dateDue'}, 0);
        
        my $pqf = "\@attr 1=5000 $loan->{'barcode'}";
        my ($resultSize, $result) = srch_searchRecord($dbh, 0, 'b', $pqf, $ENV{'Z_INDEX_BASE'}, 1);
        if ($result) 
        {
            my ($numTotal, $numLoan, $numReserve, $numHold, $items)
                = circ_infoRecord($dbh, $result->[0]->{'rid'}, $result->[0]->{'itemList'});
            $loan->{'rid'}     = $result->[0]->{'rid'};
            $loan->{'title'}   = $result->[0]->{'title'};
            $loan->{'author'}  = $result->[0]->{'author'};
            $loan->{'pubName'} = $result->[0]->{'pubName'};
            $loan->{'pubDate'} = $result->[0]->{'pubDate'};
            $loan->{'dewey'}   = $items->[0]->{'callnumber'}
        }
        else 
        {
            $loan->{'rid'}     = '';
            $loan->{'title'}   = '';
            $loan->{'author'}  = '';
            $loan->{'pubName'} = '';
            $loan->{'pubDate'} = '';
            $loan->{'dewey'}   = '';
        }
        
        push @loanRecs, {title => $loan->{'title'}, dewey => $loan->{'dewey'}, 
                barcode => $loan->{'barcode'}, dateDue => $loan->{'dateDue'}}
    }
    $query->finish;

    $template->param(ItemsInLoan => \@loanRecs);
}

__END_OF_FILE:

