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