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