4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Koha qw( GetAuthorisedValues );
27 use C4::Reports qw( GetDelimiterChoices );
32 plugin that shows a stats on borrowers
39 my $do_it=$input->param('do_it');
40 my $fullreportname = "reports/cat_issues_top.tt";
41 my $limit = $input->param("Limit");
42 my $column = $input->param("Criteria");
43 my @filters = $input->multi_param("Filter");
45 my $output = $input->param("output");
46 my $basename = $input->param("basename");
47 #warn "calcul : ".$calc;
48 my ($template, $borrowernumber, $cookie)
49 = get_template_and_user({template_name => $fullreportname,
52 flagsrequired => { reports => '*'},
54 our $sep = $input->param("sep");
55 $sep = "\t" if ($sep eq 'tabulation');
56 $template->param(do_it => $do_it,
60 my $results = calculate($limit, $column, \@filters);
61 if ($output eq "screen"){
62 # Printing results to screen
63 $template->param(mainloop => $results,
65 output_html_with_http_headers $input, $cookie, $template->output;
68 # Printing to a csv file
69 print $input->header(-type => 'application/vnd.sun.xml.calc',
71 -attachment=>"$basename.csv",
72 -filename=>"$basename.csv" );
73 my $cols = @$results[0]->{loopcol};
74 my $lines = @$results[0]->{looprow};
76 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
78 foreach my $col ( @$cols ) {
79 print $col->{coltitle}.$sep;
83 foreach my $line ( @$lines ) {
84 my $x = $line->{loopcell};
85 print $line->{rowtitle}.$sep;
86 foreach my $cell (@$x) {
87 print $cell->{value}.$sep;
88 print $cell->{count} // '';
96 my $dbh = C4::Context->dbh;
98 my $CGIextChoice = ( 'CSV' ); # FIXME translation
99 my $CGIsepChoice=GetDelimiterChoices;
102 my $itemtypes = Koha::ItemTypes->search_with_localization;
105 my $ccodes = GetAuthorisedValues('CCODE');
107 for my $thisccode (@$ccodes) {
108 my %row = (value => $thisccode->{authorised_value},
109 description => $thisccode->{lib},
111 push @ccodeloop, \%row;
114 @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
117 my $shelvinglocs = GetAuthorisedValues('LOC');
119 for my $thisloc (@$shelvinglocs) {
120 my %row = (value => $thisloc->{authorised_value},
121 description => $thisloc->{lib},
123 push @shelvinglocloop, \%row;
126 @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
128 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
131 CGIextChoice => $CGIextChoice,
132 CGIsepChoice => $CGIsepChoice,
133 itemtypes => $itemtypes,
134 ccodeloop =>\@ccodeloop,
135 shelvinglocloop =>\@shelvinglocloop,
136 patron_categories => $patron_categories,
138 output_html_with_http_headers $input, $cookie, $template->output;
145 my ($line, $column, $filters) = @_;
152 my $dbh = C4::Context->dbh;
158 for (my $i=0;$i<=12;$i++) {
160 if ( @$filters[$i] ) {
161 if (($i==1) and (@$filters[$i-1])) {
162 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
164 $cell{filter} .= @$filters[$i];
165 $cell{crit} .="Issue From" if ($i==0);
166 $cell{crit} .="Issue To" if ($i==1);
167 $cell{crit} .="Return From" if ($i==2);
168 $cell{crit} .="Return To" if ($i==3);
169 $cell{crit} .="Branch" if ($i==4);
170 $cell{crit} .="Doc Type" if ($i==5);
171 $cell{crit} .="Call number" if ($i==6);
172 $cell{crit} .="Collection code" if ($i==7);
173 $cell{crit} .="Shelving location" if ($i==8);
174 $cell{crit} .="Bor Cat" if ($i==9);
175 $cell{crit} .="Day" if ($i==10);
176 $cell{crit} .="Month" if ($i==11);
177 $cell{crit} .="Year" if ($i==12);
178 push @loopfilter, \%cell;
184 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/issuedate/));
185 if($column=~/itemtype/){
186 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
188 $column = "borrowers.".$column if $column=~/categorycode/;
190 $colfilter[0] = @$filters[0] if ($column =~ /issuedate/ ) ;
191 $colfilter[1] = @$filters[1] if ($column =~ /issuedate/ ) ;
192 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
193 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
194 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
195 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
196 # These limits does not currently exist, maybe later?
197 # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ ) ;
198 # $colfilter[0] = @$filters[7] if ($column =~ /location/ ) ;
199 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
200 # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
201 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
202 $colfilter[0] = @$filters[9] if ($column =~ /issuedate/ ) ;
203 $colfilter[0] = @$filters[10] if ($column =~ /issuedate/ ) ;
204 $colfilter[0] = @$filters[11] if ($column =~ /issuedate/ ) ;
205 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
208 if ($column eq "Day") {
210 $column = "old_issues.issuedate";
211 $colfield .="dayname($column)";
212 $colorder .="weekday($column)";
213 } elsif ($column eq "Month") {
215 $column = "old_issues.issuedate";
216 $colfield .="monthname($column)";
217 $colorder .="month($column)";
218 } elsif ($column eq "Year") {
220 $column = "old_issues.issuedate";
221 $colfield .="Year($column)";
222 $colorder .= $column;
224 $colfield .= $column;
225 $colorder .= $column;
229 $strsth2 .= "SELECT distinctrow $colfield
231 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
232 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
233 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
235 if (($column=~/issuedate/) or ($column=~/returndate/)){
236 if ($colfilter[1] and ($colfilter[0])){
237 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
238 } elsif ($colfilter[1]) {
239 $strsth2 .= " and $column < '$colfilter[1]' " ;
240 } elsif ($colfilter[0]) {
241 $strsth2 .= " and $column > '$colfilter[0]' " ;
243 } elsif ($colfilter[0]) {
244 $colfilter[0] =~ s/\*/%/g;
245 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
247 $strsth2 .=" group by $colfield";
248 $strsth2 .=" order by $colorder";
250 my $sth2 = $dbh->prepare( $strsth2 );
251 if (( @colfilter ) and ($colfilter[1])){
252 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
253 } elsif ($colfilter[0]) {
254 $sth2->execute($colfilter[0]);
260 while (my ($celvalue) = $sth2->fetchrow) {
262 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
263 push @loopcol, \%cell;
265 # warn "fin des titres colonnes";
272 #Initialization of cell values.....
276 for (my $i=1;$i<=$line;$i++) {
277 foreach my $col ( @loopcol ) {
278 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
279 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
284 # preparing calculation
287 # Processing average loanperiods
288 $strcalc .= "SELECT biblio.title, COUNT(biblio.biblionumber) AS `RANK`, biblio.biblionumber AS ID";
289 $strcalc .= " , $colfield " if ($colfield);
290 $strcalc .= " FROM `old_issues`
291 LEFT JOIN items USING(itemnumber)
292 LEFT JOIN biblio USING(biblionumber)
293 LEFT JOIN biblioitems USING(biblionumber)
294 LEFT JOIN borrowers USING(borrowernumber)
297 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
298 $strcalc .= " AND old_issues.issuedate > '" . @$filters[0] ."'" if ( @$filters[0] );
299 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
300 $strcalc .= " AND old_issues.issuedate < '" . @$filters[1] ."'" if ( @$filters[1] );
301 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
302 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
303 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
304 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
305 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
306 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
307 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
309 if(C4::Context->preference('item-level_itypes') ){
310 $strcalc .= " AND items.itype like "
312 $strcalc .= " AND biblioitems.itemtype like "
314 $strcalc .= "'" . @$filters[5] ."'" ;
316 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
317 $strcalc .= " AND itemcallnumber like '" . @$filters[6] ."'" if ( @$filters[6] );
318 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
319 $strcalc .= " AND ccode like '" . @$filters[7] ."'" if ( @$filters[7] );
320 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
321 $strcalc .= " AND location like '" . @$filters[8] ."'" if ( @$filters[8] );
322 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
323 $strcalc .= " AND borrowers.categorycode like '" . @$filters[9] ."'" if ( @$filters[9] );
324 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
325 $strcalc .= " AND dayname(old_issues.issuedate) like '" . @$filters[10]."'" if (@$filters[10]);
326 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
327 $strcalc .= " AND monthname(old_issues.issuedate) like '" . @$filters[11]."'" if (@$filters[11]);
328 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
329 $strcalc .= " AND year(old_issues.issuedate) like '" . @$filters[12] ."'" if ( @$filters[12] );
331 $strcalc .= " group by biblio.biblionumber, biblio.title";
332 $strcalc .= ", $colfield" if ($column);
333 $strcalc .= " order by `RANK` DESC";
334 $strcalc .= ", $colfield " if ($colfield);
336 my $dbcalc = $dbh->prepare($strcalc);
339 while (my @data = $dbcalc->fetchrow) {
340 my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
341 $col = "zzEMPTY" if (!defined($col));
342 $indice{$col}=1 if (not($indice{$col}));
343 $table[$indice{$col}]->{$col}->{'name'}=$row;
344 $table[$indice{$col}]->{$col}->{'count'}=$rank;
345 $table[$indice{$col}]->{$col}->{'link'}=$id;
349 push @loopcol,{coltitle => "Global"} if not($column);
351 for ($i=1; $i<=$line;$i++) {
353 #@loopcol ensures the order for columns is common with column titles
354 # and the number matches the number of columns
356 foreach my $col ( @loopcol ) {
361 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
362 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
363 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
365 $value =$table[$i]->{"zzEMPTY"}->{'name'};
366 $count =$table[$i]->{"zzEMPTY"}->{'count'};
367 $link =$table[$i]->{"zzEMPTY"}->{'link'};
369 push @loopcell, {value => $value, count =>$count, reference => $link} ;
371 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
372 push @looprow,{ 'rowtitle' => $i ,
373 'loopcell' => \@loopcell,
374 'hilighted' => ($hilighted >0),
376 $hilighted = -$hilighted;
381 # the header of the table
382 $globalline{loopfilter}=\@loopfilter;
383 # the core of the table
384 $globalline{looprow} = \@looprow;
385 $globalline{loopcol} = \@loopcol;
386 # # the foot (totals by borrower type)
387 $globalline{total}= $grantotal;
388 $globalline{line} = $line;
389 $globalline{column} = $column;
390 push @mainloop,\%globalline;