#!/usr/bin/perl
use strict;
use Opals::Context;
use Time::localtime;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);

use Opals::Date qw(
    date_text
);

use Opals::User qw(
    user_getInformationById

);
use Opals::Transaction qw(
    trans_getReceiptDetail
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
my $cgi    = CGI->new;
my $input  = $cgi->Vars();


my $notPrtOnload= $input->{"notPrtOnload"};
my $pref = tmpl_preference($dbh);
my $libname  =  $pref->{'libname'};
   $libname =~ s/<br>/ /g;
my $libraryType=  $pref->{'libraryType'};
my $isK_12=($libraryType eq 'k-12')?1:0;
my $isAcademy=($libraryType eq 'academy')?1:0;;
my $isPublic=($libraryType eq 'public')?1:0;;
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'billing/receipt.tmpl',
            reqPermission   => 'fine|payment',
        }
);

    my $tm = localtime;
    my $dateToday = sprintf("%04d-%02d-%02d", 
                            $tm->year+1900, 
                            ($tm->mon)+1, 
                            $tm->mday);

# See User.pm for the list of permissions
if ($permission && $permission->{'fine'}) {
    my $pid = $input->{'pid'};
    my $msgValMap ={};
    my $msgMap            =loc_getMsgFile('circ/payment.msg',$msgValMap);
    loc_write($template,$msgMap);

    if ($pid >= 0){
                        
  
       my $receipt = trans_getReceiptDetail($dbh,$pid);
       translateChageType($receipt->{'itemList'},$msgMap);
       my $idx=0;
         $template->param(
            paid           =>($receipt->{'recieved'}>0)?$receipt->{'recieved'}:0,
            refund         =>($receipt->{'withdrawn'}>0)?$receipt->{'withdrawn'}:0,
            payMethod      => $receipt->{'payMethod'},
            paidCash       =>($receipt->{'payMethod'} eq 'cash')?1:0,
            tender         => $receipt->{'tender'},
            change         => $receipt->{'changeAmnt'},
            balance        => $receipt->{'accountBalance'},
            receiptDate    => date_text($receipt->{'ondate'},1),
            dateToday      => date_text($dateToday,1),
            itemList       => $receipt->{'itemList'},
            notPrtOnload   =>$notPrtOnload
        );
 

        my ($userInfo, $guardian) = user_getInformationById($dbh, $receipt->{'uid'});
        $template->param(
            user_uid            => $userInfo->{'uid'},
            user_barcode        => $userInfo->{'userbarcode'},
            user_username       => $userInfo->{'username'},
            user_firstname      => $userInfo->{'firstname'},
            user_lastname       => $userInfo->{'lastname'},
            homeroom            => $userInfo->{'homeroom'},
            teacher             => $userInfo->{'teacher'},
            notes               => $userInfo->{'notes'},
            addrLine1           => $userInfo->{'addrLine1'},
            state               => $userInfo->{'state'},
            city                => $userInfo->{'city'},
            zip                 => $userInfo->{'zip'},
            country             => $userInfo->{'country'},
            studies             => $userInfo->{'studies'},
            program             => $userInfo->{'program'},
            libname             => $libname,

        );
        $template->param( isK_12    =>$isK_12);
        $template->param( isAcademy =>$isAcademy);
        $template->param( isPublic  =>$isPublic);

     }#//if ($pid >= 0)
}


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

#==========================================================
sub translateChageType{
    my($transList,$msgMap)=@_;
    my $tMap={
                OVERDUE_CHARGED=>'overdueTxt' ,
                LOST_CHARGED=>'lostTxt'    ,
                DAMAGE_CHARGED=>'damageTxt' , 
                PAYMENT=>'paymentTxt',         
                FORGIVE=>'waiveTxt',         
                CREDIT_REFUND=>'creditRefundTxt',   
                RATE_ADJUSTMENT=>'rateAdjTxt', 
                REFUND=>'refundTxt',          
                SERVICE_FEE=>'feeTxt'  
             };
    foreach my $t(@$transList){
        my $msgKey=$tMap->{$t->{'chargeCode'}};
        $t->{'chargeName'}=(defined$msgMap->{$msgKey})?$msgMap->{$msgKey}:$t->{'chargeCode'};
    }
}

