Bug 24488: [DISCUSSION] For comparaison
[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 = CGI->new;
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         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
130         transferbook({
131             from_branch => C4::Context->userenv->{'branch'},
132             to_branch => $tobranchcd,
133             barcode => $barcode,
134             ignore_reserves => $ignoreRs,
135             trigger => 'Manual'
136         });
137     my $item = Koha::Items->find({ barcode => $barcode });
138     $found = $messages->{'ResFound'};
139     if ($transferred) {
140         my %trsfitem;
141         my $frbranchcd =  C4::Context->userenv->{'branch'};
142         $trsfitem{item}     = $item;
143         $trsfitem{counter}  = 0;
144         $trsfitem{frombrcd} = $frbranchcd;
145         $trsfitem{tobrcd}   = $tobranchcd;
146         push( @trsfitemloop, \%trsfitem );
147     }
148 }
149
150 foreach ( $query->param ) {
151     (next) unless (/bc-(\d*)/);
152     my $counter = $1;
153     my %trsfitem;
154     my $bc    = $query->param("bc-$counter");
155     my $frbcd = $query->param("fb-$counter");
156     my $tobcd = $query->param("tb-$counter");
157     $counter++;
158     $trsfitem{counter}  = $counter;
159     $trsfitem{frombrcd} = $frbcd;
160     $trsfitem{tobrcd}   = $tobcd;
161     my $item = Koha::Items->find({ barcode => $bc });
162     $trsfitem{item}     = $item;
163     push( @trsfitemloop, \%trsfitem );
164 }
165
166 my $itemnumber;
167 my $biblionumber;
168
169 #####################
170
171 if ($found) {
172     my $res = $messages->{'ResFound'};
173     $itemnumber = $res->{'itemnumber'};
174     $borrowernumber = $res->{'borrowernumber'};
175
176     if ( $res->{'ResFound'} eq "Waiting" ) {
177         $waiting = 1;
178     }
179     elsif ( $res->{'ResFound'} eq "Reserved" ) {
180         $reserved  = 1;
181         $biblionumber = $res->{'biblionumber'};
182     }
183 }
184
185 my @errmsgloop;
186 foreach my $code ( keys %$messages ) {
187     if ( $code ne 'WasTransfered' ) {
188         my %err;
189         if ( $code eq 'BadBarcode' ) {
190             $err{msg}        = $messages->{'BadBarcode'};
191             $err{errbadcode} = 1;
192         }
193         elsif ( $code eq "NotAllowed" ) {
194             warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
195             # Do we really want a error log message here? --atz
196             $err{errnotallowed} =  1;
197             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
198             $err{tbr}      = $tbr;
199             $err{code}     = $typecode;
200         }
201         elsif ( $code eq 'WasReturned' ) {
202             $err{errwasreturned} = 1;
203             $err{borrowernumber} = $messages->{'WasReturned'};
204             my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
205             if ( $patron ) { # Just in case...
206                 $err{patron} = $patron;
207             }
208         }
209         $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
210         push( @errmsgloop, \%err );
211     }
212 }
213
214 # use Data::Dumper;
215 # warn "FINAL ============= ".Dumper(@trsfitemloop);
216
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     errmsgloop              => \@errmsgloop,
231     PatronAutoComplete    => C4::Context->preference("PatronAutoComplete"),
232 );
233
234 # Checking if there is a Fast Cataloging Framework
235 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
236
237 output_html_with_http_headers $query, $cookie, $template->output;
238