From 49c470cc552ceda7c137fa78035e91c1e03433ab Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Tue, 1 Aug 2017 08:10:38 +0000
Subject: [PATCH] Checks and transform SMS alert number

This patch adds a new package C4::SMSNumber::Fr (new ones could be added
for other countries)
and a scrip (misc/check_smsalertnumber.pl):
  check_smsalertnumber.pl -a|--area fr [-h|--help] [-c|--confirm]
[-o|--country-option] [-l|--liste-mode]

Test plan #1:
  - Create or modify patrons and add some SMS alert numbers like:
     06 98 35 72 28, 0656871221, +336-01-55-83-11, 0033 6.23.54.54.00,
0126698376
  - launch perl misc/check_smsalertnumber.pl -a Fr
  - Transformation proposal should be like:
    - 06 98 35 72 28 => 0698357228,
    - 0656871221 => no proposal (number Ok),
    - +336-01-55-83-11 => 0601558311,
    - 0033 6.23.54.54.00 => 0623545400,
    - 0126698376 => Can't find a replacement

Test plan #2:
  - with the same numbers launch perl misc/check_smsalertnumber.pl -a Fr
    -o +33
  - Transformation proposal should be like:
    - 06 98 35 72 28 => +33698357228,
    - 0656871221 => +33656871221,
    - +336-01-55-83-11 => +33601558311,
    - 0033 6.23.54.54.00 => +33623545400,
    - 0126698376 => Can't find a replacement

Test plan #3:
  - with the same numbers launch perl misc/check_smsalertnumber.pl -a Fr
    -o 0033
  - Transformation proposal should be like:
    - 06 98 35 72 28 => 0033698357228,
    - 0656871221 => 0033656871221,
    - +336-01-55-83-11 => 0033601558311,
    - 0033 6.23.54.54.00 => 0033623545400,
    - 0126698376 => Can't find a replacement

https://bugs.koha-community.org/show_bug.cgi?id=19017
---
 C4/SMSNumber/Fr.pm           | 124 +++++++++++++++++++++++++++++++++++++++++
 misc/check_smsalertnumber.pl | 130 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 254 insertions(+)
 create mode 100644 C4/SMSNumber/Fr.pm
 create mode 100755 misc/check_smsalertnumber.pl

diff --git a/C4/SMSNumber/Fr.pm b/C4/SMSNumber/Fr.pm
new file mode 100644
index 0000000..1cf879b
--- /dev/null
+++ b/C4/SMSNumber/Fr.pm
@@ -0,0 +1,124 @@
+package C4::SMSNumber::Fr;
+
+# Copyright 2017 Biblibre
+#
+# 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;
+
+sub check {
+    my ($package, $number, $option) = @_;
+warn "Number: $number";
+
+    if ($option eq '+33') {
+        return _check33($number);
+    }
+
+    if ($option eq '0033') {
+        return _check0033($number);
+    }
+
+    if ( $number =~ /^0[6-7]\d{8}$/ ) {
+        return 1;
+    }
+
+    return 0;
+}
+
+sub _check33 {
+    my ($number) = @_;
+
+    if ( $number =~ /^\+33[6-7]\d{8}$/ ) {
+        return 1;
+    }
+
+    return 0;
+}
+
+sub _check0033 {
+    my ($number) = @_;
+
+    if ( $number =~ /^0033[6-7]\d{8}$/ ) {
+        return 1;
+    }
+
+    return 0;
+}
+
+sub transform {
+    my ($package, $number, $option) = @_;
+
+    if ($option eq '+33') {
+        return _transform33($number);
+    }
+
+    if ($option eq '0033') {
+        return _transform0033($number);
+    }
+
+    if ( $number =~ /^\+33\s*/ ) {
+        $number =~ s/^\+33\s*/0/;
+    }
+
+    if ( $number =~ /^0*33\s*/ ) {
+        $number =~ s/^0*33\s*/0/;
+    }
+
+    if ( $number =~ /^(0[6-7])[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})/ ) {
+        return "$1$2$3$4$5";
+    }
+
+    return $number;
+}
+
+sub _transform33 {
+    my ($number, $option) = @_;
+
+    if ( $number =~ /^0033\s*/ ) {
+        $number =~ s/^0033\s*/\+33/;
+    }
+
+    if ( $number =~ /^0([6-7])/ ) {
+        $number =~ s/^0([6-7])/\+33$1/;
+    }
+
+    if ( $number =~ /^\+33[\-\s\.]*([6-7])[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})/ ) {
+        return "+33$1$2$3$4$5";
+    }
+
+    return $number;
+}
+
+sub _transform0033 {
+    my ($number, $option) = @_;
+
+    if ( $number =~ /^\+33\s*/ ) {
+        $number =~ s/^\+33\s*/0033/;
+    }
+
+    if ( $number =~ /^0([6-7])/ ) {
+        $number =~ s/^0([6-7])/0033$1/;
+    }
+
+    if ( $number =~ /^0033[\-\s\.]*([6-7])[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})[\-\s\.]*(\d{2})/ ) {
+        return "0033$1$2$3$4$5";
+    }
+
+    return $number;
+}
+
+
+1;
\ No newline at end of file
diff --git a/misc/check_smsalertnumber.pl b/misc/check_smsalertnumber.pl
new file mode 100755
index 0000000..30edc44
--- /dev/null
+++ b/misc/check_smsalertnumber.pl
@@ -0,0 +1,130 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Copyright 2017 Biblibre
+#
+# 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 Pod::Usage;
+use Module::Load;
+use Getopt::Long;
+
+use Koha::Patrons;
+
+my ( $help, $area, $confirm, $list_area );
+my $area_option = '';
+
+GetOptions(
+    'h|help'                 => \$help,
+    'a|area=s'               => \$area,
+    'c|confirm'              => \$confirm,
+    'o|option=s'             => \$area_option,
+    'l|list-area'            => \$list_area
+) || pod2usage(1);
+
+if ($help) {
+    pod2usage(1);
+}
+
+if ($list_area) {
+    print "    - Fr: Checks french mobile number. options: '+33', '0033' \n";
+
+    exit;
+}
+
+my $module = "C4::SMSNumber::" . ucfirst($area);
+load $module, qw/check/;
+
+my $patrons = Koha::Patrons->search({});
+while ( my $patron = $patrons->next() ) {
+    my $number = $patron->smsalertnumber();
+    my $id = $patron->id();
+
+    next unless $number;
+
+    my $valid = $module->check($number, $area_option);
+    next if $valid;
+
+    print "Patron n° $id has an invalid phone number for area $area ($number).\n";
+
+    my $replacement = $module->transform($number, $area_option);
+    if ($replacement ne $number) {
+        print "$number  => $replacement\n\n";
+        $patron->set({ smsalertnumber => $replacement})->store if $confirm;
+    } else {
+        print "Can't find a replacement\n\n";
+    }
+}
+
+=head1 NAME
+
+check_smsalertnumber - This script check and transform SMS alert number.
+
+=head1 SYNOPSIS
+
+check_smsalertnumber.pl -a|--area fr [-h|--help] [-c|--confirm] [-o|--country-option] [-l|--liste-mode]
+
+Check SMS alert number and transform them if a replacement is found (--confirm). Mode
+can be found/added in C4/Number. add --list-area option to this script to get the
+available list.
+
+=head1 OPTIONS
+
+=over
+
+=item B<-h|--help>
+
+Print a brief help message
+
+=item B<-a|--area>
+
+Specify the area template to use. See --list-area to get all available area
+
+=item B<-c|--confirm>
+
+If this option is passed to the script, replacement are stored in database
+
+=item B<-o|--country-option>
+
+Mode option. Modes could need additional option(s)
+
+=item B<-l|--list>
+
+Print a list of available areas
+
+=back
+
+=head1 AUTHOR
+
+Alex Arnaud <alex.arnaud@biblibre.com>
+
+=head1 COPYRIGHT
+
+Copyright 2016 BibLibre
+
+=head1 LICENSE
+
+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.
+
+You should have received a copy of the GNU General Public License along
+with Koha; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+=cut
-- 
2.7.4