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