#!/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_currentUser
    user_getInformationById
);

use Opals::MarcXml qw(
    mxml_del_rec_holding
);
use Opals::Session qw(
    SessionHdl_clearVar
);
use URI::Escape;

use Opals::UnionRequest qw(
    urq_createUnionRequest
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

use Opals::BarcodeMgmt qw(
    bcm_validateBc
);

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/delBarcodeList.tmpl',
            reqPermission   => 'marc_del',
        }
);
$template->param(hlpUrl     => Opals::Constant->getHlpUrl('delExpFromHitlist'));
my ($errCode, $myCookie, $user) = user_currentUser($dbh, $cgi);
my $uid = $user->{'uid'};

my $msgValMap ={};
my $op       =  $input->{'op'};
my $source   =  $input->{'source'};
my $bcList   =  $input->{'bcList'};
my  $sth = "select  i.rid,i.barcode,l.dateLoan,l.dateReturn 
            from    opl_item i left outer join opl_loan l 
                    on i.barcode=l.barcode && l.dateReturn is null 
            where   i.barcode= ? ";
my  $sth_get_ridBc = $dbh->prepare($sth);
    
my $syspref = tmpl_preference($dbh);
my $validateBc = $syspref->{'validateBarcode'}; 
my $barcodeType = $syspref->{'barcodeType'}; 
my $updateUnion =$syspref->{'autoUpdateUnion'};

if ($op =~ m/^del$/i) {
    my ($totalRequest,$totalProcess,$totalOnloan,$totalDel) =(0,0,0,0);
    my @onloanBcList=();
    my @invalidBcList =();

    my @barcodes2Del = split /,/, $bcList;
    $totalRequest    = scalar(@barcodes2Del);
    my %h;
    my ($rid,$bc,$dateLoan,$dateReturn);
    foreach my $bcDel(@barcodes2Del){
         if($validateBc eq '1'){
             $bcDel=bcm_validateBc($dbh,$bcDel,$barcodeType);
         }
        $sth_get_ridBc->execute($bcDel);
        if(($rid,$bc,$dateLoan,$dateReturn)= $sth_get_ridBc->fetchrow_array){
            $totalProcess++;
            if($dateLoan && $dateLoan != "" && (!$dateReturn || $dateReturn == "")){
                push @onloanBcList,$bc  ;
                $totalOnloan++;
            }
            else{
                $h{$rid}{$bc}=1;
                $totalDel++;
            }
        }
        else{
            push @invalidBcList,$bcDel ;
        }
        $sth_get_ridBc->finish;
    }
   
  
     my $delOpt=1;# 0: keep xml record if remaining holdings=0; 1 delete xml record;

     my $onloanList  =join(", ",@onloanBcList);
     my $invalidList =join(", ",@invalidBcList);
     $updateUnion='1' if($updateUnion && $updateUnion ne '');
     mxml_del_rec_holding($dbh,\%h,$delOpt,$updateUnion);
     $msgValMap->{'delExpHitlist_msg_02'} ={totalRequest=>$totalRequest};
     $msgValMap->{'delExpHitlist_msg_02a'}={totalDel=>$totalDel};
     $msgValMap->{'delExpHitlist_msg_02b'}={totalOnloan=>$totalOnloan};
     $msgValMap->{'delExpHitlist_msg_02c'}={totalInvalid=>$totalRequest -  $totalProcess};
     $template->param( deleteHolding => 1,
                       delListInvalidItem=>($totalRequest>$totalProcess)?1:0,
                       delListHasLoanItem=>($totalProcess>$totalDel)?1:0,
                       invalidBcList     =>$invalidList,
                       onloanBcList      =>$onloanList,
                     );
}
$template->param(
      manual => ($source =~ m/^manual$/i)?1:0,
);

my $msgMap            =loc_getMsgFile('report/reports.msg',$msgValMap);
my $arlMsgMap         =loc_getMsgFile('search/arl.msg');
loc_write($template,$msgMap);
loc_write($template,$arlMsgMap);

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


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






