(bug fix?) What looks like possibly a line left over from debugging
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2
3 #written 11/3/2002 by Finlay
4 #script to execute branch transfers of books
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Circulation::Circ2;
27 use C4::Output;
28 use C4::Reserves2;
29
30 ###############################################
31 # constants
32
33 my %env;
34 my $headerbackgroundcolor='#99cc33';
35 my $circbackgroundcolor='#ffffcc';
36 my $circbackgroundcolor='white';
37 my $linecolor1='#ffffcc';
38 my $linecolor2='white';
39 my $backgroundimage="/images/background-mem.gif";
40
41 my $branches = getbranches();
42 my $printers = getprinters(\%env);
43
44
45 ###############################################
46 #  Getting state
47
48 my $query=new CGI;
49
50
51 my $branch = $query->param("branch");
52 my $printer = $query->param("printer");
53
54
55 ($branch) || ($branch=$query->cookie('branch')) ;
56 ($printer) || ($printer=$query->cookie('printer')) ;
57
58 my $request=$query->param('request');
59
60
61 my $tobranchcd=$query->param('tobranchcd');
62 my $frbranchcd='';
63
64 # set up the branchselect options....
65 my $tobranchoptions;
66 foreach my $br (keys %$branches) {
67 # FIXME - Dunno what this line was supposed to do, but "CU" never
68 # appears anywhere else, so this was preventing this code from
69 # working, by creating an empty <select> block later on.
70 #    (next) unless $branches->{$br}->{'CU'};
71     my $selected='';
72     ($selected='selected') if ($br eq $tobranchcd);
73     $tobranchoptions.="<option value=$br $selected>$branches->{$br}->{'branchname'}\n";
74 }
75
76 # collect the stack of books already transfered so they can printed...
77 my %transfereditems;
78 my $ritext = '';
79 my %frbranchcds;
80 my %tobranchcds;
81 foreach ($query->param){
82     (next) unless (/bc-(\d*)/);
83     my $counter=$1;
84     my $barcode=$query->param("bc-$counter");
85     my $frbcd=$query->param("fb-$counter");
86     my $tobcd=$query->param("tb-$counter");
87     $counter++;
88     $transfereditems{$counter}=$barcode;
89     $frbranchcds{$counter}=$frbcd;
90     $tobranchcds{$counter}=$tobcd;
91     $ritext.="<input type=hidden name=bc-$counter value=$barcode>\n";
92     $ritext.="<input type=hidden name=fb-$counter value=$frbcd>\n";
93     $ritext.="<input type=hidden name=tb-$counter value=$tobcd>\n";
94     }
95
96
97 # Collect a few messages here...
98 my $messagetext='';
99
100 ############
101 # Deal with the requests....
102 if ($request eq "KillWaiting") {
103     my $item = $query->param('itemnumber');
104     my $borrnum = $query->param('borrowernumber');
105     CancelReserve(0, $item, $borrnum);
106     $messagetext .= "Reserve Cancelled<br>";
107 }
108 my $ignoreRs = 0;
109 if ($request eq "SetWaiting") {
110     my $item = $query->param('itemnumber');
111     my $borrnum = $query->param('borrowernumber');
112     $tobranchcd = ReserveWaiting($item, $borrnum);
113     $ignoreRs = 1;
114     $messagetext .= "Item should now be waiting at branch: $branches->{$tobranchcd}->{'branchname'}<br>";
115 }
116 if ($request eq 'KillReserved'){
117     my $biblio = $query->param('biblionumber');
118     my $borrnum = $query->param('borrowernumber');
119     CancelReserve($biblio, 0, $borrnum);
120     $messagetext .= "Reserve Cancelled<br>";
121 }
122
123
124
125
126 # Warnings etc that get displayed at top of next page....
127 my $messages;
128 # if the barcode has been entered action that and write a message 
129 # and put onto the top of the stack...
130 my $iteminformation;
131 my $barcode = $query->param('barcode');
132 if ($barcode) {
133     my $transfered;
134     my $iteminformation;
135     ($transfered, $messages, $iteminformation) 
136                    = transferbook($tobranchcd, $barcode, $ignoreRs);
137     if ($transfered) {
138         my $frbranchcd = $iteminformation->{'holdingbranch'};
139         $ritext.="<input type=hidden name=bc-0 value=$barcode>\n";
140         $ritext.="<input type=hidden name=fb-0 value=$frbranchcd>\n";
141         $ritext.="<input type=hidden name=tb-0 value=$tobranchcd>\n";
142         $transfereditems{0}=$barcode;
143         $frbranchcds{0}=$frbranchcd;
144         $tobranchcds{0}=$tobranchcd;
145     }
146 }
147
148
149 #################################################################################
150 # Html code....
151 # collect together the various elements...
152
153 my $entrytext= << "EOF";
154 <form method=post action=/cgi-bin/koha/circ/branchtransfers.pl>
155 <table border=1 cellpadding=5 cellspacing=0 align=left>
156 <tr><td colspan=2 align=center background=$backgroundimage>
157 <font color=black><b>Select Branch</b></font></td></tr>
158 <tr><td>Destination Branch:</td>
159     <td><select name=tobranchcd> $tobranchoptions </select></td></tr></table>    
160
161 <img src="/images/holder.gif" width=24 height=50 align=left>
162
163 <table border=1 cellpadding=5 cellspacing=0 ><tr>
164 <td colspan=2 align=center background=$backgroundimage>
165 <font color=black><b>Enter Book Barcode</b></font></td></tr>
166 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td></tr>
167 </table>
168 <input type=hidden name=tobranchcd value=$tobranchcd>
169 $ritext
170 </form>
171 EOF
172
173
174
175 #####################
176
177 my $reservefoundtext;
178 if ($messages->{'ResFound'}) {
179     my $res = $messages->{'ResFound'};
180     my $reservetext;
181     my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
182     my ($borr) = getpatroninformation(\%env, $res->{'borrowernumber'}, 0);
183     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
184     my $number = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borr->{'borrowernumber'} onClick='openWindow(this,'Member', 480, 640)'>$borr->{'cardnumber'}</a>";
185     if ($res->{'ResFound'} eq "Waiting") {
186         $reservetext = <<"EOF";
187 <font color='red' size='+2'>Item marked Waiting:</font><br>
188     Item is marked waiting at $branchname for $name ($number).<br>
189 <table border=1 cellpadding=5 cellspacing=0>
190 <tr><td>Cancel reservation and then attempt transfer: </td>
191 <td>
192 <form method=post action='branchtransfers.pl'>
193 $ritext
194 <input type=hidden name=itemnumber value=$res->{'itemnumber'}>
195 <input type=hidden name=borrowernumber value=$res->{'borrowernumber'}>
196 <input type=hidden name=tobranchcd value=$tobranchcd>
197 <input type=hidden name=barcode value=$barcode>
198 <input type=hidden name=request value='KillWaiting'>
199 <input type=submit value="Cancel">
200 </form>
201 </td></tr>
202 <tr><td>Ignore and return to transfers: </td>
203 <td>
204 <form method=post action='branchtransfers.pl'>
205 $ritext
206 <input type=hidden name=tobranchcd value=$tobranchcd>
207 <input type=hidden name=barcode value=0>
208 <input type=submit value="Ignore">
209 </form>
210 </td></tr></table>
211 EOF
212     } 
213     if ($res->{'ResFound'} eq "Reserved") {
214         $reservetext = <<"EOF";
215 <font color='red' size='+2'>Reserved:</font> reserve found for $name ($number).
216 <table border=1 cellpadding=5 cellspacing=0>
217 <tr><td>Set reserve to waiting and transfer book to <b>$branchname </b>: </td>
218 <td>
219 <form method=post action='branchtransfers.pl'>
220 $ritext
221 <input type=hidden name=itemnumber value=$res->{'itemnumber'}>
222 <input type=hidden name=borrowernumber value=$res->{'borrowernumber'}>
223 <input type=hidden name=barcode value=$barcode>
224 <input type=hidden name=request value='SetWaiting'>
225 <input type=submit value="Waiting">
226 </form>
227 </td></tr>
228 <tr><td>Cancel reservation and then attempt transfer: </td>
229 <td>
230 <form method=post action='branchtransfers.pl'>
231 $ritext
232 <input type=hidden name=biblionumber value=$res->{'biblionumber'}>
233 <input type=hidden name=borrowernumber value=$res->{'borrowernumber'}>
234 <input type=hidden name=tobranchcd value=$tobranchcd>
235 <input type=hidden name=barcode value=$barcode>
236 <input type=hidden name=request value='KillReserved'>
237 <input type=submit value="Cancel">
238 </form>
239 </td></tr><tr><td>Ignore and return to transfers: </td>
240 <td>
241 <form method=post action='branchtransfers.pl'>
242 <input type=hidden name=tobranchcd value=$tobranchcd>
243 <input type=hidden name=barcode value=0>
244 $ritext
245 <input type=submit value="Ignore">
246 </form>
247 </td></tr></table>
248 EOF
249     }
250     $reservefoundtext = <<"EOF";
251 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
252 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Reserve Found</font></th></tr>
253 <tr><td> $reservetext </td></tr></table>
254 EOF
255 }
256
257 #####################
258
259 foreach my $code (keys %$messages) {
260     if ($code eq 'BadBarcode'){
261         $messagetext .= "<font color='red' size='+2'> No Item with barcode: $messages->{'BadBarcode'} </font> <br>";
262     }
263     if ($code eq 'IsPermanent'){
264         my $braname = $branches->{$messages->{'IsPermanent'}}->{'branchname'};
265         $messagetext .= "<font color='red' size='+2'> Please return item to home branch: $braname  </font> <br>";
266     }
267     if ($code eq 'DestinationEqualsHolding'){
268         $messagetext .= "<font color='red' size='+2'> Item is already at destination branch. </font> <br>";
269     }
270     if ($code eq 'WasReturned') {
271         my ($borrowerinfo) = getpatroninformation(\%env, $messages->{'WasReturned'}, 0);
272
273         my $binfo = <<"EOF";
274 <a href=/cgi-bin/koha/moremember.pl?bornum=$borrowerinfo->{'borrowernumber'} 
275 onClick="openWindow(this,'Member', 480, 640)">$borrowerinfo->{'cardnumber'}</a>
276 $borrowerinfo->{'surname'}, $borrowerinfo->{'title'} $borrowerinfo->{'firstname'}
277 EOF
278         $messagetext .= "Item was on loan to $binfo and has been returned. <br>";
279     }
280     if ($code eq 'WasTransfered'){
281 # Put code here if you want to notify the user that item was transfered...
282     }
283 }
284 $messagetext = substr($messagetext, 0, -4);
285
286 my $messagetable;
287 if ($messagetext) {
288     $messagetable = << "EOF";
289 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
290 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
291 <tr><td> $messagetext </td></tr></table>
292 <img src="/images/holder.gif" width=24 height=24>
293 EOF
294 }
295
296 #######################################################################################
297 # Make the page .....
298
299 print $query->header;
300 print startpage;
301 #print startmenu('circulation');
302 my @inp=startmenu('circulation');
303 my $count=@inp;
304 for (my $i=0;$i<$count;$i++){
305         $inp[$i]=~ s/document.forms\[0\].elements\[0\]/document\.forms\[0\]\.elements\[1\]/;
306     }
307 print @inp;
308
309 #foreach my $key (%$messages) {
310 #    print $key." : ".$messages->{$key}."<br>";
311 #}
312
313 print <<"EOF";
314 <table align=right><tr><td>
315 <a href=circulation.pl>
316 <img src="/images/button-issues.gif" width="99" height="42" border="0" alt="Next Borrower"></a> &nbsp
317 <a href=returns.pl>
318 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
319 </td></tr></table>
320
321 <FONT SIZE=6><em>Circulation: Transfers</em></FONT><br>
322 <b>Branch:</b> $branches->{$branch}->{'branchname'} &nbsp
323 <b>Printer:</b> $printers->{$printer}->{'printername'}<br>
324 <a href=selectbranchprinter.pl>Change Settings</a>
325 <input type=hidden name=branch value=$branch>
326 <input type=hidden name=printer value=$printer>
327 <p>           
328 EOF
329
330 if ($reservefoundtext) {
331     print $reservefoundtext;
332 } else {
333     print $messagetable;
334     print $entrytext;
335
336     if (%transfereditems) {
337         print << "EOF";
338 <p>
339 <table border=1 cellpadding=5 cellspacing=0 bgcolor=#dddddd>                                                                
340 <tr><th colspan=6 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Transfered Items</font></th></tr>
341 <tr><th>Bar Code</th><th>Title</th><th>Author</th><th>Type</th><th>From</th><th>To</th></tr>
342 EOF
343         my $color='';
344         foreach (sort keys %transfereditems) {
345             ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
346             my $barcode=$transfereditems{$_};
347             my $frbcd=$frbranchcds{$_};
348             my $tobcd=$tobranchcds{$_};
349             my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
350             print << "EOF";
351 <tr><td bgcolor=$color align=center>
352 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}
353 &type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a></td>
354 <td bgcolor=$color>$iteminformation->{'title'}</td>
355 <td bgcolor=$color>$iteminformation->{'author'}</td>
356 <td bgcolor=$color align=center>$iteminformation->{'itemtype'}</td>
357 <td bgcolor=$color align=center>$branches->{$frbcd}->{'branchname'}</td>
358 <td bgcolor=$color align=center>$branches->{$tobcd}->{'branchname'}</td>
359 </tr>\n
360 EOF
361         }
362         print "</table>\n";
363     }
364 }
365
366 print endmenu('circulation');
367 print endpage;
368
369