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