Bug 29723: Add a "Configure table" button for KohaTable tables
[koha.git] / reports / catalogue_out.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 # use Date::Manip;  # TODO: add not borrowed since date X criteria
27
28 =head1 catalogue_out
29
30 Report that shows unborrowed items.
31
32 =cut
33
34 my $input   = CGI->new;
35 my $do_it   = $input->param('do_it');
36 my $limit   = $input->param("Limit") || 10;
37 my $column  = $input->param("Criteria");
38 my @filters = $input->multi_param("Filter");
39
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41     {
42         template_name   => "reports/catalogue_out.tt",
43         query           => $input,
44         type            => "intranet",
45         flagsrequired   => { reports => 'execute_reports' },
46     }
47 );
48
49 $template->param( do_it => $do_it );
50 if ($do_it) {
51     my $results = calculate( $limit, $column, \@filters );
52     $template->param( mainloop => $results );
53     output_html_with_http_headers $input, $cookie, $template->output;
54     exit;    # in either case, exit after do_it
55 }
56
57 my $itemtypes = Koha::ItemTypes->search_with_localization;
58 $template->param(
59     itemtypes => $itemtypes,
60 );
61 output_html_with_http_headers $input, $cookie, $template->output;
62
63 sub calculate {
64     my ( $limit, $column, $filters ) = @_;
65     my %globalline;
66     my %columns = ();
67     my $dbh     = C4::Context->dbh;
68
69     # Filters
70     # Checking filters
71     #
72     my @loopfilter;
73     for ( my $i = 0 ; $i <= 6 ; $i++ ) {
74         if ( @$filters[$i] ) {
75             my %cell = ( filter => @$filters[$i] );
76             if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
77                 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
78             }
79             $cell{crit} = "Branch"   if ( $i == 0 );
80             $cell{crit} = "Doc Type" if ( $i == 1 );
81             push @loopfilter, \%cell;
82         }
83     }
84     push @loopfilter, { crit => 'limit', filter => $limit } if ($limit);
85     if ($column) {
86         push @loopfilter, { crit => 'by', filter => $column };
87         my $tablename = ( $column =~ /branchcode/ ) ? 'branches' : 'items';
88         $column =
89           ( $column =~ /branchcode/ or $column =~ /itype/ )
90           ? "$tablename.$column"
91           : $column;
92         my $strsth2 =
93           ( $tablename eq 'branches' )
94           ? "SELECT $column as coltitle, count(items.itemnumber) AS coltitle_count FROM $tablename LEFT JOIN items ON items.homebranch=$column "
95           : "SELECT $column as coltitle, count(*)                AS coltitle_count FROM $tablename ";
96         if ( $tablename eq 'branches' ) {
97             my $f = @$filters[0];
98             $f =~ s/\*/%/g;
99             $strsth2 .= " AND $column LIKE '$f' ";
100         }
101         $strsth2 .= " GROUP BY $column ORDER BY $column ";    # needed for count
102         push @loopfilter, { crit => 'SQL', sql => 1, filter => $strsth2 };
103         my $sth2 = $dbh->prepare($strsth2);
104         $sth2->execute;
105
106         while ( my ( $celvalue, $count ) = $sth2->fetchrow ) {
107             ($celvalue) or $celvalue = 'UNKNOWN';
108             $columns{$celvalue} = $count;
109         }
110     }
111
112     my %tables = ( map { $_ => [] } keys %columns );
113
114     # preparing calculation
115     my @exe_args = ();
116     my $query    = "
117         SELECT items.barcode        as barcode,
118                items.homebranch     as branch,
119                items.itemcallnumber as itemcallnumber,
120                biblio.title         as title,
121                biblio.biblionumber  as biblionumber,
122                biblio.author        as author";
123     ($column) and $query .= ",\n$column as col ";
124     $query .= "
125         FROM items
126         LEFT JOIN biblio      USING (biblionumber)
127         LEFT JOIN     issues  USING (itemnumber)
128         LEFT JOIN old_issues  USING (itemnumber)
129           WHERE       issues.itemnumber IS NULL
130            AND    old_issues.itemnumber IS NULL
131         ";
132     if ( $filters->[0] ) {
133         $filters->[0] =~ s/\*/%/g;
134         push @exe_args, $filters->[0];
135         $query .= " AND items.homebranch LIKE ?";
136     }
137     if ( $filters->[1] ) {
138         $filters->[1] =~ s/\*/%/g;
139         push @exe_args, $filters->[1];
140         $query .= " AND items.itype      LIKE ?";
141     }
142     if ($column) {
143         $query .= " AND $column = ? GROUP BY items.itemnumber, $column ";
144         # placeholder handled below
145     }
146     else {
147         $query .= " GROUP BY items.itemnumber ";
148     }
149     $query .= " ORDER BY items.itemcallnumber DESC, barcode";
150     $query .= " LIMIT 0,$limit" if ($limit);
151
152     push @loopfilter, { crit => 'SQL', sql => 1, filter => $query };
153     my $dbcalc = $dbh->prepare($query);
154
155     if ($column) {
156         foreach ( sort keys %columns ) {
157             # execute(@exe_args,$_) would fail when the array is empty
158             # but @more_exe_args will work
159             my (@more_exe_args) = @exe_args;
160             push @more_exe_args, $_;
161             $dbcalc->execute(@more_exe_args)
162               or die "Query execute(@more_exe_args) failed: $query";
163             while ( my $data = $dbcalc->fetchrow_hashref ) {
164                 my $col = $data->{col} || 'NULL';
165                 $tables{$col} or $tables{$col} = [];
166                 push @{ $tables{$col} }, $data;
167             }
168         }
169     }
170     else {
171         ( scalar @exe_args ) ? $dbcalc->execute(@exe_args) : $dbcalc->execute;
172         while ( my $data = $dbcalc->fetchrow_hashref ) {
173             my $col = $data->{col} || 'NULL';
174             $tables{$col} or $tables{$col} = [];
175             push @{ $tables{$col} }, $data;
176         }
177     }
178
179     foreach my $tablename ( sort keys %tables ) {
180         my (@temptable);
181         my $i = 0;
182         foreach my $cell ( @{ $tables{$tablename} } ) {
183             push @temptable, $cell;
184         }
185         my $count    = scalar(@temptable);
186         my $allitems = $columns{$tablename};
187         $globalline{total_looptable_count} += $count;
188         $globalline{total_coltitle_count}  += $allitems;
189         push @{ $globalline{looptables} },
190           {
191             looprow         => \@temptable,
192             coltitle        => $tablename,
193             coltitle_count  => $allitems,
194             looptable_count => $count,
195             looptable_first => ($count) ? $temptable[0]->{itemcallnumber} : '',
196             looptable_last  => ($count) ? $temptable[-1]->{itemcallnumber} : '',
197           };
198     }
199
200     # the header of the table
201     $globalline{loopfilter} = \@loopfilter;
202     $globalline{limit}      = $limit;
203     $globalline{column}     = $column;
204     return [ ( \%globalline ) ];    # reference to array of reference to hash
205 }
206
207 1;
208 __END__
209