Fix to redirect the user ot the select branch page if they try to use
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2 # WARNING: This file uses 4-character tabs!
3
4 #written 11/3/2002 by Finlay
5 #script to execute branch transfers of books
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;
27 use C4::Output;
28 use C4::Reserves;
29 use C4::Biblio;
30 use C4::Auth qw/:DEFAULT get_session/;
31 use C4::Branch; # GetBranches
32 use C4::Koha;
33
34 ###############################################
35 #  Getting state
36
37 my $query = new CGI;
38
39 if (!C4::Context->userenv){
40         my $sessionID = $query->cookie("CGISESSID");
41         my $session = get_session($sessionID);
42         if ($session->param('branch') eq 'NO_LIBRARY_SET'){
43                 # no branch set we can't transfer
44                 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
45                 exit;
46         }
47 }   
48
49
50 #######################################################################################
51 # Make the page .....
52 my ( $template, $cookie );
53 my $user;
54 ( $template, $user, $cookie ) = get_template_and_user(
55     {
56         template_name   => "circ/branchtransfers.tmpl",
57         query           => $query,
58         type            => "intranet",
59         authnotrequired => 0,
60         flagsrequired   => { circulate => 1 },
61     }
62 );
63
64 my $branches = GetBranches;
65 my $branch  = GetBranch( $query,  $branches );
66
67 my $messages;
68 my $found;
69 my $reserved;
70 my $waiting;
71 my $reqmessage;
72 my $cancelled;
73 my $setwaiting;
74
75 my $request        = $query->param('request');
76 my $borrowernumber = $query->param('borrowernumber');
77 my $tobranchcd     = $query->param('tobranchcd');
78
79 ############
80 # Deal with the requests....
81 if ( $request eq "KillWaiting" ) {
82     my $item = $query->param('itemnumber');
83
84     CancelReserve( 0, $item, $borrowernumber );
85     $cancelled   = 1;
86     $reqmessage  = 1;
87 }
88
89 my $ignoreRs = 0;
90 if ( $request eq "SetWaiting" ) {
91     my $item = $query->param('itemnumber');
92     ModReserveAffect( $item, $borrowernumber );
93     $ignoreRs    = 1;
94     $setwaiting  = 1;
95     $reqmessage  = 1;
96 }
97 if ( $request eq 'KillReserved' ) {
98     my $biblio = $query->param('biblionumber');
99     CancelReserve( $biblio, 0, $borrowernumber );
100     $cancelled   = 1;
101     $reqmessage  = 1;
102 }
103
104 # set up the branchselect options....
105 my @branchoptionloop;
106 foreach my $br ( keys %$branches ) {
107     my %branch;
108     $branch{selected} = ( $br eq $tobranchcd );
109     $branch{code}     = $br;
110     $branch{name}     = $branches->{$br}->{'branchname'};
111     push( @branchoptionloop, \%branch );
112 }
113
114 # collect the stack of books already transfered so they can printed...
115 my @trsfitemloop;
116 my %transfereditems;
117 my $transfered;
118 my $barcode = $query->param('barcode');
119 # strip whitespace
120 $barcode =~ s/\s*//g;
121 # warn "barcode : $barcode";
122 if ($barcode) {
123
124     my $iteminformation;
125     ( $transfered, $messages, $iteminformation ) =
126       transferbook( $tobranchcd, $barcode, $ignoreRs );
127 #       use Data::Dumper;
128 #       warn "Transfered : $transfered / ".Dumper($messages);
129     $found = $messages->{'ResFound'};
130     if ($transfered) {
131         my %item;
132         my $frbranchcd =  C4::Context->userenv->{'branch'};
133 #         if ( not($found) ) {
134         $item{'biblionumber'} = $iteminformation->{'biblionumber'};
135         $item{'title'}        = $iteminformation->{'title'};
136         $item{'author'}       = $iteminformation->{'author'};
137         $item{'itemtype'}     = $iteminformation->{'itemtype'};
138         $item{'ccode'}        = $iteminformation->{'ccode'};
139         $item{'frbrname'}     = $branches->{$frbranchcd}->{'branchname'};
140         $item{'tobrname'}     = $branches->{$tobranchcd}->{'branchname'};
141 #         }
142         $item{counter}  = 0;
143         $item{barcode}  = $barcode;
144         $item{frombrcd} = $frbranchcd;
145         $item{tobrcd}   = $tobranchcd;
146         push( @trsfitemloop, \%item );
147 #         warn Dumper(@trsfitemloop);
148     }
149 }
150
151 foreach ( $query->param ) {
152     (next) unless (/bc-(\d*)/);
153     my $counter = $1;
154     my %item;
155     my $bc    = $query->param("bc-$counter");
156     my $frbcd = $query->param("fb-$counter");
157     my $tobcd = $query->param("tb-$counter");
158     $counter++;
159     $item{counter}  = $counter;
160     $item{barcode}  = $bc;
161     $item{frombrcd} = $frbcd;
162     $item{tobrcd}   = $tobcd;
163     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
164     $item{'biblionumber'} = $iteminformation->{'biblionumber'};
165     $item{'title'}        = $iteminformation->{'title'};
166     $item{'author'}       = $iteminformation->{'author'};
167     $item{'itemtype'}     = $iteminformation->{'itemtype'};
168     $item{'ccode'}        = $iteminformation->{'ccode'};
169     $item{'frbrname'}     = $branches->{$frbcd}->{'branchname'};
170     $item{'tobrname'}     = $branches->{$tobcd}->{'branchname'};
171     push( @trsfitemloop, \%item );
172 }
173
174 my $itemnumber;
175 my $biblionumber;
176
177 #####################
178
179 if ($found) {
180     my $res = $messages->{'ResFound'};
181     $itemnumber = $res->{'itemnumber'};
182
183     if ( $res->{'ResFound'} eq "Waiting" ) {
184         $waiting = 1;
185     }
186     if ( $res->{'ResFound'} eq "Reserved" ) {
187         $reserved  = 1;
188         $biblionumber = $res->{'biblionumber'};
189     }
190 }
191
192 #####################
193
194 my @errmsgloop;
195 foreach my $code ( keys %$messages ) {
196     my %err;
197
198     if ( $code eq 'BadBarcode' ) {
199         $err{msg}        = $messages->{'BadBarcode'};
200         $err{errbadcode} = 1;
201     }
202
203     if ( $code eq 'IsPermanent' ) {
204         $err{errispermanent} = 1;
205         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
206     }
207     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
208
209     if ( $code eq 'WasReturned' ) {
210         $err{errwasreturned} = 1;
211     }
212     push( @errmsgloop, \%err );
213 }
214
215 # use Data::Dumper;
216 # warn "FINAL ============= ".Dumper(@trsfitemloop);
217 $template->param(
218     found                   => $found,
219     reserved                => $reserved,
220     waiting                 => $waiting,
221     borrowernumber          => $borrowernumber,
222     itemnumber              => $itemnumber,
223     barcode                 => $barcode,
224     biblionumber            => $biblionumber,
225     tobranchcd              => $tobranchcd,
226     reqmessage              => $reqmessage,
227     cancelled               => $cancelled,
228     setwaiting              => $setwaiting,
229     trsfitemloop            => \@trsfitemloop,
230     branchoptionloop        => \@branchoptionloop,
231     errmsgloop              => \@errmsgloop,
232     CircAutocompl           => C4::Context->preference("CircAutocompl")
233 );
234 output_html_with_http_headers $query, $cookie, $template->output;
235
236 sub name {
237     my ($borinfo) = @_;
238     return $borinfo->{'surname'} . " "
239       . $borinfo->{'title'} . " "
240       . $borinfo->{'firstname'};
241 }
242
243 # Local Variables:
244 # tab-width: 4
245 # End: