3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
27 use C4
::Branch
; # GetBranches
29 use C4
::Koha
; # GetItemTypes
30 use C4
::Reports
; # GetDelimiterChoices
32 # use Date::Manip; # TODO: add not borrowed since date X criteria
37 Report that shows unborrowed items.
42 my $do_it = $input->param('do_it');
43 my $limit = $input->param("Limit");
44 my $column = $input->param("Criteria");
45 my @filters = $input->param("Filter");
46 my $output = $input->param("output");
47 my $basename = $input->param("basename") || 'catalogue_out';
48 my $mime = $input->param("MIME");
49 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
50 template_name
=> "reports/catalogue_out.tmpl",
54 flagsrequired
=> {reports
=> '*'},
58 our $sep = $input->param("sep");
59 $sep = "\t" if ((! defined $sep) or $sep eq 'tabulation');
61 $template->param(do_it
=> $do_it);
63 my $results = calculate
($limit, $column, \
@filters);
64 if ($output eq "screen") {
65 # Printing results to screen
66 $template->param(mainloop
=> $results);
67 output_html_with_http_headers
$input, $cookie, $template->output;
69 # Printing to a csv file FIXME: This is broken rather badly, if it ever worked at all here.
71 -type
=> 'application/vnd.sun.xml.calc',
73 -attachment
=>"$basename.csv",
74 -filename
=>"$basename.csv" );
75 my $cols = @
$results[0]->{loopcol
};
76 my $lines = @
$results[0]->{looprow
};
78 print "num /". @
$results[0]->{column
} .$sep;
80 foreach my $col ( @
$cols ) {
81 print $col->{coltitle
}.$sep;
85 foreach my $line ( @
$lines ) {
86 my $x = $line->{loopcell
}; # FIXME: No Such thing.
87 print $line->{rowtitle
}.$sep;
88 foreach my $cell (@
$x) {
89 print $cell->{value
}.$sep;
91 print $line->{totalrow
}, "\n";
95 foreach my $col ( @
$cols ) {
96 print $sep.$col->{totalcol
};
98 print $sep.@
$results[0]->{total
};
100 exit(1); # in either case, exit after do_it
103 # Displaying choices (i.e., not do_it)
107 my @mime = ( map { +{type
=>$_} } (split /[;:]/,C4
::Context
->preference("MIME")) );
108 my $itemtypes = GetItemTypes
;
110 foreach (sort {$itemtypes->{$a}->{description
} cmp $itemtypes->{$b}->{description
}} keys %$itemtypes) {
111 push @itemtypeloop, {
113 description
=> $itemtypes->{$_}->{'description'},
118 CGIextChoice
=> \
@mime,
119 CGIsepChoice
=> GetDelimiterChoices
,
120 itemtypeloop
=> \
@itemtypeloop,
121 branchloop
=> GetBranchesLoop
($input->param("branch") || C4
::Context
->userenv->{branch
}),
123 output_html_with_http_headers
$input, $cookie, $template->output;
127 my ($limit, $column, $filters) = @_;
132 my $dbh = C4
::Context
->dbh;
138 for (my $i=0;$i<=6;$i++) {
139 if ( @
$filters[$i] ) {
140 my %cell = (filter
=>@
$filters[$i]);
141 if (($i==1) and (@
$filters[$i-1])) {
142 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
144 $cell{crit
} = "Branch" if ($i==0);
145 $cell{crit
} = "Doc Type" if ($i==1);
146 push @loopfilter, \
%cell;
149 push @loopfilter, {crit
=>'limit', filter
=>$limit} if ($limit);
151 push @loopfilter, {crit
=>'by', filter
=>$column};
152 my $tablename = ($column =~ /branchcode/) ?
'branches' : 'items';
153 $column = ($column =~ /branchcode/ or $column =~ /itype/) ?
"$tablename.$column" : $column;
154 my $strsth2 = ($tablename eq 'branches') ?
155 "SELECT $column as coltitle, count(items.itemnumber) AS coltitle_count FROM $tablename LEFT JOIN items ON items.homebranch=$column " :
156 "SELECT $column as coltitle, count(*) AS coltitle_count FROM $tablename " ;
157 if ($tablename eq 'branches') {
158 my $f = @
$filters[0];
160 $strsth2 .= " AND $column LIKE '$f' " ;
162 $strsth2 .=" GROUP BY $column ORDER BY $column "; # needed for count
163 push @loopfilter, {crit
=>'SQL', sql
=>1, filter
=>$strsth2};
164 $debug and warn "catalogue_out SQL: ". $strsth2;
165 my $sth2 = $dbh->prepare($strsth2);
168 while (my ($celvalue, $count) = $sth2->fetchrow) {
169 ($celvalue) or $celvalue = 'UNKNOWN';
170 $columns{$celvalue} = $count;
174 my %tables = (map {$_=>[]} keys %columns);
176 # preparing calculation
179 SELECT items.barcode as barcode,
180 items.homebranch as branch,
181 items.itemcallnumber as itemcallnumber,
182 biblio.title as title,
183 biblio.biblionumber as biblionumber,
184 biblio.author as author";
185 ($column) and $query .= ",\n$column as col ";
188 LEFT JOIN biblio USING (biblionumber)
189 LEFT JOIN issues USING (itemnumber)
190 LEFT JOIN old_issues USING (itemnumber)
191 WHERE issues.itemnumber IS NULL
192 AND old_issues.itemnumber IS NULL
195 $filters->[0]=~ s/\*/%/g;
196 push @exe_args, $filters->[0];
197 $query .= " AND items.homebranch LIKE ?";
200 $filters->[1]=~ s/\*/%/g;
201 push @exe_args, $filters->[1];
202 $query .= " AND items.itype LIKE ?";
205 $query .= " AND $column = ? GROUP BY items.itemnumber, $column "; # placeholder handled below
207 $query .= " GROUP BY items.itemnumber ";
209 $query .= " ORDER BY items.itemcallnumber DESC, barcode";
210 $query .= " LIMIT 0,$limit" if ($limit);
211 $debug and warn "SQL : $query";
212 # warn "SQL : $query";
213 push @loopfilter, {crit
=>'SQL', sql
=>1, filter
=>$query};
214 my $dbcalc = $dbh->prepare($query);
217 foreach (sort keys %columns) {
218 my (@more_exe_args) = @exe_args; # execute(@exe_args,$_) would fail when the array is empty.
219 push @more_exe_args, $_; # but @more_exe_args will work
220 $dbcalc->execute(@more_exe_args) or die "Query execute(@more_exe_args) failed: $query";
221 while (my $data = $dbcalc->fetchrow_hashref) {
222 my $col = $data->{col
} || 'NULL';
223 $tables{$col} or $tables{$col} = [];
224 push @
{$tables{$col}}, $data;
228 (scalar @exe_args) ?
$dbcalc->execute(@exe_args) : $dbcalc->execute;
229 while (my $data = $dbcalc->fetchrow_hashref) {
230 my $col = $data->{col
} || 'NULL';
231 $tables{$col} or $tables{$col} = [];
232 push @
{$tables{$col}}, $data;
236 foreach my $tablename (sort keys %tables) {
239 foreach my $cell (@
{$tables{$tablename}}) {
240 if (0 == $i++ and $debug) {
241 my $dump = Dumper
($cell);
244 print STDERR
"first cell for $tablename: $dump";
246 push @temptable, $cell;
248 my $count = scalar(@temptable);
249 my $allitems = $columns{$tablename};
250 $globalline{total_looptable_count
} += $count;
251 $globalline{total_coltitle_count
} += $allitems;
252 push @
{$globalline{looptables
}}, {
253 looprow
=> \
@temptable,
254 coltitle
=> $tablename,
255 coltitle_count
=> $allitems,
256 looptable_count
=> $count,
257 looptable_first
=> ($count) ?
$temptable[ 0]->{itemcallnumber
} : '',
258 looptable_last
=> ($count) ?
$temptable[-1]->{itemcallnumber
} : '',
262 # the header of the table
263 $globalline{loopfilter
}=\
@loopfilter;
264 $globalline{limit
} = $limit;
265 $globalline{column
} = $column;
266 return [(\
%globalline)]; # reference to array of reference to hash