#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Digest::SHA qw(
    sha1_base64
    sha1_hex
);

use Opals::Context;
use Opals::Template_ajax qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use JSON;
use Opals::Fines qw(
    fine_getFineList
);
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi      = CGI->new;
my $input    = $cgi->Vars();
my $lidList       = $input->{"lidList"};


my ($status,$errorCode,$errorMsg)    ;
my ($permission, $cookie, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'ajax/circ/fineInfo.tmpl',
        reqPermission   => 'circ_loan|circ_return',
    }
);
if ($permission && ($permission->{'circ_loan'}|| $permission->{'circ_return'})) {
    my $syspref = tmpl_preference($dbh);
    my @fineList =fine_getFineList($dbh,$lidList);
    my @retFineList=();
    foreach my $fine(@fineList){
        my @fa=();
        foreach my $f(@{$fine->{'fineInfo'}}){
            my $type =$f->{'type'};
            next if($type eq 'overdue' && isODExempt($dbh,$fine->{'l_uid'}));
            $fine->{"$type" . "_odlId"}=$f->{'odl_id'};
            if($type eq 'overdue'){
                $fine->{"daysOverdue"}  = $f->{'daysOverdue'};
                $fine->{"hoursOverdue"} = $f->{'hoursOverdue'};
            }
            push @fa,$f;
        }

        if(scalar(@fa)>0){
            $fine->{'fineInfo'}=\@fa;
            push @retFineList,$fine;
        }

    }
   # $template->param(fineList => \@retFineList);
  
   my $fList=[];
   foreach my $f( @retFineList){
       push @$fList,{
           fine=>{
               lid=>($f->{'idloan'}||0),
               overdue=>{
                   odl_id       =>($f->{'overdue_odlId'}||0),
                   daysOverdue  =>($f->{'daysOverdue'}  || 0),
                   hoursOverdue =>($f->{'hoursOverdue'} || 0),
                   loanType     =>($f->{'loanType'} || 'daily')
               },
               lost     =>{odl_id=>($f->{'lost_odlId'}||0)},
               damaged  =>{odl_id=>($f->{'damaged_odlId'}||0)}
               
           },
           userInfo=>{
               uid      =>$f->{'l_uid'},
               firstName=>$f->{'l_firstName'},
               lastName=>$f->{'l_lastName'}
           },
           itemInfo=>{
               barcode  =>$f->{'barcode'},
               itemType =>$f->{'itemType'},
               title    =>$f->{'title'},
               price    =>($f->{'price'}|| 0.0)
           }
           
       };
   }
 #open debug,">/tmp/fineInfo.js"; print debug to_json({fineList=>$fList},{pretty=>1});close debug; 
  print "Content-type: text/plain\n\n";
print   to_json({fineList=>$fList},{pretty=>1});

}

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

sub isODExempt{
    my($dbh,$uid)=@_;
    my $ex=0;
    ($ex)=$dbh->selectrow_array("select ODexempt from opl_category c inner join opl_user u on u.categorycode=c.catid where uid=?",undef,$uid);
    return $ex;
}
#------------------------------------------------------------------------------
