Bug 28386: Fix Pawel history notes
[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 or $session->param('branch') eq 'NO_LIBRARY_SET'){
49                 # no branch set we can't transfer
50                 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.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         flagsrequired   => { circulate => "circulate_remaining_permissions" },
63     }
64 );
65
66 # Check transfers is allowed from system preference
67 if ( C4::Context->preference("IndependentBranchesTransfers") && !C4::Context->IsSuperLibrarian() ) {
68     print $query->redirect("/cgi-bin/koha/errors/403.pl");
69     exit;
70 }
71
72 my $messages;
73 my $found;
74 my $reserved;
75 my $waiting;
76 my $reqmessage;
77 my $cancelled;
78 my $setwaiting;
79
80 my $request        = $query->param('request')        || '';
81 my $borrowernumber = $query->param('borrowernumber') ||  0;
82 my $tobranchcd     = $query->param('tobranchcd')     || '';
83
84 my $ignoreRs = 0;
85 ############
86 # Deal with the requests....
87 if ( $request eq "KillWaiting" ) {
88     my $item = $query->param('itemnumber');
89     my $holds = Koha::Holds->search({
90         itemnumber     => $item,
91         borrowernumber => $borrowernumber
92     });
93     if ( $holds->count ) {
94         $holds->next->cancel;
95         $cancelled   = 1;
96         $reqmessage  = 1;
97     } # FIXME else?
98 }
99 elsif ( $request eq "SetWaiting" ) {
100     my $item = $query->param('itemnumber');
101     ModReserveAffect( $item, $borrowernumber );
102     $ignoreRs    = 1;
103     $setwaiting  = 1;
104     $reqmessage  = 1;
105 }
106 elsif ( $request eq 'KillReserved' ) {
107     my $biblionumber = $query->param('biblionumber');
108     my $holds = Koha::Holds->search({
109         biblionumber   => $biblionumber,
110         borrowernumber => $borrowernumber
111     });
112     if ( $holds->count ) {
113         $holds->next->cancel;
114         $cancelled   = 1;
115         $reqmessage  = 1;
116     } # FIXME else?
117 }
118
119 # collect the stack of books already transferred so they can printed...
120 my @trsfitemloop;
121 my $transferred;
122 my $barcode = $query->param('barcode');
123 # remove leading/trailing whitespace
124 defined $barcode and $barcode =~ s/^\s*|\s*$//g;  # FIXME: barcodeInputFilter
125 # warn "barcode : $barcode";
126 if ($barcode) {
127
128     ( $transferred, $messages ) =
129       transferbook( $tobranchcd, $barcode, $ignoreRs );
130     my $item = Koha::Items->find({ barcode => $barcode });
131     $found = $messages->{'ResFound'};
132     if ($transferred) {
133         my %trsfitem;
134         my $frbranchcd =  C4::Context->userenv->{'branch'};
135         $trsfitem{item}     = $item;
136         $trsfitem{counter}  = 0;
137         $trsfitem{frombrcd} = $frbranchcd;
138         $trsfitem{tobrcd}   = $tobranchcd;
139         push( @trsfitemloop, \%trsfitem );
140     }
141 }
142
143 foreach ( $query->param ) {
144     (next) unless (/bc-(\d*)/);
145     my $counter = $1;
146     my %trsfitem;
147     my $bc    = $query->param("bc-$counter");
148     my $frbcd = $query->param("fb-$counter");
149     my $tobcd = $query->param("tb-$counter");
150     $counter++;
151     $trsfitem{counter}  = $counter;
152     $trsfitem{frombrcd} = $frbcd;
153     $trsfitem{tobrcd}   = $tobcd;
154     my $item = Koha::Items->find({ barcode => $bc });
155     $trsfitem{item}     = $item;
156     push( @trsfitemloop, \%trsfitem );
157 }
158
159 my $itemnumber;
160 my $biblionumber;
161
162 #####################
163
164 if ($found) {
165     my $res = $messages->{'ResFound'};
166     $itemnumber = $res->{'itemnumber'};
167     $borrowernumber = $res->{'borrowernumber'};
168
169     if ( $res->{'ResFound'} eq "Waiting" ) {
170         $waiting = 1;
171     }
172     elsif ( $res->{'ResFound'} eq "Reserved" ) {
173         $reserved  = 1;
174         $biblionumber = $res->{'biblionumber'};
175     }
176 }
177
178 my @errmsgloop;
179 foreach my $code ( keys %$messages ) {
180     if ( $code ne 'WasTransfered' ) {
181         my %err;
182         if ( $code eq 'BadBarcode' ) {
183             $err{msg}        = $messages->{'BadBarcode'};
184             $err{errbadcode} = 1;
185         }
186         elsif ( $code eq "NotAllowed" ) {
187             warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
188             # Do we really want a error log message here? --atz
189             $err{errnotallowed} =  1;
190             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
191             $err{tbr}      = $tbr;
192             $err{code}     = $typecode;
193         }
194         elsif ( $code eq 'WasReturned' ) {
195             $err{errwasreturned} = 1;
196             $err{borrowernumber} = $messages->{'WasReturned'};
197             my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
198             if ( $patron ) { # Just in case...
199                 $err{patron} = $patron;
200             }
201         }
202         $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
203         push( @errmsgloop, \%err );
204     }
205 }
206
207 # use Data::Dumper;
208 # warn "FINAL ============= ".Dumper(@trsfitemloop);
209
210 $template->param(
211     found                   => $found,
212     reserved                => $reserved,
213     waiting                 => $waiting,
214     borrowernumber          => $borrowernumber,
215     itemnumber              => $itemnumber,
216     barcode                 => $barcode,
217     biblionumber            => $biblionumber,
218     tobranchcd              => $tobranchcd,
219     reqmessage              => $reqmessage,
220     cancelled               => $cancelled,
221     setwaiting              => $setwaiting,
222     trsfitemloop            => \@trsfitemloop,
223     errmsgloop              => \@errmsgloop,
224     PatronAutoComplete    => C4::Context->preference("PatronAutoComplete"),
225 );
226
227 # Checking if there is a Fast Cataloging Framework
228 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
229
230 output_html_with_http_headers $query, $cookie, $template->output;
231