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