(bug #3353) permit librarians to cancel orders
[koha.git] / opac / opac-topissues.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Search;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Branch;
31 use Date::Manip;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =over 2
40
41 =cut
42
43 my $input = new CGI;
44 my $branches = GetBranches();
45 my $itemtypes = GetItemTypes();
46
47 my ($template, $borrowernumber, $cookie)
48         = get_template_and_user({template_name => 'opac-topissues.tmpl',
49                                 query => $input,
50                                 type => "opac",
51                                 authnotrequired => 1,
52                                 debug => 1,
53                                 });
54 my $dbh = C4::Context->dbh;
55 # Displaying results
56 my $limit = $input->param('limit') || 10;
57 my $branch = $input->param('branch') || '';
58 my $itemtype = $input->param('itemtype') || '';
59 my $timeLimit = $input->param('timeLimit') || 3;
60 my $whereclause='';
61 $whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
62 $whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
63 $whereclause =~ s/ AND $//;
64 my $query;
65 if(C4::Context->preference('AdvancedSearchTypes') eq 'ccode'){
66     $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
67     $query = "SELECT datecreated, biblio.biblionumber, title, 
68                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
69                     biblioitems.publishercode,biblioitems.publicationyear,
70                     authorised_values.lib as description
71                     FROM biblio
72                     LEFT JOIN items USING (biblionumber)
73                     LEFT JOIN biblioitems USING (biblionumber)
74                     LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
75                     WHERE 1
76                     $whereclause
77                     AND authorised_values.category = 'ccode' 
78                     GROUP BY biblio.biblionumber
79                     HAVING tot >0
80                     ORDER BY tot DESC
81                     LIMIT $limit
82                     ";
83 }else{
84     $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype) if $itemtype;
85     $query = "SELECT datecreated, biblio.biblionumber, title, 
86                     author, sum( items.issues ) AS tot, biblioitems.itemtype,
87                     biblioitems.publishercode,biblioitems.publicationyear,
88                     itemtypes.description
89                     FROM biblio
90                     LEFT JOIN items USING (biblionumber)
91                     LEFT JOIN biblioitems USING (biblionumber)
92                     LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
93                     WHERE 1
94                     $whereclause
95                     GROUP BY biblio.biblionumber
96                     HAVING tot >0
97                     ORDER BY tot DESC
98                     LIMIT $limit
99                     ";
100 }
101 my $sth = $dbh->prepare($query);
102 $sth->execute();
103 my @results;
104 while (my $line= $sth->fetchrow_hashref) {
105     push @results, $line;
106 }
107
108 my $timeLimitFinite = $timeLimit;
109 if($timeLimit eq 999){ $timeLimitFinite = 0 };
110
111 $template->param(do_it => 1,
112                 limit => $limit,
113                 branch => $branches->{$branch}->{branchname} || 'all locations',
114                 itemtype => $itemtypes->{$itemtype}->{description} || 'item types',
115                 timeLimit => $timeLimit,
116                 timeLimitFinite => $timeLimit,
117                 results_loop => \@results,
118                 );
119
120 # load the branches             ## again??
121 $branches = GetBranches();
122 my @branch_loop;
123 for my $branch_hash (sort keys %$branches ) {
124     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
125     push @branch_loop,
126       {
127         value      => "$branch_hash",
128         branchname => $branches->{$branch_hash}->{'branchname'},
129         selected => $selected
130       };
131 }
132 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
133
134 #doctype
135 $itemtypes = GetItemTypes;
136 my @itemtypeloop;
137 foreach my $thisitemtype (sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
138         my $selected = 1 if $thisitemtype eq $itemtype;
139         my %row =(value => $thisitemtype,
140                     description => $itemtypes->{$thisitemtype}->{'description'},
141                     selected => $selected,
142                  );
143         push @itemtypeloop, \%row;
144 }
145
146 $template->param(
147                  itemtypeloop =>\@itemtypeloop,
148                  dateformat    => C4::Context->preference("dateformat"),
149                 );
150 output_html_with_http_headers $input, $cookie, $template->output;
151