LP1944986 Has / No-Has Penalty filters
authorBill Erickson <berickxx@gmail.com>
Tue, 12 Jul 2022 15:10:07 +0000 (11:10 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 3 May 2023 19:39:08 +0000 (15:39 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Elizabeth Davis <elizabeth.davis@sparkpa.org>
Open-ILS/src/support-scripts/patron-penalties-batch.pl

index c187fc0..ff9ee6f 100755 (executable)
@@ -35,10 +35,13 @@ my $max_id;
 my $has_open_circ = 0;
 my $owes_more_than;
 my $owes_less_than;
+my $has_penalty;
+my $no_has_penalty;
 my $verbose;
 my $help;
 my $batch_size = 100;
 my $authtoken;
+my $e;
 
 my $ops = GetOptions(
     'osrf-config=s'     => \$osrf_config,
@@ -49,6 +52,8 @@ my $ops = GetOptions(
     'has-open-circ'     => \$has_open_circ,
     'owes-more-than=s'  => \$owes_more_than,
     'owes-less-than=s'  => \$owes_less_than,
+    'has-penalty=s'     => \$has_penalty,
+    'no-has-penalty=s'  => \$no_has_penalty,
     'verbose'           => \$verbose,
     'help'              => \$help
 );
@@ -70,6 +75,12 @@ sub help {
             Username of an Evergreen account to use for creating the
             internal auth session.  Defaults to 'admin'.
 
+        --has-penalty <penalty-type-id>
+            Limit to patrons that currently have a specific penalty.
+
+        --no-has-penalty <penalty-type-id>
+            Limit to patrons that do not currently have a specific penalty.
+
         --min-id <id>
             Lowest patron ID to process. 
 
@@ -117,13 +128,6 @@ sub announce {
     print "$date_str $msg\n";
 }
 
-# connect to osrf...
-OpenSRF::System->bootstrap_client(config_file => $osrf_config);
-Fieldmapper->import(IDL => 
-    OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
-OpenILS::Utils::CStoreEditor::init();
-my $e = OpenILS::Utils::CStoreEditor->new;
-
 sub get_user_ids {
     my ($limit, $offset) = @_;
 
@@ -180,6 +184,45 @@ sub get_user_ids {
         });
     }
 
+    if ($has_penalty) {
+        
+        push(@where, {
+            '-exists' => {
+                select => {ausp => ['id']},
+                from => 'ausp',
+                where => {
+                    usr => {'=' => {'+au' => 'id'}},
+                    standing_penalty => $has_penalty,
+                    '-or' => [
+                        {stop_date => undef},
+                        {stop_date => {'>' => 'now'}}
+                    ]
+                },
+                limit => 1
+            }
+        });
+    }
+
+    if ($no_has_penalty) {
+        push(@where, {
+            '-not' => {
+                '-exists' => {
+                    select => {ausp => ['id']},
+                    from => 'ausp',
+                    where => {
+                        usr => {'=' => {'+au' => 'id'}},
+                        standing_penalty => $no_has_penalty,
+                        '-or' => [
+                            {stop_date => undef},
+                            {stop_date => {'>' => 'now'}}
+                        ]
+                    },
+                    limit => 1
+                }
+            }
+        });
+    }
+
     # For owes more / less, there is a special case because not all
     # patrons have a money.usr_summary row.  If they don't, they
     # effectively owe $0.00.
@@ -270,7 +313,8 @@ sub process_users {
         $batches++;
 
         announce('debug', 
-            "Processing batch $batches with $num patrons and offset $offset.");
+            "Processing batch $batches; count=$num; offset=$offset; ids=" .
+            @$user_ids[0] . '..' . @$user_ids[$#$user_ids]);
 
         for my $user_id (@$user_ids) {
 
@@ -284,7 +328,6 @@ sub process_users {
         }
 
         $offset += $batch_size;
-        announce('debug', "$counter patrons processed.");
     }
 
     announce('debug', "$counter total patrons processed.");
@@ -310,6 +353,13 @@ sub login {
     );
 }
 
+# connect to osrf...
+OpenSRF::System->bootstrap_client(config_file => $osrf_config);
+Fieldmapper->import(IDL => 
+    OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
+OpenILS::Utils::CStoreEditor::init();
+$e = OpenILS::Utils::CStoreEditor->new;
+
 login();
 process_users();