#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;use POSIX qw(
    floor
);

use Time::localtime;
use Opals::Constant;

use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::User qw(
    user_list
    user_getInformationById
    user_balance
);
use Opals::Circulation qw(
    circ_getItemInfo
    circ_getRecID
);
use Opals::Date qw(
    date_parse
    date_today
    date_text
    date_validateWorkday
    date_deltaWorkDay
    date_addDeltaWorkday
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_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/printLabels.tmpl',
            reqPermission   => 'report',
        }
);
my $syspref = tmpl_preference($dbh);
my @cookieList = ($cookie);

my $tm = localtime;
my $todayStr = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);
my ($bc,$rid,@ridArr, @ncrArr,$op, $dateExpiry);
my $holdingList;
  
my $sField  = $cgi->param('sf');
($sField && $sField =~ m/^(title|author|callnumber|barcode)$/i) || ($sField = 'barcode');
     
my $sValue  = $cgi->param('kw0');

my $itemInfo; 
if ($permission && $permission->{'report'}) {
    if($sValue && $sValue ne ''){
         $template->param(
            sValue   => $sValue,
        );
    }#if($sField eq 'barcode')
           
       }

#Tue, Jan 12, 2010 @ 10:31:41 EST
my $msgValMap ={};
my $msgMap            =loc_getMsgFile('report/reports.msg',$msgValMap);
loc_write($template,$msgMap);

tmpl_write($dbh, $cgi, \@cookieList, $template);


################################################################################

sub getRecordHolding {
my ($dbh, $rid,$barcode) = @_;
 
    my $sth = $dbh->prepare(<<_STH_);
select  barcode
from    opl_item 
where   rid = ?
_STH_
    $sth->execute($rid);
    my @holdingList;
    while (my $holding = $sth->fetchrow_hashref) {
        if($holding->{'barcode'} =~m/^\_\_\_(.*)\_[\d]+$/){
            $holding->{'barcode'}=$1;
        }
        
        if ($barcode eq $holding->{'barcode'}){
            $holding->{'checked'}= 1;
        }
         
        push @holdingList, $holding;
    }
    $sth->finish;
 
    return \@holdingList;
}

################################################################################








