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