Bug 16966: Koha::Patrons - Move GetBorrowersWithIssuesHistoryOlderThan to search_patr...
[koha.git] / misc / cronjobs / cleanup_database.pl
index e882b4b..52028f2 100755 (executable)
@@ -35,15 +35,15 @@ BEGIN {
 }
 
 use C4::Context;
-use C4::Dates;
 use C4::Search;
 use C4::Search::History;
 use Getopt::Long;
 use C4::Log;
+use C4::Accounts;
 
 sub usage {
     print STDERR <<USAGE;
-Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions]
+Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS]
 
    -h --help          prints this help message, and exits, ignoring all
                       other options
@@ -61,6 +61,13 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu
                       Defaults to 60 days if no days specified.
    --z3950            purge records from import tables that are the result
                       of Z39.50 searches
+   --fees DAYS        purge entries accountlines older than DAYS days, where
+                      amountoutstanding is 0 or NULL.
+                      In the case of --fees, DAYS must be greater than
+                      or equal to 1.
+                      WARNING: Fees and payments may not be deleted together.
+                      This will not affect the account balance but may be
+                      confusing to staff.
    --logs DAYS        purge entries from action_logs older than DAYS days.
                       Defaults to 180 days if no days specified.
    --searchhistory DAYS  purge entries from search_history older than DAYS days.
@@ -71,47 +78,50 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu
                          Defaults to 30 days if no days specified.
     --all-restrictions   purge all expired patrons restrictions.
    --del-exp-selfreg  Delete expired self registration accounts
-   --del-unv-selfreg DAYS  Delete unverified self registrations older than DAYS
+   --del-unv-selfreg  DAYS  Delete unverified self registrations older than DAYS
+   --unique-holidays DAYS  Delete all unique holidays older than DAYS
 USAGE
     exit $_[0];
 }
 
-my (
-    $help,
-    $sessions,
-    $sess_days,
-    $verbose,
-    $zebraqueue_days,
-    $mail,
-    $purge_merged,
-    $pImport,
-    $pLogs,
-    $pSearchhistory,
-    $pZ3950,
-    $pListShareInvites,
-    $pDebarments,
-    $allDebarments,
-    $pExpSelfReg,
-    $pUnvSelfReg,
-);
+my $help;
+my $sessions;
+my $sess_days;
+my $verbose;
+my $zebraqueue_days;
+my $mail;
+my $purge_merged;
+my $pImport;
+my $pLogs;
+my $pSearchhistory;
+my $pZ3950;
+my $pListShareInvites;
+my $pDebarments;
+my $allDebarments;
+my $pExpSelfReg;
+my $pUnvSelfReg;
+my $fees_days;
+my $special_holidays_days;
 
 GetOptions(
-    'h|help'          => \$help,
-    'sessions'        => \$sessions,
-    'sessdays:i'      => \$sess_days,
-    'v|verbose'       => \$verbose,
-    'm|mail:i'        => \$mail,
-    'zebraqueue:i'    => \$zebraqueue_days,
-    'merged'          => \$purge_merged,
-    'import:i'        => \$pImport,
-    'z3950'           => \$pZ3950,
-    'logs:i'          => \$pLogs,
-    'searchhistory:i' => \$pSearchhistory,
-    'list-invites:i'  => \$pListShareInvites,
-    'restrictions:i'  => \$pDebarments,
-    'all-restrictions' => \$allDebarments,
-    'del-exp-selfreg' => \$pExpSelfReg,
-    'del-unv-selfreg' => \$pUnvSelfReg,
+    'h|help'            => \$help,
+    'sessions'          => \$sessions,
+    'sessdays:i'        => \$sess_days,
+    'v|verbose'         => \$verbose,
+    'm|mail:i'          => \$mail,
+    'zebraqueue:i'      => \$zebraqueue_days,
+    'merged'            => \$purge_merged,
+    'import:i'          => \$pImport,
+    'z3950'             => \$pZ3950,
+    'logs:i'            => \$pLogs,
+    'fees:i'            => \$fees_days,
+    'searchhistory:i'   => \$pSearchhistory,
+    'list-invites:i'    => \$pListShareInvites,
+    'restrictions:i'    => \$pDebarments,
+    'all-restrictions'  => \$allDebarments,
+    'del-exp-selfreg'   => \$pExpSelfReg,
+    'del-unv-selfreg'   => \$pUnvSelfReg,
+    'unique-holidays:i' => \$special_holidays_days,
 ) || usage(1);
 
 # Use default values
@@ -134,6 +144,7 @@ unless ( $sessions
     || $purge_merged
     || $pImport
     || $pLogs
+    || $fees_days
     || $pSearchhistory
     || $pZ3950
     || $pListShareInvites
@@ -141,6 +152,7 @@ unless ( $sessions
     || $allDebarments
     || $pExpSelfReg
     || $pUnvSelfReg
+    || $special_holidays_days
 ) {
     print "You did not specify any cleanup work for the script to do.\n\n";
     usage(1);
@@ -242,6 +254,12 @@ if ($pLogs) {
     print "Done with purging action_logs.\n" if $verbose;
 }
 
+if ($fees_days) {
+    print "Purging records from accountlines.\n" if $verbose;
+    purge_zero_balance_fees( $fees_days );
+    print "Done purging records from accountlines.\n" if $verbose;
+}
+
 if ($pSearchhistory) {
     print "Purging records older than $pSearchhistory from search_history.\n" if $verbose;
     C4::Search::History::delete({ interval => $pSearchhistory });
@@ -280,6 +298,10 @@ if( $pUnvSelfReg ) {
     DeleteUnverifiedSelfRegs( $pUnvSelfReg );
 }
 
+if ($special_holidays_days) {
+    DeleteSpecialHolidays( abs($special_holidays_days) );
+}
+
 exit(0);
 
 sub RemoveOldSessions {
@@ -350,7 +372,7 @@ sub PurgeZ3950 {
 }
 
 sub PurgeDebarments {
-    require Koha::Borrower::Debarments;
+    require Koha::Patron::Debarments;
     my $days = shift;
     $count = 0;
     $sth   = $dbh->prepare(
@@ -362,7 +384,7 @@ sub PurgeDebarments {
     );
     $sth->execute($days) or die $dbh->errstr;
     while ( my ($borrower_debarment_id) = $sth->fetchrow_array ) {
-        Koha::Borrower::Debarments::DelDebarment($borrower_debarment_id);
+        Koha::Patron::Debarments::DelDebarment($borrower_debarment_id);
         $count++;
     }
     return $count;
@@ -377,3 +399,14 @@ sub DeleteUnverifiedSelfRegs {
     my $cnt= C4::Members::DeleteUnverifiedOpacRegistrations( $_[0] );
     print "Removed $cnt unverified self-registrations\n" if $verbose;
 }
+
+sub DeleteSpecialHolidays {
+    my ( $days ) = @_;
+
+    my $sth = $dbh->prepare(q{
+        DELETE FROM special_holidays
+        WHERE DATE( CONCAT( year, '-', month, '-', day ) ) < DATE_SUB( CAST(NOW() AS DATE), INTERVAL ? DAY );
+    });
+    my $count = $sth->execute( $days ) + 0;
+    print "Removed $count unique holidays\n" if $verbose;
+}