started changing app name to 3rd, changing file names to not have eq in them
[eq/.git] / bin / import_ward_data
index 9b830aada27441ac44ca6c56ff56bbbb3f718d05..b42a7550b3d585bd27179593c2e1d41b3cc380c2 100755 (executable)
@@ -54,6 +54,38 @@ sub csv_to_hash
     close(FILE);
 }
 
+sub optional_csv_to_hash
+{
+    my ($filename, $hashref) = @_;
+
+    my $opened = open(FILE,$filename);
+
+    if ($opened) {
+        my $found_header = 0; my $index = 0;
+        while(<FILE>)
+        {
+           $line = $_;
+           @data = split /\",/, $line;
+           if(!$found_header) { @header = @data; $found_header = 1; }
+           else {
+               foreach $i (0..$#data-1) {
+                   $data[$i] =~ s/\"//g;
+                   $header[$i] =~ s/\"//g;
+                   $hashref->{$index}{$header[$i]} = $data[$i];
+                   #print "$index: $i: $header[$i]: $data[$i]\n";
+               }
+               $index++;
+           }
+        }
+    
+    close(FILE);
+    }
+    else
+    {
+        print "-W- could not open optional csv file $filename\n";
+    }
+}
+
 ######################################################################
 sub print_hash
 {
@@ -70,21 +102,22 @@ sub print_hash
 
 ######################################################################
 
-# EQ_AARONIC
+# 3RD_AARONIC
 #+-------+--------------------+------+-----+---------+-------+
 #| Field | Type               | Null | Key | Default | Extra |
 #+-------+--------------------+------+-----+---------+-------+
 #| aaronic | int(16) unsigned |      | PRI | 0       |   A   |
 #| name    | varchar(60)      | YES  |     | NULL    |       |
 #| phone   | varchar(12)      | YES  |     | NULL    |       |
+#| email   | varchar(120)     | YES  |     | NULL    |       |
 #| valid   | tinyint(1)       | YES  |     | NULL    |       |
 #+-------+--------------------+------+-----+---------+-------+
-sub update_eq_aaronic_table
+sub update_3rd_aaronic_table
 {
-    print "\n-> Updating eq_aaronic table\n";
+    print "\n-> Updating 3rd_aaronic table\n";
 
     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
-    $sth = $dbh->prepare("update eq_aaronic set valid=0");
+    $sth = $dbh->prepare("update 3rd_aaronic set valid=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     
     foreach $index (keys %membership_data)
@@ -96,10 +129,12 @@ sub update_eq_aaronic_table
                $membership_data{$index}{$key} =~ /^Teacher\s*$/i ||
                $membership_data{$index}{$key} =~ /^Priest\s*$/i)) {
                $aaronic_name = $membership_data{$index}{'Preferred Name'};
-               $phone = $membership_data{$index}{'Phone 1'};
-               if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
+               $phone = $membership_data{$index}{'Household Phone'};
+               if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
                if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
-               $sth = $dbh->prepare("select * from eq_aaronic where name='$aaronic_name'");
+               $email = $membership_data{$index}{'Individual E-mail'};
+               if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
+               $sth = $dbh->prepare("select * from 3rd_aaronic where name='$aaronic_name'");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                my @data = ();
                while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
@@ -107,14 +142,16 @@ sub update_eq_aaronic_table
                if($rows == 0) {
                    # No existing records found for this aaronic, make a new entry
                    print "   Adding new Aaronic: $aaronic_name\n";
-                   $sth = $dbh->prepare("insert into eq_aaronic values (NULL,'$aaronic_name','$phone',1)");
+                   $sth = $dbh->prepare("insert into 3rd_aaronic values (NULL,'$aaronic_name','$phone','$email',1)");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } elsif($rows == 1) {
                    # An existing record was found for this aaronic, update it, mark it valid!
                    print "   Updating existing aaronic: $aaronic_name\n";
-                   $sth = $dbh->prepare("update eq_aaronic set phone='$phone' where name='$aaronic_name'");
+                   $sth = $dbh->prepare("update 3rd_aaronic set phone='$phone' where name='$aaronic_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_aaronic set valid=1 where name='$aaronic_name'");
+                   $sth = $dbh->prepare("update 3rd_aaronic set email='$email' where name='$aaronic_name'");
+                   $sth->execute or die "-E- DB error: $DBI::errstr\n";
+                   $sth = $dbh->prepare("update 3rd_aaronic set valid=1 where name='$aaronic_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } else {
                    # More than one record was found. Error! This shouldn't happen.
@@ -126,13 +163,16 @@ sub update_eq_aaronic_table
     $sth->finish();
 }
 
-# EQ_ELDER
+# 3RD_INDIV
 #+-------------+------------------+------+-----+---------+----------------+
 #| Field       | Type             | Null | Key | Default | Extra          |
 #+-------------+------------------+------+-----+---------+----------------+
-#| elder       | int(16) unsigned |      | PRI | NULL    | auto_increment |
+#| indiv       | int(16) unsigned |      | PRI | NULL    | auto_increment |
+#| indiv_id    | int(16) unsigned |      |     | NULL    |                |
 #| name        | varchar(60)      | YES  |     | NULL    |                |
 #| phone       | varchar(12)      | YES  |     | NULL    |                |
+#| email       | varchar(120)     | YES  |     | NULL    |                |
+#| priesthood  | enum             | YES  |     | NULL    |                |
 #| ppi_pri     | int(10) unsigned | YES  |     | 1       |                |
 #| ppi_notes   | varchar(128)     | YES  |     | NULL    |                |
 #| int_pri     | int(10) unsigned | YES  |     | 1       |                |
@@ -140,22 +180,43 @@ sub update_eq_aaronic_table
 #| attending   | tinyint(1)       | YES  |     | 0       |                |
 #| valid       | tinyint(1)       | YES  |     | NULL    |                |
 #+-------------+------------------+------+-----+---------+----------------+
-sub update_eq_elder_table
+sub update_3rd_indiv_table
 {
-    print "\n-> Updating eq_elder table\n";
+    print "\n-> Updating 3rd_indiv table\n";
 
     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
-    $sth = $dbh->prepare("update eq_elder set valid=0");
+    $sth = $dbh->prepare("update 3rd_indiv set valid=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     
     foreach $index (keys %membership_data)
     {
        $hashref = $membership_data{$index};
        foreach $key (keys %$hashref) {
-           if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
+           if($key =~ /Priesthood/i && 
+              ($membership_data{$index}{$key} =~ /Deacon/i ||
+               $membership_data{$index}{$key} =~ /Teacher/i ||
+               $membership_data{$index}{$key} =~ /Priest/i ||
+               $membership_data{$index}{$key} =~ /Elder/i ||
+               $membership_data{$index}{$key} =~ /Unordained/i)) {
+               
+               # check if this is a prospective elder under the stewardship of the EQ
+               if($membership_data{$index}{$key} !~ /Elder/i) {
+                       $found = 0;
+                       foreach $i (keys %prospective_elder_data) {
+                               #print "$membership_data{$index}{'Full Name'}\n";
+                               #print "$prospective_elder_data{$i}{'Full Name'}\n";
+                               if($membership_data{$index}{'Full Name'} eq $prospective_elder_data{$i}{'Full Name'}) {
+                                       $found = 1;
+                                       last;
+                               }
+                       }
+                       if($found == 0) { next;}
+               }
+               
                $id = $membership_data{$index}{'Indiv ID'};
-               $elder_name = $membership_data{$index}{'Preferred Name'};
-               $phone = $membership_data{$index}{'Phone 1'};
+               $indiv_name = $membership_data{$index}{'Preferred Name'};
+               $phone = $membership_data{$index}{'Household Phone'};
+               $priesthood = $membership_data{$index}{'Priesthood'};
                $organization = $organization_by_id{$id};
                $attending = 0;
                if(($organization =~ /Elders/) ||
@@ -163,34 +224,42 @@ sub update_eq_elder_table
                   ($organization =~ /Sunday School/) ||
                   ($organization =~ /Primary/)
                   ) { $attending = 1; }
-               if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
+               if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
                if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
-               $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
+               $email = $membership_data{$index}{'indiv E-mail'};
+               if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
+               $sth = $dbh->prepare("select * from 3rd_indiv where name='$indiv_name'");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                my @data = ();
                while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
                my $rows = scalar @data;
                if($rows == 0) {
-                   # No existing records found for this elder, make a new entry
-                   print "   Adding new Elder: $elder_name\n";
-                   $sth = $dbh->prepare("insert into eq_elder values (NULL,'$elder_name','$phone','1','','1','',$attending,1)");
+                   # No existing records found for this indiv, make a new entry
+                   print "   Adding new indiv: $indiv_name\n";
+                   $sth = $dbh->prepare("insert into 3rd_indiv values (NULL,'$id','$indiv_name','$phone','$email','$priesthood','n','1','','1','',$attending,1)");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } elsif($rows == 1) {
-                   # An existing record was found for this elder, update it
-                   print "   Updating existing Elder: $elder_name\n";
-                   $sth = $dbh->prepare("update eq_elder set valid=1 where name='$elder_name'");
+                   # An existing record was found for this indiv, update it
+                   print "   Updating existing indiv: $indiv_name\n";
+                   $sth = $dbh->prepare("update 3rd_indiv set valid=1 where name='$indiv_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                    if($phone ne "") { 
-                       $sth = $dbh->prepare("update eq_elder set phone='$phone' where name='$elder_name'");
+                       $sth = $dbh->prepare("update 3rd_indiv set phone='$phone' where name='$indiv_name'");
                    } else {
-                       $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_name'");
+                       $sth = $dbh->prepare("update 3rd_indiv set phone=NULL where name='$indiv_name'");
                    }
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_elder set attending='$attending' where name='$elder_name'");
+                   $sth = $dbh->prepare("update 3rd_indiv set attending='$attending' where name='$indiv_name'");
+                   $sth->execute or die "-E- DB error: $DBI::errstr\n";
+                   $sth = $dbh->prepare("update 3rd_indiv set indiv_id='$id' where name='$indiv_name'");
+                   $sth->execute or die "-E- DB error: $DBI::errstr\n";
+                   $sth = $dbh->prepare("update 3rd_indiv set priesthood='$priesthood' where name='$indiv_name'");
+                   $sth->execute or die "-E- DB error: $DBI::errstr\n";
+                   $sth = $dbh->prepare("update 3rd_indiv set email='$email' where name='$indiv_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } else {
                    # More than one record was found. Error! This shouldn't happen.
-                   print "   -E- More than one record found ($rows) for Elder: $elder_name\n";
+                   print "   -E- More than one record found ($rows) for indiv: $indiv_name\n";
                }
            }
        }
@@ -198,7 +267,7 @@ sub update_eq_elder_table
     $sth->finish();
 }
 
-# EQ_CALLING
+# 3RD_CALLING
 #+--------------+------------------+------+-----+---------+-------+
 #| Field        | Type             | Null | Key | Default | Extra |
 #+--------------+------------------+------+-----+---------+-------+
@@ -209,16 +278,16 @@ sub update_eq_elder_table
 #| sequence     | int(16) unsigned | YES  |     | NULL    |       |
 #| sustained    | date             | YES  |     | NULL    |       |
 #+--------------+------------------+------+-----+---------+-------+
-sub update_eq_calling_table()
+sub update_3rd_calling_table()
 {
-    print "\n-> Updating eq_calling table\n";
+    print "\n-> Updating 3rd_calling table\n";
 
     #print "-> Organization Data Dump\n\n";
     #&print_hash(\%organization_data);
     
     # Delete all records from the calling table. We have no history to
     # save here. Just re-populate with the latest calling information.
-    $sth = $dbh->prepare("delete from eq_calling ");
+    $sth = $dbh->prepare("delete from 3rd_calling ");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     
     foreach $index (keys %organization_data)
@@ -235,12 +304,12 @@ sub update_eq_calling_table()
        $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
        if($name eq "") { next; }
        print "   Adding new Calling: $name -> $position\n";
-       $sth = $dbh->prepare("insert into eq_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
+       $sth = $dbh->prepare("insert into 3rd_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
        $sth->execute or die "-E- DB error: $DBI::errstr\n";
     }
 }
 
-# EQ_DISTRICT
+# 3RD_DISTRICT
 #+------------+------------------+------+-----+---------+-------+
 #| Field      | Type             | Null | Key | Default | Extra |
 #+------------+------------------+------+-----+---------+-------+
@@ -249,47 +318,47 @@ sub update_eq_calling_table()
 #| supervisor | int(16) unsigned | YES  |     | NULL    |       |
 #| valid      | tinyint(1)       | YES  |     | NULL    |       |
 #+------------+------------------+------+-----+---------+-------+
-sub update_eq_district_table
+sub update_3rd_district_table
 {
     # Districts should be created by hand. This subroutine only
     # updates the supervisor's ID in each district.
-    print "\n-> Updating eq_district table\n";
-    $sth = $dbh->prepare("select * from eq_district");
+    print "\n-> Updating 3rd_district table\n";
+    $sth = $dbh->prepare("select * from 3rd_district");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     while($sqlhashref = $sth->fetchrow_hashref) {
        $supervisor_name = $sqlhashref->{name};
        $district = $sqlhashref->{district};
-       $sth2 = $dbh->prepare("select * from eq_elder where name='$supervisor_name'");
+       $sth2 = $dbh->prepare("select * from 3rd_indiv where name='$supervisor_name'");
        $sth2->execute or die "-E- DB error: $DBI::errstr\n";
        $sqlhashref2 = $sth2->fetchrow_hashref;
-       $supervisor_id = $sqlhashref2->{elder};
+       $supervisor_id = $sqlhashref2->{indiv};
        $sth2->finish();
-       $sth2 = $dbh->prepare("update eq_district set supervisor='$supervisor_id' where district='$district'");
+       $sth2 = $dbh->prepare("update 3rd_district set supervisor='$supervisor_id' where district='$district'");
        $sth2->execute or die "-E- DB error: $DBI::errstr\n";
        $sth2->finish();
     }
     $sth->finish();
 }
 
-# EQ_COMPANIONSHIP
+# 3RD_COMPANIONSHIP
 #+---------------+------------------+------+-----+---------+-------+
 #| Field         | Type             | Null | Key | Default | Extra |
 #+---------------+------------------+------+-----+---------+-------+
 #| companionship | int(16) unsigned |      |     | 0       |       |
-#| elder         | int(16) unsigned | YES  |     | NULL    |       |
+#| indiv         | int(16) unsigned | YES  |     | NULL    |       |
 #| aaronic       | int(16) unsigned | YES  |     | NULL    |       |
 #| district      | int(16) unsigned | YES  |     | NULL    |       |
 #| valid         | tinyint(1)       | YES  |     | NULL    |       |
 #+---------------+------------------+------+-----+---------+-------+
-sub update_eq_companionship_table
+sub update_3rd_companionship_table
 {
-    print "\n-> Updating eq_companionship table\n";
+    print "\n-> Updating 3rd_companionship table\n";
 
     # First, mark all existing companionships as invalid in case they have been dissolved
-    $sth = $dbh->prepare("update eq_companionship set valid=0");
+    $sth = $dbh->prepare("update 3rd_companionship set valid=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
-    $sth = $dbh->prepare("update eq_aaronic set valid=0");
+    $sth = $dbh->prepare("update 3rd_aaronic set valid=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     
     foreach $index (keys %hometeaching_data)
@@ -298,54 +367,54 @@ sub update_eq_companionship_table
        foreach $key (keys %$hashref) {
            if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
                foreach $field ("Home Teacher 1","Home Teacher 2") {
-                   $elder_name = $hometeaching_data{$index}{$field};
-                   if($elder_name eq "") { next; }
-                   $sth2 = $dbh->prepare("select * from eq_elder where name='$elder_name'");
+                   $indiv_name = $hometeaching_data{$index}{$field};
+                   if($indiv_name eq "") { next; }
+                   $sth2 = $dbh->prepare("select * from 3rd_indiv where name='$indiv_name'");
                    $sth2->execute or die "-E- DB error: $DBI::errstr\n";
                    $sqlhashref2 = $sth2->fetchrow_hashref;
-                   $elder = $sqlhashref2->{elder};
+                   $indiv = $sqlhashref2->{indiv};
                    $aaronic = "NULL";
-                   if($elder eq "") {
-                       $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_name'");
+                   if($indiv eq "") {
+                       $sth2 = $dbh->prepare("select * from 3rd_aaronic where name='$indiv_name'");
                        $sth2->execute or die "-E- DB error: $DBI::errstr\n";
                        $sqlhashref2 = $sth2->fetchrow_hashref;
                        $aaronic = $sqlhashref2->{aaronic};
-                       $elder = "NULL";
-                       if($aaronic eq "") { print "-W- Unable to find $elder_name in eq_elder or eq_aaronic tables\n"; next; }
+                       $indiv = "NULL";
+                       if($aaronic eq "") { print "-W- Unable to find $indiv_name in 3rd_indiv or 3rd_aaronic tables\n"; next; }
                    } 
                    $id = $hometeaching_data{$index}{'Comp ID'};
                    $district = $hometeaching_data{$index}{'HT District'};
-                   $sth = $dbh->prepare("select * from eq_companionship where elder='$elder' and aaronic='$aaronic' and companionship='$id'");
+                   $sth = $dbh->prepare("select * from 3rd_companionship where indiv='$indiv' and aaronic='$aaronic' and companionship='$id'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                    my @data = ();
                    while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
                    my $rows = scalar @data;
                    if($rows == 0) {
                        # No existing records found for this companionship, make a new entry
-                       print "   Adding Companion to companionship: $elder_name -> $id\n";
-                       $sth = $dbh->prepare("insert into eq_companionship values ($id,'$elder','$aaronic','$district',1)");
+                       print "   Adding Companion to companionship: $indiv_name -> $id\n";
+                       $sth = $dbh->prepare("insert into 3rd_companionship values ($id,'$indiv','$aaronic','$district',1)");
                        $sth->execute or die "-E- DB error: $DBI::errstr\n";
                    } else {
                        # An existing companionship was found for this companionship, update it
-                       $sth2 = $dbh->prepare("select * from eq_companionship where district='$district' and companionship='$id'");
+                       $sth2 = $dbh->prepare("select * from 3rd_companionship where district='$district' and companionship='$id'");
                        $sth2->execute or die "-E- DB error: $DBI::errstr\n";
-                       if($elder ne "NULL") {
-                           print "   Updating Companionship with Elder: $elder_name ($elder) -> $id\n";
-                           $sth = $dbh->prepare("update eq_companionship set district='$district' where elder='$elder' and companionship='$id'");
+                       if($indiv ne "NULL") {
+                           print "   Updating Companionship with indiv: $indiv_name ($indiv) -> $id\n";
+                           $sth = $dbh->prepare("update 3rd_companionship set district='$district' where indiv='$indiv' and companionship='$id'");
                            $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
-                           $sth = $dbh->prepare("update eq_companionship set elder='$elder' where elder='$elder' and companionship='$id'");
-                           $sth->execute or die "-E- DB error 'elder': $DBI::errstr\n";
-                           $sth = $dbh->prepare("update eq_companionship set valid=1 where elder='$elder' and companionship='$id'");
+                           $sth = $dbh->prepare("update 3rd_companionship set indiv='$indiv' where indiv='$indiv' and companionship='$id'");
+                           $sth->execute or die "-E- DB error 'indiv': $DBI::errstr\n";
+                           $sth = $dbh->prepare("update 3rd_companionship set valid=1 where indiv='$indiv' and companionship='$id'");
                            $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
                        } else {
-                           print "   Updating Companionship with Aaronic: $elder_name ($aaronic) -> $id\n";
-                           $sth = $dbh->prepare("update eq_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
+                           print "   Updating Companionship with Aaronic: $indiv_name ($aaronic) -> $id\n";
+                           $sth = $dbh->prepare("update 3rd_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
                            $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                           $sth = $dbh->prepare("update eq_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
+                           $sth = $dbh->prepare("update 3rd_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
                            $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                           $sth = $dbh->prepare("update eq_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
+                           $sth = $dbh->prepare("update 3rd_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
                            $sth->execute or die "-E- DB error: $DBI::errstr\n";                            
-                           $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
+                           $sth = $dbh->prepare("update 3rd_aaronic set valid=1 where aaronic='$aaronic'");
                            $sth->execute or die "-E- DB error: $DBI::errstr\n";
                        }
                    }
@@ -357,7 +426,7 @@ sub update_eq_companionship_table
     }
 }
 
-# EQ_FAMILY
+# 3RD_FAMILY
 #+---------------+------------------+------+-----+---------+-------+
 #| Field         | Type             | Null | Key | Default | Extra |
 #+---------------+------------------+------+-----+---------+-------+
@@ -365,20 +434,20 @@ sub update_eq_companionship_table
 #| hofh_id       | int(16) unsigned | YES  |     | NULL    |       |
 #| name          | varchar(30)      | YES  |     | NULL    |       |
 #| name_id       | varchar(30)      | YES  |     | NULL    |       |
-#| elder_id      | int(16) unsigned | YES  |     | NULL    |       |
+#| indiv_id      | int(16) unsigned | YES  |     | NULL    |       |
 #| companionship | int(16) unsigned | YES  |     | NULL    |       |
 #| visit_pri     | int(10) unsigned | YES  |     | 1       |       |
 #| visit_notes   | varchar(128)     | YES  |     | NULL    |       |
 #| valid         | tinyint(1)       | YES  |     | NULL    |       |
 #+---------------+------------------+------+-----+---------+-------+
-sub update_eq_family_table
+sub update_3rd_family_table
 {
-    print "\n-> Updating eq_family table\n";
+    print "\n-> Updating 3rd_family table\n";
 
     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
-    $sth = $dbh->prepare("update eq_family set valid=0");
+    $sth = $dbh->prepare("update 3rd_family set valid=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
-    $sth = $dbh->prepare("update eq_family set companionship=0");
+    $sth = $dbh->prepare("update 3rd_family set companionship=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     
     foreach $index (keys %membership_data)
@@ -392,7 +461,7 @@ sub update_eq_family_table
                $name_id = uc($family_name);
 
                # Find out how many families match this family's name
-               $sth = $dbh->prepare("select * from eq_family where name_id='$name_id'");
+               $sth = $dbh->prepare("select * from 3rd_family where name_id='$name_id'");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                my @data = ();
                while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
@@ -401,27 +470,27 @@ sub update_eq_family_table
                if($rows == 0) {
                    # No existing records found for this family, make a new entry
                    print "   Adding new Family: $family_name\n";
-                   $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','$name_id','0','0','1','',1)");
+                   $sth = $dbh->prepare("insert into 3rd_family values (NULL,$id,'$family_name','$name_id','0','0','1','',1)");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } elsif($rows == 1) {
                    # An existing record was found for this family, update it
                    print "   Updating existing family: $family_name\n";
-                   $sth = $dbh->prepare("update eq_family set hofh_id=$id where name_id='$name_id'");
+                   $sth = $dbh->prepare("update 3rd_family set hofh_id=$id where name_id='$name_id'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_family set valid=1 where name_id='$name_id'");
+                   $sth = $dbh->prepare("update 3rd_family set valid=1 where name_id='$name_id'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } else {
                    # More than one record was found. Error! This shouldn't happen.
                    print "   -E- More than one record found ($rows) for family name: $family_name\n";
                }
 
-                # Now update the elder_id field for this family
-               $sth = $dbh->prepare("select * from eq_elder WHERE name='$family_name'");
+                # Now update the indiv_id field for this family
+               $sth = $dbh->prepare("select * from 3rd_indiv WHERE name='$family_name'");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                while($sqlhashref = $sth->fetchrow_hashref) {
-                   $elder_id = $sqlhashref->{elder};
-                   print "   Updating family elder_id: $family_name -> $elder_id\n";
-                   $sth = $dbh->prepare("update eq_family set elder_id=$elder_id where name_id='$name_id'");
+                   $indiv_id = $sqlhashref->{indiv};
+                   print "   Updating family indiv_id: $family_name -> $indiv_id\n";
+                   $sth = $dbh->prepare("update 3rd_family set indiv_id=$indiv_id where name_id='$name_id'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                }
                
@@ -445,18 +514,8 @@ sub update_eq_family_table
                        {
                            print "   Updating hometeaching assignment for $family_name family\n";
                            $companionship = $hometeaching_data{$index}{'Comp ID'};
-                           $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name_id='$name_id'");
+                           $sth = $dbh->prepare("update 3rd_family set companionship='$companionship' where name_id='$name_id'");
                            $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                           
-                           # In addition to changing the hometeaching assignment, update the eq_visit table
-                           # so that all previous visits made by the old hometeaching companionship, get
-                           # updated with the id of the new companionship. This will allow us to go in and
-                           # edit the history as needed for the family being hometaught under their new
-                           # companionship. Otherwise, we will not be able to update this history.
-                           #$family_id = $data[0]->{family};
-                           #print "   update eq_visit set companionship='$companionship' where family='$family_id' and companionship!='0'\n";
-                           #$sth = $dbh->prepare("update eq_visit set companionship='$companionship' where companionship='$old_companionship'");
-                           #$sth->execute or die "-E- DB error: $DBI::errstr\n";
                        }
                    }
                }
@@ -466,7 +525,7 @@ sub update_eq_family_table
     }
 }
 
-# EQ_PARENT
+# 3RD_PARENT
 #+----------+------------------+------+-----+---------+-------+
 #| Field    | Type             | Null | Key | Default | Extra |
 #+----------+------------------+------+-----+---------+-------+
@@ -479,12 +538,12 @@ sub update_eq_family_table
 #| indiv_id | int(16) unsigned | YES  | UNI | NULL    |       |
 #| valid    | tinyint(1)       | YES  |     | NULL    |       |
 #+----------+------------------+------+-----+---------+-------+
-sub update_eq_parent_table
+sub update_3rd_parent_table
 {
-    print "\n-> Updating eq_parent table\n";
+    print "\n-> Updating 3rd_parent table\n";
 
     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
-    $sth = $dbh->prepare("update eq_parent set valid=0");
+    $sth = $dbh->prepare("update 3rd_parent set valid=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     
     foreach $index (keys %membership_data)
@@ -502,8 +561,8 @@ sub update_eq_parent_table
                $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
                $hofh_id = $membership_data{$index}{'HofH ID'};
                $id = $membership_data{$index}{'Indiv ID'};
-               $phone = $membership_data{$index}{'Phone 1'};
-               if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
+               $phone = $membership_data{$index}{'Household Phone'};
+               if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
                if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
                $address = $membership_data{$index}{'Street 1'};
                if($membership_data{$index}{'Street 2'} ne "") { 
@@ -511,7 +570,7 @@ sub update_eq_parent_table
                }
 
                # Find the family id for this parent's HofH_ID.
-               $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
+               $sth = $dbh->prepare("select * from 3rd_family where hofh_id='$hofh_id' and valid=1");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                my @family_data = ();
                while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
@@ -520,7 +579,7 @@ sub update_eq_parent_table
                else { $family_id = 0; }
                
                # Find out how many parents match this parent's name
-               $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
+               $sth = $dbh->prepare("select * from 3rd_parent where name='$parent_name'");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                my @data = ();
                while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
@@ -529,22 +588,22 @@ sub update_eq_parent_table
                if($rows == 0 && $family_rows > 0) {
                    # No existing records found for this parent, make a new entry
                    print "   Adding new Parent: $parent_name\n";
-                   $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
+                   $sth = $dbh->prepare("insert into 3rd_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } elsif($rows == 1 && $family_rows > 0) {
                    # An existing record was found for this parent, update it
                    print "   Updating existing parent: $parent_name\n";
-                   $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
+                   $sth = $dbh->prepare("update 3rd_parent set family='$family_id' where name='$parent_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
+                   $sth = $dbh->prepare("update 3rd_parent set birthday='$year-$month-$day' where name='$parent_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_parent set phone='$phone' where name='$parent_name'");
+                   $sth = $dbh->prepare("update 3rd_parent set phone='$phone' where name='$parent_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_parent set address='$address' where name='$parent_name'");
+                   $sth = $dbh->prepare("update 3rd_parent set address='$address' where name='$parent_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
+                   $sth = $dbh->prepare("update 3rd_parent set valid=1 where name='$parent_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_parent set indiv_id='$id' where name='$parent_name'");
+                   $sth = $dbh->prepare("update 3rd_parent set indiv_id='$id' where name='$parent_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } elsif($rows > 1) {
                    # More than one record was found. Error! This shouldn't happen.
@@ -558,7 +617,7 @@ sub update_eq_parent_table
     }
 }
 
-# EQ_CHILD
+# 3RD_CHILD
 #+----------+------------------+------+-----+---------+-------+
 #| Field    | Type             | Null | Key | Default | Extra |
 #+----------+------------------+------+-----+---------+-------+
@@ -569,12 +628,12 @@ sub update_eq_parent_table
 #| indiv_id | int(16) unsigned | YES  | UNI | NULL    |       |
 #| valid    | tinyint(1)       | YES  |     | NULL    |       |
 #+----------+------------------+------+-----+---------+-------+
-sub update_eq_child_table
+sub update_3rd_child_table
 {
-    print "\n-> Updating eq_child table\n";
+    print "\n-> Updating 3rd_child table\n";
 
     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
-    $sth = $dbh->prepare("update eq_child set valid=0");
+    $sth = $dbh->prepare("update 3rd_child set valid=0");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     
     foreach $index (keys %membership_data)
@@ -590,7 +649,7 @@ sub update_eq_child_table
                $hofh_id = $membership_data{$index}{'HofH ID'};
 
                # Find the family id for this child's HofH_ID.
-               $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
+               $sth = $dbh->prepare("select * from 3rd_family where hofh_id='$hofh_id' and valid=1");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                my @family_data = ();
                while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
@@ -599,7 +658,7 @@ sub update_eq_child_table
                else { $family_id = 0; }
                
                # Find out how many children have the same name for the same family
-               $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
+               $sth = $dbh->prepare("select * from 3rd_child where name='$child_name'");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
                my @data = ();
                while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
@@ -608,18 +667,18 @@ sub update_eq_child_table
                if($rows == 0 && $family_rows > 0) {
                    # No existing records found for this child, make a new entry
                    print "   Adding new Child: $child_name\n";
-                   $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
+                   $sth = $dbh->prepare("insert into 3rd_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } elsif($rows == 1 && $family_rows > 0) {
                    # An existing record was found for this child, update it
                    print "   Updating existing child: $child_name\n";
-                   $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
+                   $sth = $dbh->prepare("update 3rd_child set family='$family_id' where name='$child_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
+                   $sth = $dbh->prepare("update 3rd_child set birthday='$year-$month-$day' where name='$child_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
+                   $sth = $dbh->prepare("update 3rd_child set valid=1 where name='$child_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-                   $sth = $dbh->prepare("update eq_child set indiv_id='$id' where name='$child_name'");
+                   $sth = $dbh->prepare("update 3rd_child set indiv_id='$id' where name='$child_name'");
                    $sth->execute or die "-E- DB error: $DBI::errstr\n";
                } else {
                    # More than one record was found. Error! This shouldn't happen.
@@ -631,6 +690,113 @@ sub update_eq_child_table
     }
 }
 
+# 3RD_VISIT
+#+----------------+------------------+------+-----+---------+-------+
+#| Field          | Type             | Null | Key | Default | Extra |
+#+----------------+------------------+------+-----+---------+-------+
+#| visit          | int(16) unsigned |      | PRI | 0       |   A   |
+#| family         | int(16) unsigned | YES  | UNI | NULL    |       |
+#| companionship  | int(16) unsigned | YES  |     | NULL    |       |
+#| date           | date             | YES  |     | NULL    |       |
+#| notes          | varchar(128)     | YES  |     | NULL    |       |
+#| visited        | varchar(1)       | YES  |     | NULL    |       |
+#+----------------+------------------+------+-----+---------+-------+
+sub update_3rd_visit_table
+{
+       print "\n-> updating 3rd_visit table\n";
+       
+       my $month_header_retrieved = 0;
+       my $month_header;
+       my @data_months;
+       my %months = ('Jan', 1, 'Feb', 2, 'Mar', 3, 'Apr', 4, 'May', 5, 'Jun', 6, 'Jul', 7, 'Aug', 8, 'Sep', 9, 'Oct', 10, 'Nov', 11, 'Dec', 12);
+       ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
+       my %visit_status = ('X', 'y', '-', 'n', '', '');
+       
+       foreach $index (keys %hometeaching_stats_data)
+       {
+               $hashref = $hometeaching_stats_data{$index};
+               #foreach $key (keys %$hashref) {print "$key\n";}
+               
+               $family_name = $hometeaching_stats_data{$index}{"Preferred Name"};
+               print "   Updating visit data: $family_name\n";
+
+               # get family id from 3rd_family
+               $sth = $dbh->prepare("select * from 3rd_family where name=\"$family_name\" and valid=1");
+               $sth->execute or die "-E- DB error: $DBI::errstr\n";
+               my @family_data = ();
+               while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
+               my $family_rows = scalar @family_data;
+               if($family_rows > 0) { 
+                       $family_id = $family_data[0]->{'family'}; 
+                       $comp_id = $family_data[0]->{'companionship'};
+               }
+               else { next; }
+               #print "family_id = $family_id\n";
+               #print "comp_id = $comp_id\n";
+               
+               # ignore visits that weren't done by the EQ
+               if ($comp_id == 0) { next; }
+               
+               # retrieve the month header if not already done
+               if ($month_header_retrieved == 0)
+               {
+                       foreach $key (keys %$hashref) 
+                       {
+                               if (($key ne "Preferred Name") && ($key ne "Home Teachers"))
+                               {
+                                       $month_header = $key;
+                                       @data_months = split /\t/, $key;
+                               }
+                       }
+                       $month_header_retrieved = 1;
+               }
+               
+               # loop through history data
+               @history = split /\t/, $hometeaching_stats_data{$index}{$month_header};
+               my $data_year = 1900 + $yearOffset;
+               my $data_month = $months{$data_months[-1]};
+               #print "$month_header\n";
+               #print $hometeaching_stats_data{$index}{$month_header};
+               #print "\n";
+               foreach $i (reverse(0..$#history)) {
+                       # went back a calendar year, decrement $data_year
+                       if ($months{$data_months[$i]} > $data_month)
+                       {
+                               $data_year -= 1;
+                       }
+                       $data_month = $months{$data_months[$i]};
+                       my $visit_date = sprintf("%4d-%02d-01\n", $data_year, $data_month);
+                       #print "$visit_date\n";
+                       my $importing_status = $visit_status{$history[$i]};
+                       #print "importing_status = $importing_status\n";
+                       #print "select * from 3rd_visit where family=$family_id and companionship=$comp_id and date='$visit_date'\n";
+                       $sth = $dbh->prepare("select * from 3rd_visit where family=$family_id and companionship=$comp_id and date='$visit_date'");
+                       $sth->execute or die "-E- DB error: $DBI::errstr\n";
+                       my @visit_data = ();
+                       while($sqlhashref = $sth->fetchrow_hashref) { push(@visit_data, $sqlhashref); }
+                       my $visit_rows = scalar @visit_data;
+                       if($visit_rows > 0) { 
+                               my $visited = $visit_data[0]->{'visited'}; 
+                               #print "visited = $visited\n";
+                               # update visit if data is different in 3rd_visit
+                               if ($visited ne $importing_status)
+                               {
+                                       #print "importing_status = $importing_status\n";
+                                       $sth = $dbh->prepare("update 3rd_visit set visited='$importing_status' where family='$family_id' and date='$visit_date' and companionship='$comp_id'");
+                                       $sth->execute or die "-E- DB error: $DBI::errstr\n";
+                               }
+                       } else {
+                               if ($importing_status ne '')
+                               {
+                                       # add visit if it doesn't exist in 3rd_visit
+                                       $sth = $dbh->prepare("insert into 3rd_visit values (NULL, '$family_id', '$comp_id', '', '', '$visit_date', '', '$importing_status', 'hometeaching')");
+                                       $sth->execute or die "-E- DB error: $DBI::errstr\n";
+                               }
+                       }
+               }
+       }
+}
+
 ######################################################################
 sub check_for_changed_ids
 {
@@ -708,9 +874,11 @@ print "\n-> Processing all ward data files in $datadir\n";
 
 ###################################################
 # Parse Ward Data Files
+&optional_csv_to_hash("$datadir/EQ\ Prospective\ Elders.csv", \%prospective_elder_data);
 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
+&optional_csv_to_hash("$datadir/Home\ Teacher\ per\ Companionship.csv", \%hometeaching_stats_data);
 %organization_by_name = ();
 %organization_by_id = ();
 
@@ -721,19 +889,22 @@ if($opt_v) {
     &print_hash(\%hometeaching_data);
     print "-> Organization Data Dump\n\n";
     &print_hash(\%organization_data);
+    print "-> HomeTeaching Stats Data Dump\n\n";
+    &print_hash(\%hometeaching_stats_data);
 }
 
 if($opt_s) { $dbh->disconnect(); exit; }
 
 # Now update the various eq DB tables
-&update_eq_calling_table();
-&update_eq_elder_table();
-&update_eq_aaronic_table();
-&update_eq_district_table();
-&update_eq_companionship_table();
-&update_eq_family_table();
-&update_eq_parent_table();
-&update_eq_child_table();
+&update_3rd_calling_table();
+&update_3rd_indiv_table();
+&update_3rd_aaronic_table();
+&update_3rd_district_table();
+&update_3rd_companionship_table();
+&update_3rd_family_table();
+&update_3rd_parent_table();
+&update_3rd_child_table();
+&update_3rd_visit_table();
 
 print "\n-> Import Successful! DONE...\n";