Added missing "use" statement
[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 $query = new CGI;
19 my ($template, $borrowernumber, $cookie) 
20     = get_template_and_user({template_name => "opac-reserve.tmpl",
21                              query => $query,
22                              type => "opac",
23                              authnotrequired => 0,
24                              flagsrequired => {borrow => 1},
25                              debug => 1,
26                              });
27
28 # get borrower information ....
29 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
30 my @bordat;
31 $bordat[0] = $borr;
32 $template->param(BORROWER_INFO => \@bordat);
33
34 # get biblionumber.....
35 my $biblionumber = $query->param('bib');
36 $template->param(biblionumber => $biblionumber);
37
38 my $bibdata = bibdata($biblionumber);
39 $template->param($bibdata);
40
41 # get the rank number....
42 my ($rank,$reserves) = FindReserves($biblionumber);
43 $template->param(reservecount => $rank);
44
45 foreach my $res (@$reserves) {
46     if ($res->{'found'} eq 'W') {
47         $rank--;
48     }
49 }
50
51
52
53 $rank++;
54 $template->param(rank => $rank);
55
56
57 # pass the pickup branch along....
58 my $branch = $query->param('branch');
59 $template->param(branch => $branch);
60
61 my $branches = getbranches();
62 $template->param(branchname => $branches->{$branch}->{'branchname'});
63
64
65 # make branch selection options...
66 my $branchoptions = '';
67 my @branches;
68 foreach my $br (keys %$branches) {
69     (next) unless $branches->{$br}->{'IS'};
70     my $selected = "";
71     if ($br eq $branch) {
72         $selected = "selected";
73     }
74     $branchoptions .= "<option value=$br $selected>$branches->{$br}->{'branchname'}</option>\n";
75     push @branches, {branchcode => $br, branchname => $branches->{$br}->{'branchname'}, selected => $selected};
76 }
77 $template->param( branchoptions => $branchoptions);
78 $template->param(BRANCHES => \@branches);
79
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');
83 my @temp;
84 foreach my $itm (@items) {
85     push @temp, $itm if $itm->{'itemtype'};
86 }
87 @items = @temp;
88 my $itemcount = @items;
89 $template->param(itemcount => $itemcount);
90
91 my %types;
92 my %itemtypes;
93 my @duedates;
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;
106     } else {
107         unless ($types{$pty}->{$itm->{'itemtype'}}) {
108             $types{$pty}->{'count'}++;
109             $types{$pty}->{$itm->{'itemtype'}} = 1;
110             push @{$types{$pty}->{'items'}}, $itm;
111         }
112     }
113 }
114
115
116 $template->param(ITEMS => \@duedates);
117
118 my $width = keys %types;
119 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
120 my $typecount;
121 foreach my $pt (@publictypes) {
122     $typecount += $pt->{'count'};
123 }
124 $template->param(onlyone => 1) if $typecount == 1;
125
126 my @typerows;
127 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
128     my @row;
129     foreach my $pty (@publictypes) {
130         my @items = @{$pty->{'items'}};
131         push @row, $items[$rownum] if defined $items[$rownum];
132     }
133     my $last = @row; 
134     $row[$last-1]->{'last'} =1 if $last == $width; 
135     my $fill = ($width - $last)*2;
136     $fill-- if $fill;
137     push @typerows, {ROW => \@row, fill => $fill};
138 }
139 $template->param(TYPE_ROWS => \@typerows);
140 $width = 2*$width -1;
141 $template->param(totalwidth => 2*$width-1);
142
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');
146     my $fee = 0;
147     my $proceed = 0;
148     if (@itemtypes) {
149         my %newtypes;
150         foreach my $itmtype (@itemtypes) {
151             $newtypes{$itmtype} = $itemtypes{$itmtype};
152         }
153         my @types = values %newtypes;
154         $template->param(TYPES => \@types);
155         foreach my $type (@itemtypes) {
156             my @reqbibs;
157             foreach my $item (@items) {
158                 if ($item->{'itemtype'} eq $type) {
159                     push @reqbibs, $item->{'biblioitemnumber'};
160                 }
161             }
162             $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
163         }
164         $proceed = 1;
165     } elsif ($query->param('all')) {
166         $template->param(all => 1);
167         $fee = 1;
168         $proceed = 1;
169     }
170     if ($proceed) {
171         $fee = sprintf "%.02f", $fee;
172         $template->param(fee => $fee);
173         $template->param(item_types_selected => 1);
174     } else {
175         $template->param(message => 1);
176         $template->param(no_items_selected => 1);
177     }
178
179
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) {
185         my @reqbibs;
186         foreach my $item (@items) {
187             if ($item->{'itemtype'} eq $type) {
188                 push @reqbibs, $item->{'biblioitemnumber'};
189             }
190         }
191         CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
192     }
193     if ($query->param('all')) {
194         CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
195     }
196     print $query->redirect("/cgi-bin/koha/opac-user.pl");
197 } else {
198 # Here we check that the borrower can actually make reserves Stage 1.
199     my $noreserves = 0;
200     if ($borr->{'amountoutstanding'} > 5) {
201         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
202         $template->param(message => 1);
203         $noreserves = 1;
204         $template->param(too_much_oweing => $amount);
205     }
206     my ($resnum, $reserves) = FindReserves(undef, $borrowernumber);
207     $template->param(RESERVES => $reserves);
208     if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
209         $template->param(message => 1);
210         $noreserves = 1;
211         $template->param(too_many_reserves => $resnum);
212     }
213     foreach my $res (@$reserves) {
214         if ($res->{'biblionumber'} == $biblionumber) {
215             $template->param(message => 1);
216             $noreserves = 1;
217             $template->param(already_reserved => 1);
218         }
219     }
220     unless ($noreserves) {
221         $template->param(select_item_types => 1);
222     }
223 }
224 # check that you can actually make the reserve.
225
226
227
228 $template->param(BIBLIOITEMS => \@data);
229
230
231 output_html_with_http_headers $query, $cookie, $template->output;
232
233 # Local Variables:
234 # tab-width: 8
235 # End: