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