]> git.koha-community.org Git - koha.git/blob - opac/opac-reserve.pl
fix for 394
[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
17 my $MAXIMUM_NUMBER_OF_RESERVES = 5;
18
19 my $query = new CGI;
20 my ($template, $borrowernumber, $cookie)
21     = get_template_and_user({template_name => "opac-reserve.tmpl",
22                              query => $query,
23                              type => "opac",
24                              authnotrequired => 0,
25                              flagsrequired => {borrow => 1},
26                              debug => 1,
27                              });
28
29 # get borrower information ....
30 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
31 my @bordat;
32 $bordat[0] = $borr;
33
34 # get biblionumber.....
35 my $biblionumber = $query->param('bib');
36
37 my $bibdata = bibdata($biblionumber);
38  $template->param($bibdata);
39  $template->param(BORROWER_INFO => \@bordat, biblionumber => $biblionumber);
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 # pass the pickup branch along....
57 my $branch = $query->param('branch');
58 $template->param(branch => $branch);
59
60 my $branches = getbranches();
61 $template->param(branchname => $branches->{$branch}->{'branchname'});
62
63
64 # make branch selection options...
65 #my $branchoptions = '';
66 my @branches;
67 my @select_branch;
68 my %select_branches;
69
70 foreach my $branch (keys %$branches) {
71         push @select_branch, $branch;
72         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
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
173 if ($query->param('item_types_selected')) {
174         # this is what happens after the itemtypes have been selected. Stage 2
175         my @itemtypes = $query->param('itemtype');
176         my $fee = 0;
177         my $proceed = 0;
178         if (@itemtypes) {
179                 my %newtypes;
180                 foreach my $itmtype (@itemtypes) {
181                 $newtypes{$itmtype} = $itemtypes{$itmtype};
182                 }
183                 my @types = values %newtypes;
184                 $template->param(TYPES => \@types);
185                 foreach my $type (@itemtypes) {
186                 my @reqbibs;
187                 foreach my $item (@items) {
188                         if ($item->{'itemtype'} eq $type) {
189                         push @reqbibs, $item->{'biblioitemnumber'};
190                         }
191                 }
192                 $fee += CalcReserveFee(undef,$borrowernumber,$biblionumber,'o',\@reqbibs);
193                 }
194                 $proceed = 1;
195         } elsif ($query->param('all')) {
196                 $template->param(all => 1);
197                 $fee = 1;
198                 $proceed = 1;
199         }
200         warn "branch :$branch:";
201         if ($proceed && $branch) {
202                 $fee = sprintf "%.02f", $fee;
203                 $template->param(fee => $fee);
204                 $template->param(item_types_selected => 1);
205         } else {
206                 $template->param(message => 1);
207                 $template->param(no_items_selected => 1) unless ($proceed);
208                 $template->param(no_branch_selected =>1) unless ($branch);
209         }
210 } elsif ($query->param('place_reserve')) {
211         # here we actually do the reserveration. Stage 3.
212         my $title = $bibdata->{'title'};
213         my @itemtypes = $query->param('itemtype');
214         foreach my $type (@itemtypes) {
215                 my @reqbibs;
216                 foreach my $item (@items) {
217                 if ($item->{'itemtype'} eq $type) {
218                         push @reqbibs, $item->{'biblioitemnumber'};
219                 }
220                 }
221                 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
222         }
223         if ($query->param('all')) {
224                 CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'a', undef, $rank,'',$title);
225         }
226         print $query->redirect("/cgi-bin/koha/opac-user.pl");
227 } else {
228         # Here we check that the borrower can actually make reserves Stage 1.
229         my $noreserves = 0;
230         if ($borr->{'amountoutstanding'} > 5) {
231                 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
232                 $template->param(message => 1);
233                 $noreserves = 1;
234                 $template->param(too_much_oweing => $amount);
235         }
236         my ($resnum, $reserves) = FindReserves('', $borrowernumber);
237         $template->param(RESERVES => $reserves);
238         if ($resnum >= $MAXIMUM_NUMBER_OF_RESERVES) {
239                 $template->param(message => 1);
240                 $noreserves = 1;
241                 $template->param(too_many_reserves => $resnum);
242         }
243         foreach my $res (@$reserves) {
244                 if ($res->{'biblionumber'} == $biblionumber) {
245                 $template->param(message => 1);
246                 $noreserves = 1;
247                 $template->param(already_reserved => 1);
248                 }
249         }
250         unless ($noreserves) {
251                 $template->param(TYPES => \@types_old);
252                 $template->param(select_item_types => 1);
253         }
254 }
255
256 # check that you can actually make the reserve.
257
258 output_html_with_http_headers $query, $cookie, $template->output;
259
260 # Local Variables:
261 # tab-width: 8
262 # End: