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