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