#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
);
use Opals::Circulation qw(
    circ_infoRecord
    circ_saveFine
);
use Opals::Search qw(
    srch_searchRecord
);
use Opals::Date qw(
    date_parse
    date_today
    date_text
);

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        => 'circ/userfine.tmpl',
            reqPermission   => 'fine|payment',
        }
);

if ($permission && $permission->{'circ_loan'}) 
{
    if ( $input->{'save'} )
    {
        circ_saveFine($dbh, $input);
    }

    my $arrLost = GetLostList($dbh, $template);
    my $arrDamage = GetDamageList($dbh, $template);
    $template->param(HaveSave => scalar(@$arrLost) || scalar(@$arrDamage) );
}
else 
{
}

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



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

    my $query = $dbh->prepare("select b.idloan, b.price, b.orderfee, b.shipfee,
         b.fdrtax, b.prvtax, b.amount, b.ondate, b.final, l.barcode
         from opl_lost as b, opl_loan as l where final=0 && b.idloan=l.idloan");
    $query->execute();
   
    my $count = 0; 
    my @LostList = ();
    my $bZebraServerDown = 0;

    while (my $lost = $query->fetchrow_hashref)
    {
        $lost->{'number'} = $count++;

        my $pqf = "\@attr 1=5000 $lost->{'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'});

            $lost->{'rid'}     = $result->[0]->{'rid'};
            $lost->{'title'}   = $result->[0]->{'title'};
            $lost->{'author'}  = $result->[0]->{'author'};
            $lost->{'pubName'} = $result->[0]->{'pubName'};
            $lost->{'pubDate'} = $result->[0]->{'pubDate'};
            $lost->{'dewey'}   = $items->[0]->{'callnumber'};
        }
        else 
        {
            $bZebraServerDown = 1;            
        }
        
        push @LostList, $lost;
    }
    $query->finish;
    
    # Set template
    $template->param(LostCount => scalar(@LostList) );
    $template->param(lostfineList => \@LostList);

    return \@LostList;
}


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

    my $query = $dbh->prepare("select d.idloan, d.fine, d.amount, 
         d.fdrtax, d.prvtax, d.ondate, d.final, l.barcode
         from opl_damage as d, opl_loan as l where final=0 && d.idloan=l.idloan");
    $query->execute();
    
    my @DamageList = ();
    my $bZebraServerDown = 0;

    for (my $i=0; my $damage = $query->fetchrow_hashref; $i++ )
    {
        $damage->{'number'} = $i;

        my $pqf = "\@attr 1=5000 $damage->{'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'});

            $damage->{'rid'}     = $result->[0]->{'rid'};
            $damage->{'title'}   = $result->[0]->{'title'};
            $damage->{'author'}  = $result->[0]->{'author'};
            $damage->{'pubName'} = $result->[0]->{'pubName'};
            $damage->{'pubDate'} = $result->[0]->{'pubDate'};
            $damage->{'dewey'}   = $items->[0]->{'callnumber'};
        }
        else 
        {
            $bZebraServerDown = 1;            
        }
        
        push @DamageList, $damage;
    }
    $query->finish;
    
    # Set template
    $template->param(DamageCount => scalar(@DamageList) );
    $template->param(damagefineList => \@DamageList);

    return \@DamageList;
}

__END_OF_FILE:
