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);
87 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
88 # get the itemtype data....
89 my @items = ItemInfo(undef, $biblionumber, 'opac');
91 #######################################################
92 # old version, add so that old templates still work
94 foreach my $itm (@items) {
95 my $ity = $itm->{'itemtype'};
96 unless ($types_old {$ity}) {
97 $types_old{$ity}->{'itemtype'} = $ity;
98 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} = 1;
99 $types_old{$ity}->{'description'} = $itm->{'description'};
101 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} ++;
105 foreach my $type (values %types_old) {
107 foreach my $bc (keys %{$type->{'branchinfo'}}) {
108 $copies .= $branches->{$bc}->{'branchname'}."(".$type->{'branchinfo'}->{$bc}.")";
110 $type->{'copies'} = $copies;
113 my @types_old = values %types_old;
116 ################################
119 foreach my $itm (@items) {
120 push @temp, $itm if $itm->{'itemtype'};
123 my $itemcount = @items;
124 $template->param(itemcount => $itemcount);
129 foreach my $itm (@items) {
130 push @duedates, {date_due => format_date($itm->{'date_due'})} if defined $itm->{'date_due'};
131 $itm->{$itm->{'publictype'}} = 1;
132 # FIXME CalcReserveFee is supposed to be internal-use-only
133 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
134 $fee = sprintf "%.02f", $fee;
135 $itm->{'reservefee'} = $fee;
136 my $pty = $itm->{'publictype'};
137 $itemtypes{$itm->{'itemtype'}} = $itm;
138 unless ($types {$pty}) {
139 $types{$pty}->{'count'} = 1;
140 $types{$pty}->{$itm->{'itemtype'}} = 1;
141 push @{$types{$pty}->{'items'}}, $itm;
143 unless ($types{$pty}->{$itm->{'itemtype'}}) {
144 $types{$pty}->{'count'}++;
145 $types{$pty}->{$itm->{'itemtype'}} = 1;
146 push @{$types{$pty}->{'items'}}, $itm;
152 $template->param(ITEMS => \@duedates);
154 my $width = keys %types;
155 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
157 foreach my $pt (@publictypes) {
158 $typecount += $pt->{'count'};
160 $template->param(onlyone => 1) if $typecount == 1;
163 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
165 foreach my $pty (@publictypes) {
166 my @items = @{$pty->{'items'}};
167 push @row, $items[$rownum] if defined $items[$rownum];
170 $row[$last-1]->{'last'} =1 if $last == $width;
171 my $fill = ($width - $last)*2;
173 push @typerows, {ROW => \@row, fill => $fill};
175 $template->param(TYPE_ROWS => \@typerows);
176 $width = 2*$width -1;
177 $template->param(totalwidth => 2*$width-1,
178 LibraryName => C4::Context->preference("LibraryName"),
179 suggestion => C4::Context->preference("suggestion"),
180 virtualshelves => C4::Context->preference("virtualshelves"),
183 if ($query->param('item_types_selected')) {
184 # this is what happens after the itemtypes have been selected. Stage 2
185 my @itemtypes = $query->param('itemtype');
190 foreach my $itmtype (@itemtypes) {
191 $newtypes{$itmtype} = $itemtypes{$itmtype};
193 my @types = values %newtypes;
194 $template->param(TYPES => \@types);
195 foreach my $type (@itemtypes) {
197 foreach my $item (@items) {
198 if ($item->{'itemtype'} eq $type) {
199 push @reqbibs, $item->{'biblioitemnumber'};
202 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
205 } elsif ($query->param('all')) {
206 $template->param(all => 1);
210 if ($proceed && $branch) {
211 $fee = sprintf "%.02f", $fee;
212 $template->param(fee => $fee,istherefee => $fee>0?1:0);
213 $template->param(item_types_selected => 1);
214 warn "Branch is ==$branch==";
215 $template->param(no_branch_selected => 1) unless ($branch!='');
217 $template->param(message => 1);
218 $template->param(no_items_selected => 1) unless ($proceed);
219 $template->param(no_branch_selected => 1) unless ($branch);
220 warn "Branch is ==$branch==";
222 } elsif ($query->param('place_reserve')) {
223 # here we actually do the reserveration. Stage 3.
224 my $title = $bibdata->{'title'};
225 my @itemtypes = $query->param('itemtype');
226 foreach my $type (@itemtypes) {
228 foreach my $item (@items) {
229 if ($item->{'itemtype'} eq $type) {
230 push @reqbibs, $item->{'biblioitemnumber'};
233 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
235 if ($query->param('all')) {
236 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
238 print $query->redirect("/cgi-bin/koha/opac-user.pl");
240 # Here we check that the borrower can actually make reserves Stage 1.
242 my $maxoutstanding = C4::Context->preference("maxoustanding");
243 $template->param(noreserve => 1) unless $maxoutstanding;
244 if ($borr->{'amountoutstanding'} > $maxoutstanding) {
245 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
246 $template->param(message => 1);
248 $template->param(too_much_oweing => $amount);
250 if ($borr->{gonenoaddress} eq 1) {
252 $template->param(message => 1,
255 if ($borr->{lost} eq 1) {
257 $template->param(message => 1,
260 if ($borr->{debarred} eq 1) {
262 $template->param(message => 1,
265 my ($resnum, $reserves) = FindReserves('', $borrowernumber);
266 $template->param(RESERVES => $reserves);
267 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
268 $template->param(message => 1);
270 $template->param(too_many_reserves => $resnum);
272 foreach my $res (@$reserves) {
273 if ($res->{'biblionumber'} == $biblionumber) {
274 $template->param(message => 1);
276 $template->param(already_reserved => 1);
279 unless ($noreserves) {
280 $template->param(TYPES => \@types_old);
281 $template->param(select_item_types => 1);
285 # check that you can actually make the reserve.
287 output_html_with_http_headers $query, $cookie, $template->output;