batch import rework -- implement stage-commit-undo for batch import
[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::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\r\n\r\n\r\n\r\n\r\n\r\n";
128     close PRINTER;
129
130     #system("lpr /tmp/$file");
131 }
132
133 sub printreserve {
134     my ( $branchname, $bordata, $itemdata ) = @_;
135     my $file    = time;
136     my $printer = '';
137     (return) unless ( C4::Context->boolean_preference('printreserveslips') );
138     if ( $printer eq "" || $printer eq 'nulllp' ) {
139         open( PRINTER, ">>/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( time() );
146     my $todaysdate = "$da[2]:$da[1]  $da[3]/$da[4]/$da[5]";
147
148 #(1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
149     my $slip = <<"EOF";
150 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151 Date: $todaysdate;
152
153 ITEM RESERVED: 
154 $itemdata->{'title'} ($itemdata->{'author'})
155 barcode: $itemdata->{'barcode'}
156
157 COLLECT AT: $branchname
158
159 BORROWER:
160 $bordata->{'surname'}, $bordata->{'firstname'}
161 card number: $bordata->{'cardnumber'}
162 Phone: $bordata->{'phone'}
163 $bordata->{'streetaddress'}
164 $bordata->{'suburb'}
165 $bordata->{'town'}
166 $bordata->{'emailaddress'}
167
168
169 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170 EOF
171     print PRINTER $slip;
172     close PRINTER;
173     return $slip;
174 }
175
176 =item printslip
177
178   &printslip($borrowernumber)
179
180   print a slip for the given $borrowernumber
181   
182 =cut
183
184 #'
185 sub printslip {
186     my ( $borrowernumber ) = @_;
187     my ( $borrower, $flags ) = GetMemberDetails( $borrowernumber);
188         my ($countissues,$issueslist) = GetPendingIssues($borrowernumber); 
189         foreach my $it (@$issueslist){
190                 $it->{'date_due'}=format_date($it->{'date_due'});
191     }           
192     my @issues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @$issueslist;
193     remoteprint(\@issues, $borrower );
194 }
195
196 END { }    # module clean-up code here (global destructor)
197
198 1;
199 __END__
200
201 =back
202
203 =head1 AUTHOR
204
205 Koha Developement team <info@koha.org>
206
207 =head1 SEE ALSO
208
209 C4::Circulation::Circ2(3)
210
211 =cut