e2280a229b4e62b15c06306c25e9691c346db253
[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         authnotrequired => 0,
63         flagsrequired   => { circulate => "circulate_remaining_permissions" },
64     }
65 );
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') ||  0;
77 my $tobranchcd     = $query->param('tobranchcd')     || '';
78
79 my $ignoreRs = 0;
80 ############
81 # Deal with the requests....
82 if ( $request eq "KillWaiting" ) {
83     my $item = $query->param('itemnumber');
84     my $holds = Koha::Holds->search({
85         itemnumber     => $item,
86         borrowernumber => $borrowernumber
87     });
88     if ( $holds->count ) {
89         $holds->next->cancel;
90         $cancelled   = 1;
91         $reqmessage  = 1;
92     } # FIXME else?
93 }
94 elsif ( $request eq "SetWaiting" ) {
95     my $item = $query->param('itemnumber');
96     ModReserveAffect( $item, $borrowernumber );
97     $ignoreRs    = 1;
98     $setwaiting  = 1;
99     $reqmessage  = 1;
100 }
101 elsif ( $request eq 'KillReserved' ) {
102     my $biblionumber = $query->param('biblionumber');
103     my $holds = Koha::Holds->search({
104         biblionumber   => $biblionumber,
105         borrowernumber => $borrowernumber
106     });
107     if ( $holds->count ) {
108         $holds->next->cancel;
109         $cancelled   = 1;
110         $reqmessage  = 1;
111     } # FIXME else?
112 }
113
114 # collect the stack of books already transferred so they can printed...
115 my @trsfitemloop;
116 my $transferred;
117 my $barcode = $query->param('barcode');
118 # remove leading/trailing whitespace
119 defined $barcode and $barcode =~ s/^\s*|\s*$//g;  # FIXME: barcodeInputFilter
120 # warn "barcode : $barcode";
121 if ($barcode) {
122
123     ( $transferred, $messages ) =
124       transferbook( $tobranchcd, $barcode, $ignoreRs );
125     my $item = Koha::Items->find({ barcode => $barcode });
126     $found = $messages->{'ResFound'};
127     if ($transferred) {
128         my %item;
129         my $biblio = $item->biblio;
130         my $frbranchcd =  C4::Context->userenv->{'branch'};
131         $item{'biblionumber'}          = $item->biblionumber;
132         $item{'itemnumber'}            = $item->itemnumber;
133         $item{'title'}                 = $biblio->title;
134         $item{'author'}                = $biblio->author;
135         $item{'itemtype'}              = $biblio->biblioitem->itemtype;
136         $item{'ccode'}                 = $item->ccode;
137         $item{'itemcallnumber'}        = $item->itemcallnumber;
138         my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $item->location });
139         $item{'location'}              = $av->count ? $av->next->lib : '';
140         $item{counter}  = 0;
141         $item{barcode}  = $barcode;
142         $item{frombrcd} = $frbranchcd;
143         $item{tobrcd}   = $tobranchcd;
144         push( @trsfitemloop, \%item );
145     }
146 }
147
148 foreach ( $query->param ) {
149     (next) unless (/bc-(\d*)/);
150     my $counter = $1;
151     my %item;
152     my $bc    = $query->param("bc-$counter");
153     my $frbcd = $query->param("fb-$counter");
154     my $tobcd = $query->param("tb-$counter");
155     $counter++;
156     $item{counter}  = $counter;
157     $item{barcode}  = $bc;
158     $item{frombrcd} = $frbcd;
159     $item{tobrcd}   = $tobcd;
160     my $item = Koha::Items->find({ barcode => $bc });
161     my $biblio = $item->biblio;
162     $item{'biblionumber'}          = $item->biblionumber;
163     $item{'itemnumber'}            = $item->itemnumber;
164     $item{'title'}                 = $biblio->title;
165     $item{'author'}                = $biblio->author;
166     $item{'itemtype'}              = $biblio->biblioitem->itemtype;
167     $item{'ccode'}                 = $item->ccode;
168     $item{'itemcallnumber'}        = $item->itemcallnumber;
169     my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $item->location });
170     $item{'location'}              = $av->count ? $av->next->lib : '';
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     elsif ( $res->{'ResFound'} eq "Reserved" ) {
187         $reserved  = 1;
188         $biblionumber = $res->{'biblionumber'};
189     }
190 }
191
192 my @errmsgloop;
193 foreach my $code ( keys %$messages ) {
194     if ( $code ne 'WasTransfered' ) {
195         my %err;
196         if ( $code eq 'BadBarcode' ) {
197             $err{msg}        = $messages->{'BadBarcode'};
198             $err{errbadcode} = 1;
199         }
200         elsif ( $code eq "NotAllowed" ) {
201             warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
202             # Do we really want a error log message here? --atz
203             $err{errnotallowed} =  1;
204             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
205             $err{tbr}      = $tbr;
206             $err{code}     = $typecode;
207         }
208         elsif ( $code eq 'WasReturned' ) {
209             $err{errwasreturned} = 1;
210             $err{borrowernumber} = $messages->{'WasReturned'};
211             my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
212             if ( $patron ) { # Just in case...
213                 $err{patron} = $patron;
214             }
215         }
216         $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
217         push( @errmsgloop, \%err );
218     }
219 }
220
221 # use Data::Dumper;
222 # warn "FINAL ============= ".Dumper(@trsfitemloop);
223
224 $template->param(
225     found                   => $found,
226     reserved                => $reserved,
227     waiting                 => $waiting,
228     borrowernumber          => $borrowernumber,
229     itemnumber              => $itemnumber,
230     barcode                 => $barcode,
231     biblionumber            => $biblionumber,
232     tobranchcd              => $tobranchcd,
233     reqmessage              => $reqmessage,
234     cancelled               => $cancelled,
235     setwaiting              => $setwaiting,
236     trsfitemloop            => \@trsfitemloop,
237     errmsgloop              => \@errmsgloop,
238     CircAutocompl           => C4::Context->preference("CircAutocompl"),
239 );
240
241 # Checking if there is a Fast Cataloging Framework
242 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
243
244 output_html_with_http_headers $query, $cookie, $template->output;
245