7 use C4::Auth; # checkauth, getborrowernumber.
9 use C4::Circulation::Circ2;
13 my $MAXIMUM_NUMBER_OF_RESERVES = 5;
16 my ($template, $borrowernumber, $cookie)
17 = get_template_and_user({template_name => "opac-reserve.tmpl",
21 flagsrequired => {borrow => 1},
25 # get borrower information ....
26 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
29 $template->param(BORROWER_INFO => \@bordat);
31 # get biblionumber.....
32 my $biblionumber = $query->param('bib');
33 $template->param(biblionumber => $biblionumber);
35 my $bibdata = bibdata($biblionumber);
36 $template->param($bibdata);
38 # get the rank number....
39 my ($rank,$reserves) = FindReserves($biblionumber);
40 $template->param(reservecount => $rank);
42 foreach my $res (@$reserves) {
43 if ($res->{'found'} eq 'W') {
51 $template->param(rank => $rank);
54 # pass the pickup branch along....
55 my $branch = $query->param('branch');
56 $template->param(branch => $branch);
58 my $branches = getbranches();
59 $template->param(branchname => $branches->{$branch}->{'branchname'});
62 # make branch selection options...
63 my $branchoptions = '';
65 foreach my $br (keys %$branches) {
66 (next) unless $branches->{$br}->{'IS'};
69 $selected = "selected";
71 $branchoptions .= "<option value=$br $selected>$branches->{$br}->{'branchname'}</option>\n";
72 push @branches, {branchcode => $br, branchname => $branches->{$br}->{'branchname'}, selected => $selected};
74 $template->param( branchoptions => $branchoptions);
75 $template->param(BRANCHES => \@branches);
77 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
78 # get the itemtype data....
79 my @items = ItemInfo(undef, $biblionumber, 'opac');
81 foreach my $itm (@items) {
82 push @temp, $itm if $itm->{'itemtype'};
85 my $itemcount = @items;
86 $template->param(itemcount => $itemcount);
91 foreach my $itm (@items) {
92 push @duedates, {date_due => slashifyDate($itm->{'date_due'})} if defined $itm->{'date_due'};
93 $itm->{$itm->{'publictype'}} = 1;
94 my $fee = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
95 $fee = sprintf "%.02f", $fee;
96 $itm->{'reservefee'} = $fee;
97 my $pty = $itm->{'publictype'};
98 $itemtypes{$itm->{'itemtype'}} = $itm;
99 unless ($types {$pty}) {
100 $types{$pty}->{'count'} = 1;
101 $types{$pty}->{$itm->{'itemtype'}} = 1;
102 push @{$types{$pty}->{'items'}}, $itm;
104 unless ($types{$pty}->{$itm->{'itemtype'}}) {
105 $types{$pty}->{'count'}++;
106 $types{$pty}->{$itm->{'itemtype'}} = 1;
107 push @{$types{$pty}->{'items'}}, $itm;
113 $template->param(ITEMS => \@duedates);
115 my $width = keys %types;
116 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
118 foreach my $pt (@publictypes) {
119 $typecount += $pt->{'count'};
121 $template->param(onlyone => 1) if $typecount == 1;
124 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
126 foreach my $pty (@publictypes) {
127 my @items = @{$pty->{'items'}};
128 push @row, $items[$rownum] if defined $items[$rownum];
131 $row[$last-1]->{'last'} =1 if $last == $width;
132 my $fill = ($width - $last)*2;
134 push @typerows, {ROW => \@row, fill => $fill};
136 $template->param(TYPE_ROWS => \@typerows);
137 $width = 2*$width -1;
138 $template->param(totalwidth => 2*$width-1);
140 if ($query->param('item_types_selected')) {
141 # this is what happens after the itemtypes have been selected. Stage 2
142 my @itemtypes = $query->param('itemtype');
147 foreach my $itmtype (@itemtypes) {
148 $newtypes{$itmtype} = $itemtypes{$itmtype};
150 my @types = values %newtypes;
151 $template->param(TYPES => \@types);
152 foreach my $type (@itemtypes) {
154 foreach my $item (@items) {
155 if ($item->{'itemtype'} eq $type) {
156 push @reqbibs, $item->{'biblioitemnumber'};
159 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
162 } elsif ($query->param('all')) {
163 $template->param(all => 1);
168 $fee = sprintf "%.02f", $fee;
169 $template->param(fee => $fee);
170 $template->param(item_types_selected => 1);
172 $template->param(message => 1);
173 $template->param(no_items_selected => 1);
177 } elsif ($query->param('place_reserve')) {
178 # here we actually do the reserveration. Stage 3.
179 my $title = $bibdata->{'title'};
180 my @itemtypes = $query->param('itemtype');
181 foreach my $type (@itemtypes) {
183 foreach my $item (@items) {
184 if ($item->{'itemtype'} eq $type) {
185 push @reqbibs, $item->{'biblioitemnumber'};
188 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
190 if ($query->param('all')) {
191 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
193 print $query->redirect("/cgi-bin/koha/opac-user.pl");
195 # Here we check that the borrower can actually make reserves Stage 1.
197 if ($borr->{'amountoutstanding'} > 5) {
198 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
199 $template->param(message => 1);
201 $template->param(too_much_oweing => $amount);
203 my ($resnum, $reserves) = FindReserves(undef, $borrowernumber);
204 $template->param(RESERVES => $reserves);
205 if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
206 $template->param(message => 1);
208 $template->param(too_many_reserves => $resnum);
210 foreach my $res (@$reserves) {
211 if ($res->{'biblionumber'} == $biblionumber) {
212 $template->param(message => 1);
214 $template->param(already_reserved => 1);
217 unless ($noreserves) {
218 $template->param(select_item_types => 1);
221 # check that you can actually make the reserve.
225 $template->param(BIBLIOITEMS => \@data);
228 print $query->header(-cookie => $cookie), $template->output;