7 use C4::Auth; # checkauth, getborrowernumber.
9 use C4::Circulation::Circ2;
13 my ($template, $borrowernumber, $cookie)
14 = get_template_and_user({template_name => "opac-reserve.tmpl",
18 flagsrequired => {borrow => 1},
22 # get borrower information ....
23 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
26 $template->param(BORROWER_INFO => \@bordat);
28 # get biblionumber.....
29 my $biblionumber = $query->param('bib');
30 $template->param(biblionumber => $biblionumber);
32 my $bibdata = bibdata($biblionumber);
33 $template->param($bibdata);
35 # get the rank number....
36 my ($rank,$reserves) = FindReserves($biblionumber);
37 $template->param(reservecount => $rank);
39 foreach my $res (@$reserves) {
40 if ($res->{'found'} eq 'W') {
45 $template->param(rank => $rank);
49 # pass the pickup branch along....
50 my $branch = $query->param('branch');
51 $template->param(branch => $branch);
53 my $branches = getbranches();
54 $template->param(branchname => $branches->{$branch}->{'branchname'});
57 # make branch selection options...
58 my $branchoptions = '';
60 foreach my $br (keys %$branches) {
61 (next) unless $branches->{$br}->{'IS'};
64 $selected = "selected";
66 $branchoptions .= "<option value=$br $selected>$branches->{$br}->{'branchname'}</option>\n";
67 push @branches, {branchcode => $br, branchname => $branches->{$br}->{'branchname'}, selected => $selected};
69 $template->param( branchoptions => $branchoptions);
70 $template->param(BRANCHES => \@branches);
72 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
73 # get the itemtype data....
74 my @items = ItemInfo(undef, $biblionumber, 'opac');
76 foreach my $itm (@items) {
77 push @temp, $itm if $itm->{'itemtype'};
80 my $itemcount = @items;
81 $template->param(itemcount => $itemcount);
86 foreach my $itm (@items) {
87 push @duedates, {date_due => slashifyDate($itm->{'date_due'})} if defined $itm->{'date_due'};
88 $itm->{$itm->{'publictype'}} = 1;
89 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
90 $fee = sprintf "%.02f", $fee;
91 $itm->{'reservefee'} = $fee;
92 my $pty = $itm->{'publictype'};
93 $itemtypes{$itm->{'itemtype'}} = $itm;
94 unless ($types {$pty}) {
95 $types{$pty}->{'count'} = 1;
96 $types{$pty}->{$itm->{'itemtype'}} = 1;
97 push @{$types{$pty}->{'items'}}, $itm;
99 unless ($types{$pty}->{$itm->{'itemtype'}}) {
100 $types{$pty}->{'count'}++;
101 $types{$pty}->{$itm->{'itemtype'}} = 1;
102 push @{$types{$pty}->{'items'}}, $itm;
108 $template->param(ITEMS => \@duedates);
110 my $width = keys %types;
111 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
113 foreach my $pt (@publictypes) {
114 $typecount += $pt->{'count'};
116 $template->param(onlyone => 1) if $typecount == 1;
119 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
121 foreach my $pty (@publictypes) {
122 my @items = @{$pty->{'items'}};
123 push @row, $items[$rownum] if defined $items[$rownum];
126 $row[$last-1]->{'last'} =1 if $last == $width;
127 my $fill = ($width - $last)*2;
129 push @typerows, {ROW => \@row, fill => $fill};
131 $template->param(TYPE_ROWS => \@typerows);
132 $width = 2*$width -1;
133 $template->param(totalwidth => 2*$width-1);
135 if ($query->param('item_types_selected')) {
136 # this is what happens after the itemtypes have been selected. Stage 2
137 my @itemtypes = $query->param('itemtype');
142 foreach my $itmtype (@itemtypes) {
143 $newtypes{$itmtype} = $itemtypes{$itmtype};
145 my @types = values %newtypes;
146 $template->param(TYPES => \@types);
147 foreach my $type (@itemtypes) {
149 foreach my $item (@items) {
150 if ($item->{'itemtype'} eq $type) {
151 push @reqbibs, $item->{'biblioitemnumber'};
154 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
157 } elsif ($query->param('all')) {
158 $template->param(all => 1);
163 $fee = sprintf "%.02f", $fee;
164 $template->param(fee => $fee);
165 $template->param(item_types_selected => 1);
167 $template->param(message => 1);
168 $template->param(no_items_selected => 1);
172 } elsif ($query->param('place_reserve')) {
173 # here we actually do the reserveration. Stage 3.
174 my $title = $bibdata->{'title'};
175 my @itemtypes = $query->param('itemtype');
176 foreach my $type (@itemtypes) {
178 foreach my $item (@items) {
179 if ($item->{'itemtype'} eq $type) {
180 push @reqbibs, $item->{'biblioitemnumber'};
183 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
185 if ($query->param('all')) {
186 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
188 print $query->redirect("/cgi-bin/koha/opac-user.pl");
190 # Here we check that the borrower can actually make reserves Stage 1.
192 if ($borr->{'amountoutstanding'} > 5) {
193 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
194 $template->param(message => 1);
196 $template->param(too_much_oweing => $amount);
198 my ($resnum, $reserves) = FindReserves(undef, $borrowernumber);
199 $template->param(RESERVES => $reserves);
200 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
201 $template->param(message => 1);
203 $template->param(too_many_reserves => $resnum);
205 foreach my $res (@$reserves) {
206 if ($res->{'biblionumber'} == $biblionumber) {
207 $template->param(message => 1);
209 $template->param(already_reserved => 1);
212 unless ($noreserves) {
213 $template->param(select_item_types => 1);
216 # check that you can actually make the reserve.
220 $template->param(BIBLIOITEMS => \@data);
223 print $query->header(-cookie => $cookie), $template->output;