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'};
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 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
98 $fee = sprintf "%.02f", $fee;
99 $itm->{'reservefee'} = $fee;
100 my $pty = $itm->{'publictype'};
101 $itemtypes{$itm->{'itemtype'}} = $itm;
102 unless ($types {$pty}) {
103 $types{$pty}->{'count'} = 1;
104 $types{$pty}->{$itm->{'itemtype'}} = 1;
105 push @{$types{$pty}->{'items'}}, $itm;
107 unless ($types{$pty}->{$itm->{'itemtype'}}) {
108 $types{$pty}->{'count'}++;
109 $types{$pty}->{$itm->{'itemtype'}} = 1;
110 push @{$types{$pty}->{'items'}}, $itm;
116 $template->param(ITEMS => \@duedates);
118 my $width = keys %types;
119 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
121 foreach my $pt (@publictypes) {
122 $typecount += $pt->{'count'};
124 $template->param(onlyone => 1) if $typecount == 1;
127 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
129 foreach my $pty (@publictypes) {
130 my @items = @{$pty->{'items'}};
131 push @row, $items[$rownum] if defined $items[$rownum];
134 $row[$last-1]->{'last'} =1 if $last == $width;
135 my $fill = ($width - $last)*2;
137 push @typerows, {ROW => \@row, fill => $fill};
139 $template->param(TYPE_ROWS => \@typerows);
140 $width = 2*$width -1;
141 $template->param(totalwidth => 2*$width-1);
143 if ($query->param('item_types_selected')) {
144 # this is what happens after the itemtypes have been selected. Stage 2
145 my @itemtypes = $query->param('itemtype');
150 foreach my $itmtype (@itemtypes) {
151 $newtypes{$itmtype} = $itemtypes{$itmtype};
153 my @types = values %newtypes;
154 $template->param(TYPES => \@types);
155 foreach my $type (@itemtypes) {
157 foreach my $item (@items) {
158 if ($item->{'itemtype'} eq $type) {
159 push @reqbibs, $item->{'biblioitemnumber'};
162 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
165 } elsif ($query->param('all')) {
166 $template->param(all => 1);
171 $fee = sprintf "%.02f", $fee;
172 $template->param(fee => $fee);
173 $template->param(item_types_selected => 1);
175 $template->param(message => 1);
176 $template->param(no_items_selected => 1);
180 } elsif ($query->param('place_reserve')) {
181 # here we actually do the reserveration. Stage 3.
182 my $title = $bibdata->{'title'};
183 my @itemtypes = $query->param('itemtype');
184 foreach my $type (@itemtypes) {
186 foreach my $item (@items) {
187 if ($item->{'itemtype'} eq $type) {
188 push @reqbibs, $item->{'biblioitemnumber'};
191 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
193 if ($query->param('all')) {
194 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
196 print $query->redirect("/cgi-bin/koha/opac-user.pl");
198 # Here we check that the borrower can actually make reserves Stage 1.
200 if ($borr->{'amountoutstanding'} > 5) {
201 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
202 $template->param(message => 1);
204 $template->param(too_much_oweing => $amount);
206 my ($resnum, $reserves) = FindReserves(undef, $borrowernumber);
207 $template->param(RESERVES => $reserves);
208 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
209 $template->param(message => 1);
211 $template->param(too_many_reserves => $resnum);
213 foreach my $res (@$reserves) {
214 if ($res->{'biblionumber'} == $biblionumber) {
215 $template->param(message => 1);
217 $template->param(already_reserved => 1);
220 unless ($noreserves) {
221 $template->param(select_item_types => 1);
224 # check that you can actually make the reserve.
228 $template->param(BIBLIOITEMS => \@data);
231 output_html_with_http_headers $query, $cookie, $template->output;