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;
17 my $MAXIMUM_NUMBER_OF_RESERVES = 5;
19 my $MAXIMUM_NUMBER_OF_RESERVES = 10;
22 my ($template, $borrowernumber, $cookie)
23 = get_template_and_user({template_name => "opac-reserve.tmpl",
27 flagsrequired => {borrow => 1},
31 # get borrower information ....
32 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
35 $template->param(BORROWER_INFO => \@bordat);
37 # get biblionumber.....
38 my $biblionumber = $query->param('bib');
39 $template->param(biblionumber => $biblionumber);
41 my $bibdata = bibdata($biblionumber);
42 $template->param($bibdata);
44 # get the rank number....
45 my ($rank,$reserves) = FindReserves($biblionumber);
46 $template->param(reservecount => $rank);
48 foreach my $res (@$reserves) {
49 if ($res->{'found'} eq 'W') {
57 $template->param(rank => $rank);
60 # pass the pickup branch along....
61 my $branch = $query->param('branch');
62 $template->param(branch => $branch);
64 my $branches = getbranches();
65 $template->param(branchname => $branches->{$branch}->{'branchname'});
68 # make branch selection options...
69 my $branchoptions = '';
71 foreach my $br (keys %$branches) {
72 #(next) unless $branches->{$br}->{'IS'}; # FIXME disabled to fix bug 202
75 $selected = "selected";
77 $branchoptions .= "<option value=$br $selected>$branches->{$br}->{'branchname'}</option>\n";
78 push @branches, {branchcode => $br, branchname => $branches->{$br}->{'branchname'}, selected => $selected};
80 $template->param( branchoptions => $branchoptions);
81 $template->param(BRANCHES => \@branches);
83 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
84 # get the itemtype data....
85 my @items = ItemInfo(undef, $biblionumber, 'opac');
87 #######################################################
88 # old version, add so that old templates still work
90 foreach my $itm (@items) {
91 my $ity = $itm->{'itemtype'};
92 unless ($types_old {$ity}) {
93 $types_old{$ity}->{'itemtype'} = $ity;
94 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} = 1;
95 $types_old{$ity}->{'description'} = $itm->{'description'};
97 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} ++;
101 foreach my $type (values %types_old) {
103 foreach my $bc (keys %{$type->{'branchinfo'}}) {
104 $copies .= $branches->{$bc}->{'branchname'}."(".$type->{'branchinfo'}->{$bc}.")";
106 $type->{'copies'} = $copies;
109 my @types_old = values %types_old;
112 ################################
115 foreach my $itm (@items) {
116 push @temp, $itm if $itm->{'itemtype'};
119 my $itemcount = @items;
120 $template->param(itemcount => $itemcount);
125 foreach my $itm (@items) {
126 push @duedates, {date_due => format_date($itm->{'date_due'})} if defined $itm->{'date_due'};
127 $itm->{$itm->{'publictype'}} = 1;
128 # FIXME CalcReserveFee is supposed to be internal-use-only
129 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
130 $fee = sprintf "%.02f", $fee;
131 $itm->{'reservefee'} = $fee;
132 my $pty = $itm->{'publictype'};
133 $itemtypes{$itm->{'itemtype'}} = $itm;
134 unless ($types {$pty}) {
135 $types{$pty}->{'count'} = 1;
136 $types{$pty}->{$itm->{'itemtype'}} = 1;
137 push @{$types{$pty}->{'items'}}, $itm;
139 unless ($types{$pty}->{$itm->{'itemtype'}}) {
140 $types{$pty}->{'count'}++;
141 $types{$pty}->{$itm->{'itemtype'}} = 1;
142 push @{$types{$pty}->{'items'}}, $itm;
148 $template->param(ITEMS => \@duedates);
150 my $width = keys %types;
151 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
153 foreach my $pt (@publictypes) {
154 $typecount += $pt->{'count'};
156 $template->param(onlyone => 1) if $typecount == 1;
159 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
161 foreach my $pty (@publictypes) {
162 my @items = @{$pty->{'items'}};
163 push @row, $items[$rownum] if defined $items[$rownum];
166 $row[$last-1]->{'last'} =1 if $last == $width;
167 my $fill = ($width - $last)*2;
169 push @typerows, {ROW => \@row, fill => $fill};
171 $template->param(TYPE_ROWS => \@typerows);
172 $width = 2*$width -1;
173 $template->param(totalwidth => 2*$width-1);
175 if ($query->param('item_types_selected')) {
176 # this is what happens after the itemtypes have been selected. Stage 2
177 my @itemtypes = $query->param('itemtype');
182 foreach my $itmtype (@itemtypes) {
183 $newtypes{$itmtype} = $itemtypes{$itmtype};
185 my @types = values %newtypes;
186 $template->param(TYPES => \@types);
187 foreach my $type (@itemtypes) {
189 foreach my $item (@items) {
190 if ($item->{'itemtype'} eq $type) {
191 push @reqbibs, $item->{'biblioitemnumber'};
194 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
197 } elsif ($query->param('all')) {
198 $template->param(all => 1);
203 $fee = sprintf "%.02f", $fee;
204 $template->param(fee => $fee);
205 $template->param(item_types_selected => 1);
207 $template->param(message => 1);
208 $template->param(no_items_selected => 1);
212 } elsif ($query->param('place_reserve')) {
213 # here we actually do the reserveration. Stage 3.
214 my $title = $bibdata->{'title'};
215 my @itemtypes = $query->param('itemtype');
216 foreach my $type (@itemtypes) {
218 foreach my $item (@items) {
219 if ($item->{'itemtype'} eq $type) {
220 push @reqbibs, $item->{'biblioitemnumber'};
223 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
225 if ($query->param('all')) {
226 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
228 print $query->redirect("/cgi-bin/koha/opac-user.pl");
230 # Here we check that the borrower can actually make reserves Stage 1.
232 if ($borr->{'amountoutstanding'} > 5) {
233 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
234 $template->param(message => 1);
236 $template->param(too_much_oweing => $amount);
238 my ($resnum, $reserves) = FindReserves(undef, $borrowernumber);
239 $template->param(RESERVES => $reserves);
240 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
241 $template->param(message => 1);
243 $template->param(too_many_reserves => $resnum);
245 foreach my $res (@$reserves) {
246 if ($res->{'biblionumber'} == $biblionumber) {
247 $template->param(message => 1);
249 $template->param(already_reserved => 1);
252 unless ($noreserves) {
253 $template->param(TYPES => \@types_old);
254 $template->param(select_item_types => 1);
258 # check that you can actually make the reserve.
260 output_html_with_http_headers $query, $cookie, $template->output;