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