synch'ing 2.2 and head
[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 use C4::Date;
16 use C4::Context;
17
18 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
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
35 # get biblionumber.....
36 my $biblionumber = $query->param('bib');
37
38 my $bibdata = bibdata($biblionumber);
39  $template->param($bibdata);
40  $template->param(biblionumber => $biblionumber);
41
42 # get the rank number....
43 my ($rank,$reserves) = FindReserves($biblionumber,'');
44 $template->param(reservecount => $rank);
45
46 foreach my $res (@$reserves) {
47     if ($res->{'found'} eq 'W') {
48         $rank--;
49     }
50 }
51
52 $rank++;
53 $template->param(rank => $rank);
54
55 # pass the pickup branch along....
56 my $branch = $query->param('branch');
57 $template->param(branch => $branch);
58
59 my $branches = getbranches();
60 $template->param(branchname => $branches->{$branch}->{'branchname'});
61
62 # make branch selection options...
63 #my $branchoptions = '';
64 my @branches;
65 my @select_branch;
66 my %select_branches;
67
68 foreach my $branch (keys %$branches) {
69         if ($branch) {
70                 push @select_branch, $branch;
71                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
72         }
73 }
74 my $CGIbranch=CGI::scrolling_list( -name     => 'branch',
75                         -values   => \@select_branch,
76                         -labels   => \%select_branches,
77                         -size     => 1,
78                         -multiple => 0 );
79 $template->param( CGIbranch => $CGIbranch);
80
81 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
82 # get the itemtype data....
83 my @items = ItemInfo(undef, $biblionumber, 'opac');
84
85 #######################################################
86 # old version, add so that old templates still work
87 my %types_old;
88 foreach my $itm (@items) {
89     my $ity = $itm->{'itemtype'};
90     unless ($types_old {$ity}) {
91         $types_old{$ity}->{'itemtype'} = $ity;
92         $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} = 1;
93         $types_old{$ity}->{'description'} = $itm->{'description'};
94     } else {
95         $types_old{$ity}->{'branchinfo'}->{$itm->{'branchcode'}} ++;
96     }
97 }
98
99 foreach my $type (values %types_old) {
100     my $copies = "";
101     foreach my $bc (keys %{$type->{'branchinfo'}}) {
102         $copies .= $branches->{$bc}->{'branchname'}."(".$type->{'branchinfo'}->{$bc}.")";
103     }
104     $type->{'copies'} = $copies;
105 }
106
107 my @types_old = values %types_old;
108
109 # end old version
110 ################################
111
112 my @temp;
113 foreach my $itm (@items) {
114     push @temp, $itm if $itm->{'itemtype'};
115 }
116 @items = @temp;
117 my $itemcount = @items;
118 $template->param(itemcount => $itemcount);
119
120 my %types;
121 my %itemtypes;
122 my @duedates;
123 foreach my $itm (@items) {
124     push @duedates, {date_due => format_date($itm->{'date_due'})} if defined $itm->{'date_due'};
125     $itm->{$itm->{'publictype'}} = 1;
126     # FIXME CalcReserveFee is supposed to be internal-use-only
127     my $fee  = CalcReserveFee(undef, $borrowernumber, $itm->{'biblionumber'},'a',($itm->{'biblioitemnumber'}));
128     $fee = sprintf "%.02f", $fee;
129     $itm->{'reservefee'} = $fee;
130     my $pty = $itm->{'publictype'};
131     $itemtypes{$itm->{'itemtype'}} = $itm;
132     unless ($types {$pty}) {
133         $types{$pty}->{'count'} = 1;
134         $types{$pty}->{$itm->{'itemtype'}} = 1;
135         push @{$types{$pty}->{'items'}}, $itm;
136     } else {
137         unless ($types{$pty}->{$itm->{'itemtype'}}) {
138             $types{$pty}->{'count'}++;
139             $types{$pty}->{$itm->{'itemtype'}} = 1;
140             push @{$types{$pty}->{'items'}}, $itm;
141         }
142     }
143 }
144
145
146 $template->param(ITEMS => \@duedates);
147
148 my $width = keys %types;
149 my @publictypes = sort {$b->{'count'} <=> $a->{'count'}} values %types;
150 my $typecount;
151 foreach my $pt (@publictypes) {
152     $typecount += $pt->{'count'};
153 }
154 $template->param(onlyone => 1) if $typecount == 1;
155
156 my @typerows;
157 for (my $rownum=0;$rownum<$publictypes[0]->{'count'} ;$rownum++) {
158     my @row;
159     foreach my $pty (@publictypes) {
160         my @items = @{$pty->{'items'}};
161         push @row, $items[$rownum] if defined $items[$rownum];
162     }
163     my $last = @row;
164     $row[$last-1]->{'last'} =1 if $last == $width;
165     my $fill = ($width - $last)*2;
166     $fill-- if $fill;
167     push @typerows, {ROW => \@row, fill => $fill};
168 }
169 $template->param(TYPE_ROWS => \@typerows);
170 $width = 2*$width -1;
171 $template->param(totalwidth => 2*$width-1,
172                              LibraryName => C4::Context->preference("LibraryName"),
173                                 suggestion => C4::Context->preference("suggestion"),
174                                 virtualshelves => C4::Context->preference("virtualshelves"),
175 );
176
177 if ($query->param('item_types_selected')) {
178         # this is what happens after the itemtypes have been selected. Stage 2
179         my @itemtypes = $query->param('itemtype');
180         my $fee = 0;
181         my $proceed = 0;
182         if (@itemtypes) {
183                 my %newtypes;
184                 foreach my $itmtype (@itemtypes) {
185                 $newtypes{$itmtype} = $itemtypes{$itmtype};
186                 }
187                 my @types = values %newtypes;
188                 $template->param(TYPES => \@types);
189                 foreach my $type (@itemtypes) {
190                 my @reqbibs;
191                 foreach my $item (@items) {
192                         if ($item->{'itemtype'} eq $type) {
193                         push @reqbibs, $item->{'biblioitemnumber'};
194                         }
195                 }
196                 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
197                 }
198                 $proceed = 1;
199         } elsif ($query->param('all')) {
200                 $template->param(all => 1);
201                 $fee = 1;
202                 $proceed = 1;
203         }
204         if ($proceed && $branch) {
205                 $fee = sprintf "%.02f", $fee;
206                 $template->param(fee => $fee,istherefee => $fee>0?1:0);
207                 $template->param(item_types_selected => 1);
208         } else {
209                 $template->param(message => 1);
210                 $template->param(no_items_selected => 1) unless ($proceed);
211                 $template->param(no_branch_selected =>1) unless ($branch);
212         }
213 } elsif ($query->param('place_reserve')) {
214         # here we actually do the reserveration. Stage 3.
215         my $title = $bibdata->{'title'};
216         my @itemtypes = $query->param('itemtype');
217         foreach my $type (@itemtypes) {
218                 my @reqbibs;
219                 foreach my $item (@items) {
220                 if ($item->{'itemtype'} eq $type) {
221                         push @reqbibs, $item->{'biblioitemnumber'};
222                 }
223                 }
224                 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
225         }
226         if ($query->param('all')) {
227                 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
228         }
229         print $query->redirect("/cgi-bin/koha/opac-user.pl");
230 } else {
231         # Here we check that the borrower can actually make reserves Stage 1.
232         my $noreserves = 0;
233         my $maxoutstanding = C4::Context->preference("maxoustanding");
234         $template->param(noreserve => 1) unless $maxoutstanding;
235         if ($borr->{'amountoutstanding'} > $maxoutstanding) {
236                 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
237                 $template->param(message => 1);
238                 $noreserves = 1;
239                 $template->param(too_much_oweing => $amount);
240         }
241         if ($borr->{gonenoaddress} eq 1) {
242                 $noreserves = 1;
243                 $template->param(message => 1,
244                                                 GNA => 1);
245         }
246         if ($borr->{lost} eq 1) {
247                 $noreserves = 1;
248                 $template->param(message => 1,
249                                                 lost => 1);
250         }
251         if ($borr->{debarred} eq 1) {
252                 $noreserves = 1;
253                 $template->param(message => 1,
254                                                 debarred => 1);
255         }
256         my ($resnum, $reserves) = FindReserves('', $borrowernumber);
257         $template->param(RESERVES => $reserves);
258         if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
259                 $template->param(message => 1);
260                 $noreserves = 1;
261                 $template->param(too_many_reserves => $resnum);
262         }
263         foreach my $res (@$reserves) {
264                 if ($res->{'biblionumber'} == $biblionumber) {
265                 $template->param(message => 1);
266                 $noreserves = 1;
267                 $template->param(already_reserved => 1);
268                 }
269         }
270         unless ($noreserves) {
271                 $template->param(TYPES => \@types_old);
272                 $template->param(select_item_types => 1);
273         }
274 }
275
276 # check that you can actually make the reserve.
277
278 output_html_with_http_headers $query, $cookie, $template->output;
279
280 # Local Variables:
281 # tab-width: 8
282 # End: