Modifying Members : Add Mod and GetMember
[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 warnings;
26 use CGI;
27 use C4::Circulation;
28 use C4::Output;
29 use C4::Reserves;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Auth qw/:DEFAULT get_session/;
33 use C4::Branch; # GetBranches
34 use C4::Koha;
35 use C4::Members;
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) = get_template_and_user(
56     {
57         template_name   => "circ/branchtransfers.tmpl",
58         query           => $query,
59         type            => "intranet",
60         authnotrequired => 0,
61         flagsrequired   => { circulate => "circulate_remaining_permissions" },
62     }
63 );
64
65 my $branches = GetBranches;
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     CancelReserve( 0, $item, $borrowernumber );
85     $cancelled   = 1;
86     $reqmessage  = 1;
87 }
88 elsif ( $request eq "SetWaiting" ) {
89     my $item = $query->param('itemnumber');
90     ModReserveAffect( $item, $borrowernumber );
91     $ignoreRs    = 1;
92     $setwaiting  = 1;
93     $reqmessage  = 1;
94 }
95 elsif ( $request eq 'KillReserved' ) {
96     my $biblio = $query->param('biblionumber');
97     CancelReserve( $biblio, 0, $borrowernumber );
98     $cancelled   = 1;
99     $reqmessage  = 1;
100 }
101
102 # collect the stack of books already transfered so they can printed...
103 my @trsfitemloop;
104 my %transfereditems;
105 my $transfered;
106 my $barcode = $query->param('barcode');
107 # strip whitespace
108 defined $barcode and $barcode =~ s/\s*//g;  # FIXME: barcodeInputFilter
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     elsif ( $res->{'ResFound'} eq "Reserved" ) {
175         $reserved  = 1;
176         $biblionumber = $res->{'biblionumber'};
177     }
178 }
179
180 #####################
181
182 # Used for branch transfer limits error messages.
183 my $codeTypeDescription = 'Collection Code';
184 my $codeType = C4::Context->preference("BranchTransferLimitsType");
185 if ( $codeType eq 'itemtype' ) {   
186     $codeTypeDescription = 'Item Type';
187 }
188
189 my @errmsgloop;
190 foreach my $code ( keys %$messages ) {
191     my %err;
192     if ( $code eq 'BadBarcode' ) {
193         $err{msg}        = $messages->{'BadBarcode'};
194         $err{errbadcode} = 1;
195     }
196     elsif ( $code eq "NotAllowed" ) {
197         warn "NotAllowed: $messages->{'NotAllowed'} to  " . $branches->{ $messages->{'NotAllowed'} }->{'branchname'};
198         # Do we really want a error log message here? --atz
199         $err{errnotallowed} =  1;
200         my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
201         $err{tbr}      = $branches->{ $tbr }->{'branchname'};
202         $err{code}     = $typecode;
203         $err{codeType} = $codeTypeDescription; 
204     }
205     elsif ( $code eq 'IsPermanent' ) {
206         $err{errispermanent} = 1;
207         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
208     }
209     elsif ( $code eq 'WasReturned' ) {
210         $err{errwasreturned} = 1;
211                 $err{borrowernumber} = $messages->{'WasReturned'};
212                 my $borrower = GetMember('borrowernumber'=>$messages->{'WasReturned'});
213                 $err{title}      = $borrower->{'title'};
214                 $err{firstname}  = $borrower->{'firstname'};
215                 $err{surname}    = $borrower->{'surname'};
216                 $err{cardnumber} = $borrower->{'cardnumber'};
217     }
218     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
219     push( @errmsgloop, \%err );
220 }
221
222 # use Data::Dumper;
223 # warn "FINAL ============= ".Dumper(@trsfitemloop);
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     branchoptionloop        => GetBranchesLoop($tobranchcd),
238     errmsgloop              => \@errmsgloop,
239     CircAutocompl           => C4::Context->preference("CircAutocompl")
240 );
241 output_html_with_http_headers $query, $cookie, $template->output;
242