From 81006eaeb534416e324885cdb2dbffcf4a50db13 Mon Sep 17 00:00:00 2001 From: Timothy Alexis Vass Date: Tue, 24 Nov 2020 08:07:31 +0000 Subject: [PATCH] Bug 27080 - Add --statute-barred option to cleanup_database.pl to purge all fees where the item has been returned and the fee is older than the given number of days. According to Swedish law, fees become statute-barred after 3 years. With this option, we can use cleanup_database.pl to purge those from accountlines. NOTE: This commit also fixes indentations. You can use the -w option with git to ignore space differences. To test: 1) You will need a record in the accountlines table WHERE status = 'REMOVED' AND amountoutstanding > 0. 2) Run ./misc/cronjobs/cleanup_database.pl --statute-barred DAYS Where DAYS is 1 or greater. This will delete records from accountlines WHERE date is DAYS day(s) or older. 3) Confirm that records have been deleted. Sponsored-by: Lunds Universitetsbibliotek --- C4/Accounts.pm | 31 +++++++ misc/cronjobs/cleanup_database.pl | 179 +++++++++++++++++++++----------------- 2 files changed, 129 insertions(+), 81 deletions(-) diff --git a/C4/Accounts.pm b/C4/Accounts.pm index 7d7ac76018..a89a412b77 100644 --- a/C4/Accounts.pm +++ b/C4/Accounts.pm @@ -39,6 +39,7 @@ BEGIN { @EXPORT = qw( &chargelostitem &purge_zero_balance_fees + &purge_statute_barred_fees ); } @@ -171,6 +172,36 @@ sub purge_zero_balance_fees { $sth->execute($days) or die $dbh->errstr; } +=head2 purge_statute_barred_fees + + purge_statute_barred_fees( $days ); + +Delete accountlines entries where status is RETURNED and amountoutstanding is greater than 0 which are more than a given number of days old. + +B<$days> -- Statute-barred fees older than B<$days> days old will be deleted. + +B Because fines and payments are not linked in accountlines, it is +possible for a fine to be deleted without the accompanying payment, +or vise versa. This won't affect the account balance, but might be +confusing to staff. + +=cut + +sub purge_statute_barred_fees { + my $days = shift; + + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( + q{ + DELETE FROM accountlines + WHERE date < date_sub(curdate(), INTERVAL ? DAY) + AND status = 'RETURNED' + AND amountoutstanding > 0 + } + ); + $sth->execute($days) or die $dbh->errstr; +} + END { } # module clean-up code here (global destructor) 1; diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index c2d8e9832c..ca7bb537cc 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -53,56 +53,64 @@ use Koha::PseudonymizedTransactions; sub usage { print STDERR < \$help, - 'confirm' => \$confirm, - '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, - 'temp-uploads' => \$temp_uploads, - 'temp-uploads-days:i' => \$temp_uploads_days, - 'uploads-missing:i' => \$uploads_missing, - 'oauth-tokens' => \$oauth_tokens, - 'statistics:i' => \$pStatistics, - 'deleted-catalog:i' => \$pDeletedCatalog, - 'deleted-patrons:i' => \$pDeletedPatrons, - 'old-issues:i' => \$pOldIssues, - 'old-reserves:i' => \$pOldReserves, - 'transfers:i' => \$pTransfers, - 'pseudo-transactions:i' => \$pPseudoTransactions, - 'pseudo-transactions-from:s' => \$pPseudoTransactionsFrom, - 'pseudo-transactions-to:s' => \$pPseudoTransactionsTo, + 'h|help' => \$help, + 'confirm' => \$confirm, + '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, + 'statute-barred:i' => \$statute_barred_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, + 'temp-uploads' => \$temp_uploads, + 'temp-uploads-days:i' => \$temp_uploads_days, + 'uploads-missing:i' => \$uploads_missing, + 'oauth-tokens' => \$oauth_tokens, + 'statistics:i' => \$pStatistics, + 'deleted-catalog:i' => \$pDeletedCatalog, + 'deleted-patrons:i' => \$pDeletedPatrons, + 'old-issues:i' => \$pOldIssues, + 'old-reserves:i' => \$pOldReserves, + 'transfers:i' => \$pTransfers, + 'pseudo-transactions:i' => \$pPseudoTransactions, + 'pseudo-transactions-from:s' => \$pPseudoTransactionsFrom, + 'pseudo-transactions-to:s' => \$pPseudoTransactionsTo, ) || usage(1); # Use default values @@ -194,6 +204,7 @@ unless ( $sessions || $pImport || $pLogs || $fees_days + || $statute_barred_days || $pSearchhistory || $pZ3950 || $pListShareInvites @@ -340,6 +351,12 @@ if ($fees_days) { print "Done purging records from accountlines.\n" if $verbose; } +if ($statute_barred_days) { + print "Purging statute-barred fees from accountlines.\n" if $verbose; + purge_statute_barred_fees( $statute_barred_days ); + print "Done purging statute-barred fees 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 }) if $confirm; -- 2.11.0