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;
16 my $MAXIMUM_NUMBER_OF_RESERVES = 5;
19 my ($template, $borrowernumber, $cookie)
20 = get_template_and_user({template_name => "opac-reserve.tmpl",
24 flagsrequired => {borrow => 1},
28 # get borrower information ....
29 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
32 $template->param(BORROWER_INFO => \@bordat);
34 # get biblionumber.....
35 my $biblionumber = $query->param('bib');
36 $template->param(biblionumber => $biblionumber);
38 my $bibdata = bibdata($biblionumber);
39 $template->param($bibdata);
41 # get the rank number....
42 my ($rank,$reserves) = FindReserves($biblionumber);
43 $template->param(reservecount => $rank);
45 foreach my $res (@$reserves) {
46 if ($res->{'found'} eq 'W') {
54 $template->param(rank => $rank);
57 # pass the pickup branch along....
58 my $branch = $query->param('branch');
59 $template->param(branch => $branch);
61 my $branches = getbranches();
62 $template->param(branchname => $branches->{$branch}->{'branchname'});
65 # make branch selection options...
66 my $branchoptions = '';
68 foreach my $br (keys %$branches) {
69 #(next) unless $branches->{$br}->{'IS'}; # FIXME disabled to fix bug 202
72 $selected = "selected";
74 $branchoptions .= "<option value=$br $selected>$branches->{$br}->{'branchname'}</option>\n";
75 push @branches, {branchcode => $br, branchname => $branches->{$br}->{'branchname'}, selected => $selected};
77 $template->param( branchoptions => $branchoptions);
78 $template->param(BRANCHES => \@branches);
80 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
81 # get the itemtype data....
82 my @items = ItemInfo(undef, $biblionumber, 'opac');
84 foreach my $itm (@items) {
85 push @temp, $itm if $itm->{'itemtype'};
88 my $itemcount = @items;
89 $template->param(itemcount => $itemcount);
94 foreach my $itm (@items) {
95 push @duedates, {date_due => slashifyDate($itm->{'date_due'})} if defined $itm->{'date_due'};
96 $itm->{$itm->{'publictype'}} = 1;
97 # FIXME CalcReserveFee is supposed to be internal-use-only
98 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
99 $fee = sprintf "%.02f", $fee;
100 $itm->{'reservefee'} = $fee;
101 my $pty = $itm->{'publictype'};
102 $itemtypes{$itm->{'itemtype'}} = $itm;
103 unless ($types {$pty}) {
104 $types{$pty}->{'count'} = 1;
105 $types{$pty}->{$itm->{'itemtype'}} = 1;
106 push @{$types{$pty}->{'items'}}, $itm;
108 unless ($types{$pty}->{$itm->{'itemtype'}}) {
109 $types{$pty}->{'count'}++;
110 $types{$pty}->{$itm->{'itemtype'}} = 1;
111 push @{$types{$pty}->{'items'}}, $itm;
117 $template->param(ITEMS => \@duedates);
119 my $width = keys %types;
120 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
122 foreach my $pt (@publictypes) {
123 $typecount += $pt->{'count'};
125 $template->param(onlyone => 1) if $typecount == 1;
128 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
130 foreach my $pty (@publictypes) {
131 my @items = @{$pty->{'items'}};
132 push @row, $items[$rownum] if defined $items[$rownum];
135 $row[$last-1]->{'last'} =1 if $last == $width;
136 my $fill = ($width - $last)*2;
138 push @typerows, {ROW => \@row, fill => $fill};
140 $template->param(TYPE_ROWS => \@typerows);
141 $width = 2*$width -1;
142 $template->param(totalwidth => 2*$width-1);
144 if ($query->param('item_types_selected')) {
145 # this is what happens after the itemtypes have been selected. Stage 2
146 my @itemtypes = $query->param('itemtype');
151 foreach my $itmtype (@itemtypes) {
152 $newtypes{$itmtype} = $itemtypes{$itmtype};
154 my @types = values %newtypes;
155 $template->param(TYPES => \@types);
156 foreach my $type (@itemtypes) {
158 foreach my $item (@items) {
159 if ($item->{'itemtype'} eq $type) {
160 push @reqbibs, $item->{'biblioitemnumber'};
163 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
166 } elsif ($query->param('all')) {
167 $template->param(all => 1);
172 $fee = sprintf "%.02f", $fee;
173 $template->param(fee => $fee);
174 $template->param(item_types_selected => 1);
176 $template->param(message => 1);
177 $template->param(no_items_selected => 1);
181 } elsif ($query->param('place_reserve')) {
182 # here we actually do the reserveration. Stage 3.
183 my $title = $bibdata->{'title'};
184 my @itemtypes = $query->param('itemtype');
185 foreach my $type (@itemtypes) {
187 foreach my $item (@items) {
188 if ($item->{'itemtype'} eq $type) {
189 push @reqbibs, $item->{'biblioitemnumber'};
192 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
194 if ($query->param('all')) {
195 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
197 print $query->redirect("/cgi-bin/koha/opac-user.pl");
199 # Here we check that the borrower can actually make reserves Stage 1.
201 if ($borr->{'amountoutstanding'} > 5) {
202 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
203 $template->param(message => 1);
205 $template->param(too_much_oweing => $amount);
207 my ($resnum, $reserves) = FindReserves(undef, $borrowernumber);
208 $template->param(RESERVES => $reserves);
209 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
210 $template->param(message => 1);
212 $template->param(too_many_reserves => $resnum);
214 foreach my $res (@$reserves) {
215 if ($res->{'biblionumber'} == $biblionumber) {
216 $template->param(message => 1);
218 $template->param(already_reserved => 1);
221 unless ($noreserves) {
222 $template->param(select_item_types => 1);
225 # check that you can actually make the reserve.
227 $template->param(BIBLIOITEMS => \@data);
230 output_html_with_http_headers $query, $cookie, $template->output;