fixed a couple of bugs. Note this version of opac-reserve.pl is designed to work...
[koha.git] / opac / opac-reserve.pl
1 #!/usr/bin/perl
2 # NOTE: This file uses standard 8-character tabs
3
4 use strict;
5 require Exporter;
6 use CGI;
7
8 use C4::Search;
9 use C4::Auth;         # checkauth, getborrowernumber.
10 use C4::Koha;
11 use C4::Circulation::Circ2;
12 use C4::Reserves2;
13 use C4::Interface::CGI::Output;
14 use HTML::Template;
15
16 my $MAXIMUM_NUMBER_OF_RESERVES = 5;
17
18 my $MAXIMUM_NUMBER_OF_RESERVES = 10;
19
20 my $query = new CGI;
21 my ($template, $borrowernumber, $cookie) 
22     = get_template_and_user({template_name => "opac-reserve.tmpl",
23                              query => $query,
24                              type => "opac",
25                              authnotrequired => 0,
26                              flagsrequired => {borrow => 1},
27                              debug => 1,
28                              });
29
30 # get borrower information ....
31 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
32 my @bordat;
33 $bordat[0] = $borr;
34 $template->param(BORROWER_INFO => \@bordat);
35
36 # get biblionumber.....
37 my $biblionumber = $query->param('bib');
38 $template->param(biblionumber => $biblionumber);
39
40 my $bibdata = bibdata($biblionumber);
41 $template->param($bibdata);
42
43 # get the rank number....
44 my ($rank,$reserves) = FindReserves($biblionumber);
45 $template->param(reservecount => $rank);
46
47 foreach my $res (@$reserves) {
48     if ($res->{'found'} eq 'W') {
49         $rank--;
50     }
51 }
52
53
54
55 $rank++;
56 $template->param(rank => $rank);
57
58
59 # pass the pickup branch along....
60 my $branch = $query->param('branch');
61 $template->param(branch => $branch);
62
63 my $branches = getbranches();
64 $template->param(branchname => $branches->{$branch}->{'branchname'});
65
66
67 # make branch selection options...
68 my $branchoptions = '';
69 my @branches;
70 foreach my $br (keys %$branches) {
71     #(next) unless $branches->{$br}->{'IS'}; # FIXME disabled to fix bug 202
72     my $selected = "";
73     if ($br eq $branch) {
74         $selected = "selected";
75     }
76     $branchoptions .= "<option value=$br $selected>$branches->{$br}->{'branchname'}</option>\n";
77     push @branches, {branchcode => $br, branchname => $branches->{$br}->{'branchname'}, selected => $selected};
78 }
79 $template->param( branchoptions => $branchoptions);
80 $template->param(BRANCHES => \@branches);
81
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');
85 my @temp;
86 foreach my $itm (@items) {
87     push @temp, $itm if $itm->{'itemtype'};
88 }
89 @items = @temp;
90 my $itemcount = @items;
91 $template->param(itemcount => $itemcount);
92
93 my %types;
94 my %itemtypes;
95 my @duedates;
96 foreach my $itm (@items) {
97     push @duedates, {date_due => slashifyDate($itm->{'date_due'})} if defined $itm->{'date_due'};
98     $itm->{$itm->{'publictype'}} = 1;
99     # FIXME CalcReserveFee is supposed to be internal-use-only
100     my $fee  = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
101     $fee = sprintf "%.02f", $fee;
102     $itm->{'reservefee'} = $fee;
103     my $pty = $itm->{'publictype'};
104     $itemtypes{$itm->{'itemtype'}} = $itm;
105     unless ($types {$pty}) {
106         $types{$pty}->{'count'} = 1;
107         $types{$pty}->{$itm->{'itemtype'}} = 1;
108         push @{$types{$pty}->{'items'}}, $itm;
109     } else {
110         unless ($types{$pty}->{$itm->{'itemtype'}}) {
111             $types{$pty}->{'count'}++;
112             $types{$pty}->{$itm->{'itemtype'}} = 1;
113             push @{$types{$pty}->{'items'}}, $itm;
114         }
115     }
116 }
117
118
119 $template->param(ITEMS => \@duedates);
120
121 my $width = keys %types;
122 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
123 my $typecount;
124 foreach my $pt (@publictypes) {
125     $typecount += $pt->{'count'};
126 }
127 $template->param(onlyone => 1) if $typecount == 1;
128
129 my @typerows;
130 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
131     my @row;
132     foreach my $pty (@publictypes) {
133         my @items = @{$pty->{'items'}};
134         push @row, $items[$rownum] if defined $items[$rownum];
135     }
136     my $last = @row; 
137     $row[$last-1]->{'last'} =1 if $last == $width; 
138     my $fill = ($width - $last)*2;
139     $fill-- if $fill;
140     push @typerows, {ROW => \@row, fill => $fill};
141 }
142 $template->param(TYPE_ROWS => \@typerows);
143 $width = 2*$width -1;
144 $template->param(totalwidth => 2*$width-1);
145
146 if ($query->param('item_types_selected')) {
147 # this is what happens after the itemtypes have been selected. Stage 2
148     my @itemtypes = $query->param('itemtype');
149     my $fee = 0;
150     my $proceed = 0;
151     if (@itemtypes) {
152         my %newtypes;
153         foreach my $itmtype (@itemtypes) {
154             $newtypes{$itmtype} = $itemtypes{$itmtype};
155         }
156         my @types = values %newtypes;
157         $template->param(TYPES => \@types);
158         foreach my $type (@itemtypes) {
159             my @reqbibs;
160             foreach my $item (@items) {
161                 if ($item->{'itemtype'} eq $type) {
162                     push @reqbibs, $item->{'biblioitemnumber'};
163                 }
164             }
165             $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
166         }
167         $proceed = 1;
168     } elsif ($query->param('all')) {
169         $template->param(all => 1);
170         $fee = 1;
171         $proceed = 1;
172     }
173     if ($proceed) {
174         $fee = sprintf "%.02f", $fee;
175         $template->param(fee => $fee);
176         $template->param(item_types_selected => 1);
177     } else {
178         $template->param(message => 1);
179         $template->param(no_items_selected => 1);
180     }
181
182
183 } elsif ($query->param('place_reserve')) {
184 # here we actually do the reserveration. Stage 3.
185     my $title = $bibdata->{'title'};
186     my @itemtypes = $query->param('itemtype');
187     foreach my $type (@itemtypes) {
188         my @reqbibs;
189         foreach my $item (@items) {
190             if ($item->{'itemtype'} eq $type) {
191                 push @reqbibs, $item->{'biblioitemnumber'};
192             }
193         }
194         CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
195     }
196     if ($query->param('all')) {
197         CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
198     }
199     print $query->redirect("/cgi-bin/koha/opac-user.pl");
200 } else {
201 # Here we check that the borrower can actually make reserves Stage 1.
202     my $noreserves = 0;
203     if ($borr->{'amountoutstanding'} > 5) {
204         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
205         $template->param(message => 1);
206         $noreserves = 1;
207         $template->param(too_much_oweing => $amount);
208     }
209     my ($resnum, $reserves) = FindReserves(undef, $borrowernumber);
210     $template->param(RESERVES => $reserves);
211     if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
212         $template->param(message => 1);
213         $noreserves = 1;
214         $template->param(too_many_reserves => $resnum);
215     }
216     foreach my $res (@$reserves) {
217         if ($res->{'biblionumber'} == $biblionumber) {
218             $template->param(message => 1);
219             $noreserves = 1;
220             $template->param(already_reserved => 1);
221         }
222     }
223     unless ($noreserves) {
224         $template->param(select_item_types => 1);
225     }
226 }
227 # check that you can actually make the reserve.
228
229 # $template->param(BIBLIOITEMS => \@data);
230
231
232 output_html_with_http_headers $query, $cookie, $template->output;
233
234 # Local Variables:
235 # tab-width: 8
236 # End: