Commit 599887a1 authored by Chris Nighswonger's avatar Chris Nighswonger Committed by Galen Charlton

Bugfix: [1/3] overdue_notices.pl does not process all advertised fields

Note: overdue_notices.pl really needs to be completely re-written.

The script does not process all fields advertised in tools/letter.pl
This patch adds code to process all fields advertised as well as any
from the items table.

It also adds two additional tags for use in the letter templates:

<item></item> which should enclose all fields from the biblio, biblioitems,
and items tables.

<fine></fine> which should be enclosed by the item tag and should
enclose a currency identifier per ISO 4217. If this tag is present with
a proper identifier, the fine for that item will be displayed in the
proper currency format. Note: ISO 4217 changes from time to time therefore
all currencies may not be supported. If you find one that is not
supported, please file a bug with the Locale::Currency::Format author
Tan D Nguyen <tnguyen at cpan doe org>.

An example of the implimentation of these two tags in a notice template
might be like:

The following item(s) is/are currently overdue:

<item>"<<biblio.title>>" by <<biblio.author>>, <<items.itemcallnumber>>, Barcode: <<items.barcode>> Fine: <fine>GBP</fine></item>

Which, assuming two items were overdue, would result in a notice like:

The following item(s) is/are currently overdue:

"A Short History of Western Civilization" by Harrison, John B, 909.09821 H2451, Barcode: 08030003 Fine: £3.50
"History of Western Civilization" by Hayes, Carlton Joseph Huntley, 909.09821 H3261 v.1, Barcode: 08030004 Fine: £3.50
Signed-off-by: Galen Charlton's avatarGalen Charlton <gmcharlt@gmail.com>
parent 8594f3f1
......@@ -22,12 +22,13 @@ use warnings;
use MIME::Lite;
use Mail::Sendmail;
use Encode;
use Carp;
use C4::Members;
use C4::Log;
use C4::SMS;
use C4::Debug;
use Encode;
use Carp;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
......@@ -506,7 +507,6 @@ sub parseletter {
carp "ERROR: parseletter() 1st argument 'letter' empty";
return;
}
# warn "Parseletter : ($letter, $table, $pk ...)";
my $sth = parseletter_sth($table);
unless ($sth) {
warn "parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
......@@ -520,15 +520,18 @@ sub parseletter {
my $values = $sth->fetchrow_hashref;
# and get all fields from the table
my $columns = C4::Context->dbh->prepare("SHOW COLUMNS FROM $table");
$columns->execute;
while ( ( my $field ) = $columns->fetchrow_array ) {
my $replacefield = "<<$table.$field>>";
$values->{$field} =~ s/\p{P}(?=$)//g if $values->{$field};
my $replacedby = $values->{$field} || '';
($letter->{title} ) and $letter->{title} =~ s/$replacefield/$replacedby/g;
($letter->{content}) and $letter->{content} =~ s/$replacefield/$replacedby/g;
}
return $letter;
}
=head2 EnqueueLetter
......@@ -770,7 +773,7 @@ ENDSQL
return $sth->fetchall_arrayref({});
}
sub _send_message_by_email ($) {
sub _send_message_by_email ($;$$$) {
my $message = shift or return;
my $to_address = $message->{to_address};
......
......@@ -19,6 +19,7 @@
use strict;
use warnings;
use utf8;
BEGIN {
......@@ -28,14 +29,17 @@ BEGIN {
eval { require "$FindBin::Bin/../kohalib.pl" };
}
use Getopt::Long;
use Pod::Usage;
use Text::CSV_XS;
use Locale::Currency::Format 1.28;
use Encode;
use C4::Context;
use C4::Dates qw/format_date/;
use C4::Debug;
use C4::Letters;
use Getopt::Long;
use Pod::Usage;
use Text::CSV_XS;
use C4::Overdues qw(GetFine);
=head1 NAME
......@@ -244,7 +248,7 @@ my @branchcodes; # Branch(es) passed as parameter
my $csvfilename;
my $triggered = 0;
my $listall = 0;
my $itemscontent = join( ',', qw( issuedate title barcode author ) );
my $itemscontent = join( ',', qw( issuedate title barcode author biblionumber ) );
my @myborcat;
my @myborcatout;
......@@ -436,24 +440,27 @@ END_SQL
C4::Members::DebarMember($borrowernumber);
$verbose and warn "debarring $borrowernumber $firstname $lastname\n";
}
$sth2->execute( ($listall) ? ( $borrowernumber , 1 , $MAX ) : ( $borrowernumber, $mindays, $maxdays ) );
my @params = ($listall ? ( $borrowernumber , 1 , $MAX ) : ( $borrowernumber, $mindays, $maxdays ));
$sth2->execute(@params);
my $itemcount = 0;
my $titles = "";
my @items = ();
while ( my $item_info = $sth2->fetchrow_hashref() ) {
my @item_info = map { $_ =~ /^date|date$/ ? format_date( $item_info->{$_} ) : $item_info->{$_} || '' } @item_content_fields;
$titles .= join("\t", @item_info) . "\n";
$itemcount++;
push (@items, $item_info->{'biblionumber'});
}
$sth2->finish;
$letter = parse_letter(
{ letter => $letter,
borrowernumber => $borrowernumber,
branchcode => $branchcode,
substitute => {
bib => $branch_details->{'branchname'},
'items.content' => $titles
}
{ letter => $letter,
borrowernumber => $borrowernumber,
branchcode => $branchcode,
biblionumber => \@items,
substitute => { # this appears to be a hack to overcome incomplete features in this code.
bib => $branch_details->{'branchname'}, # maybe 'bib' is a typo for 'lib<rary>'?
'items.content' => $titles
}
}
);
......@@ -576,7 +583,7 @@ substituted keys and values.
=cut
sub parse_letter {
sub parse_letter { # FIXME: this code should probably be moved to C4::Letters:parseletter
my $params = shift;
foreach my $required (qw( letter borrowernumber )) {
return unless exists $params->{$required};
......@@ -585,23 +592,42 @@ sub parse_letter {
if ( $params->{'substitute'} ) {
while ( my ( $key, $replacedby ) = each %{ $params->{'substitute'} } ) {
my $replacefield = "<<$key>>";
$params->{'letter'}->{title} =~ s/$replacefield/$replacedby/g;
$params->{'letter'}->{content} =~ s/$replacefield/$replacedby/g;
}
}
C4::Letters::parseletter( $params->{'letter'}, 'borrowers', $params->{'borrowernumber'} );
$params->{'letter'} = C4::Letters::parseletter( $params->{'letter'}, 'borrowers', $params->{'borrowernumber'} );
if ( $params->{'branchcode'} ) {
C4::Letters::parseletter( $params->{'letter'}, 'branches', $params->{'branchcode'} );
$params->{'letter'} = C4::Letters::parseletter( $params->{'letter'}, 'branches', $params->{'branchcode'} );
}
if ( $params->{'biblionumber'} ) {
C4::Letters::parseletter( $params->{'letter'}, 'biblio', $params->{'biblionumber'} );
C4::Letters::parseletter( $params->{'letter'}, 'biblioitems', $params->{'biblionumber'} );
}
my $item_format = '';
PROCESS_ITEMS:
while (scalar(@{$params->{'biblionumber'}}) > 0) {
my $item = shift @{$params->{'biblionumber'}};
my $fine = GetFine($item, $params->{'borrowernumber'});
if (!$item_format) {
$params->{'letter'}->{'content'} =~ m/(<item>.*<\/item>)/;
$item_format = $1;
}
if ($params->{'letter'}->{'content'} =~ m/<fine>(.*)<\/fine>/) { # process any fine tags...
no strict; # currency_format behaves badly if we quote the bareword for some reason...
my $formatted_fine = currency_format("$1", "$fine", FMT_SYMBOL);
use strict;
$formatted_fine = Encode::encode("utf8", $formatted_fine);
$params->{'letter'}->{'content'} =~ s/<fine>.*<\/fine>/$formatted_fine/;
}
$params->{'letter'} = C4::Letters::parseletter( $params->{'letter'}, 'biblio', $item );
$params->{'letter'} = C4::Letters::parseletter( $params->{'letter'}, 'biblioitems', $item );
$params->{'letter'} = C4::Letters::parseletter( $params->{'letter'}, 'items', $item );
$params->{'letter'}->{'content'} =~ s/(<item>.*<\/item>)/$1\n$item_format/ if scalar(@{$params->{'biblionumber'}} > 0);
}
}
$params->{'letter'}->{'content'} =~ s/<\/{0,1}?item>//g; # strip all remaining item tags...
return $params->{'letter'};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment