Bug 3883: Fixes blank line in routing list.
[koha.git] / C4 / Print.pm
1 package C4::Print;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Context;
23 use C4::Circulation;
24 use C4::Members;
25 use C4::Dates qw(format_date);
26
27 use vars qw($VERSION @ISA @EXPORT);
28
29 BEGIN {
30         # set the version for version checking
31         $VERSION = 3.01;
32         require Exporter;
33         @ISA    = qw(Exporter);
34         @EXPORT = qw(&remoteprint &printreserve &printslip);
35 }
36
37 =head1 NAME
38
39 C4::Print - Koha module dealing with printing
40
41 =head1 SYNOPSIS
42
43   use C4::Print;
44
45 =head1 DESCRIPTION
46
47 The functions in this module handle sending text to a printer.
48
49 =head1 FUNCTIONS
50
51 =over 2
52
53 =item remoteprint
54
55   &remoteprint($items, $borrower);
56
57 Prints the list of items in C<$items> to a printer.
58
59 C<$borrower> is a reference-to-hash giving information about a patron.
60 This may be gotten from C<&GetMemberDetails>. The patron's name
61 will be printed in the output.
62
63 C<$items> is a reference-to-list, where each element is a
64 reference-to-hash describing a borrowed item. C<$items> may be gotten
65 from C<&GetBorrowerIssues>.
66
67 =cut
68
69 # FIXME - It'd be nifty if this could generate pretty PostScript.
70 sub remoteprint ($$) {
71     my ($items, $borrower) = @_;
72
73     (return)
74       unless ( C4::Context->boolean_preference('printcirculationslips') );
75     my $queue = '';
76
77     # FIXME - If 'queue' is undefined or empty, then presumably it should
78     # mean "use the default queue", whatever the default is. Presumably
79     # the default depends on the physical location of the machine.
80     # FIXME - Perhaps "print to file" should be a supported option. Just
81     # set the queue to "file" (or " file", if real queues aren't allowed
82     # to have spaces in them). Or perhaps if $queue eq "" and
83     # $env->{file} ne "", then that should mean "print to $env->{file}".
84     if ( $queue eq "" || $queue eq 'nulllp' ) {
85         open( PRINTER, ">/tmp/kohaiss" );
86     }
87     else {
88
89         # FIXME - This assumes that 'lpr' exists, and works as expected.
90         # This is a reasonable assumption, but only because every other
91         # printing package has a wrapper script called 'lpr'. It'd still
92         # be better to be able to customize this.
93         open( PRINTER, "| lpr -P $queue > /dev/null" )
94           or die "Couldn't write to queue:$queue!\n";
95     }
96
97     #  print $queue;
98     #open (FILE,">/tmp/$file");
99     my $i      = 0;
100     # FIXME - This is HLT-specific. Put this stuff in a customizable
101     # site-specific file somewhere.
102     print PRINTER "Horowhenua Library Trust\r\n";
103     print PRINTER "Phone: 368-1953\r\n";
104     print PRINTER "Fax:    367-9218\r\n";
105     print PRINTER "Email:  renewals\@library.org.nz\r\n\r\n\r\n";
106     print PRINTER "$borrower->{'cardnumber'}\r\n";
107     print PRINTER
108       "$borrower->{'title'} $borrower->{'initials'} $borrower->{'surname'}\r\n";
109
110     # FIXME - Use   for ($i = 0; $items->[$i]; $i++)
111     # Or better yet,   foreach $item (@{$items})
112     while ( $items->[$i] ) {
113
114         #    print $i;
115         my $itemdata = $items->[$i];
116
117         # FIXME - This is just begging for a Perl format.
118         print PRINTER "$i $itemdata->{'title'}\r\n";
119         print PRINTER "$itemdata->{'barcode'}";
120         print PRINTER " " x 15;
121         print PRINTER "$itemdata->{'date_due'}\r\n";
122         $i++;
123     }
124     print PRINTER "\r\n" x 7 ;
125     close PRINTER;
126
127     #system("lpr /tmp/$file");
128 }
129
130 sub printreserve {
131     my ( $branchname, $bordata, $itemdata ) = @_;
132     my $printer = '';
133     (return) unless ( C4::Context->boolean_preference('printreserveslips') );
134     if ( $printer eq "" || $printer eq 'nulllp' ) {
135         open( PRINTER, ">>/tmp/kohares" )
136                   or die "Could not write to /tmp/kohares";
137     }
138     else {
139         open( PRINTER, "| lpr -P $printer >/dev/null" )
140           or die "Couldn't write to queue:$!\n";
141     }
142     my @da = localtime();
143     my $todaysdate = "$da[2]:$da[1]  " . C4::Dates->today();
144     my $slip = <<"EOF";
145 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146 Date: $todaysdate;
147
148 ITEM RESERVED: 
149 $itemdata->{'title'} ($itemdata->{'author'})
150 barcode: $itemdata->{'barcode'}
151
152 COLLECT AT: $branchname
153
154 BORROWER:
155 $bordata->{'surname'}, $bordata->{'firstname'}
156 card number: $bordata->{'cardnumber'}
157 Phone: $bordata->{'phone'}
158 $bordata->{'streetaddress'}
159 $bordata->{'suburb'}
160 $bordata->{'town'}
161 $bordata->{'emailaddress'}
162
163
164 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165 EOF
166     print PRINTER $slip;
167     close PRINTER;
168     return $slip;
169 }
170
171 =item printslip
172
173   &printslip($borrowernumber)
174
175   print a slip for the given $borrowernumber
176   
177 =cut
178
179 #'
180 sub printslip ($) {
181     my $borrowernumber = shift;
182     my $borrower   = GetMemberDetails($borrowernumber);
183         my $issueslist = GetPendingIssues($borrowernumber); 
184         foreach my $it (@$issueslist){
185                 $it->{'date_due'}=format_date($it->{'date_due'});
186     }           
187     my @issues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @$issueslist;
188     remoteprint(\@issues, $borrower );
189 }
190
191 END { }    # module clean-up code here (global destructor)
192
193 1;
194 __END__
195
196 =back
197
198 =head1 AUTHOR
199
200 Koha Developement team <info@koha.org>
201
202 =head1 SEE ALSO
203
204 C4::Circulation::Circ2(3)
205
206 =cut