#!/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;
fixRqStatus_cancel_reserve();
fixRqStatus_cancel_hold();


sub fixRqStatus_cancel_reserve{
    my $sth=$dbh->prepare("select r.id,r.uid,s.dateCancel from opl_requestReserve r inner join opl_reserve s using(idReserve) where r.status='reserve' && s.dateCancel is not null");
    $sth->execute();
    while( my($id,$uid,$dateCancel) =$sth->fetchrow_array){
        $dbh->do("update opl_requestReserve set status='cancel' where id=?",undef,$id);
        my $log=sprintf "{\"notes\":\"\",\"dateProcess\":\"%s\"}",$dateCancel;
        $dbh->do("insert into opl_reqReserveHistory set reqId=?,uid=?,action='loan',dateLog=?,log=?",undef,$id,$uid,$dateCancel,$log);

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

    }
}

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