item rework: various changes
[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::Reserves;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Auth qw/:DEFAULT get_session/;
32 use C4::Branch; # GetBranches
33 use C4::Koha;
34
35 ###############################################
36 #  Getting state
37
38 my $query = new CGI;
39
40 if (!C4::Context->userenv){
41         my $sessionID = $query->cookie("CGISESSID");
42         my $session = get_session($sessionID);
43         if ($session->param('branch') eq 'NO_LIBRARY_SET'){
44                 # no branch set we can't transfer
45                 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
46                 exit;
47         }
48 }   
49
50
51 #######################################################################################
52 # Make the page .....
53 my ( $template, $cookie );
54 my $user;
55 ( $template, $user, $cookie ) = get_template_and_user(
56     {
57         template_name   => "circ/branchtransfers.tmpl",
58         query           => $query,
59         type            => "intranet",
60         authnotrequired => 0,
61         flagsrequired   => { circulate => 1 },
62     }
63 );
64
65 my $branches = GetBranches;
66 my $branch  = GetBranch( $query,  $branches );
67
68 my $messages;
69 my $found;
70 my $reserved;
71 my $waiting;
72 my $reqmessage;
73 my $cancelled;
74 my $setwaiting;
75
76 my $request        = $query->param('request');
77 my $borrowernumber = $query->param('borrowernumber');
78 my $tobranchcd     = $query->param('tobranchcd');
79
80 ############
81 # Deal with the requests....
82 if ( $request eq "KillWaiting" ) {
83     my $item = $query->param('itemnumber');
84
85     CancelReserve( 0, $item, $borrowernumber );
86     $cancelled   = 1;
87     $reqmessage  = 1;
88 }
89
90 my $ignoreRs = 0;
91 if ( $request eq "SetWaiting" ) {
92     my $item = $query->param('itemnumber');
93     ModReserveAffect( $item, $borrowernumber );
94     $ignoreRs    = 1;
95     $setwaiting  = 1;
96     $reqmessage  = 1;
97 }
98 if ( $request eq 'KillReserved' ) {
99     my $biblio = $query->param('biblionumber');
100     CancelReserve( $biblio, 0, $borrowernumber );
101     $cancelled   = 1;
102     $reqmessage  = 1;
103 }
104
105 # set up the branchselect options....
106 my @branchoptionloop;
107 foreach my $br ( keys %$branches ) {
108     my %branch;
109     $branch{selected} = ( $br eq $tobranchcd );
110     $branch{code}     = $br;
111     $branch{name}     = $branches->{$br}->{'branchname'};
112     push( @branchoptionloop, \%branch );
113 }
114
115 # collect the stack of books already transfered so they can printed...
116 my @trsfitemloop;
117 my %transfereditems;
118 my $transfered;
119 my $barcode = $query->param('barcode');
120 # strip whitespace
121 $barcode =~ s/\s*//g;
122 # warn "barcode : $barcode";
123 if ($barcode) {
124
125     my $iteminformation;
126     ( $transfered, $messages, $iteminformation ) =
127       transferbook( $tobranchcd, $barcode, $ignoreRs );
128 #       use Data::Dumper;
129 #       warn "Transfered : $transfered / ".Dumper($messages);
130     $found = $messages->{'ResFound'};
131     if ($transfered) {
132         my %item;
133         my $frbranchcd =  C4::Context->userenv->{'branch'};
134 #         if ( not($found) ) {
135         $item{'biblionumber'} = $iteminformation->{'biblionumber'};
136         $item{'title'}        = $iteminformation->{'title'};
137         $item{'author'}       = $iteminformation->{'author'};
138         $item{'itemtype'}     = $iteminformation->{'itemtype'};
139         $item{'ccode'}        = $iteminformation->{'ccode'};
140         $item{'frbrname'}     = $branches->{$frbranchcd}->{'branchname'};
141         $item{'tobrname'}     = $branches->{$tobranchcd}->{'branchname'};
142 #         }
143         $item{counter}  = 0;
144         $item{barcode}  = $barcode;
145         $item{frombrcd} = $frbranchcd;
146         $item{tobrcd}   = $tobranchcd;
147         push( @trsfitemloop, \%item );
148 #         warn Dumper(@trsfitemloop);
149     }
150 }
151
152 foreach ( $query->param ) {
153     (next) unless (/bc-(\d*)/);
154     my $counter = $1;
155     my %item;
156     my $bc    = $query->param("bc-$counter");
157     my $frbcd = $query->param("fb-$counter");
158     my $tobcd = $query->param("tb-$counter");
159     $counter++;
160     $item{counter}  = $counter;
161     $item{barcode}  = $bc;
162     $item{frombrcd} = $frbcd;
163     $item{tobrcd}   = $tobcd;
164     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
165     $item{'biblionumber'} = $iteminformation->{'biblionumber'};
166     $item{'title'}        = $iteminformation->{'title'};
167     $item{'author'}       = $iteminformation->{'author'};
168     $item{'itemtype'}     = $iteminformation->{'itemtype'};
169     $item{'ccode'}        = $iteminformation->{'ccode'};
170     $item{'frbrname'}     = $branches->{$frbcd}->{'branchname'};
171     $item{'tobrname'}     = $branches->{$tobcd}->{'branchname'};
172     push( @trsfitemloop, \%item );
173 }
174
175 my $itemnumber;
176 my $biblionumber;
177
178 #####################
179
180 if ($found) {
181     my $res = $messages->{'ResFound'};
182     $itemnumber = $res->{'itemnumber'};
183
184     if ( $res->{'ResFound'} eq "Waiting" ) {
185         $waiting = 1;
186     }
187     if ( $res->{'ResFound'} eq "Reserved" ) {
188         $reserved  = 1;
189         $biblionumber = $res->{'biblionumber'};
190     }
191 }
192
193 #####################
194
195 my @errmsgloop;
196 foreach my $code ( keys %$messages ) {
197     my %err;
198
199     if ( $code eq 'BadBarcode' ) {
200         $err{msg}        = $messages->{'BadBarcode'};
201         $err{errbadcode} = 1;
202     }
203
204     if ( $code eq 'IsPermanent' ) {
205         $err{errispermanent} = 1;
206         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
207     }
208     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
209
210     if ( $code eq 'WasReturned' ) {
211         $err{errwasreturned} = 1;
212     }
213     push( @errmsgloop, \%err );
214 }
215
216 # use Data::Dumper;
217 # warn "FINAL ============= ".Dumper(@trsfitemloop);
218 $template->param(
219     found                   => $found,
220     reserved                => $reserved,
221     waiting                 => $waiting,
222     borrowernumber          => $borrowernumber,
223     itemnumber              => $itemnumber,
224     barcode                 => $barcode,
225     biblionumber            => $biblionumber,
226     tobranchcd              => $tobranchcd,
227     reqmessage              => $reqmessage,
228     cancelled               => $cancelled,
229     setwaiting              => $setwaiting,
230     trsfitemloop            => \@trsfitemloop,
231     branchoptionloop        => \@branchoptionloop,
232     errmsgloop              => \@errmsgloop,
233     CircAutocompl           => C4::Context->preference("CircAutocompl")
234 );
235 output_html_with_http_headers $query, $cookie, $template->output;
236
237 sub name {
238     my ($borinfo) = @_;
239     return $borinfo->{'surname'} . " "
240       . $borinfo->{'title'} . " "
241       . $borinfo->{'firstname'};
242 }
243
244 # Local Variables:
245 # tab-width: 4
246 # End: