From 46ac0122416b051890b9f20b74661f0e7b1388ee Mon Sep 17 00:00:00 2001
From: Katrin Fischer <Katrin.Fischer.83@web.de>
Date: Mon, 10 Nov 2014 22:30:16 +0100
Subject: [PATCH] Bug 12567: Make catalog statistics wizard publication year
 work for MARC21
Content-Type: text/plain; charset=utf-8

Publication year was useless for MARC21, as it was using the
biblioitems.publicationyear (UNIMARC) instead of biblio.copyrightdate
(MARC21).

This patch tries to fix the search options to work for both MARC
flavours.

To test:
In Reports > Statistics wizards > Catalog:
- Test the publication year search options on a UNIMARC and
  a MARC21 installation.
- Make sure search results make sense for both.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
The changes are fine with me, but this script is certainly not bug free.
Also note that all its SQL should be moved out according to our
coding guidelines. When doing so, a rewrite would be appropriate.
---
 reports/catalogue_stats.pl |   27 ++++++++++++++++++++++-----
 1 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/reports/catalogue_stats.pl b/reports/catalogue_stats.pl
index a19334a..2ab9bfe 100755
--- a/reports/catalogue_stats.pl
+++ b/reports/catalogue_stats.pl
@@ -59,6 +59,12 @@ if(C4::Context->preference('item-level_itypes')) {
 } else {
 	$item_itype = "itemtype";
 }
+if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
+    $line = "copyrightdate";
+}
+if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
+    $column = "copyrightdate";
+}
 
 my ($template, $borrowernumber, $cookie)
 	= get_template_and_user({template_name => $fullreportname,
@@ -284,7 +290,8 @@ if($barcodefilter){
 	}
 
 	my $strsth = "SELECT DISTINCTROW $linefield FROM biblioitems 
-	                INNER JOIN items USING (biblioitemnumber)
+                    INNER JOIN items USING (biblioitemnumber)
+                    INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
 	              WHERE $line IS NOT NULL ";
     $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
 	if ( @linefilter ) {
@@ -337,6 +344,8 @@ if($barcodefilter){
 	FROM   biblioitems
 	INNER JOIN items
 		USING (biblioitemnumber)
+    INNER JOIN biblio
+        ON (biblioitems.biblionumber = biblio.biblionumber)
 	WHERE $column IS NOT NULL ";
 	$strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
 	
@@ -386,7 +395,12 @@ if($barcodefilter){
 	}
 
 # preparing calculation
-	my $strcalc = "SELECT $linefield, $colfield, count(*) FROM biblioitems INNER JOIN  items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1 ";
+    my $strcalc = "
+        SELECT $linefield, $colfield, count(*)
+        FROM biblioitems
+        INNER JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
+        INNER JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
+        WHERE 1 ";
 	$strcalc .= "AND barcode $not like ? " if ($barcodefilter); 
 	
 	if (@$filters[0]){
@@ -429,11 +443,15 @@ if($barcodefilter){
 	}
 	if (@$filters[8]){
 		@$filters[8]=~ s/\*/%/g;
-		$strcalc .= " AND publicationyear >" . @$filters[8];
+        $strcalc .= " AND " .
+        (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
+        . ">" . @$filters[8];
 	}
 	if (@$filters[9]){
 		@$filters[9]=~ s/\*/%/g;
-		$strcalc .= " AND publicationyear <" . @$filters[9];
+        $strcalc .= " AND " .
+        (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
+        . "<" . @$filters[9];
 	}
 	if (@$filters[10]){
 		@$filters[10]=~ s/\*/%/g;
@@ -457,7 +475,6 @@ if($barcodefilter){
 	   $dbcalc->execute();
 	}
 #	warn "filling table";
-	
 	my $emptycol; 
 	while (my ($row, $col, $value) = $dbcalc->fetchrow) {
 #		warn "filling table $row / $col / $value ";
-- 
1.7.7.6