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(BORROWER_INFO => \@bordat, 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 $template->param(branchname => $branches->{$branch}->{'branchname'});
63 # make branch selection options...
64 #my $branchoptions = '';
69 foreach my $branch (keys %$branches) {
71 push @select_branch, $branch;
72 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
75 my $CGIbranch=CGI::scrolling_list( -name => 'branch',
76 -values => \@select_branch,
77 -labels => \%select_branches,
80 $template->param( CGIbranch => $CGIbranch);
82 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
83 # get the itemtype data....
84 my @items = ItemInfo(undef, $biblionumber, 'opac');
86 #######################################################
87 # old version, add so that old templates still work
89 foreach my $itm (@items) {
90 my $ity = $itm->{'itemtype'};
91 unless ($types_old {$ity}) {
92 $types_old{$ity}->{'itemtype'} = $ity;
93 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} = 1;
94 $types_old{$ity}->{'description'} = $itm->{'description'};
96 $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} ++;
100 foreach my $type (values %types_old) {
102 foreach my $bc (keys %{$type->{'branchinfo'}}) {
103 $copies .= $branches->{$bc}->{'branchname'}."(".$type->{'branchinfo'}->{$bc}.")";
105 $type->{'copies'} = $copies;
108 my @types_old = values %types_old;
111 ################################
114 foreach my $itm (@items) {
115 push @temp, $itm if $itm->{'itemtype'};
118 my $itemcount = @items;
119 $template->param(itemcount => $itemcount);
124 foreach my $itm (@items) {
125 push @duedates, {date_due => format_date($itm->{'date_due'})} if defined $itm->{'date_due'};
126 $itm->{$itm->{'publictype'}} = 1;
127 # FIXME CalcReserveFee is supposed to be internal-use-only
128 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
129 $fee = sprintf "%.02f", $fee;
130 $itm->{'reservefee'} = $fee;
131 my $pty = $itm->{'publictype'};
132 $itemtypes{$itm->{'itemtype'}} = $itm;
133 unless ($types {$pty}) {
134 $types{$pty}->{'count'} = 1;
135 $types{$pty}->{$itm->{'itemtype'}} = 1;
136 push @{$types{$pty}->{'items'}}, $itm;
138 unless ($types{$pty}->{$itm->{'itemtype'}}) {
139 $types{$pty}->{'count'}++;
140 $types{$pty}->{$itm->{'itemtype'}} = 1;
141 push @{$types{$pty}->{'items'}}, $itm;
147 $template->param(ITEMS => \@duedates);
149 my $width = keys %types;
150 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
152 foreach my $pt (@publictypes) {
153 $typecount += $pt->{'count'};
155 $template->param(onlyone => 1) if $typecount == 1;
158 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
160 foreach my $pty (@publictypes) {
161 my @items = @{$pty->{'items'}};
162 push @row, $items[$rownum] if defined $items[$rownum];
165 $row[$last-1]->{'last'} =1 if $last == $width;
166 my $fill = ($width - $last)*2;
168 push @typerows, {ROW => \@row, fill => $fill};
170 $template->param(TYPE_ROWS => \@typerows);
171 $width = 2*$width -1;
172 $template->param(totalwidth => 2*$width-1,
173 LibraryName => C4::Context->preference("LibraryName"),
176 if ($query->param('item_types_selected')) {
177 # this is what happens after the itemtypes have been selected. Stage 2
178 my @itemtypes = $query->param('itemtype');
183 foreach my $itmtype (@itemtypes) {
184 $newtypes{$itmtype} = $itemtypes{$itmtype};
186 my @types = values %newtypes;
187 $template->param(TYPES => \@types);
188 foreach my $type (@itemtypes) {
190 foreach my $item (@items) {
191 if ($item->{'itemtype'} eq $type) {
192 push @reqbibs, $item->{'biblioitemnumber'};
195 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
198 } elsif ($query->param('all')) {
199 $template->param(all => 1);
203 warn "branch :$branch:";
204 if ($proceed && $branch) {
205 $fee = sprintf "%.02f", $fee;
206 $template->param(fee => $fee);
207 $template->param(item_types_selected => 1);
209 $template->param(message => 1);
210 $template->param(no_items_selected => 1) unless ($proceed);
211 $template->param(no_branch_selected =>1) unless ($branch);
213 } elsif ($query->param('place_reserve')) {
214 # here we actually do the reserveration. Stage 3.
215 my $title = $bibdata->{'title'};
216 my @itemtypes = $query->param('itemtype');
217 foreach my $type (@itemtypes) {
219 foreach my $item (@items) {
220 if ($item->{'itemtype'} eq $type) {
221 push @reqbibs, $item->{'biblioitemnumber'};
224 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
226 if ($query->param('all')) {
227 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
229 print $query->redirect("/cgi-bin/koha/opac-search.pl");
231 # Here we check that the borrower can actually make reserves Stage 1.
233 my $maxoutstanding = C4::Context->preference("maxoustanding");
234 if ($borr->{'amountoutstanding'} > $maxoutstanding) {
235 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
236 $template->param(message => 1);
238 $template->param(too_much_oweing => $amount);
240 my ($resnum, $reserves) = FindReserves('', $borrowernumber);
241 $template->param(RESERVES => $reserves);
242 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
243 $template->param(message => 1);
245 $template->param(too_many_reserves => $resnum);
247 foreach my $res (@$reserves) {
248 if ($res->{'biblionumber'} == $biblionumber) {
249 $template->param(message => 1);
251 $template->param(already_reserved => 1);
254 unless ($noreserves) {
255 $template->param(TYPES => \@types_old);
256 $template->param(select_item_types => 1);
260 # check that you can actually make the reserve.
262 output_html_with_http_headers $query, $cookie, $template->output;