From 65bebc7bcfc396a4efecd17efc312f5bd4f68c01 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle.m.hall@gmail.com>
Date: Fri, 5 Feb 2010 16:04:53 +0000
Subject: [PATCH] Bug 4118 - Add Collection Code to Statistics Table
Content-Type: text/plain; charset="utf-8"

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
---
 C4/Circulation.pm                      |    8 ++++----
 C4/Stats.pm                            |    8 ++++----
 installer/data/mysql/updatedatabase.pl |   12 ++++++++++++
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 48b7b4a..e88aaa7 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -732,7 +732,7 @@ sub CanBookBeIssued {
     #
     if ( $borrower->{'category_type'} eq 'X' && (  $item->{barcode}  )) { 
     	# stats only borrower -- add entry to statistics table, and return issuingimpossible{STATS} = 1  .
-        &UpdateStats(C4::Context->userenv->{'branch'},'localuse','','',$item->{'itemnumber'},$item->{'itemtype'},$borrower->{'borrowernumber'});
+        &UpdateStats(C4::Context->userenv->{'branch'},'localuse','','',$item->{'itemnumber'},$item->{'itemtype'},$borrower->{'borrowernumber'}, undef, $item->{'ccode'});
         ModDateLastSeen( $item->{'itemnumber'} );
         return( { STATS => 1 }, {});
     }
@@ -1172,7 +1172,7 @@ sub AddIssue {
             C4::Context->userenv->{'branch'},
             'issue', $charge,
             ($sipmode ? "SIP-$sipmode" : ''), $item->{'itemnumber'},
-            $item->{'itype'}, $borrower->{'borrowernumber'}
+            $item->{'itype'}, $borrower->{'borrowernumber'}, undef, $item->{'ccode'}
         );
 
         # Send a checkout slip.
@@ -1740,7 +1740,7 @@ sub AddReturn {
         $branch, $stat_type, '0', '',
         $item->{'itemnumber'},
         $biblio->{'itemtype'},
-        $borrowernumber
+        $borrowernumber, undef, $item->{'ccode'}
     );
 
     # Send a check-in slip. # NOTE: borrower may be undef.  probably shouldn't try to send messages then.
@@ -2472,7 +2472,7 @@ sub AddRenewal {
             'Rent', $charge, $itemnumber );
     }
     # Log the renewal
-    UpdateStats( $branch, 'renew', $charge, '', $itemnumber, $item->{itype}, $borrowernumber);
+    UpdateStats( $branch, 'renew', $charge, '', $itemnumber, $item->{itype}, $borrowernumber, undef, $item->{'ccode'});
 	return $datedue;
 }
 
diff --git a/C4/Stats.pm b/C4/Stats.pm
index 7457983..e1cbd42 100644
--- a/C4/Stats.pm
+++ b/C4/Stats.pm
@@ -76,20 +76,20 @@ sub UpdateStats {
     my (
         $branch,         $type,
         $amount,   $other,          $itemnum,
-        $itemtype, $borrowernumber, $accountno
+        $itemtype, $borrowernumber, $accountno, $ccode
       )
       = @_;
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare(
         "INSERT INTO statistics
         (datetime, branch, type, value,
-         other, itemnumber, itemtype, borrowernumber, proccode)
-         VALUES (now(),?,?,?,?,?,?,?,?)"
+         other, itemnumber, itemtype, borrowernumber, proccode, ccode)
+         VALUES (now(),?,?,?,?,?,?,?,?,?)"
     );
     $sth->execute(
         $branch,    $type,    $amount,
         $other,     $itemnum, $itemtype, $borrowernumber,
-		$accountno
+		$accountno, $ccode
     );
 }
 
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 3c4b02a..eb4f2fe 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -5715,6 +5715,18 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     SetVersion ($DBversion);
 }
 
+$DBversion = '3.09.00.XXX';
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+	$dbh->do("ALTER TABLE statistics ADD COLUMN ccode VARCHAR ( 10 ) NULL AFTER associatedborrower");
+	$dbh->do("UPDATE statistics SET statistics.ccode = ( SELECT items.ccode FROM items WHERE statistics.itemnumber = items.itemnumber )");
+	$dbh->do("UPDATE statistics SET statistics.ccode = (
+	  SELECT deleteditems.ccode FROM deleteditems
+          WHERE statistics.itemnumber = deleteditems.itemnumber
+	  ) WHERE statistics.ccode IS NULL");
+	print "Upgrade done ( Added Collection Code to Statistics table. )\n";
+    SetVersion ($DBversion);
+}
+
 =head1 FUNCTIONS
 
 =head2 TableExists($table)
-- 
1.7.2.5