#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_redirect
);
use Opals::Circulation qw(
    circ_lostDeclarating
    circ_infoRecord
    circ_saveFine
    circ_GetDamagedNumber
    circ_GetLostNumber
);
use Opals::Search qw(
    srch_searchRecord
);
use Opals::Date qw(
    date_parse
    date_today
    date_text
);
use Opals::User qw(
    user_getInformation
    user_balance
    user_getInformationById
);

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

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $srchField = $cgi->param('sf');
($srchField) || ($srchField = 0);
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'circ/bill.tmpl',
            reqPermission   => 'fine',
        }
);

if ($permission && $permission->{'circ_loan'}) 
{
    my $uid = $cgi->cookie('borrower');
    my ($user, $ddd) = user_getInformationById($dbh, $uid);
        
    if ( $input->{'save'} )
    {
        circ_saveFine($dbh, $input);
                tmpl_redirect($cgi, 'loan');
        goto __END_OF_FILE;
    }
    else { circ_lostDeclarating($dbh, $input); }

    # prepare data for displaying
    GetLostList($dbh, $uid, $template);
    my $balance = user_balance($dbh, $uid);

    $template->param(
        user_uid       => $user->{'uid'},
        user_username       => $user->{'username'},
        user_firstname      => $user->{'firstname'},
        user_lastname       => $user->{'lastname'},
        user_addrLine1   => $user->{'addrLine1'},
        user_city           => $user->{'city'},
        user_zip     => $user->{'zip'},
        user_state     => $user->{'state'},
        user_phone          => $user->{'phone'},
        user_email          => $user->{'email'},
        balance             => $balance,
        damaged             => (circ_GetDamagedNumber($dbh, $user->{'uid'})),
        lost                => (circ_GetLostNumber($dbh, $user->{'uid'})),
    );
}
else 
{
}

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



#----------------------------------------------------------
sub GetLostList
{
    my ($dbh, $uid, $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 && l.uid=?");
    $query->execute($uid);

    my @LostList = ();
    my $count = 0;
    my $bZebraServerDown = 0;

    while ( my $lost = $query->fetchrow_hashref )
    {
        $lost->{'number'} = $count;
        ++$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
    # get federal tax and province tax from system preference
    $query = $dbh->prepare("select * from opl_preference where var=?");
    
    $query->execute('fdrtax');
    my $rec = $query->fetchrow_hashref;
    $query->finish;
    $template->param(curfdrtax => $rec->{'val'});
    
    $query->execute('prvtax');
    $rec = $query->fetchrow_hashref;
    $query->finish;
    $template->param(curprvtax => $rec->{'val'});

    $template->param(LostCount => scalar(@LostList) );
    $template->param(lostfineList => \@LostList);
}

