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