#!/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_getInvoice
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_setMsgValue
    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/invoice.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 $cid       = $input->{'cid'};
    #my $invoiceNo = sprintf("%08d", $cid);
    if ($cid >= 0){
        my $invoice = trans_getInvoice($dbh,$cid);
        $template->param(
            title       =>$invoice->{'title'},
            author      =>$invoice->{'author'},
            barcode     =>$invoice->{'barcode'},
            cid         =>$invoice->{'cid'},
            chargeCode  =>$invoice->{'type'},
            chargeAmnt  =>$invoice->{'chargeAmnt'},
            fee         =>$invoice->{'fee'},
            waive       =>$invoice->{'waive'},
            totalCharge =>$invoice->{'totalCharge'},
            paid        =>$invoice->{'paid'},
            balance     =>$invoice->{'balance'},
            chargeDate  =>substr($invoice->{'date'},0,10),
            dateToday   => date_text($dateToday,1),
            #invoiceNo   =>$invoiceNo
        );

       my ($userInfo, $guardian) = user_getInformationById($dbh, $invoice->{'uid'});
        $template->param(
            user_uid            => $userInfo->{'uid'},
            user_barcode        => $userInfo->{'userbarcode'},
            user_username       => $userInfo->{'username'},
            user_firstname      => $userInfo->{'firstname'},
            user_lastname       => $userInfo->{'lastname'},
            buildingcode        => $userInfo->{'buildingcode'},
            homeroom            => $userInfo->{'homeroom'},
            teacher             => $userInfo->{'teacher'},
            grade               => $userInfo->{'grade'},
            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 ($cid >= 0)
}
my $itemStatusMsgMap =loc_getMsgFile('circ/booking.msg',{});
loc_write($template,$itemStatusMsgMap);
my $paymentMsgMap =loc_getMsgFile('circ/payment.msg',{});
loc_write($template,$paymentMsgMap);

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

