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