#!/usr/bin/perl -w

BEGIN {
    if (!$ENV{'PERL5LIB'} || !$ENV{'OPALS_CONF'}) {
        exit 1;
    }
}


use Opals::Context;

use strict;
my $dbh = Opals::Context->dbh();;
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}
$| = 1;

my $sth=$dbh->prepare("select r.id,r.uid,h.dateLoan from opl_requestReserve r inner join opl_hold h using(idReserve) where r.status<>'loan' &&idloan>0");
$sth->execute();
while( my($id,$uid,$dateLoan) =$sth->fetchrow_array){
    $dbh->do("update opl_requestReserve set status='loan' where id=?",undef,$id);
    my $log=sprintf "{\"notes\":\"\",\"dateProcess\":\"%s\"}",$dateLoan;
    $dbh->do("insert into opl_reqReserveHistory set reqId=?,uid=?,action='loan',dateLog=?,log=?",undef,$id,$uid,$dateLoan,$log);

}

exit 0;
############################################################
