From 6cb1a21ad82d6a4c700a3e353f103c5b5f99b56f Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Fri, 7 Jun 2013 15:51:43 +0200
Subject: [PATCH] Bug 10419: Followup - Script for deleting patrons

This followup adds:
- execute flag (+x) for the cronjob script.
- replaces --dry-run with --confirm (according with existing scripts).
- changes output text (remove 'first person' style).
- updates the doc and simplifies the dates parameters.
- changes flags PrintError and RaiseError for the dbh in order to catch
  something if an error occurs...

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
---
 misc/cronjobs/delete_patrons.pl | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
 mode change 100644 => 100755 misc/cronjobs/delete_patrons.pl

diff --git a/misc/cronjobs/delete_patrons.pl b/misc/cronjobs/delete_patrons.pl
old mode 100644
new mode 100755
index ad3c875..2d869f5
--- a/misc/cronjobs/delete_patrons.pl
+++ b/misc/cronjobs/delete_patrons.pl
@@ -9,14 +9,14 @@ use C4::Members;
 use Koha::DateUtils;
 
 my ( $help, $verbose, $not_borrowered_since, $expired_before, $category_code,
-    $dryrun );
+    $confirm );
 GetOptions(
     'h|help'                 => \$help,
     'v|verbose'              => \$verbose,
     'not_borrowered_since:s' => \$not_borrowered_since,
     'expired_before:s'       => \$expired_before,
     'category_code:s'        => \$category_code,
-    'dry-run'                => \$dryrun,
+    'c|confirm'              => \$confirm,
 ) || pod2usage(1);
 
 if ($help) {
@@ -42,20 +42,25 @@ my $members = GetBorrowersToExpunge(
     }
 );
 
-say "I found " . scalar(@$members) . " patrons to delete";
+say scalar(@$members) . " patrons to delete";
+
+my $dbh = C4::Context->dbh;
+$dbh->{RaiseError} = 1;
+$dbh->{PrintError} = 0;
+
 for my $member (@$members) {
     print "Trying to delete patron " . $member->{borrowernumber} . "... ";
     eval {
         C4::Members::MoveMemberToDeleted( $member->{borrowernumber} )
-          unless $dryrun;
+          if $confirm;
     };
     if ($@) {
-        say "Failed, I cannot move this patron ($@)";
+        say "Failed, cannot move this patron ($@)";
         next;
     }
-    eval { C4::Members::DelMember( $member->{borrowernumber} ) unless $dryrun; };
+    eval { C4::Members::DelMember( $member->{borrowernumber} ) if $confirm; };
     if ($@) {
-        say "Failed, I cannot delete this patron ($@)";
+        say "Failed, cannot delete this patron ($@)";
         next;
     }
     say "OK";
@@ -67,7 +72,9 @@ delete_patrons - This script deletes patrons
 
 =head1 SYNOPSIS
 
-delete_patrons.pl [-h -v] --not_borrowered_since=`date -d '-3 month' "+%Y-%m-%d"` --expired_before=`date -d '-3 month' "+%Y-%m-%d"` --category_code=CAT
+delete_patrons.pl [-h -v -c] --not_borrowered_since=2013-07-21 --expired_before=2013-07-21 --category_code=CAT
+
+dates can be generated with `date -d '-3 month' "+%Y-%m-%d"`
 
 Options are cumulatives.
 
@@ -91,9 +98,9 @@ Delete patrons with an account expired before this date.
 
 Delete patrons who have this category code.
 
-=item B<--dry-run>
+=item B<-c|--confirm>
 
-Dry run mode. To use with the verbose mode.
+Without this flag set, this script will do nothing.
 
 =item B<-v|--verbose>
 
-- 
1.8.1.2