improving issues_stats by adding location & itemcallnumber filters
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2 # WARNING: This file uses 4-character tabs!
3
4 #written 11/3/2002 by Finlay
5 #script to execute branch transfers of books
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Circulation;
27 use C4::Output;
28 use C4::Reserves2;
29 use C4::Biblio;
30 use C4::Auth;
31 use C4::Interface::CGI::Output;
32 use C4::Branch; # GetBranches
33 use C4::Koha;
34
35 ###############################################
36 #  Getting state
37
38 my $query = new CGI;
39
40 #######################################################################################
41 # Make the page .....
42 my ( $template, $cookie );
43 my $user;
44 ( $template, $user, $cookie ) = get_template_and_user(
45     {
46         template_name   => "circ/branchtransfers.tmpl",
47         query           => $query,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { circulate => 1 },
51     }
52 );
53
54 my $branches = GetBranches;
55 my $branch  = GetBranch( $query,  $branches );
56
57 my $messages;
58 my $found;
59 my $reserved;
60 my $waiting;
61 my $reqmessage;
62 my $cancelled;
63 my $setwaiting;
64
65 my $request        = $query->param('request');
66 my $borrowernumber = $query->param('borrowernumber');
67 my $tobranchcd     = $query->param('tobranchcd');
68
69 ############
70 # Deal with the requests....
71 if ( $request eq "KillWaiting" ) {
72     my $item = $query->param('itemnumber');
73
74     CancelReserve( 0, $item, $borrowernumber );
75     $cancelled   = 1;
76     $reqmessage  = 1;
77 }
78
79 my $ignoreRs = 0;
80 if ( $request eq "SetWaiting" ) {
81     my $item = $query->param('itemnumber');
82     $tobranchcd  = ReserveWaiting( $item, $borrowernumber );
83     $ignoreRs    = 1;
84     $setwaiting  = 1;
85     $reqmessage  = 1;
86 }
87 if ( $request eq 'KillReserved' ) {
88     my $biblio = $query->param('biblionumber');
89     CancelReserve( $biblio, 0, $borrowernumber );
90     $cancelled   = 1;
91     $reqmessage  = 1;
92 }
93
94 # set up the branchselect options....
95 my @branchoptionloop;
96 foreach my $br ( keys %$branches ) {
97     my %branch;
98     $branch{selected} = ( $br eq $tobranchcd );
99     $branch{code}     = $br;
100     $branch{name}     = $branches->{$br}->{'branchname'};
101     push( @branchoptionloop, \%branch );
102 }
103
104 # collect the stack of books already transfered so they can printed...
105 my @trsfitemloop;
106 my %transfereditems;
107 my $transfered;
108 my $barcode = $query->param('barcode');
109 # warn "barcode : $barcode";
110 if ($barcode) {
111
112     my $iteminformation;
113     ( $transfered, $messages, $iteminformation ) =
114       transferbook( $tobranchcd, $barcode, $ignoreRs );
115 #       use Data::Dumper;
116 #       warn "Transfered : $transfered / ".Dumper($messages);
117     $found = $messages->{'ResFound'};
118     if ($transfered) {
119         my %item;
120         my $frbranchcd =  C4::Context->userenv->{'branch'};
121 #         if ( not($found) ) {
122         $item{'biblionumber'} = $iteminformation->{'biblionumber'};
123         $item{'title'}        = $iteminformation->{'title'};
124         $item{'author'}       = $iteminformation->{'author'};
125         $item{'itemtype'}     = $iteminformation->{'itemtype'};
126         $item{'ccode'}        = $iteminformation->{'ccode'};
127         $item{'frbrname'}     = $branches->{$frbranchcd}->{'branchname'};
128         $item{'tobrname'}     = $branches->{$tobranchcd}->{'branchname'};
129 #         }
130         $item{counter}  = 0;
131         $item{barcode}  = $barcode;
132         $item{frombrcd} = $frbranchcd;
133         $item{tobrcd}   = $tobranchcd;
134         push( @trsfitemloop, \%item );
135 #         warn Dumper(@trsfitemloop);
136     }
137 }
138
139 foreach ( $query->param ) {
140     (next) unless (/bc-(\d*)/);
141     my $counter = $1;
142     my %item;
143     my $bc    = $query->param("bc-$counter");
144     my $frbcd = $query->param("fb-$counter");
145     my $tobcd = $query->param("tb-$counter");
146     $counter++;
147     $item{counter}  = $counter;
148     $item{barcode}  = $bc;
149     $item{frombrcd} = $frbcd;
150     $item{tobrcd}   = $tobcd;
151     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
152     $item{'biblionumber'} = $iteminformation->{'biblionumber'};
153     $item{'title'}        = $iteminformation->{'title'};
154     $item{'author'}       = $iteminformation->{'author'};
155     $item{'itemtype'}     = $iteminformation->{'itemtype'};
156     $item{'ccode'}        = $iteminformation->{'ccode'};
157     $item{'frbrname'}     = $branches->{$frbcd}->{'branchname'};
158     $item{'tobrname'}     = $branches->{$tobcd}->{'branchname'};
159     push( @trsfitemloop, \%item );
160 }
161
162 my $itemnumber;
163 my $biblionumber;
164
165 #####################
166
167 if ($found) {
168     my $res = $messages->{'ResFound'};
169     $itemnumber = $res->{'itemnumber'};
170
171     if ( $res->{'ResFound'} eq "Waiting" ) {
172         $waiting = 1;
173     }
174     if ( $res->{'ResFound'} eq "Reserved" ) {
175         $reserved  = 1;
176         $biblionumber = $res->{'biblionumber'};
177     }
178 }
179
180 #####################
181
182 my @errmsgloop;
183 foreach my $code ( keys %$messages ) {
184     my %err;
185
186     if ( $code eq 'BadBarcode' ) {
187         $err{msg}        = $messages->{'BadBarcode'};
188         $err{errbadcode} = 1;
189     }
190
191     if ( $code eq 'IsPermanent' ) {
192         $err{errispermanent} = 1;
193         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
194     }
195     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
196
197     if ( $code eq 'WasReturned' ) {
198         $err{errwasreturned} = 1;
199     }
200     push( @errmsgloop, \%err );
201 }
202
203 # use Data::Dumper;
204 # warn "FINAL ============= ".Dumper(@trsfitemloop);
205 $template->param(
206     found                   => $found,
207     reserved                => $reserved,
208     waiting                 => $waiting,
209     borrowernumber          => $borrowernumber,
210     itemnumber              => $itemnumber,
211     barcode                 => $barcode,
212     biblionumber            => $biblionumber,
213     tobranchcd              => $tobranchcd,
214     reqmessage              => $reqmessage,
215     cancelled               => $cancelled,
216     setwaiting              => $setwaiting,
217     trsfitemloop            => \@trsfitemloop,
218     branchoptionloop        => \@branchoptionloop,
219     errmsgloop              => \@errmsgloop,
220 );
221 output_html_with_http_headers $query, $cookie, $template->output;
222
223 sub name {
224     my ($borinfo) = @_;
225     return $borinfo->{'surname'} . " "
226       . $borinfo->{'title'} . " "
227       . $borinfo->{'firstname'};
228 }
229
230 # Local Variables:
231 # tab-width: 4
232 # End: