8cd01d58bd506bee64d4095875d84b7aab65ecbb
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2
3 #script to execute branch transfers of books
4
5 # Copyright 2000-2002 Katipo Communications
6 # copyright 2010 BibLibre
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Circulation;
26 use C4::Output;
27 use C4::Reserves;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Auth qw/:DEFAULT get_session/;
31 use C4::Koha;
32 use C4::Members;
33 use Koha::BiblioFrameworks;
34 use Koha::AuthorisedValues;
35 use Koha::Holds;
36 use Koha::Items;
37 use Koha::Patrons;
38
39 ###############################################
40 #  Getting state
41
42 my $query = new CGI;
43
44 if (!C4::Context->userenv){
45         my $sessionID = $query->cookie("CGISESSID");
46     my $session;
47         $session = get_session($sessionID) if $sessionID;
48     if (!$session){
49                 # no branch set we can't transfer
50         print $query->redirect("/cgi-bin/koha/circ/set-library.pl");
51                 exit;
52         }
53 }
54
55 #######################################################################################
56 # Make the page .....
57 my ($template, $user, $cookie, $flags ) = get_template_and_user(
58     {
59         template_name   => "circ/branchtransfers.tt",
60         query           => $query,
61         type            => "intranet",
62         authnotrequired => 0,
63         flagsrequired   => { circulate => "circulate_remaining_permissions" },
64     }
65 );
66
67 # Check transfers is allowed from system preference
68 if ( C4::Context->preference("IndependentBranchesTransfers") && !C4::Context->IsSuperLibrarian() ) {
69     print $query->redirect("/cgi-bin/koha/errors/403.pl");
70     exit;
71 }
72
73 my $messages;
74 my $found;
75 my $reserved;
76 my $waiting;
77 my $reqmessage;
78 my $cancelled;
79 my $setwaiting;
80
81 my $request        = $query->param('request')        || '';
82 my $borrowernumber = $query->param('borrowernumber') ||  0;
83 my $tobranchcd     = $query->param('tobranchcd')     || '';
84
85 my $ignoreRs = 0;
86 ############
87 # Deal with the requests....
88 if ( $request eq "KillWaiting" ) {
89     my $item = $query->param('itemnumber');
90     my $holds = Koha::Holds->search({
91         itemnumber     => $item,
92         borrowernumber => $borrowernumber
93     });
94     if ( $holds->count ) {
95         $holds->next->cancel;
96         $cancelled   = 1;
97         $reqmessage  = 1;
98     } # FIXME else?
99 }
100 elsif ( $request eq "SetWaiting" ) {
101     my $item = $query->param('itemnumber');
102     ModReserveAffect( $item, $borrowernumber );
103     $ignoreRs    = 1;
104     $setwaiting  = 1;
105     $reqmessage  = 1;
106 }
107 elsif ( $request eq 'KillReserved' ) {
108     my $biblionumber = $query->param('biblionumber');
109     my $holds = Koha::Holds->search({
110         biblionumber   => $biblionumber,
111         borrowernumber => $borrowernumber
112     });
113     if ( $holds->count ) {
114         $holds->next->cancel;
115         $cancelled   = 1;
116         $reqmessage  = 1;
117     } # FIXME else?
118 }
119
120 # collect the stack of books already transferred so they can printed...
121 my @trsfitemloop;
122 my $transferred;
123 my $barcode = $query->param('barcode');
124 # remove leading/trailing whitespace
125 defined $barcode and $barcode =~ s/^\s*|\s*$//g;  # FIXME: barcodeInputFilter
126 # warn "barcode : $barcode";
127 if ($barcode) {
128
129     ( $transferred, $messages ) =
130       transferbook( $tobranchcd, $barcode, $ignoreRs, 'Manual' );
131     my $item = Koha::Items->find({ barcode => $barcode });
132     $found = $messages->{'ResFound'};
133     if ($transferred) {
134         my %trsfitem;
135         my $frbranchcd =  C4::Context->userenv->{'branch'};
136         $trsfitem{item}     = $item;
137         $trsfitem{counter}  = 0;
138         $trsfitem{frombrcd} = $frbranchcd;
139         $trsfitem{tobrcd}   = $tobranchcd;
140         push( @trsfitemloop, \%trsfitem );
141     }
142 }
143
144 foreach ( $query->param ) {
145     (next) unless (/bc-(\d*)/);
146     my $counter = $1;
147     my %trsfitem;
148     my $bc    = $query->param("bc-$counter");
149     my $frbcd = $query->param("fb-$counter");
150     my $tobcd = $query->param("tb-$counter");
151     $counter++;
152     $trsfitem{counter}  = $counter;
153     $trsfitem{frombrcd} = $frbcd;
154     $trsfitem{tobrcd}   = $tobcd;
155     my $item = Koha::Items->find({ barcode => $bc });
156     $trsfitem{item}     = $item;
157     push( @trsfitemloop, \%trsfitem );
158 }
159
160 my $itemnumber;
161 my $biblionumber;
162
163 #####################
164
165 if ($found) {
166     my $res = $messages->{'ResFound'};
167     $itemnumber = $res->{'itemnumber'};
168     $borrowernumber = $res->{'borrowernumber'};
169
170     if ( $res->{'ResFound'} eq "Waiting" ) {
171         $waiting = 1;
172     }
173     elsif ( $res->{'ResFound'} eq "Reserved" ) {
174         $reserved  = 1;
175         $biblionumber = $res->{'biblionumber'};
176     }
177 }
178
179 my @errmsgloop;
180 foreach my $code ( keys %$messages ) {
181     if ( $code ne 'WasTransfered' ) {
182         my %err;
183         if ( $code eq 'BadBarcode' ) {
184             $err{msg}        = $messages->{'BadBarcode'};
185             $err{errbadcode} = 1;
186         }
187         elsif ( $code eq "NotAllowed" ) {
188             warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
189             # Do we really want a error log message here? --atz
190             $err{errnotallowed} =  1;
191             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
192             $err{tbr}      = $tbr;
193             $err{code}     = $typecode;
194         }
195         elsif ( $code eq 'WasReturned' ) {
196             $err{errwasreturned} = 1;
197             $err{borrowernumber} = $messages->{'WasReturned'};
198             my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
199             if ( $patron ) { # Just in case...
200                 $err{patron} = $patron;
201             }
202         }
203         $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
204         push( @errmsgloop, \%err );
205     }
206 }
207
208 # use Data::Dumper;
209 # warn "FINAL ============= ".Dumper(@trsfitemloop);
210
211 $template->param(
212     found                   => $found,
213     reserved                => $reserved,
214     waiting                 => $waiting,
215     borrowernumber          => $borrowernumber,
216     itemnumber              => $itemnumber,
217     barcode                 => $barcode,
218     biblionumber            => $biblionumber,
219     tobranchcd              => $tobranchcd,
220     reqmessage              => $reqmessage,
221     cancelled               => $cancelled,
222     setwaiting              => $setwaiting,
223     trsfitemloop            => \@trsfitemloop,
224     errmsgloop              => \@errmsgloop,
225     PatronAutoComplete    => C4::Context->preference("PatronAutoComplete"),
226 );
227
228 # Checking if there is a Fast Cataloging Framework
229 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
230
231 output_html_with_http_headers $query, $cookie, $template->output;
232