package	Opals::Fines;


use	Exporter;
@ISA       = qw(Exporter);
@EXPORT_OK = qw(
    fine_getFineList
    fine_getFineRate
    fine_getUserByOdl_id
);


# Version number
$VERSION   = 0.01;


#use utf8;
use strict;

use Opals::Context;
use Opals::Transactions qw(
    trans_getTaxTable
);
use Opals::Circulation qw(
    circ_getItemInfo
);
#######################################
sub fine_getFineList{
    my ($dbh,$lidList)=@_;
    my @idArr = split /,/,$lidList;
    my @fineList;
    sort {$a <=> $b} (@idArr);
    my $curLid="";
    my $indexBase=0;
    my $p;
    foreach my $id(@idArr){
        if($curLid ne $id){
            $curLid=$id;
           my @fineInfo   = fineInfo($dbh,$id,$indexBase);
           if(scalar(@fineInfo)>0){
              my $loanItemInfo = getLoanItemInfo($dbh,$id);
              $p = (!$loanItemInfo->{'price'}) ? '0.00':  $loanItemInfo->{'price'};
              $p =~ s/^\D+|$\D+//gi;
              push @fineList, {idloan       => $id,
                               l_uid        => $loanItemInfo->{'uid'},
                               l_firstName  => $loanItemInfo->{'firstname'},
                               l_lastName   => $loanItemInfo->{'lastname'},
                               barcode      => $loanItemInfo->{'barcode'},
                               itemType     => $loanItemInfo->{'itemType'},
                               title        => $loanItemInfo->{'title'},
                               dateLoan     => $loanItemInfo->{'dateLoan'},
                               dateDue      => $loanItemInfo->{'dateDue'},
                               dateReturn   => $loanItemInfo->{'dateReturn'},
                               loanType     => $loanItemInfo->{'loanType'},  
                               price        => $p,
                               numOfFines   => scalar(@fineInfo),
                               fineInfo     => \@fineInfo};
              $indexBase += scalar(@fineInfo);
           }
        }
   } 
   return  @fineList;      
}
#######################################
sub fine_getFineRate{
    my ($dbh)=@_;
    my $sth = $dbh->prepare(<<_STH_);
    select      i.id as itemType ,i.description, 
                if(r.overdue,r.overdue,0) as overdue,
                if(r.overduePerHour,r.overduePerHour,0) as overduePerHour,
                if(r.maxOverdue,r.maxOverdue,0) as maxOverdue,
                if(r.damage,r.damage,0) as damage
    from        opl_itemType i left outer join opl_fineRate r on r.itemType=i.id
    where       i.id != ''    
    order by    i.defaultType desc,i.id 
_STH_
    $sth->execute();
 
        my @finerateTbl;
        while (my $rec = $sth->fetchrow_hashref) {
            if($rec->{'overdue'} ne '' || $rec->{'maxOverdue'} ne ''  || $rec->{'damage'} ne'' ){
                $rec->{'settled'}=1;
            }
             push @finerateTbl, $rec;
        }
        $sth->finish;
        return \@finerateTbl;

}
#######################################
sub fineInfo{
    my ($dbh,$idloan,$indexBase) = @_;
    my $sth = $dbh->prepare(<<_STH_);
    select * from  opl_odl where idloan =?
_STH_
    $sth->execute($idloan);

    
    my @fineInfo; 
    my $fine;
    my $i=$indexBase;
    while (my $rec = $sth->fetchrow_hashref) {
        $fine={};
        if($rec->{'type'} eq 'overdue'){
           $fine->{'daysOverdue'}=$rec->{'days_overdue'};
           $fine->{'hoursOverdue'}=$rec->{'hours_overdue'};
        }
        $fine->{'type'}=$rec->{'type'};
        $fine->{$rec->{'type'}} =1;
        $fine->{'idloan'}=$rec->{'idloan'};
        $fine->{'odl_id'} =$rec->{'odl_id'};
        $fine->{'ondate'}=substr $rec->{'ondate'},0,10;
        $fine->{'idx'} =$i;
        $i++;
        push @fineInfo,$fine;
    }
    $sth->finish;
    return @fineInfo;
}
#######################################

sub getLoanItemInfo{
    my ($dbh,$idloan) = @_;
    my $sth = $dbh->prepare(<<_STH_);
    select i.price,i.typeId as itemType, m.title, l.*, u.firstname,u.lastname 
    from  opl_loan l inner join opl_user u on u.uid=l.uid inner join opl_item i on i.barcode=l.barcode
    inner join opl_marcRecord m on i.rid=m.rid
    where l.idloan=? 
_STH_
    $sth->execute($idloan);
    
    my $rec = $sth->fetchrow_hashref;
    if($rec){
        my $itemInfo = circ_getItemInfo($dbh,$rec->{'barcode'},$rec->{'uid'});
        my $loanType =$itemInfo->{'loanUnit'} eq 'hour'?'hourly':'daily'; 
        $rec->{'loanType'}  = $loanType;
        $rec->{'dateLoan'}  = substr $rec->{'dateLoan'},0,10;
        $rec->{'dateDue'}   = substr $rec->{'dateDue'},0,10;
        $rec->{'dateReturn'}= substr $rec->{'dateReturn'},0,10;
        $rec->{'price'}     =~ s/^[^0-9]+|[^0-9]+$//g;
        if (!$rec->{'price'}){  $rec->{'price'}= '0.00'};
     }   
    $sth->finish;
    return $rec;
     
}
#######################################

sub fine_getUserByOdl_id{
    my($dbh,$odl_id)=@_;
    my $sth = $dbh->prepare(<<_STH_);
    select u.* 
    from opl_user u inner join opl_loan l on l.uid=u.uid 
    inner join opl_odl o on o.idloan=l.idloan
    where o.odl_id=?
_STH_

    $sth->execute($odl_id);
    my $user = $sth->fetchrow_hashref;
    $sth->finish;

    return $user;
}
#######################################
sub fine_updateFineRate{
}
#######################################

sub fine_addNewFineRate{
}
