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