Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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;
24 use C4::Context;
25 use C4::Debug;
26 use C4::Output;
27 # use Date::Manip;  # TODO: add not borrowed since date X criteria
28 use Data::Dumper;
29
30 =head1 catalogue_out
31
32 Report that shows unborrowed items.
33
34 =cut
35
36 my $input   = new CGI;
37 my $do_it   = $input->param('do_it');
38 my $limit   = $input->param("Limit") || 10;
39 my $column  = $input->param("Criteria");
40 my @filters = $input->multi_param("Filter");
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "reports/catalogue_out.tt",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { reports => 'execute_reports' },
49         debug           => 1,
50     }
51 );
52
53 $template->param( do_it => $do_it );
54 if ($do_it) {
55     my $results = calculate( $limit, $column, \@filters );
56     $template->param( mainloop => $results );
57     output_html_with_http_headers $input, $cookie, $template->output;
58     exit;    # in either case, exit after do_it
59 }
60
61 my $itemtypes = Koha::ItemTypes->search_with_localization;
62 $template->param(
63     itemtypes => $itemtypes,
64 );
65 output_html_with_http_headers $input, $cookie, $template->output;
66
67 sub calculate {
68     my ( $limit, $column, $filters ) = @_;
69     my %globalline;
70     my %columns = ();
71     my $dbh     = C4::Context->dbh;
72
73     # Filters
74     # Checking filters
75     #
76     my @loopfilter;
77     for ( my $i = 0 ; $i <= 6 ; $i++ ) {
78         if ( @$filters[$i] ) {
79             my %cell = ( filter => @$filters[$i] );
80             if ( ( $i == 1 ) and ( @$filters[ $i - 1 ] ) ) {
81                 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
82             }
83             $cell{crit} = "Branch"   if ( $i == 0 );
84             $cell{crit} = "Doc Type" if ( $i == 1 );
85             push @loopfilter, \%cell;
86         }
87     }
88     push @loopfilter, { crit => 'limit', filter => $limit } if ($limit);
89     if ($column) {
90         push @loopfilter, { crit => 'by', filter => $column };
91         my $tablename = ( $column =~ /branchcode/ ) ? 'branches' : 'items';
92         $column =
93           ( $column =~ /branchcode/ or $column =~ /itype/ )
94           ? "$tablename.$column"
95           : $column;
96         my $strsth2 =
97           ( $tablename eq 'branches' )
98           ? "SELECT $column as coltitle, count(items.itemnumber) AS coltitle_count FROM $tablename LEFT JOIN items ON items.homebranch=$column "
99           : "SELECT $column as coltitle, count(*)                AS coltitle_count FROM $tablename ";
100         if ( $tablename eq 'branches' ) {
101             my $f = @$filters[0];
102             $f =~ s/\*/%/g;
103             $strsth2 .= " AND $column LIKE '$f' ";
104         }
105         $strsth2 .= " GROUP BY $column ORDER BY $column ";    # needed for count
106         push @loopfilter, { crit => 'SQL', sql => 1, filter => $strsth2 };
107         $debug and warn "catalogue_out SQL: " . $strsth2;
108         my $sth2 = $dbh->prepare($strsth2);
109         $sth2->execute;
110
111         while ( my ( $celvalue, $count ) = $sth2->fetchrow ) {
112             ($celvalue) or $celvalue = 'UNKNOWN';
113             $columns{$celvalue} = $count;
114         }
115     }
116
117     my %tables = ( map { $_ => [] } keys %columns );
118
119     # preparing calculation
120     my @exe_args = ();
121     my $query    = "
122         SELECT items.barcode        as barcode,
123                items.homebranch     as branch,
124                items.itemcallnumber as itemcallnumber,
125                biblio.title         as title,
126                biblio.biblionumber  as biblionumber,
127                biblio.author        as author";
128     ($column) and $query .= ",\n$column as col ";
129     $query .= "
130         FROM items
131         LEFT JOIN biblio      USING (biblionumber)
132         LEFT JOIN     issues  USING (itemnumber)
133         LEFT JOIN old_issues  USING (itemnumber)
134           WHERE       issues.itemnumber IS NULL
135            AND    old_issues.itemnumber IS NULL
136         ";
137     if ( $filters->[0] ) {
138         $filters->[0] =~ s/\*/%/g;
139         push @exe_args, $filters->[0];
140         $query .= " AND items.homebranch LIKE ?";
141     }
142     if ( $filters->[1] ) {
143         $filters->[1] =~ s/\*/%/g;
144         push @exe_args, $filters->[1];
145         $query .= " AND items.itype      LIKE ?";
146     }
147     if ($column) {
148         $query .= " AND $column = ? GROUP BY items.itemnumber, $column ";
149         # placeholder handled below
150     }
151     else {
152         $query .= " GROUP BY items.itemnumber ";
153     }
154     $query .= " ORDER BY items.itemcallnumber DESC, barcode";
155     $query .= " LIMIT 0,$limit" if ($limit);
156     $debug and warn "SQL : $query";
157
158     # warn "SQL : $query";
159     push @loopfilter, { crit => 'SQL', sql => 1, filter => $query };
160     my $dbcalc = $dbh->prepare($query);
161
162     if ($column) {
163         foreach ( sort keys %columns ) {
164             # execute(@exe_args,$_) would fail when the array is empty
165             # but @more_exe_args will work
166             my (@more_exe_args) = @exe_args;
167             push @more_exe_args, $_;
168             $dbcalc->execute(@more_exe_args)
169               or die "Query execute(@more_exe_args) failed: $query";
170             while ( my $data = $dbcalc->fetchrow_hashref ) {
171                 my $col = $data->{col} || 'NULL';
172                 $tables{$col} or $tables{$col} = [];
173                 push @{ $tables{$col} }, $data;
174             }
175         }
176     }
177     else {
178         ( scalar @exe_args ) ? $dbcalc->execute(@exe_args) : $dbcalc->execute;
179         while ( my $data = $dbcalc->fetchrow_hashref ) {
180             my $col = $data->{col} || 'NULL';
181             $tables{$col} or $tables{$col} = [];
182             push @{ $tables{$col} }, $data;
183         }
184     }
185
186     foreach my $tablename ( sort keys %tables ) {
187         my (@temptable);
188         my $i = 0;
189         foreach my $cell ( @{ $tables{$tablename} } ) {
190             if ( 0 == $i++ and $debug ) {
191                 my $dump = Dumper($cell);
192                 $dump =~ s/\n/ /gs;
193                 $dump =~ s/\s+/ /gs;
194                 print STDERR "first cell for $tablename: $dump";
195             }
196             push @temptable, $cell;
197         }
198         my $count    = scalar(@temptable);
199         my $allitems = $columns{$tablename};
200         $globalline{total_looptable_count} += $count;
201         $globalline{total_coltitle_count}  += $allitems;
202         push @{ $globalline{looptables} },
203           {
204             looprow         => \@temptable,
205             coltitle        => $tablename,
206             coltitle_count  => $allitems,
207             looptable_count => $count,
208             looptable_first => ($count) ? $temptable[0]->{itemcallnumber} : '',
209             looptable_last  => ($count) ? $temptable[-1]->{itemcallnumber} : '',
210           };
211     }
212
213     # the header of the table
214     $globalline{loopfilter} = \@loopfilter;
215     $globalline{limit}      = $limit;
216     $globalline{column}     = $column;
217     return [ ( \%globalline ) ];    # reference to array of reference to hash
218 }
219
220 1;
221 __END__
222