5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
32 use C4::Interface::CGI::Output;
33 use C4::Circulation::Circ2;
37 plugin that shows a stats on borrowers
47 my $do_it=$input->param('do_it');
48 my $fullreportname = "reports/acquisitions_stats.tmpl";
49 my $line = $input->param("Line");
50 my $column = $input->param("Column");
51 my @filters = $input->param("Filter");
52 my $podsp = $input->param("PlacedOnDisplay");
53 my $rodsp = $input->param("ReceivedOnDisplay");
54 my $aodsp = $input->param("AcquiredOnDisplay"); ##added by mason.
55 my $calc = $input->param("Cellvalue");
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my $mime = $input->param("MIME");
59 my $del = $input->param("sep");
60 #warn "calcul : ".$calc;
61 my ($template, $borrowernumber, $cookie)
62 = get_template_and_user({template_name => $fullreportname,
66 flagsrequired => {editcatalogue => 1},
69 $template->param(do_it => $do_it);
71 warn "line=$line, col=$column, pod=$podsp, rod=$rodsp, aod=$aodsp, calc=$calc, filters=@filters\n";
73 my $results = calculate($line, $column, $podsp, $rodsp, $aodsp, $calc, \@filters);
74 if ($output eq "screen"){
75 $template->param(mainloop => $results);
76 output_html_with_http_headers $input, $cookie, $template->output;
79 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -name=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 $sep =C4::Context->preference("delimiter");
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
109 my $dbh = C4::Context->dbh;
114 $req = $dbh->prepare("SELECT distinctrow id,name FROM aqbooksellers ORDER BY name");
119 while (my ($value, $desc) =$req->fetchrow) {
121 # $select{$value}=$desc;
123 my $CGIBookSellers=CGI::scrolling_list( -name => 'Filter',
126 # -labels => \%select,
130 $req = $dbh->prepare( "SELECT DISTINCTROW itemtype,description FROM itemtypes ORDER BY description");
136 while (my ($value,$desc) =$req->fetchrow) {
137 push @select, $value;
138 $select{$value}=$desc;
140 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
147 $req = $dbh->prepare( "SELECT DISTINCTROW bookfundid,bookfundname FROM aqbookfund ORDER BY bookfundname");
153 while (my ($value,$desc) =$req->fetchrow) {
154 push @select, $value;
155 $select{$value}=$desc;
157 my $CGIBudget=CGI::scrolling_list( -name => 'Filter',
164 $req = $dbh->prepare("SELECT DISTINCTROW sort1 FROM aqorders WHERE sort1 IS NOT NULL ORDER BY sort1");
169 while (my ($value) =$req->fetchrow) {
170 $hassort1 =1 if ($value);
171 push @select, $value;
173 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
179 $req = $dbh->prepare("SELECT DISTINCTROW sort2 FROM aqorders WHERE sort2 IS NOT NULL ORDER BY sort2");
185 while (my ($value) =$req->fetchrow) {
186 $hassort2 =1 if ($value);
187 $hglghtsort2= !($hassort1);
188 push @select, $value;
190 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
196 my @mime = ( C4::Context->preference("MIME") );
197 foreach my $mime (@mime){
201 my $CGIextChoice=CGI::scrolling_list(
208 my @dels = ( C4::Context->preference("delimiter") );
209 my $CGIsepChoice=CGI::scrolling_list(
217 CGIBookSeller => $CGIBookSellers,
218 CGIItemType => $CGIItemTypes,
219 CGIBudget => $CGIBudget,
220 hassort1=> $hassort1,
221 hassort2=> $hassort2,
222 HlghtSort2 => $hglghtsort2,
223 CGISort1 => $CGISort1,
224 CGISort2 => $CGISort2,
225 CGIextChoice => $CGIextChoice,
226 CGIsepChoice => $CGIsepChoice
230 output_html_with_http_headers $input, $cookie, $template->output;
235 my ($line, $column, $podsp, $rodsp, $aodsp, $process, $filters) = @_;
244 my $dbh = C4::Context->dbh;
250 for (my $i=0;$i<=8;$i++) {
252 if ( @$filters[$i] ) {
253 if ((($i==1) or ($i==3)) and (@$filters[$i-1])) {
254 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
256 $cell{filter} .= @$filters[$i];
257 $cell{crit} .="Placed On From" if ($i==0);
258 $cell{crit} .="Placed On To" if ($i==1);
259 $cell{crit} .="Received On From" if ($i==2);
260 $cell{crit} .="Received On To" if ($i==3);
262 $cell{crit} .="Acquired On From" if ($i==4);
263 $cell{crit} .="Acquired On To" if ($i==5);
265 $cell{crit} .="BookSeller" if ($i==6);
266 $cell{crit} .="Doc Type" if ($i==7);
267 $cell{crit} .="Budget" if ($i==8);
268 $cell{crit} .="Sort1" if ($i==9);
269 $cell{crit} .="Sort2" if ($i==10);
270 push @loopfilter, \%cell;
275 # warn "filtres ".@filters[0];
276 # warn "filtres ".@filters[1];
277 # warn "filtres ".@filters[2];
278 # warn "filtres ".@filters[3];
280 $linefilter[0] = @$filters[0] if ($line =~ /closedate/ ) ;
281 $linefilter[1] = @$filters[1] if ($line =~ /closedate/ ) ;
282 $linefilter[0] = @$filters[2] if ($line =~ /received/ ) ;
283 $linefilter[1] = @$filters[3] if ($line =~ /received/ ) ;
285 $linefilter[0] = @$filters[4] if ($line =~ /acquired/ ) ;
286 $linefilter[1] = @$filters[5] if ($line =~ /acquired/ ) ;
288 $linefilter[0] = @$filters[6] if ($line =~ /bookseller/ ) ;
289 $linefilter[0] = @$filters[7] if ($line =~ /itemtype/ ) ;
290 $linefilter[0] = @$filters[8] if ($line =~ /bookfund/ ) ;
291 $linefilter[0] = @$filters[9] if ($line =~ /sort1/ ) ;
292 $linefilter[0] = @$filters[10] if ($line =~ /sort2/ ) ;
293 #warn "filtre lignes".$linefilter[0]." ".$linefilter[1];
296 $colfilter[0] = @$filters[0] if ($column =~ /closedate/ ) ;
297 $colfilter[1] = @$filters[1] if ($column =~ /closedate/ ) ;
298 $colfilter[0] = @$filters[2] if ($column =~ /received/ ) ;
299 $colfilter[1] = @$filters[3] if ($column =~ /received/ ) ;
301 $colfilter[0] = @$filters[4] if ($column =~ /acquired/ ) ;
302 $colfilter[1] = @$filters[5] if ($column =~ /acquired/ ) ;
304 $colfilter[0] = @$filters[6] if ($column =~ /bookseller/ );
305 $colfilter[0] = @$filters[7] if ($column =~ /itemtype/ ) ;
306 $colfilter[0] = @$filters[8] if ($column =~ /bookfund/ ) ;
307 $colfilter[0] = @$filters[9] if ($column =~ /sort1/ ) ;
308 $colfilter[0] = @$filters[10] if ($column =~ /sort2/ ) ;
309 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
311 warn "line=$line, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
315 if (($line =~/closedate/) and ($podsp == 1)) {
317 $linefield .="dayname($line)";
318 } elsif (($line=~/closedate/) and ($podsp == 2)) {
320 $linefield .="monthname($line)";
321 } elsif (($line=~/closedate/) and ($podsp == 3)) {
323 $linefield .="Year($line)";
325 } elsif (($line =~/received/) and ($rodsp == 1)) {
327 $linefield .="dayname($line)";
328 } elsif (($line=~/received/) and ($rodsp == 2)) {
330 $linefield .="monthname($line)";
331 } elsif (($line=~/received/) and ($rodsp == 3)) {
333 $linefield .="Year($line)";
335 } elsif (($line =~/acquired/) and ($aodsp == 1)) {
337 $linefield .="dayname($line)";
338 } elsif (($line=~/acquired/) and ($aodsp == 2)) {
340 $linefield .="monthname($line)";
341 } elsif (($line=~/acquired/) and ($aodsp == 3)) {
343 $linefield .="Year($line)";
351 $strsth .= "SELECT DISTINCTROW $linefield FROM aqorders, aqbasket,aqorderbreakdown
352 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
353 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
354 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
355 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
356 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) AND $line IS NOT NULL ";
361 $strsth .= " AND $line BETWEEN ? AND ? " ;
363 $strsth .= " AND $line < ? " ;
365 } elsif (($linefilter[0]) and (($line=~/closedate/) or ($line=~/received/) or ($line=~/acquired/))){
366 $strsth .= " AND $line > ? " ;
367 } elsif ($linefilter[0]) {
368 $linefilter[0] =~ s/\*/%/g;
369 $strsth .= " AND $line LIKE ? " ;
372 $strsth .=" GROUP BY $linefield";
373 $strsth .=" ORDER BY $linefield";
374 warn "377:strsth= $strsth";
376 my $sth = $dbh->prepare( $strsth );
377 if (( @linefilter ) and ($linefilter[1])){
378 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
379 } elsif ($linefilter[0]) {
380 $sth->execute($linefilter[0]);
385 while ( my ($celvalue) = $sth->fetchrow) {
388 $cell{rowtitle} = $celvalue;
390 # $cell{rowtitle} = "";
393 push @loopline, \%cell;
396 warn "column=$column, podsp=$podsp, rodsp=$rodsp, aodsp=$aodsp\n";
399 if (($column =~/closedate/) and ($podsp == 1)) {
401 $colfield .="dayname($column)";
402 } elsif (($column=~/closedate/) and ($podsp == 2)) {
404 $colfield .="monthname($column)";
405 } elsif (($column=~/closedate/) and ($podsp == 3)) {
407 $colfield .="Year($column)";
409 } elsif (($column =~/deliverydate/) and ($rodsp == 1)) {
411 $colfield .="dayname($column)";
412 } elsif (($column=~/deliverydate/) and ($rodsp == 2)) {
414 $colfield .="monthname($column)";
415 } elsif (($column=~/deliverydate/) and ($rodsp == 3)) {
417 $colfield .="Year($column)";
419 } elsif (($column =~/dateaccessioned/) and ($aodsp == 1)) {
421 $colfield .="dayname($column)";
422 } elsif (($column=~/dateaccessioned/) and ($aodsp == 2)) {
424 $colfield .="monthname($column)";
425 } elsif (($column=~/dateaccessioned/) and ($aodsp == 3)) {
427 $colfield .="Year($column)";
430 $colfield .= $column;
434 $strsth2 .= "SELECT distinctrow $colfield FROM aqorders, aqbasket,aqorderbreakdown
435 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
436 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
437 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
438 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id)
439 WHERE (aqorders.basketno=aqbasket.basketno) AND (aqorderbreakdown.ordernumber=aqorders.ordernumber)
440 AND $column IS NOT NULL";
445 $strsth2 .= " AND $column BETWEEN ? AND ? " ;
447 $strsth2 .= " AND $column < ? " ;
449 } elsif (($colfilter[0]) and (($column=~/closedate/) or ($line=~/received/) or ($line=~/acquired/))){
450 $strsth2 .= " AND $column > ? " ;
451 } elsif ($colfilter[0]) {
452 $colfilter[0] =~ s/\*/%/g;
453 $strsth2 .= " AND $column LIKE ? " ;
456 $strsth2 .=" GROUP BY $colfield";
457 $strsth2 .=" ORDER BY $colfield";
458 # warn "MASON:. $strsth2";
460 my $sth2 = $dbh->prepare( $strsth2 );
461 if (( @colfilter ) and ($colfilter[1])){
462 # warn "from : ".$colfilter[0]." To :".$colfilter[1];
463 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
464 } elsif ($colfilter[0]) {
465 $sth2->execute($colfilter[0]);
470 while (my ($celvalue) = $sth2->fetchrow) {
473 # warn "coltitle :".$celvalue;
474 $cell{coltitle} = $celvalue;
476 push @loopcol, \%cell;
478 # warn "fin des titres colonnes";
484 #Initialization of cell values.....
487 foreach my $row ( @loopline ) {
488 foreach my $col ( @loopcol ) {
489 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
490 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
492 $table{$row->{rowtitle}}->{totalrow}=0;
495 # preparing calculation
497 $strcalc .= "SELECT $linefield, $colfield, ";
498 $strcalc .= "COUNT( aqorders.ordernumber ) " if ($process ==1);
499 $strcalc .= "SUM( aqorders.quantity * aqorders.listprice ) " if ($process ==2);
500 $strcalc .= "FROM aqorders, aqbasket,aqorderbreakdown
501 LEFT JOIN items ON (aqorders.biblioitemnumber= items.biblioitemnumber)
502 LEFT JOIN biblioitems ON (aqorders.biblioitemnumber= biblioitems.biblioitemnumber)
503 LEFT JOIN aqorderdelivery ON (aqorders.ordernumber =aqorderdelivery.ordernumber )
504 LEFT JOIN aqbooksellers ON (aqbasket.booksellerid=aqbooksellers.id) WHERE (aqorders.basketno=aqbasket.basketno)
505 AND (aqorderbreakdown.ordernumber=aqorders.ordernumber) ";
507 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
508 $strcalc .= " AND aqbasket.closedate > '" . @$filters[0] ."'" if ( @$filters[0] );
509 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
510 $strcalc .= " AND aqbasket.closedate < '" . @$filters[1] ."'" if ( @$filters[1] );
511 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
512 $strcalc .= " AND aqorderdelivery.deliverydate > '" . @$filters[2] ."'" if ( @$filters[2] );
513 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
514 $strcalc .= " AND aqorderdelivery.deliverydate < '" . @$filters[3] ."'" if ( @$filters[3] );
515 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
516 $strcalc .= " AND aqbasket.closedate > '" . @$filters[4] ."'" if ( @$filters[4] );
517 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
518 $strcalc .= " AND aqbasket.closedate < '" . @$filters[5] ."'" if ( @$filters[5] );
519 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
520 $strcalc .= " AND aqbooksellers.name LIKE '" . @$filters[6] ."'" if ( @$filters[6] );
521 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
522 $strcalc .= " AND biblioitems.itemtype LIKE '" . @$filters[7] ."'" if ( @$filters[7] );
523 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
524 $strcalc .= " AND aqbookfund.bookfundid LIKE '" . @$filters[8] ."'" if ( @$filters[8] );
525 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
526 $strcalc .= " AND aqorders.sort1 LIKE '" . @$filters[9] ."'" if ( @$filters[9] );
527 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
528 $strcalc .= " AND aqorders.sort2 LIKE '" . @$filters[10] ."'" if ( @$filters[10] );
529 $strcalc .= " GROUP BY $linefield, $colfield ORDER BY $linefield,$colfield";
530 # warn "/n/n". $strcalc;
531 my $dbcalc = $dbh->prepare($strcalc);
534 # warn "filling table";
536 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
537 # warn "filling table $row / $col / $value ";
538 $emptycol = 1 if ($col eq undef);
539 $col = "zzEMPTY" if ($col eq undef);
540 $row = "zzEMPTY" if ($row eq undef);
542 $table{$row}->{$col}+=$value;
543 $table{$row}->{totalrow}+=$value;
544 $grantotal += $value;
547 push @loopcol,{coltitle => "NULL"} if ($emptycol);
549 foreach my $row ( sort keys %table ) {
551 #@loopcol ensures the order for columns is common with column titles
552 # and the number matches the number of columns
553 foreach my $col ( @loopcol ) {
554 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
555 push @loopcell, {value => $value } ;
557 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
558 'loopcell' => \@loopcell,
559 'hilighted' => ($hilighted >0),
560 'totalrow' => $table{$row}->{totalrow}
562 $hilighted = -$hilighted;
565 # warn "footer processing";
566 foreach my $col ( @loopcol ) {
568 foreach my $row ( @looprow ) {
569 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
570 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
572 # warn "summ for column ".$col->{coltitle}." = ".$total;
573 push @loopfooter, {'totalcol' => $total};
577 # the header of the table
578 # $globalline{loopfilter}=\@loopfilter;
579 # the core of the table
580 $globalline{looprow} = \@looprow;
581 $globalline{loopcol} = \@loopcol;
582 # # the foot (totals by borrower type)
583 $globalline{loopfooter} = \@loopfooter;
584 $globalline{total}= $grantotal;
585 $globalline{line} = $line;
586 $globalline{column} = $column;
587 push @mainloop,\%globalline;