From 333085672b762412cc62eccf7acd4f7027aab6bb Mon Sep 17 00:00:00 2001
From: Lucas Gass <lucas@bywatersolutions.com>
Date: Thu, 2 Feb 2023 01:36:25 +0000
Subject: [PATCH] Bug 30928: Add interface column: DB update, schema changes,
 and UpdateStats

To test:
Apply patch, updatedatabase, and restart_all

Do each of the following actions and check the statistics table making sure the interface is properly being recorded:

RENEWALS:
-Renewal from staff client
-Renew from OPAC
-Renew from API. You could add something like this to IntranetUSerJS:
$.ajax({
    url: '/api/v1/checkouts/338/renewal',
    method: 'POST',
    contentType: 'application/json',
    success: function(result) {
        console.log('renewed')
    },
});
-Renewal from automatic_renewals
-Renewal from SIP ( can use sip_cli_emulator.pl )

CHECKOUTS:
-Checkout from staff client
-Checkout from SCO
-Checkout from API
-Checkout from SIP ( can use sip_cli_emulator.pl )

CHECKIN:
-Checkin from staff client
-Check in from batch
-Check in from API
-Check in from SCI
-Check in from SIP ( can use sip_cli_emulator.pl )

RECALLS:
-OPAC recall
-Staff client recall

ACCOUNTING:
-Make a payment
-Make a payemnt from SIP ( can use sip_cli_emulator.pl )

prove -v /kohadevbox/koha/t/db_dependent/Circulation.t
Tests should all pass

Signed-off-by: emlam <emily.lamancusa@montgomerycountymd.gov>
---
 C4/Circulation.pm                              |  6 +++++-
 C4/Stats.pm                                    |  7 +++++--
 Koha/Account.pm                                |  1 +
 Koha/Recalls.pm                                |  3 ++-
 Koha/Schema/Result/Statistic.pm                | 10 ++++++++++
 installer/data/mysql/atomicupdate/bug_30928.pl | 18 ++++++++++++++++++
 installer/data/mysql/kohastructure.sql         |  1 +
 7 files changed, 42 insertions(+), 4 deletions(-)
 create mode 100755 installer/data/mysql/atomicupdate/bug_30928.pl

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index c5c9701201..80cbe6fef5 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -825,6 +825,7 @@ sub CanBookBeIssued {
                 ccode          => $item_object->ccode,
                 categorycode   => $patron->categorycode,
                 location       => $item_object->location,
+                interface      => C4::Context->interface,
             }
         );
         ModDateLastSeen( $item_object->itemnumber ); # FIXME Move to Koha::Item
@@ -1754,7 +1755,8 @@ sub AddIssue {
                     location       => $item_object->location,
                     borrowernumber => $borrower->{'borrowernumber'},
                     ccode          => $item_object->ccode,
-                    categorycode   => $borrower->{'categorycode'}
+                    categorycode   => $borrower->{'categorycode'},
+                    interface      => C4::Context->interface,
                 }
             );
 
@@ -2380,6 +2382,7 @@ sub AddReturn {
         borrowernumber => $borrowernumber,
         ccode          => $item->ccode,
         categorycode   => $categorycode,
+        interface      => C4::Context->interface,
     });
 
     # Send a check-in slip. # NOTE: borrower may be undef. Do not try to send messages then.
@@ -3229,6 +3232,7 @@ sub AddRenewal {
                 borrowernumber => $borrowernumber,
                 ccode          => $item_object->ccode,
                 categorycode   => $patron->categorycode,
+                interface      => C4::Context->interface,
             }
         );
 
diff --git a/C4/Stats.pm b/C4/Stats.pm
index a833ba2e92..f8f1aa03a8 100644
--- a/C4/Stats.pm
+++ b/C4/Stats.pm
@@ -65,7 +65,8 @@ C<$params> is an hashref whose expected keys are:
     other              : sipmode
     itemtype           : the type of the item
     ccode              : the collection code of the item
-    categorycode           : the categorycode of the patron
+    categorycode       : the categorycode of the patron
+    interface          : the context this action was taken in
 
 type key is mandatory.
 For types used in C4::Circulation (renew,issue,localuse,return), the following other keys are mandatory:
@@ -83,7 +84,7 @@ sub UpdateStats {
 # make some controls
     return () if ! defined $params;
 # change these arrays if new types of transaction or new parameters are allowed
-    my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode);
+    my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode interface);
     my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
     my @allowed_accounts_types = qw (writeoff payment);
     my @circulation_mandatory_keys = qw (type branch borrowernumber itemnumber ccode itemtype);
@@ -125,6 +126,7 @@ sub UpdateStats {
     my $location          = exists $params->{location}       ? $params->{location}       : undef;
     my $ccode             = exists $params->{ccode}          ? $params->{ccode}          : '';
     my $categorycode      = exists $params->{categorycode}   ? $params->{categorycode}   : undef;
+    my $interface         = exists $params->{interface}      ? $params->{interface}      : undef;
 
     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
     my $statistic = Koha::Statistic->new(
@@ -140,6 +142,7 @@ sub UpdateStats {
             borrowernumber => $borrowernumber,
             ccode          => $ccode,
             categorycode   => $categorycode,
+            interface      => $interface,
         }
     )->store;
 
diff --git a/Koha/Account.pm b/Koha/Account.pm
index e37e646596..b0f3dc5254 100644
--- a/Koha/Account.pm
+++ b/Koha/Account.pm
@@ -254,6 +254,7 @@ sub add_credit {
                         type           => lc($credit_type),
                         amount         => $amount,
                         borrowernumber => $self->{patron_id},
+                        interface      => $interface,
                     }
                 ) if grep { $credit_type eq $_ } ( 'PAYMENT', 'WRITEOFF' );
 
diff --git a/Koha/Recalls.pm b/Koha/Recalls.pm
index 36d891d195..03d1727562 100644
--- a/Koha/Recalls.pm
+++ b/Koha/Recalls.pm
@@ -173,7 +173,8 @@ sub add_recall {
                 borrowernumber => $recall->patron_id,
                 itemtype       => $item->effective_itemtype,
                 ccode          => $item->ccode,
-                categorycode   => $checkout->patron->categorycode
+                categorycode   => $checkout->patron->categorycode,
+                interface      => $interface,
             }
         );
 
diff --git a/Koha/Schema/Result/Statistic.pm b/Koha/Schema/Result/Statistic.pm
index e6096eae2b..6477af1aab 100644
--- a/Koha/Schema/Result/Statistic.pm
+++ b/Koha/Schema/Result/Statistic.pm
@@ -108,6 +108,14 @@ foreign key from the items table, links transaction to a specific collection cod
 
 foreign key from the borrowers table, links transaction to a specific borrower category
 
+=head2 interface
+
+  data_type: 'varchar'
+  is_nullable: 1
+  size: 30
+
+interface acted upon
+
 =cut
 
 __PACKAGE__->add_columns(
@@ -137,6 +145,8 @@ __PACKAGE__->add_columns(
   { data_type => "varchar", is_nullable => 1, size => 80 },
   "categorycode",
   { data_type => "varchar", is_nullable => 1, size => 10 },
+  "interface",
+  { data_type => "varchar", is_nullable => 1, size => 30 },
 );
 
 
diff --git a/installer/data/mysql/atomicupdate/bug_30928.pl b/installer/data/mysql/atomicupdate/bug_30928.pl
new file mode 100755
index 0000000000..726c9c2452
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_30928.pl
@@ -0,0 +1,18 @@
+use Modern::Perl;
+return {
+    bug_number => "30928",
+    description => "Add interface field to statistics table",
+    up => sub {
+        my ($args) = @_;
+        my ($dbh, $out) = @$args{qw(dbh out)};
+        if( !column_exists( 'statistics', 'interface' ) ) {
+            $dbh->do(q{
+                ALTER TABLE statistics
+                ADD COLUMN interface varchar(30) NULL DEFAULT NULL
+                COMMENT "interface"
+                AFTER categorycode
+            });
+            say $out "Added column 'statistics.interface'";
+        }
+    },
+}
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index afbd7e1d30..d80c77d655 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -5341,6 +5341,7 @@ CREATE TABLE `statistics` (
   `borrowernumber` int(11) DEFAULT NULL COMMENT 'foreign key from the borrowers table, links transaction to a specific borrower',
   `ccode` varchar(80) DEFAULT NULL COMMENT 'foreign key from the items table, links transaction to a specific collection code',
   `categorycode` varchar(10) DEFAULT NULL COMMENT 'foreign key from the borrowers table, links transaction to a specific borrower category',
+  `interface` varchar(30) DEFAULT NULL COMMENT 'the context this action was taken in',
   KEY `timeidx` (`datetime`),
   KEY `branch_idx` (`branch`),
   KEY `type_idx` (`type`),
-- 
2.30.2