#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use JSON;
use Opals::Constant;
use Opals::Context; 
use Opals::MarcXml qw(
    mxml_del_rec_holding
);
use Opals::Session qw(
    SessionHdl_getSSID
);
use Opals::Template_ajax qw(
    tmpl_preference
);

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
my $cgi      = CGI->new;
my $ssid     =SessionHdl_getSSID($cgi);
my $input    = $cgi->Vars();
my $loginuid = $ENV{'curUserId'};
my $ssVar    ="hitList_deletion";
my $syspref = tmpl_preference($dbh);
my $updateUnion =$syspref->{'autoUpdateUnion'};
my ($totalRequest,$totalDel) =(0,0);
    
my $sth =$dbh->prepare("select i.rid,s.barcode 
                        from   opl_sessionVar s inner join opl_item i on i.barcode=s.barcode 
                        where ssid=? && var=? order by i.rid");

    $sth->execute($ssid,$ssVar);
    my %h;
    while(my ($rid,$bc)=$sth->fetchrow_array){
        $h{$rid}{$bc}=1;
        $totalDel++;
    }
    my $delOpt=1;# 0: keep xml record if remaining holdings=0; 1 delete xml record;
    $updateUnion='1' if($updateUnion && $updateUnion ne '');
    mxml_del_rec_holding($dbh,\%h,$delOpt,$updateUnion);

    
    #Clear all from table opl_sessionVar 
    $dbh->do("delete from opl_sessionVar where ssid=? && var=?",undef,$ssid,$ssVar);
    
    

print "Content-type: text/plain\n\n";
print  to_json({status=>1,task=>'deletion',totalDel=>$totalDel});


