#!/usr/bin/perl
use strict;
use Opals::Context;

use JSON;
use Opals::Template qw(
    tmpl_preference
);
use Opals::User qw(
    user_getInformationById
);
use Opals::Tb_Transactions qw(
    trans_getDetailBalance
    trans_getReceiptDetail
    trans_getUnpaidFineList
    trans_getTaxTable
    trans_getPreviousBalance
    trans_getBalance
);
use Opals::Date qw(
    date_now
     date_text
);
use Opals::Locale qw(
    loc_getMsgFile
);

use Time::localtime;
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
my $cgi    = CGI->new;
my $input = $cgi->Vars();
my $todayStr   = date_now();
my $syspref    = tmpl_preference($dbh);
my $lang = $syspref->{'lang'} || 'en';
$todayStr =   date_text($todayStr,1,$lang),
my $msgMap            =loc_getMsgFile('circ/payment.msg',{});

my $pid = $input->{'pid'};
my $emptyLine = "<tr><td></td><td colspan=4></td></tr>";
my $hrLine = "<tr><td></td><td colspan=4><hr></td></tr>";
my $receipt = _trans_getReceiptDetailById($dbh,$pid);
my $uInfo = "<tr><th>$msgMap->{'nameTxt'}</th><td>$receipt->{'userInfo'}->{'firstname'} $receipt->{'userInfo'}->{'lastname'}</td></tr>";
$uInfo .= "<tr><th>$msgMap->{'userIDBcTxt'}</th><td>$receipt->{'userInfo'}->{'userbarcode'}</td></tr><tr>";
$uInfo .= "<th>$msgMap->{'homeroomTxt'}</th><td>$receipt->{'userInfo'}->{'homeroom'}</td></tr>";
$uInfo .= "<tr><th>$msgMap->{'teacherTxt'}</th><td>$receipt->{'userInfo'}->{'teacher'}</td></tr>";
my $transHead = "<tr><th></th><th>$msgMap->{'barcodeTxt'}</th><th>$msgMap->{'titleTxt'}</th><th>$msgMap->{'typeTxt'}</th><th>$msgMap->{'amountTxt'}</th></tr>";
my $transList;
foreach my $t(@{$receipt->{'transList'}}){
    $transList .= "<tr>";
    $transList .= "<td></td><td>".$t->{'barcode'}."</td><td>".$t->{'title'}."</td><td>". uc($t->{'type'}) ."</td><td>" .$t->{'fineAmount'}."</td>"  ;
    $transList .= "</tr>";
}
my $transDate;
$transDate = "<tr><td></td>.<td colspan=3>$msgMap->{'trancDateTxt'} : ". date_text($receipt->{'ondate'},1,$lang) ."</td><td></td></tr>";
my $summary;
$summary  = "<tr><td colspan=3></td><td>$msgMap->{'paymentTxt'}</td><td>\$".$receipt->{'received'}."</td></tr>";
$summary  .= "<tr><td colspan=3></td><td>$msgMap->{'paidTypeTxt'}</td><td>".$receipt->{'payMethod'}."</td></tr>";
$summary  .= "<tr><td colspan=3></td><td>$msgMap->{'amountPaidTxt'}</td><td>\$".$receipt->{'tender'}."</td></tr>";
$summary  .= "<tr><td colspan=3></td><td>$msgMap->{'changeTxt'}</td><td>\$".$receipt->{'changeAmnt'}."</td></tr>";

my $balance;
$balance =  "<tr><td colspan=3></td><td>$msgMap->{'remainingBalanceTxt'}</td><td>\$".$receipt->{'balance'}."</td></tr>";

my $strHTML = <<_HTML_; 
<!DOCTYPE html>
<html>  
    <header> <p style="text-align:center">$msgMap->{'paymentReceiptTxt'}</p></header>
    <body >
        <div style="float:right"> $todayStr </div>
        <div style="float:left"> $syspref->{'libname'} </div>
        <br><br>
        <div style="float:clear;display:inline-block;text-align:left;margin:20px;">
            <table style="width:800px;">
                $uInfo
                $hrLine
                $transHead
                $emptyLine
                $transList
                $hrLine
                $emptyLine
                $transDate
                $summary
                $emptyLine
                $balance
            </table>
        </div>
    </body>
</html>

_HTML_

print "Content-type: text/html\n\n";
print   $strHTML;

sub _trans_getReceiptDetailById {
    my ($dbh,$pid) = @_;
    my $receipt;
    my $sth_receipt = $dbh->prepare(<<_STH_);
select * from tb_paymentReceipt where pid=?
_STH_
    my $sth_trans=$dbh->prepare(<<_STH_);
select  t.tid,t.ptid,t.code,t.amount,t.balance,d.odl_id,d.rate,d.fineAmount,o.type 
    from  tb_transactions t  
        inner join tb_transactiondetail d on d.tid= t.ptid 
        inner join tb_odl o on o.odl_id=d.odl_id 
    where t.pid=? && t.uid=?
_STH_
    $sth_receipt->execute($pid);
    if($receipt=$sth_receipt->fetchrow_hashref){
        $sth_trans->execute($pid,$receipt->{'uid'});
        while (my $t=$sth_trans->fetchrow_hashref){
            my $itemInfo = getItemInfo($dbh,$t->{'odl_id'});
            $t->{'title'} = $itemInfo->{'title'};
            $t->{'barcode'} = $itemInfo->{'barcode'};
            $t->{'type'} = uc($t->{'type'});
            push @{$receipt->{'transList'}},$t;
        }
        my $balance =trans_getBalance($dbh,$receipt->{'uid'});
        $receipt->{'balance'} =sprintf("%.2f",$balance);
        my ($userInfo, $guardian) = user_getInformationById($dbh, $receipt->{'uid'});
        $receipt->{'userInfo'} = {
            userbarcode => $userInfo->{'userbarcode'},
            firstname   => $userInfo->{'firstname'},
            lastname    => $userInfo->{'lastname'},
            homerome    => $userInfo->{'homename'},
            teacher     => $userInfo->{'teacher'}
        }
    }
    return ($receipt);
}

sub getItemInfo(){
    my ($dbh,$odl_id)=@_;
    my $query="select l.barcode,r.fVal as title from tb_loan l inner join tb_odl o on o.idloan=l.id 
inner join tb_items i on l.barcode=i.barcode inner join tb_records r on i.rid=r.rid && r.fId = '245_a'
where o.odl_id=?";
    my $sth = $dbh->prepare($query); 
    $sth->execute($odl_id)  ;
    my $rec=$sth->fetchrow_hashref;    
    $sth->finish;
    return $rec;
}

