- if ( @linefilter ) {
- if ($linefilter[1]){
- $strsth .= " AND $line >= ? " ;
- $strsth .= " AND $line <= ? " ;
- } elsif ($linefilter[0]) {
- $linefilter[0] =~ s/\*/%/g;
- $strsth .= " AND $line LIKE ? " ;
- }
- }
- $strsth .=" ORDER BY $linefield";
- $debug and print STDERR "catalogue_stats SQL: $strsth\n";
-
- my $sth = $dbh->prepare( $strsth );
- if (( @linefilter ) and ($linefilter[1])){
- $sth->execute($barcodefilter,$linefilter[0],$linefilter[1]);
- } elsif ($barcodefilter,$linefilter[0]) {
- $sth->execute($barcodefilter,$linefilter[0]);
- } elsif ($barcodefilter) {
- $sth->execute($barcodefilter);
- }else{
- $sth->execute();
- }
- while ( my ($celvalue) = $sth->fetchrow) {
- my %cell;
- if ($celvalue) {
- $cell{rowtitle} = $celvalue;
-# } else {
-# $cell{rowtitle} = "";
- }
- $cell{totalrow} = 0;
- push @loopline, \%cell;
- }
-
-# 2nd, loop cols.
- my $colfield;
- if (($column =~/dewey/) and ($deweydigits)) {
- $colfield = "left($column,$deweydigits)";
- }elsif (($column=~/lccn/) and ($lccndigits)) {
- $colfield = "left($column,$lccndigits)";
- }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
- $colfield = "left($column,$cotedigits)";
- }else {
- $colfield = $column;
- }
-
- my $strsth2 = "
- SELECT distinctrow $colfield
- FROM biblioitems
- INNER JOIN items
- USING (biblioitemnumber)
- WHERE $column IS NOT NULL ";
- $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
-
- if (( @colfilter ) and ($colfilter[1])) {
- $strsth2 .= " AND $column> ? AND $column< ?";
- }elsif ($colfilter[0]){
- $colfilter[0] =~ s/\*/%/g;
- $strsth2 .= " AND $column LIKE ? ";
- }
- $strsth2 .= " ORDER BY $colfield";
- $debug and print STDERR "SQL: $strsth2";
- my $sth2 = $dbh->prepare( $strsth2 );
- if ((@colfilter) and ($colfilter[1])) {
- $sth2->execute($barcodefilter,$colfilter[0],$colfilter[1]);
- } elsif ($colfilter[0]){
- $sth2->execute($barcodefilter,$colfilter[0]);
- } elsif ($barcodefilter){
- $sth2->execute($barcodefilter);
- } else {
- $sth2->execute();
- }
- while (my ($celvalue) = $sth2->fetchrow) {
- my %cell;
- my %ft;
- if ($celvalue) {
- $cell{coltitle} = $celvalue;
-# } else {
-# $cell{coltitle} = "";
- }
- $ft{totalcol} = 0;
- push @loopcol, \%cell;
- }
-
- my $i=0;
- my @totalcol;
- my $hilighted=-1;
-
- #Initialization of cell values.....
- my %table;
-# warn "init table";
- foreach my $row ( @loopline ) {
- foreach my $col ( @loopcol ) {
-# warn " init table : $row->{rowtitle} / $col->{coltitle} ";
- $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
- }
- $table{$row->{rowtitle}}->{totalrow}=0;
- }
-
-# preparing calculation
- my $strcalc = "SELECT $linefield, $colfield, count(*) FROM biblioitems INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1 ";
- $strcalc .= "AND barcode $not like ? " if ($barcodefilter);
-
- if (@$filters[0]){
- @$filters[0]=~ s/\*/%/g;
- $strcalc .= " AND dewey >" . @$filters[0];
- }
- if (@$filters[1]){
- @$filters[1]=~ s/\*/%/g ;
- $strcalc .= " AND dewey <" . @$filters[1];
- }
- if (@$filters[2]){
- @$filters[2]=~ s/\*/%/g ;
- $strcalc .= " AND lccn >" . @$filters[2];
- }
- if (@$filters[3]){
- @$filters[3]=~ s/\*/%/g;
- $strcalc .= " AND lccn <" . @$filters[3];
- }
- if (@$filters[4]){
- @$filters[4]=~ s/\*/%/g ;
- $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
- }
-
- if (@$filters[5]){
- @$filters[5]=~ s/\*/%/g;
- $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
- }
-
- if (@$filters[6]){
- @$filters[6]=~ s/\*/%/g;
- $strcalc .= " AND " .
- (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
- . " LIKE '" . @$filters[6] ."'";
- }
-
- if (@$filters[7]){
- @$filters[7]=~ s/\*/%/g;
- @$filters[7].="%" unless @$filters[7]=~/%/;
- $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
- }
- if (@$filters[8]){
- @$filters[8]=~ s/\*/%/g;
- $strcalc .= " AND publicationyear >" . @$filters[8];
- }
- if (@$filters[9]){
- @$filters[9]=~ s/\*/%/g;
- $strcalc .= " AND publicationyear <" . @$filters[9];
- }
- if (@$filters[10]){
- @$filters[10]=~ s/\*/%/g;
- $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
- }
- if (@$filters[11]){
- @$filters[11]=~ s/\*/%/g;
- $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
- }
- if (@$filters[12]){
- @$filters[12]=~ s/\*/%/g;
- $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
- }
-
- $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
- $debug and warn "SQL: $strcalc";
- my $dbcalc = $dbh->prepare($strcalc);
- if($barcodefilter){
- $dbcalc->execute($barcodefilter);
- }else{
- $dbcalc->execute();
- }
-# warn "filling table";
-
- my $emptycol;
- while (my ($row, $col, $value) = $dbcalc->fetchrow) {
-# warn "filling table $row / $col / $value ";
- $emptycol = 1 if (!defined($col));
- $col = "zzEMPTY" if (!defined($col));
- $row = "zzEMPTY" if (!defined($row));
-
- $table{$row}->{$col}+=$value;
- $table{$row}->{totalrow}+=$value;
- $grantotal += $value;
- }
-
-# my %cell = {rowtitle => 'zzROWEMPTY'};
-# push @loopline,\%cell;
-# undef %cell;
-# my %cell;
-# %cell = {coltitle => "zzEMPTY"};
- push @loopcol,{coltitle => "NULL"} if ($emptycol);
-
- foreach my $row ( sort keys %table ) {
- my @loopcell;
- #@loopcol ensures the order for columns is common with column titles
- # and the number matches the number of columns
- foreach my $col ( @loopcol ) {
- my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
- push @loopcell, {value => $value } ;
- }
- push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
- 'loopcell' => \@loopcell,
- 'hilighted' => ($hilighted *= -1 > 0),
- 'totalrow' => $table{$row}->{totalrow}
- };
- }
-
-# warn "footer processing";
- foreach my $col ( @loopcol ) {
- my $total=0;
- foreach my $row ( @looprow ) {
- $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
-# warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
- }
-# warn "summ for column ".$col->{coltitle}." = ".$total;
- push @loopfooter, {'totalcol' => $total};
- }
-
-
- # the header of the table
- $globalline{loopfilter}=\@loopfilter;
- # the core of the table
- $globalline{looprow} = \@looprow;
- $globalline{loopcol} = \@loopcol;
-# # the foot (totals by borrower type)
- $globalline{loopfooter} = \@loopfooter;
- $globalline{total}= $grantotal;
- $globalline{line} = $line;
- $globalline{column} = $column;
- push @mainloop,\%globalline;
- return \@mainloop;
-}
+ if (@linefilter) {
+ if ( $linefilter[1] ) {
+ $strsth .= " AND $line >= ? ";
+ $strsth .= " AND $line <= ? ";
+ } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
+ $linefilter[0] =~ s/\*/%/g;
+ $strsth .= " AND $line LIKE ? ";
+ }
+ }
+ $strsth .= " ORDER BY $linefield";
+ $debug and print STDERR "catalogue_stats SQL: $strsth\n";
+
+ my $sth = $dbh->prepare($strsth);
+ if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
+ $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
+ } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
+ $sth->execute( $linefilter[0], $linefilter[1] );
+ } elsif ( $barcodefilter and $linefilter[0] ) {
+ $sth->execute( $barcodefilter, $linefilter[0] );
+ } elsif ( $linefilter[0] ) {
+ $sth->execute($linefilter[0]);
+ } elsif ($barcodefilter) {
+ $sth->execute($barcodefilter);
+ } else {
+ $sth->execute();
+ }
+ my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
+ while ( my ($celvalue) = $sth->fetchrow ) {
+ my %cell;
+ if (defined $celvalue and $celvalue ne '') {
+ if($rowauthvals and $rowauthvals->{$celvalue}) {
+ $cell{rowtitle} = $rowauthvals->{$celvalue};
+ } else {
+ $cell{rowtitle} = $celvalue;
+ }
+ $cell{value} = $celvalue;
+ }
+ else {
+ $cell{rowtitle} = "NULL";
+ $cell{value} = "zzEMPTY";
+ }
+ $cell{totalrow} = 0;
+ push @loopline, \%cell;
+ }
+
+ # 2nd, loop cols.
+ my $origcolumn = $column;
+ $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
+ my $colfield;
+ if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
+ $colfield = "left($column,$cotedigits)";
+ } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
+ $colfield = "DATE($column)";
+ } else {
+ $colfield = $column;
+ }
+
+ my $strsth2 = "
+ SELECT distinctrow $colfield
+ FROM $itemstable
+ LEFT JOIN biblioitems
+ USING (biblioitemnumber)
+ LEFT JOIN biblio
+ ON (biblioitems.biblionumber = biblio.biblionumber)
+ WHERE 1 ";
+ $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
+
+ if ( (@colfilter) and ( $colfilter[1] ) ) {
+ $strsth2 .= " AND $column >= ? AND $column <= ?";
+ } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
+ $colfilter[0] =~ s/\*/%/g;
+ $strsth2 .= " AND $column LIKE ? ";
+ }
+ $strsth2 .= " ORDER BY $colfield";
+ $debug and print STDERR "SQL: $strsth2";
+ my $sth2 = $dbh->prepare($strsth2);
+ if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
+ $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
+ } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
+ $sth2->execute( $colfilter[0], $colfilter[1] );
+ } elsif ( $barcodefilter && $colfilter[0] ) {
+ $sth2->execute( $barcodefilter , $colfilter[0] );
+ } elsif ( $colfilter[0]) {
+ $sth2->execute( $colfilter[0] );
+ } elsif ($barcodefilter) {
+ $sth2->execute($barcodefilter);
+ } else {
+ $sth2->execute();
+ }
+ my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
+ while ( my ($celvalue) = $sth2->fetchrow ) {
+ my %cell;
+ if (defined $celvalue and $celvalue ne '') {
+ if($colauthvals and $colauthvals->{$celvalue}) {
+ $cell{coltitle} = $colauthvals->{$celvalue};
+ } else {
+ $cell{coltitle} = $celvalue;
+ }
+ $cell{value} = $celvalue;
+ }
+ else {
+ $cell{coltitle} = "NULL";
+ $cell{value} = "zzEMPTY";
+ }
+ $cell{totalcol} = 0;
+ push @loopcol, \%cell;
+ }
+
+ my $i = 0;
+ my $hilighted = -1;
+
+ #Initialization of cell values.....
+ my %table;
+
+ foreach my $row (@loopline) {
+ foreach my $col (@loopcol) {
+ $table{ $row->{value} }->{ $col->{value} } = 0;
+ }
+ $table{ $row->{value} }->{totalrow} = 0;
+ }
+
+ # preparing calculation
+ my $select_cellvalue = " COUNT(*) ";
+ $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
+ my $strcalc = "
+ SELECT $linefield, $colfield, $select_cellvalue
+ FROM $itemstable
+ LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
+ LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
+ WHERE 1 ";
+
+ my @sqlargs;
+
+ if ($barcodefilter) {
+ $strcalc .= "AND barcode $not like ? ";
+ push @sqlargs, $barcodefilter;
+ }
+
+ if ( @$filters[0] ) {
+ $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
+ @$filters[0] =~ s/\*/%/g;
+ push @sqlargs, @$filters[0];
+ }