Bug 16530: Adding a circ sidebar navigation menu and circSidebar syspref to activate...
[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 strict;
24 use warnings;
25 use CGI qw ( -utf8 );
26 use C4::Circulation;
27 use C4::Output;
28 use C4::Reserves;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Auth qw/:DEFAULT get_session/;
32 use C4::Koha;
33 use C4::Members;
34 use Koha::BiblioFrameworks;
35 use Koha::AuthorisedValues;
36
37 ###############################################
38 #  Getting state
39
40 my $query = new CGI;
41
42 if (!C4::Context->userenv){
43         my $sessionID = $query->cookie("CGISESSID");
44     my $session;
45         $session = get_session($sessionID) if $sessionID;
46         if (!$session or $session->param('branch') eq 'NO_LIBRARY_SET'){
47                 # no branch set we can't transfer
48                 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
49                 exit;
50         }
51 }
52
53 #######################################################################################
54 # Make the page .....
55 my ($template, $user, $cookie, $flags ) = get_template_and_user(
56     {
57         template_name   => "circ/branchtransfers.tt",
58         query           => $query,
59         type            => "intranet",
60         authnotrequired => 0,
61         flagsrequired   => { circulate => "circulate_remaining_permissions" },
62     }
63 );
64
65 my $messages;
66 my $found;
67 my $reserved;
68 my $waiting;
69 my $reqmessage;
70 my $cancelled;
71 my $setwaiting;
72
73 my $request        = $query->param('request')        || '';
74 my $borrowernumber = $query->param('borrowernumber') ||  0;
75 my $tobranchcd     = $query->param('tobranchcd')     || '';
76
77 my $ignoreRs = 0;
78 ############
79 # Deal with the requests....
80 if ( $request eq "KillWaiting" ) {
81     my $item = $query->param('itemnumber');
82     CancelReserve({
83         itemnumber     => $item,
84         borrowernumber => $borrowernumber
85     });
86     $cancelled   = 1;
87     $reqmessage  = 1;
88 }
89 elsif ( $request eq "SetWaiting" ) {
90     my $item = $query->param('itemnumber');
91     ModReserveAffect( $item, $borrowernumber );
92     $ignoreRs    = 1;
93     $setwaiting  = 1;
94     $reqmessage  = 1;
95 }
96 elsif ( $request eq 'KillReserved' ) {
97     my $biblio = $query->param('biblionumber');
98     CancelReserve({
99         biblionumber   => $biblio,
100         borrowernumber => $borrowernumber
101     });
102     $cancelled   = 1;
103     $reqmessage  = 1;
104 }
105
106 # collect the stack of books already transfered so they can printed...
107 my @trsfitemloop;
108 my $transfered;
109 my $barcode = $query->param('barcode');
110 # remove leading/trailing whitespace
111 defined $barcode and $barcode =~ s/^\s*|\s*$//g;  # FIXME: barcodeInputFilter
112 # warn "barcode : $barcode";
113 if ($barcode) {
114
115     my $iteminformation;
116     ( $transfered, $messages, $iteminformation ) =
117       transferbook( $tobranchcd, $barcode, $ignoreRs );
118 #       use Data::Dumper;
119 #       warn "Transfered : $transfered / ".Dumper($messages);
120     $found = $messages->{'ResFound'};
121     if ($transfered) {
122         my %item;
123         my $frbranchcd =  C4::Context->userenv->{'branch'};
124 #         if ( not($found) ) {
125         $item{'biblionumber'}          = $iteminformation->{'biblionumber'};
126         $item{'itemnumber'}            = $iteminformation->{'itemnumber'};
127         $item{'title'}                 = $iteminformation->{'title'};
128         $item{'author'}                = $iteminformation->{'author'};
129         $item{'itemtype'}              = $iteminformation->{'itemtype'};
130         $item{'ccode'}                 = $iteminformation->{'ccode'};
131         $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
132         my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $iteminformation->{location} });
133         $item{'location'}              = $av->count ? $av->next->lib : '';
134 #         }
135         $item{counter}  = 0;
136         $item{barcode}  = $barcode;
137         $item{frombrcd} = $frbranchcd;
138         $item{tobrcd}   = $tobranchcd;
139         push( @trsfitemloop, \%item );
140 #         warn Dumper(@trsfitemloop);
141     }
142 }
143
144 foreach ( $query->param ) {
145     (next) unless (/bc-(\d*)/);
146     my $counter = $1;
147     my %item;
148     my $bc    = $query->param("bc-$counter");
149     my $frbcd = $query->param("fb-$counter");
150     my $tobcd = $query->param("tb-$counter");
151     $counter++;
152     $item{counter}  = $counter;
153     $item{barcode}  = $bc;
154     $item{frombrcd} = $frbcd;
155     $item{tobrcd}   = $tobcd;
156     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
157     $item{'biblionumber'}          = $iteminformation->{'biblionumber'};
158     $item{'itemnumber'}            = $iteminformation->{'itemnumber'};
159     $item{'title'}                 = $iteminformation->{'title'};
160     $item{'author'}                = $iteminformation->{'author'};
161     $item{'itemtype'}              = $iteminformation->{'itemtype'};
162     $item{'ccode'}                 = $iteminformation->{'ccode'};
163     $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
164     my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $iteminformation->{location} });
165     $item{'location'}              = $av->count ? $av->next->lib : '';
166     push( @trsfitemloop, \%item );
167 }
168
169 my $itemnumber;
170 my $biblionumber;
171
172 #####################
173
174 if ($found) {
175     my $res = $messages->{'ResFound'};
176     $itemnumber = $res->{'itemnumber'};
177
178     if ( $res->{'ResFound'} eq "Waiting" ) {
179         $waiting = 1;
180     }
181     elsif ( $res->{'ResFound'} eq "Reserved" ) {
182         $reserved  = 1;
183         $biblionumber = $res->{'biblionumber'};
184     }
185 }
186
187 my @errmsgloop;
188 foreach my $code ( keys %$messages ) {
189     if ( $code ne 'WasTransfered' ) {
190         my %err;
191         if ( $code eq 'BadBarcode' ) {
192             $err{msg}        = $messages->{'BadBarcode'};
193             $err{errbadcode} = 1;
194         }
195         elsif ( $code eq "NotAllowed" ) {
196             warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
197             # Do we really want a error log message here? --atz
198             $err{errnotallowed} =  1;
199             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
200             $err{tbr}      = $tbr;
201             $err{code}     = $typecode;
202         }
203         elsif ( $code eq 'IsPermanent' ) {
204             $err{errispermanent} = 1;
205             $err{msg} = $messages->{'IsPermanent'};
206         }
207         elsif ( $code eq 'WasReturned' ) {
208             $err{errwasreturned} = 1;
209             $err{borrowernumber} = $messages->{'WasReturned'};
210             my $borrower = GetMember('borrowernumber'=>$messages->{'WasReturned'});
211             $err{title}      = $borrower->{'title'};
212             $err{firstname}  = $borrower->{'firstname'};
213             $err{surname}    = $borrower->{'surname'};
214             $err{cardnumber} = $borrower->{'cardnumber'};
215         }
216         $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
217         push( @errmsgloop, \%err );
218     }
219 }
220
221 # use Data::Dumper;
222 # warn "FINAL ============= ".Dumper(@trsfitemloop);
223 $template->param(
224     found                   => $found,
225     reserved                => $reserved,
226     waiting                 => $waiting,
227     borrowernumber          => $borrowernumber,
228     itemnumber              => $itemnumber,
229     barcode                 => $barcode,
230     biblionumber            => $biblionumber,
231     tobranchcd              => $tobranchcd,
232     reqmessage              => $reqmessage,
233     cancelled               => $cancelled,
234     setwaiting              => $setwaiting,
235     trsfitemloop            => \@trsfitemloop,
236     errmsgloop              => \@errmsgloop,
237     CircAutocompl           => C4::Context->preference("CircAutocompl")
238 );
239
240 # Checking if there is a Fast Cataloging Framework
241 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
242
243 # Checking if the transfer page needs to be displayed
244 $template->param( display_transfer => 1 ) if ( ($flags->{'superlibrarian'} == 1) || (C4::Context->preference("IndependentBranches") == 0) );
245
246 output_html_with_http_headers $query, $cookie, $template->output;
247