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