Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 328 lines (259 sloc) 10.617 kb
#!/usr/bin/env perl
# Copyright 2011 Kyle M Hall <kyle@kylehall.info>
#
# This file is part of Koha.
#
# Koha 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.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
use warnings;
use CGI;
use C4::Auth;
use Koha;
use C4::Context;
use C4::Output;
use C4::Dates;
use C4::ReceiptTemplates;
my $cgi = new CGI;
my ( $template, $loggedin_borrowernumber, $cookie ) = get_template_and_user(
{
template_name => 'tools/receipt_generation.tmpl',
query => $cgi,
type => 'intranet',
authnotrequired => 0,
}
);
$template->param( delay_load => 0 );
my $action = $cgi->param('action');
my $borrowernumber = $cgi->param('borrowernumber');
my $itemnumber = $cgi->param('itemnumber');
my $branchcode = C4::Context->userenv->{'branch'};
my $receipt_template =
GetReceiptTemplate( { action => $action, branchcode => $branchcode } );
my $content = $receipt_template->{'content'};
## Process Check Out Reciepts
if ( $action eq 'check_out' || $action eq 'check_out_quick' ) {
my $today_issues_data = _get_issues_data( $borrowernumber, 'today' );
$content =
_replace_loop( $content, $today_issues_data, 'TodaysIssuesList' );
my $previous_issues_data = _get_issues_data( $borrowernumber, 'previous' );
$content =
_replace_loop( $content, $previous_issues_data, 'PreviousIssuesList' );
}
## Process Check In Reciepts
elsif ( $action eq 'check_in' ) {
my @barcodes = $cgi->param('barcode');
my $returns_data = _get_returns_data(@barcodes);
$content = _replace_loop( $content, $returns_data, 'PreviousIssuesList' );
}
## Process Check In - Barcode Not Found
elsif ( $action eq 'not_found' ) {
my $barcode = $cgi->param('barcode');
my $data;
$data->{'items.barcode'} = $barcode;
$content = _replace( $content, $data );
}
## Process Check In - Lost Item Found
elsif ( $action eq 'claims_returned_found' ) {
my $barcode = $cgi->param('barcode');
my $data = _get_returns_data($barcode);
$data = $data->[0];
$content = _replace( $content, $data );
}
## Process Fine Payments
elsif ( $action eq 'payment_received' ) {
my $borrowernumber = $cgi->param('borrowernumber');
my $data;
$data = _get_fines_data( $borrowernumber, 'today' );
$content = _replace_loop( $content, $data, 'FinesPaidTodayList' );
$content =
_replace( $content, $data->[0] )
; ## In case borrower data is used outside of a loop.
$data = _get_fines_data( $borrowernumber, 'previous' );
$content = _replace_loop( $content, $data, 'FinesPaidPreviousList' );
$content =
_replace( $content, $data->[0] )
; ## In case borrower data is used outside of a loop.
}
## Process Hold Found Reciepts
elsif ( $action eq 'hold_found' ) {
my $borrowernumber = $cgi->param('borrowernumber');
my $biblionumber = $cgi->param('biblionumber');
my $reservenumber = $cgi->param('reservenumber');
$template->param( delay_load => 1 );
my $data = _get_hold_data($reservenumber);
$content = _replace( $content, $data );
}
## Process Transit Hold Found Reciepts
elsif ( $action eq 'transit_hold' ) {
my $borrowernumber = $cgi->param('borrowernumber');
my $biblionumber = $cgi->param('biblionumber');
my $reservenumber = $cgi->param('reservenumber');
$template->param( delay_load => 1 );
my $data = _get_hold_data($reservenumber);
$content = _replace( $content, $data );
}
## Fill in branch data
my $branch_data = _get_branch_data($branchcode);
$content = _replace( $content, $branch_data );
## Fill in non-looped data about borrower
if ($borrowernumber) {
my $borrower_data = _get_borrower_data($borrowernumber);
$content = _replace( $content, $borrower_data );
}
$template->param( output => $content );
output_html_with_http_headers $cgi, $cookie, $template->output;
sub _replace {
my ( $content, $data ) = @_;
my $date = C4::Dates->new();
while ( my ( $key, $value ) = each %$data ) {
if ( _is_date($value) )
{ ## If the key looks like a date ( starts with YYY-MM-DD ), convert it to MM/DD/YYYY
$value = C4::Dates::format_date($value);
}
$content =~ s/<<$key>>/$value/g;
}
return $content;
}
sub _replace_loop {
my ( $content, $data, $tag ) = @_;
my ( $content_head, $content_loop, $content_foot ) =
_split_out_loop( $content, $tag );
my $new_content_loop;
foreach my $d (@$data) {
my $c = $content_loop // '';
while ( my ( $key, $value ) = each %$d ) {
$value //= '';
if ( _is_date($value) )
{ ## If the key looks like a date ( starts with YYY-MM-DD ), convert it to MM/DD/YYYY
$value = C4::Dates::format_date($value);
}
$c =~ s/<<$key>>/$value/g;
}
$new_content_loop .= $c;
}
return $content_head . $new_content_loop . $content_foot;
}
sub _split_out_loop {
my ( $content, $tag ) = @_;
my ( $content_head, $tmp ) = split( /<<Begin$tag>>/, $content );
my ( $content_loop, $content_foot ) = split( /<<End$tag>>/, $tmp );
return ( $content_head, $content_loop, $content_foot );
}
sub _get_branch_data {
my ($branchcode) = @_;
my $columns = MuxColumnsForSQL( GetTableColumnsFor('branches') );
my $dbh = C4::Context->dbh;
my $branch = $dbh->selectrow_hashref(
"SELECT $columns FROM branches WHERE branchcode = ?",
undef, $branchcode );
return $branch;
}
sub _get_borrower_data {
my ($borrowernumber) = @_;
my $columns = MuxColumnsForSQL( GetTableColumnsFor('borrowers') );
my $dbh = C4::Context->dbh;
my $borrower = $dbh->selectrow_hashref(
"SELECT $columns FROM borrowers WHERE borrowernumber = ?",
undef, $borrowernumber );
return $borrower;
}
sub _get_issues_data {
my ( $borrowernumber, $when ) = @_;
my @tables = ( 'biblio', 'biblioitems', 'items', 'borrowers', 'issues' );
my $columns = MuxColumnsForSQL( GetTableColumnsFor(@tables) );
my $sql = "
SELECT $columns FROM borrowers
LEFT JOIN issues ON issues.borrowernumber = borrowers.borrowernumber
LEFT JOIN items ON issues.itemnumber = items.itemnumber
LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
LEFT JOIN biblio on items.biblionumber = biblio.biblionumber
WHERE borrowers.borrowernumber = ?
";
$sql .= " AND issuedate = CURDATE()" if ( $when eq 'today' );
$sql .= " AND issuedate < CURDATE()" if ( $when eq 'previous' );
return C4::Context->dbh->selectall_arrayref( $sql, { Slice => {} },
$borrowernumber );
}
sub _get_returns_data {
my @barcodes = @_;
my @tables = ( 'biblio', 'biblioitems', 'items', 'borrowers', 'issues' );
my $columns = MuxColumnsForSQL( GetTableColumnsFor(@tables) );
my $barcodes = join( ',', @barcodes );
my $sql = "
SELECT $columns FROM items
LEFT JOIN old_issues AS issues ON issues.itemnumber = items.itemnumber
LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
LEFT JOIN biblio on biblio.biblionumber = items.biblionumber
WHERE items.barcode IN ( $barcodes )
GROUP BY issues.itemnumber
ORDER BY issuedate DESC;
";
return C4::Context->dbh->selectall_arrayref( $sql, { Slice => {} }, );
}
sub _get_hold_data {
my ($reservenumber) = @_;
my @tables = (
'biblio', 'biblioitems', 'items', 'borrowers', 'reserves',
'branchtransfers', { table => 'branches', name => 'recieving_branch' }
);
my $columns = MuxColumnsForSQL( GetTableColumnsFor(@tables) );
my $sql = "
SELECT $columns FROM reserves
LEFT JOIN borrowers ON borrowers.borrowernumber = reserves.borrowernumber
LEFT JOIN items ON items.itemnumber = reserves.itemnumber
LEFT JOIN biblio on biblio.biblionumber = reserves.biblionumber
LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
LEFT JOIN branchtransfers ON branchtransfers.itemnumber = items.itemnumber
LEFT JOIN branches AS recieving_branch ON recieving_branch.branchcode = branchtransfers.tobranch
WHERE
reserves.reservenumber = ?
";
return C4::Context->dbh->selectrow_hashref( $sql, undef, $reservenumber );
}
sub _get_fines_data {
my ( $borrowernumber, $when ) = @_;
my @tables =
( 'biblio', 'biblioitems', 'items', 'borrowers', 'accountlines' );
my $columns = MuxColumnsForSQL( GetTableColumnsFor(@tables) );
my $sql = "
SELECT $columns FROM accountlines
LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
LEFT JOIN items ON items.itemnumber = accountlines.itemnumber
LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
LEFT JOIN biblio on items.biblionumber = biblio.biblionumber
WHERE accountlines.borrowernumber = ?
";
$sql .= " AND DATE(accountlines.timestamp) = CURDATE()"
if ( $when eq 'today' );
$sql .= " AND DATE(accountlines.timestamp) < CURDATE()"
if ( $when eq 'previous' );
$sql .= "ORDER BY accountlines.timestamp DESC";
my $data = C4::Context->dbh->selectall_arrayref( $sql, { Slice => {} },
$borrowernumber );
foreach my $d (@$data) {
## Format Currency Fields
$d->{'accountlines.amount'} =
sprintf( "%.2f", $d->{'accountlines.amount'} );
$d->{'accountlines.amountoutstanding'} =
sprintf( "%.2f", $d->{'accountlines.amountoutstanding'} );
}
return $data;
}
sub _is_date {
## Returns true if $value begins with YYYY-MM-DD
my ($value) = @_;
return 0 unless $value;
return ( $value =~ /^(\d{4})(\-)(\d{1,2})\2(\d{1,2})/ );
}
Jump to Line
Something went wrong with that request. Please try again.