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