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