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