#!/usr/bin/perl
# Copyright 2001-2017 Leslie Richardson
# This file is part of Open Admin for Schools.
# Open Admin for Schools is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# Generate Receipts for Payments.
# Note: The payment id (id of record with a trans_type = 'pay'), will
# find all of the payments on existing records,
my $defaultrecords = 10;
my $receiptsperpage = 2;
# Logo Setup
my $f_ShowLogo = 0;
my $f_LogoWidth = '25mm';
my %lex = ('Fees' => 'Fees',
'Main' => 'Main',
'Total' => 'Total',
'Payment' => 'Payment',
'Date' => 'Date',
'Description' => 'Description',
'Receipt Number' => 'Receipt Number',
'Paid' => 'Paid',
'View Log File' => 'View Log File',
'Student' => 'Student',
'Action' => 'Action',
'Search' => 'Search',
'No Student(s) Found' => 'No Student(s) Found',
'Last,First/Last/Initials/Studnum' => 'Last,First/Last/Initials/Studnum',
'Print Receipt' => 'Print Receipt',
'Continue' => 'Continue',
'Amount' => 'Amount',
'Description' => 'Description',
'View/Download' => 'View/Download',
'Grade' => 'Grade',
'Receipt' => 'Receipt',
'Phone' => 'Phone',
'Fax' => 'Fax',
'Payment' => 'Payment',
'Signature' => 'Signature',
'Payee' => 'Payee',
'Name' => 'Name',
'Error' => 'Error',
'Receipts' => 'Receipts',
'View/Print' => 'View/Print',
'Start Date' => 'Start Date',
'End Date' => 'End Date',
'Start' => 'Start',
'End' => 'End',
'Deposit Report' => 'Deposit Report',
'Last' => 'Last',
'Not Found' => 'Not Found',
'Type' => 'Type',
'Paper Size' => 'Paper Size',
'Font Size' => 'Font Size',
'Letter' => 'Letter',
'Legal' => 'Legal',
'A4' => 'A4',
'A5' => 'A5',
'Homeroom' => 'Homeroom',
'Download CSV File' => 'Download CSV File',
'Outstanding Balance' => 'Outstanding Balance',
);
my $self = 'receipt.pl';
use CGI;
use DBI;
use Cwd;
use Number::Format qw(:all);
eval require "../../etc/admin.conf";
if ( $@ ) {
print qq{$lex{Error} $@
\n};
die qq{$lex{Error} $@\n};
}
eval require "../../etc/fees.conf";
if ( $@ ) {
print qq{$lex{Error} $@
\n};
die qq{$lex{Error} $@\n};
}
my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;
my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;
# Get values for supression from configuration system
my @fieldnames = qw( r_LogoFile r_SchoolAddressWidth r_ShowAdmins r_AddressSpacer );
my $sth = $dbh->prepare("select datavalue from conf_system where dataname = ?");
foreach my $var ( @fieldnames ) {
$sth->execute( $var );
my $datavalue = $sth->fetchrow;
eval $datavalue;
if ( $@ ) {
print qq{$lex{Error}: $@
\n};
die qq{$lex{Error}: $@\n};
}
}
# Show page Header
my $title = "$lex{'View/Print'} $lex{Receipts}";
print qq{$doctype\n
$title\n};
print qq{\n};
if ( not $arr{page} ) {
$focus = 1;
print qq{\n};
print qq{\n};
print qq{\n};
print qq{\n};
} else {
$focus = 0;
}
print qq{$chartype\n\n};
print qq{[ $lex{Main} |\n};
print qq{$lex{Fees} ]\n};
print qq{$title
\n};
if ( not $arr{page} ) {
showStartPage();
} elsif ( $arr{page} == 1 ) {
delete $arr{page};
showDateReceipt( $arr{sdate}, $arr{edate} ); # direct to printReceipts
} elsif ( $arr{page} == 2 ) {
delete $arr{page};
showLastReceipt( $arr{records} );
} elsif ( $arr{page} == 3 ) { # called by search form; find students.
delete $arr{page};
showStudentReceipt( $arr{student} );
} elsif ( $arr{page} == 5 ) { # Receipt Printing (from select receipts)
delete $arr{page};
if ( $arr{type} eq 'PDF' ) {
delete $arr{type};
printNewReceipt();
} elsif ( $arr{type} eq 'CSV' ) {
delete $arr{type};
exportCSV();
} else {
delete $arr{type};
printHtmlReceipt();
}
} elsif ( $arr{page} == 6 ) { # Deposit Report (Bank Reconciliation)
printDepositReport();
}
print qq{\n};
#------------------
sub printNewReceipt { # pdf version using LaTeX
#------===---------
my $fontsize = $arr{fontsize};
my $papersize = $arr{papersize};
delete $arr{fontsize};
delete $arr{papersize};
# foreach my $key ( sort keys %arr ) { print qq{K:$key V:$arr{$key}
\n}; }
# Load Latex filtering script;
eval require "../../lib/liblatex.pl";
if ( $@ ) {
print $lex{Error}. " $@
\n";
die $lex{Error}. " $@\n";
}
my $shortname = "pdfreceipt$$";
my $filename = "$shortname.tex";
# Set Paper Size, text width and height
my ( $textwidth, $textheight, $hoffset, $voffset );
my @transColWidth = qw(25mm 110mm 25mm); # transaction table column widths.
$hoffset='-12mm';
$voffset = '-30mm';
if ( $papersize eq $lex{Letter} ) {
$papersize = 'letterpaper';
$textwidth = $g_letterpaper_textwidth;
$textheight = $g_letterpaper_textheight;
} elsif ( $papersize eq $lex{Legal} ) {
$papersize = 'legalpaper';
$textwidth = $g_legalpaper_textwidth;
$textheight = $g_legalpaper_textheight;
} elsif ( $papersize eq $lex{A4} ) {
$papersize = 'a4paper';
$textwidth = $g_a4paper_textwidth;
$textheight = $g_a4paper_textheight;
} elsif ( $papersize eq $lex{A5} ) {
$papersize = 'a5paper';
$hoffset = '-20mm';
$voffset = '-30mm';
@transColWidth = qw(25mm 73mm 25mm);
# ~~ Remove these conditionals once we have a5 values stored for a while in distro
if ( not $g_a5paper_textwidth ) {
$textwidth = $g_a5paper_textwidth = '148mm';
} else { $textwidth = '148mm'; }
if ( not $g_a5paper_textheight ) {
$textheight = $g_a5paper_textheight = '210mm';
} else {
$textheight = '210mm';
}
}
# Start the TeX file...
open(TEX,">$filename") || die "Can't open tex file";
print TEX "\\documentclass[$fontsize, $papersize, landscape]{article}\n";
print TEX "\\usepackage{array,newcent,rotating,colortbl, inputenc}\n";
print TEX "$a_latex_header\n\\renewcommand{\\familydefault}{\\sfdefault}\n";
print TEX "\\pagestyle{empty}";
print TEX "\\setlength{\\textwidth}{$textwidth}\n";
print TEX "\\setlength{\\textheight}{$textheight}\n";
print TEX "\\setlength{\\hoffset}{$hoffset}\n";
print TEX "\\setlength{\\voffset}{$voffset}\n";
print TEX "\\setlength{\\evensidemargin}{0mm}\n";
print TEX "\\setlength{\\oddsidemargin}{0mm}\n";
print TEX "\\setlength{\\tabcolsep}{1mm}\n";
print TEX "\\begin{document}\n";
my $receiptcount;
# Loop through all records
foreach my $paymentid ( sort keys %arr ) {
# Get payment transaction from paymentid.
my $sth = $dbh->prepare("select studnum, trans_date, name, description, total,
receipt, trans_type from fees_jrl where id = ?");
$sth->execute( $paymentid );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
my ( $studnum, $trans_date, $name, $description, $total, $receipt, $trans_type ) =
$sth->fetchrow;
($description) = latex_filter( ($description) );
# Extract Payee information from the description.
my ($desc, $name) = split(/\(/, $description);
chop $name; # remove trailing bracket;
my ($dud, $payee) = split(/:/, $name );
# Get Records paid by this payment, if any.
@transactions = ();
$sth = $dbh->prepare("select id from fees_jrl where paid_id = ?
order by trans_date, id");
$sth->execute( $paymentid );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
while ( my $newid = $sth->fetchrow ) {
# if ( $newid == $paymentid ) { next; } # skip the payment itself.
push @transactions, $newid;
}
if ( $f_ShowLogo ) {
# if ( not $r_LogoFile and $r_logoFile ) { $r_LogoFile = $r_logoFile; }
print TEX "\\includegraphics[width=$f_LogoWidth]{";
print TEX "../../etc/$r_LogoFile}\\quad \n";
print TEX "\\parbox[b]{$r_SchoolAddressWidth}{\\begin{center}{";
print TEX "\\Large\\bf $schoolname}\\\\";
if ( $schooladdr1 ) {
print TEX "$schooladdr1\\\\";
}
print TEX "$schoolcity, $schoolprov $schoolpcode\\\\";
# Phones Lines
if ( $schoolphone ) {
print TEX $lex{Phone}. " $schoolphone ";
if ( $r_ShowAdmins ) { print TEX "\\hfill ";}
else { print TEX "\\qquad "; }
}
if ( $schoolfax ) {
print TEX $lex{Fax}. " $schoolfax";
}
print TEX "\\\\\n\n";
print TEX "\\vspace{$r_AddressSpacer}\n\n";
print TEX " \\end{center}}\\qquad\n";
}
print TEX "\\bigskip\n\n";
print TEX "\\begin{tabular}{p{75mm}|p{75mm}|p{75mm}|}\n";
print TEX "\\hfil Bank Al-Habib Limited \\hfil & \\hfil Bank Al-Habib Limited \\hfil & ";
print TEX "\\hfil Bank Al-Habib Limited \\hfil \\\\ \n";
print TEX "\\hfil A/C No 123456 \\hfil & \\hfil A/C No 123456 \\hfil & \\hfil ";
print TEX "A/C No 123456 \\hfil \\\\ \n";
print TEX "\\bigskip & \\bigskip & \\bigskip \\\\ \n";
# get Student Info
#$sth = $dbh->prepare("select lastname, firstname, grade, address1, city1, pcode1
$sth = $dbh->prepare("select * from studentall where studnum = ?");
$sth->execute( $studnum );
if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; }
my $stud_ref = $sth->fetchrow_hashref;
my %stud = %$stud_ref;
#($lastname, $firstname, $grade, $address1, $city1, $pcode1 )
foreach my $key ( keys %stud ) {
( $stud{$key} ) = latex_filter( $stud{$key} );
}
# print Student Info
# print TEX "\\begin{tabular}{ll}\n";
print TEX "{\\bf $lex{Name}}: $stud{firstname} $stud{lastname}\\hfill C-ID:$studnum & ";
print TEX "{\\bf $lex{Name}}: $stud{firstname} $stud{lastname}\\hfill C-ID:$studnum & ";
print TEX "{\\bf $lex{Name}}: $stud{firstname} $stud{lastname}\\hfill C-ID:$studnum \\\\ \n";
print TEX "\\hrule & \\hrule & \\hrule \\\\ \n";
print TEX "\\end{tabular}\n";
} # payment id loop
print TEX "\n\\end{document}";
close TEX;
system("$pdflatex $filename >pdflog$$.txt");
system("mv $shortname.pdf $downloaddir");
system("mv pdflog$$.txt $downloaddir");
system("rm -f $shortname.*");
print qq{\n};
print qq{[ $lex{Main} |\n};
print qq{};
print qq{$lex{'View Log File'}\n ]