From e5a0f21821dd865b8e0fd76424f0354254dffa48 Mon Sep 17 00:00:00 2001
From: Bin Wen <bin.wen@inlibro.com>
Date: Wed, 18 Dec 2019 11:52:49 -0500
Subject: [PATCH] Bug 11879: Add a new field to patron record: main contact
 method

This patch adds the "Main Contact Method" field in the borrower
modification form.

This field is useful for reporting purposes, or to
know which contact method to use first when trying to contact a borrower.

Test plan:
0) Apply patch
1) updatedatabase.pl
2) In the staff client, edit a patron's contact info. There should be a new dropdown select to choose the main contact method
3) In the opac page, edit the personal details.There should be a new dropdown select to choose the main contact method.
4) Save the form. It should work.

Sponsored-by: CCSR ( http://www.ccsr.qc.ca )
Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: David Nind <david@davidnind.com>
---
 .../borrowers_primary_contact_method.perl     | 17 ++++++
 installer/data/mysql/kohastructure.sql        |  3 +
 .../prog/en/modules/members/memberentrygen.tt | 58 ++++++++++++++++++
 .../prog/en/modules/members/moremember.tt     | 20 +++++++
 .../bootstrap/en/modules/opac-memberentry.tt  | 59 +++++++++++++++++++
 opac/opac-memberentry.pl                      | 14 +++++
 6 files changed, 171 insertions(+)
 create mode 100644 installer/data/mysql/atomicupdate/borrowers_primary_contact_method.perl

diff --git a/installer/data/mysql/atomicupdate/borrowers_primary_contact_method.perl b/installer/data/mysql/atomicupdate/borrowers_primary_contact_method.perl
new file mode 100644
index 0000000000..e34fc015a2
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/borrowers_primary_contact_method.perl
@@ -0,0 +1,17 @@
+$DBversion = 'XXX'; # will be replaced by the RM
+if( CheckVersion( $DBversion ) ) {
+    if( !column_exists( 'borrowers', 'primary_contact_method' ) ) {
+        $dbh->do( "ALTER TABLE `borrowers` ADD COLUMN `primary_contact_method` VARCHAR(45) NULL DEFAULT NULL AFTER `anonymized`" );
+    }
+
+    if( !column_exists( 'deletedborrowers', 'primary_contact_method' ) ) {
+        $dbh->do( "ALTER TABLE `deletedborrowers` ADD COLUMN `primary_contact_method` VARCHAR(45) NULL DEFAULT NULL AFTER `anonymized`" );
+    }
+
+    if( !column_exists( 'borrower_modifications', 'primary_contact_method' ) ) {
+        $dbh->do( "ALTER TABLE `borrower_modifications` ADD COLUMN `primary_contact_method` VARCHAR(45) NULL DEFAULT NULL AFTER `gdpr_proc_consent`" );
+    }
+
+    SetVersion( $DBversion );
+    print "Upgrade to $DBversion done (Bug 11879 - Add a new field to patron record: main contact method)\n";
+}
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index ae247e67c8..b5cd8743f6 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -1305,6 +1305,7 @@ CREATE TABLE `borrower_modifications` (
   `privacy` int(11) DEFAULT NULL,
   `extended_attributes` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL,
   `gdpr_proc_consent` datetime DEFAULT NULL COMMENT 'data processing consent',
+  `primary_contact_method` varchar(45) DEFAULT NULL COMMENT 'useful for reporting purposes',
   PRIMARY KEY (`verification_token`(191),`borrowernumber`),
   KEY `verification_token` (`verification_token`(191)),
   KEY `borrowernumber` (`borrowernumber`)
@@ -1431,6 +1432,7 @@ CREATE TABLE `borrowers` (
   `overdrive_auth_token` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'persist OverDrive auth token',
   `anonymized` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'flag for data anonymization',
   `autorenew_checkouts` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'flag for allowing auto-renewal',
+  `primary_contact_method` varchar(45) DEFAULT NULL COMMENT 'useful for reporting purposes',
   PRIMARY KEY (`borrowernumber`),
   UNIQUE KEY `cardnumber` (`cardnumber`),
   UNIQUE KEY `userid` (`userid`),
@@ -2432,6 +2434,7 @@ CREATE TABLE `deletedborrowers` (
   `overdrive_auth_token` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'persist OverDrive auth token',
   `anonymized` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'flag for data anonymization',
   `autorenew_checkouts` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'flag for allowing auto-renewal',
+  `primary_contact_method` varchar(45) DEFAULT NULL COMMENT 'useful for reporting purposes',
   KEY `borrowernumber` (`borrowernumber`),
   KEY `cardnumber` (`cardnumber`),
   KEY `sms_provider_id` (`sms_provider_id`)
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
index e63e97e347..42b3ee27c6 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
@@ -740,6 +740,64 @@ legend:hover {
                                                     [% END %]
                                                 </li>
                                             [% END #/UNLESS nofax %]
+
+                                            <li>
+                                                [% IF mandatoryprimary_contact_method %]
+                                                    <label for="primary_contact_method" class="required">
+                                                [% ELSE %]
+                                                    <label for="primary_contact_method">
+                                                [% END %]
+                                                Main contact method: </label>
+
+                                                <select id="primary_contact_method" name="primary_contact_method">
+                                                    <option value=""></option>
+                                                    [% UNLESS nophone %]
+                                                        [% IF ( primary_contact_method == 'phone' ) %]
+                                                            <option value="phone" selected="selected">Primary phone</option>
+                                                        [% ELSE %]
+                                                            <option value="phone">Primary phone</option>
+                                                        [% END %]
+                                                    [% END %]
+                                                    [% UNLESS nophonepro %]
+                                                        [% IF ( primary_contact_method == 'phonepro' ) %]
+                                                            <option value="phonepro" selected="selected">Secondary phone</option>
+                                                        [% ELSE %]
+                                                            <option value="phonepro">Secondary phone</option>
+                                                        [% END %]
+                                                    [% END %]
+                                                    [% UNLESS nomobile %]
+                                                        [% IF ( primary_contact_method == 'mobile' ) %]
+                                                            <option value="mobile" selected="selected">Other phone</option>
+                                                        [% ELSE %]
+                                                            <option value="mobile">Other phone</option>
+                                                        [% END %]
+                                                    [% END %]
+                                                    [% UNLESS noemail %]
+                                                        [% IF ( primary_contact_method == 'email' ) %]
+                                                            <option value="email" selected="selected">Primary email</option>
+                                                        [% ELSE %]
+                                                            <option value="email">Primary email</option>
+                                                        [% END %]
+                                                    [% END %]
+                                                    [% UNLESS noemailpro %]
+                                                        [% IF ( primary_contact_method == 'emailpro' ) %]
+                                                            <option value="emailpro" selected="selected">Secondary email</option>
+                                                        [% ELSE %]
+                                                            <option value="emailpro">Secondary email</option>
+                                                        [% END %]
+                                                    [% END %]
+                                                    [% UNLESS nofax %]
+                                                        [% IF ( primary_contact_method == 'fax' ) %]
+                                                            <option value="fax" selected="selected">Fax</option>
+                                                        [% ELSE %]
+                                                            <option value="fax">Fax</option>
+                                                        [% END %]
+                                                    [% END %]
+                                                </select>
+                                                [% IF mandatoryprimary_contact_method %]
+                                                    <span class="required">Required</span>
+                                                [% END %]
+                                            </li>
                                         </ol>
                                     </fieldset> <!-- /#memberentry_contact -->
                                 [% END # hide fieldset %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
index 7b7fd26e67..8981a11778 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
@@ -307,6 +307,26 @@
                                                     </ul>
                                                 </li>
                                         [% END %]
+
+                                        [% IF ( primary_contact_method ) %]
+                                            <li>
+                                                <span class="label">Main contact method: </span>
+                                                [% SWITCH primary_contact_method %]
+                                                    [% CASE 'phone' %]
+                                                        Primary phone
+                                                    [% CASE 'phonepro' %]
+                                                        Secondary phone
+                                                    [% CASE 'mobile' %]
+                                                        Other phone
+                                                    [% CASE 'email' %]
+                                                        Primary email
+                                                    [% CASE 'emailpro' %]
+                                                        Secondary email
+                                                    [% CASE 'fax' %]
+                                                        Fax
+                                                [% END %]
+                                            </li>
+                                        [% END %]
                                     </ol>
                                 </div> [% # /div.rows %]
                             </div> [% # /div#patron-information %]
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt
index 57135b6b23..9d1160d1e7 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt
@@ -554,6 +554,65 @@
                                                 <div class="required_label [% required.fax | html %]">Required</div>
                                             </li>
                                         [% END %]
+
+                                        [% UNLESS hidden.defined('primary_contact_method') %]
+                                            <li>
+                                                [% IF ( mandatory.defined('primary_contact_method') ) %]
+                                                    <label for="borrower_primary_contact_method" class="required">Main contact method:</label>
+                                                [% ELSE %]
+                                                    <label for="borrower_primary_contact_method">Main contact method:</label>
+                                                [% END %]
+
+                                                <select id="borrower_primary_contact_method" name="borrower_primary_contact_method">
+                                                    <option value=""></option>
+                                                    [% FOREACH contactprincipalloo IN contactprincipalloop %]
+                                                        [% IF ( contactprincipalloo.currentis_phone ) %]
+                                                            [% IF ( borrower.primary_contact_method == 'phone' ) %]
+                                                                <option value="phone" selected="selected">Primary phone</option>
+                                                            [% ELSE %]
+                                                                <option value="phone">Primary phone</option>
+                                                            [% END %]
+                                                        [% END %]
+                                                        [% IF ( contactprincipalloo.currentis_phonepro ) %]
+                                                            [% IF ( borrower.primary_contact_method == 'phonepro' ) %]
+                                                                <option value="phonepro" selected="selected">Secondary phone</option>
+                                                            [% ELSE %]
+                                                                <option value="phonepro">Secondary phone</option>
+                                                            [% END %]
+                                                        [% END %]
+                                                        [% IF ( contactprincipalloo.currentis_mobile ) %]
+                                                            [% IF ( borrower.primary_contact_method == 'mobile' ) %]
+                                                                <option value="mobile" selected="selected">Other phone</option>
+                                                            [% ELSE %]
+                                                                <option value="mobile">Other phone</option>
+                                                            [% END %]
+                                                        [% END %]
+                                                        [% IF ( contactprincipalloo.currentis_email ) %]
+                                                            [% IF ( borrower.primary_contact_method == 'email' ) %]
+                                                                <option value="email" selected="selected">Primary email</option>
+                                                            [% ELSE %]
+                                                                <option value="email">Primary email</option>
+                                                            [% END %]
+                                                        [% END %]
+                                                        [% IF ( contactprincipalloo.currentis_emailpro ) %]
+                                                            [% IF ( borrower.primary_contact_method == 'emailpro' ) %]
+                                                                <option value="emailpro" selected="selected">Secondary email</option>
+                                                            [% ELSE %]
+                                                                <option value="emailpro">Secondary email</option>
+                                                            [% END %]
+                                                        [% END %]
+                                                        [% IF ( contactprincipalloo.currentis_fax ) %]
+                                                            [% IF ( borrower.primary_contact_method == 'fax' ) %]
+                                                                <option value="fax" selected="selected">Fax</option>
+                                                            [% ELSE %]
+                                                                <option value="fax">Fax</option>
+                                                            [% END %]
+                                                        [% END %]
+                                                    [% END %]
+                                                </select>
+                                                [% IF ( mandatory.defined('primary_contact_method') ) %]<span class="required">Required</span>[% END %]
+                                            </li>
+                                        [% END %]
                                     </ol>
                                 </fieldset>
                             </div> <!-- /.col -->
diff --git a/opac/opac-memberentry.pl b/opac/opac-memberentry.pl
index dbfd6f83dd..20ee63d31f 100755
--- a/opac/opac-memberentry.pl
+++ b/opac/opac-memberentry.pl
@@ -102,6 +102,20 @@ $template->param(
     defaultCategory  => $defaultCategory,
 );
 
+my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
+my @field_check=split(/\|/,$check_BorrowerMandatoryField);
+my @contactprincipalloop;
+my @fieldArray = qw(phone phonepro mobile email emailpro fax);
+foreach my $field (@fieldArray) {
+   if ( !(grep { $field eq $_ } @field_check)){
+        push @contactprincipalloop,{
+            'currentis_' . $field => 1
+        };
+    }
+}
+
+$template->param('contactprincipalloop' => \@contactprincipalloop);
+
 my $attributes = ParsePatronAttributes($borrowernumber,$cgi);
 my $conflicting_attribute = 0;
 
-- 
2.25.1