2 # NOTE: This file uses standard 8-character tabs
9 use C4::Auth; # checkauth, getborrowernumber.
11 use C4::Circulation::Circ2;
13 use C4::Interface::CGI::Output;
18 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
21 my ($template, $borrowernumber, $cookie)
22 = get_template_and_user({template_name => "opac-reserve.tmpl",
26 flagsrequired => {borrow => 1},
30 # get borrower information ....
31 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
35 # get biblionumber.....
36 my $biblionumber = $query->param('bib');
38 my $bibdata = bibdata($biblionumber);
39 $template->param($bibdata);
40 $template->param(biblionumber => $biblionumber);
42 # get the rank number....
43 my ($rank,$reserves) = FindReserves($biblionumber,'');
44 $template->param(reservecount => $rank);
46 foreach my $res (@$reserves) {
47 if ($res->{'found'} eq 'W') {
53 $template->param(rank => $rank);
55 # pass the pickup branch along....
56 my $branch = $query->param('branch');
57 $template->param(branch => $branch);
59 my $branches = GetBranches();
60 # make sure it's a real branch
61 if (!$branches->{$branch}) {
64 $template->param(branchname => $branches->{$branch}->{'branchname'});
66 # make branch selection options...
67 #my $branchoptions = '';
72 my @CGIbranchlooparray;
74 foreach my $branch (keys %$branches) {
77 $line{branch} = $branches->{$branch}->{'branchname'};
78 $line{value} = $branch;
79 push @CGIbranchlooparray, \%line;
82 @CGIbranchlooparray = sort {$a->{branch} cmp $b->{branch}} @CGIbranchlooparray;
83 my $CGIbranchloop = \@CGIbranchlooparray;
84 $template->param( CGIbranch => $CGIbranchloop);
86 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
87 # get the itemtype data....
88 my @items = ItemInfo(undef, $biblionumber, 'opac');
90 #######################################################
91 # old version, add so that old templates still work
93 foreach my $itm (@items) {
94 my $ity = $itm->{'itemtype'};
95 unless ($types_old {$ity}) {
96 $types_old{$ity}->{'itemtype'} = $ity;
97 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} = 1;
98 $types_old{$ity}->{'description'} = $itm->{'description'};
100 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} ++;
104 foreach my $type (values %types_old) {
106 foreach my $bc (keys %{$type->{'branchinfo'}}) {
107 $copies .= $branches->{$bc}->{'branchname'}."(".$type->{'branchinfo'}->{$bc}.")";
109 $type->{'copies'} = $copies;
112 my @types_old = values %types_old;
115 ################################
118 foreach my $itm (@items) {
119 push @temp, $itm if $itm->{'itemtype'};
122 my $itemcount = @items;
123 $template->param(itemcount => $itemcount);
128 foreach my $itm (@items) {
129 push @duedates, {date_due => format_date($itm->{'date_due'})} if defined $itm->{'date_due'};
130 $itm->{$itm->{'publictype'}} = 1;
131 # FIXME CalcReserveFee is supposed to be internal-use-only
132 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
133 $fee = sprintf "%.02f", $fee;
134 $itm->{'reservefee'} = $fee;
135 my $pty = $itm->{'publictype'};
136 $itemtypes{$itm->{'itemtype'}} = $itm;
137 unless ($types {$pty}) {
138 $types{$pty}->{'count'} = 1;
139 $types{$pty}->{$itm->{'itemtype'}} = 1;
140 push @{$types{$pty}->{'items'}}, $itm;
142 unless ($types{$pty}->{$itm->{'itemtype'}}) {
143 $types{$pty}->{'count'}++;
144 $types{$pty}->{$itm->{'itemtype'}} = 1;
145 push @{$types{$pty}->{'items'}}, $itm;
151 $template->param(ITEMS => \@duedates);
153 my $width = keys %types;
154 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
156 foreach my $pt (@publictypes) {
157 $typecount += $pt->{'count'};
159 $template->param(onlyone => 1) if $typecount == 1;
162 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
164 foreach my $pty (@publictypes) {
165 my @items = @{$pty->{'items'}};
166 push @row, $items[$rownum] if defined $items[$rownum];
169 $row[$last-1]->{'last'} =1 if $last == $width;
170 my $fill = ($width - $last)*2;
172 push @typerows, {ROW => \@row, fill => $fill};
174 $template->param(TYPE_ROWS => \@typerows);
175 $width = 2*$width -1;
176 $template->param(totalwidth => 2*$width-1,
179 if ($query->param('item_types_selected')) {
180 # this is what happens after the itemtypes have been selected. Stage 2
181 my @itemtypes = $query->param('itemtype');
186 foreach my $itmtype (@itemtypes) {
187 $newtypes{$itmtype} = $itemtypes{$itmtype};
189 my @types = values %newtypes;
190 $template->param(TYPES => \@types);
191 foreach my $type (@itemtypes) {
193 foreach my $item (@items) {
194 if ($item->{'itemtype'} eq $type) {
195 push @reqbibs, $item->{'biblioitemnumber'};
198 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
201 } elsif ($query->param('all')) {
202 $template->param(all => 1);
206 if ($proceed && $branch) {
207 $fee = sprintf "%.02f", $fee;
208 $template->param(fee => $fee,istherefee => $fee>0?1:0);
209 $template->param(item_types_selected => 1);
210 warn "Branch is ==$branch==";
211 $template->param(no_branch_selected => 1) unless ($branch!='');
213 $template->param(message => 1);
214 $template->param(no_items_selected => 1) unless ($proceed);
215 $template->param(no_branch_selected => 1) unless ($branch);
216 warn "Branch is ==$branch==";
218 } elsif ($query->param('place_reserve')) {
219 # here we actually do the reserveration. Stage 3.
220 my $title = $bibdata->{'title'};
221 my @itemtypes = $query->param('itemtype');
222 foreach my $type (@itemtypes) {
224 foreach my $item (@items) {
225 if ($item->{'itemtype'} eq $type) {
226 push @reqbibs, $item->{'biblioitemnumber'};
229 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
231 if ($query->param('all')) {
232 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
234 print $query->redirect("/cgi-bin/koha/opac-user.pl");
236 # Here we check that the borrower can actually make reserves Stage 1.
238 my $maxoutstanding = C4::Context->preference("maxoutstanding");
239 $template->param(noreserve => 1) unless $maxoutstanding;
240 if ($borr->{'amountoutstanding'} > $maxoutstanding) {
241 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
242 $template->param(message => 1);
244 $template->param(too_much_oweing => $amount);
246 if ($borr->{gonenoaddress} eq 1) {
248 $template->param(message => 1,
251 if ($borr->{lost} eq 1) {
253 $template->param(message => 1,
256 if ($borr->{debarred} eq 1) {
258 $template->param(message => 1,
261 my ($resnum, $reserves) = FindReserves('', $borrowernumber);
262 $template->param(RESERVES => $reserves);
263 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
264 $template->param(message => 1);
266 $template->param(too_many_reserves => $resnum);
268 foreach my $res (@$reserves) {
269 if ($res->{'biblionumber'} == $biblionumber) {
270 $template->param(message => 1);
272 $template->param(already_reserved => 1);
275 unless ($noreserves) {
276 $template->param(TYPES => \@types_old);
277 $template->param(select_item_types => 1);
281 # check that you can actually make the reserve.
283 output_html_with_http_headers $query, $cookie, $template->output;