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;
20 my ($template, $borrowernumber, $cookie)
21 = get_template_and_user({template_name => "opac-reserve.tmpl",
25 flagsrequired => {borrow => 1},
29 # get borrower information ....
30 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
34 # get biblionumber.....
35 my $biblionumber = $query->param('bib');
37 my $bibdata = bibdata($biblionumber);
38 $template->param($bibdata);
39 $template->param(BORROWER_INFO => \@bordat, biblionumber => $biblionumber);
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);
56 # pass the pickup branch along....
57 my $branch = $query->param('branch');
58 $template->param(branch => $branch);
60 my $branches = getbranches();
61 $template->param(branchname => $branches->{$branch}->{'branchname'});
64 # make branch selection options...
65 #my $branchoptions = '';
70 foreach my $branch (keys %$branches) {
71 push @select_branch, $branch;
72 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
74 my $CGIbranch=CGI::scrolling_list( -name => 'branch',
75 -values => \@select_branch,
76 -labels => \%select_branches,
79 $template->param( CGIbranch => $CGIbranch);
81 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
82 # get the itemtype data....
83 my @items = ItemInfo(undef, $biblionumber, 'opac');
85 #######################################################
86 # old version, add so that old templates still work
88 foreach my $itm (@items) {
89 my $ity = $itm->{'itemtype'};
90 unless ($types_old {$ity}) {
91 $types_old{$ity}->{'itemtype'} = $ity;
92 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} = 1;
93 $types_old{$ity}->{'description'} = $itm->{'description'};
95 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} ++;
99 foreach my $type (values %types_old) {
101 foreach my $bc (keys %{$type->{'branchinfo'}}) {
102 $copies .= $branches->{$bc}->{'branchname'}."(".$type->{'branchinfo'}->{$bc}.")";
104 $type->{'copies'} = $copies;
107 my @types_old = values %types_old;
110 ################################
113 foreach my $itm (@items) {
114 push @temp, $itm if $itm->{'itemtype'};
117 my $itemcount = @items;
118 $template->param(itemcount => $itemcount);
123 foreach my $itm (@items) {
124 push @duedates, {date_due => format_date($itm->{'date_due'})} if defined $itm->{'date_due'};
125 $itm->{$itm->{'publictype'}} = 1;
126 # FIXME CalcReserveFee is supposed to be internal-use-only
127 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
128 $fee = sprintf "%.02f", $fee;
129 $itm->{'reservefee'} = $fee;
130 my $pty = $itm->{'publictype'};
131 $itemtypes{$itm->{'itemtype'}} = $itm;
132 unless ($types {$pty}) {
133 $types{$pty}->{'count'} = 1;
134 $types{$pty}->{$itm->{'itemtype'}} = 1;
135 push @{$types{$pty}->{'items'}}, $itm;
137 unless ($types{$pty}->{$itm->{'itemtype'}}) {
138 $types{$pty}->{'count'}++;
139 $types{$pty}->{$itm->{'itemtype'}} = 1;
140 push @{$types{$pty}->{'items'}}, $itm;
146 $template->param(ITEMS => \@duedates);
148 my $width = keys %types;
149 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
151 foreach my $pt (@publictypes) {
152 $typecount += $pt->{'count'};
154 $template->param(onlyone => 1) if $typecount == 1;
157 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
159 foreach my $pty (@publictypes) {
160 my @items = @{$pty->{'items'}};
161 push @row, $items[$rownum] if defined $items[$rownum];
164 $row[$last-1]->{'last'} =1 if $last == $width;
165 my $fill = ($width - $last)*2;
167 push @typerows, {ROW => \@row, fill => $fill};
169 $template->param(TYPE_ROWS => \@typerows);
170 $width = 2*$width -1;
171 $template->param(totalwidth => 2*$width-1);
173 if ($query->param('item_types_selected')) {
174 # this is what happens after the itemtypes have been selected. Stage 2
175 my @itemtypes = $query->param('itemtype');
180 foreach my $itmtype (@itemtypes) {
181 $newtypes{$itmtype} = $itemtypes{$itmtype};
183 my @types = values %newtypes;
184 $template->param(TYPES => \@types);
185 foreach my $type (@itemtypes) {
187 foreach my $item (@items) {
188 if ($item->{'itemtype'} eq $type) {
189 push @reqbibs, $item->{'biblioitemnumber'};
192 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
195 } elsif ($query->param('all')) {
196 $template->param(all => 1);
200 warn "branch :$branch:";
201 if ($proceed && $branch) {
202 $fee = sprintf "%.02f", $fee;
203 $template->param(fee => $fee);
204 $template->param(item_types_selected => 1);
206 $template->param(message => 1);
207 $template->param(no_items_selected => 1) unless ($proceed);
208 $template->param(no_branch_selected =>1) unless ($branch);
210 } elsif ($query->param('place_reserve')) {
211 # here we actually do the reserveration. Stage 3.
212 my $title = $bibdata->{'title'};
213 my @itemtypes = $query->param('itemtype');
214 foreach my $type (@itemtypes) {
216 foreach my $item (@items) {
217 if ($item->{'itemtype'} eq $type) {
218 push @reqbibs, $item->{'biblioitemnumber'};
221 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
223 if ($query->param('all')) {
224 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
226 print $query->redirect("/cgi-bin/koha/opac-user.pl");
228 # Here we check that the borrower can actually make reserves Stage 1.
230 if ($borr->{'amountoutstanding'} > 5) {
231 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
232 $template->param(message => 1);
234 $template->param(too_much_oweing => $amount);
236 my ($resnum, $reserves) = FindReserves('', $borrowernumber);
237 $template->param(RESERVES => $reserves);
238 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
239 $template->param(message => 1);
241 $template->param(too_many_reserves => $resnum);
243 foreach my $res (@$reserves) {
244 if ($res->{'biblionumber'} == $biblionumber) {
245 $template->param(message => 1);
247 $template->param(already_reserved => 1);
250 unless ($noreserves) {
251 $template->param(TYPES => \@types_old);
252 $template->param(select_item_types => 1);
256 # check that you can actually make the reserve.
258 output_html_with_http_headers $query, $cookie, $template->output;