From 544857c0a7363febdf16f83cc58b91c701f8d134 Mon Sep 17 00:00:00 2001
From: Wainui Witika-Park <wainuiwitikapark@catalyst.net.nz>
Date: Mon, 7 Aug 2023 15:46:45 +1200
Subject: [PATCH] Bug 31143: Update patrons with dates of 0000-00-00 to NULL

To test:
1) Change a patron's value of one or more of the following to
0000-00-00: dateofbirth dateenrolled dateexpiry date_renewed
2) Run the fix_patron_dates.pl (-c -v)
3) Notice the value has changed from 0000-00-00 to NULL

Sponsored by: Toi Ohomai Institute of Technology, New Zealand
---
 misc/cronjobs/fix_patron_dates.pl | 53 +++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100755 misc/cronjobs/fix_patron_dates.pl

diff --git a/misc/cronjobs/fix_patron_dates.pl b/misc/cronjobs/fix_patron_dates.pl
new file mode 100755
index 0000000000..06fd72631a
--- /dev/null
+++ b/misc/cronjobs/fix_patron_dates.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use C4::Context;
+use C4::Log qw(cronlogaction);
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
+use Koha::Logger;
+use Koha::Patrons;
+use Koha::DateUtils qw( dt_from_string );
+use Koha::Script -cron;
+use Data::Dumper;
+
+my $verbose = 0;
+my $doit = 0;
+
+GetOptions(
+    'v|verbose'       => \$verbose,
+    'c|confirm'       => \$doit,
+);
+
+my $count;
+
+# search patrons that have date fields that are 0000-00-00
+foreach my $date_type (qw(dateofbirth dateenrolled dateexpiry date_renewed)) {
+    my $borrowers = Koha::Patrons->search( { $date_type=>'0000-00-00' } );
+
+    if ($doit) { # if confirm flag was run with cronjob
+        while (my $borrower = $borrowers->next()) {
+            $count++;
+            $borrower->$date_type(undef)->store();
+        }
+    }
+}
+
+if ($verbose) {
+  print "$count wrong dates found";
-- 
2.30.2