Fixed bug in unwilling elder filter
[eq/.git] / import_ward_data
index d54f8fc77d7429cce6bd0caf34fb4ec0fc4f96ce..bd707b4da75f149659a18d4c63a6acc20481d66a 100755 (executable)
@@ -82,7 +82,7 @@ sub print_hash
 #+-------+--------------------+------+-----+---------+-------+
 sub update_eq_aaronic_table
 {
-    print "-> Updating eq_aaronic table\n";
+    print "\n-> Updating eq_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");
@@ -128,17 +128,22 @@ sub update_eq_aaronic_table
 }
 
 # EQ_ELDER
-#+------------+------------------+------+-----+---------+-------+
-#| Field      | Type             | Null | Key | Default | Extra |
-#+------------+------------------+------+-----+---------+-------+
-#| elder      | int(16) unsigned |      | PRI | 0       |   A   |
-#| name       | varchar(60)      | YES  |     | NULL    |       |
-#| phone      | varchar(12)      | YES  |     | NULL    |       |
-#| valid      | tinyint(1)       | YES  |     | NULL    |       |
-#+------------+------------------+------+-----+---------+-------+
+#+-------------+------------------+------+-----+---------+----------------+
+#| Field       | Type             | Null | Key | Default | Extra          |
+#+-------------+------------------+------+-----+---------+----------------+
+#| elder       | int(16) unsigned |      | PRI | NULL    | auto_increment |
+#| name        | varchar(60)      | YES  |     | NULL    |                |
+#| phone       | varchar(12)      | YES  |     | NULL    |                |
+#| ppi_pri     | int(10) unsigned | YES  |     | 1       |                |
+#| ppi_notes   | varchar(128)     | YES  |     | NULL    |                |
+#| int_pri     | int(10) unsigned | YES  |     | 1       |                |
+#| int_notes   | varchar(128)     | YES  |     | NULL    |                |
+#| attending   | tinyint(1)       | YES  |     | 0       |                |
+#| valid       | tinyint(1)       | YES  |     | NULL    |                |
+#+-------------+------------------+------+-----+---------+----------------+
 sub update_eq_elder_table
 {
-    print "-> Updating eq_elder table\n";
+    print "\n-> Updating eq_elder 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");
@@ -149,8 +154,16 @@ sub update_eq_elder_table
        $hashref = $membership_data{$index};
        foreach $key (keys %$hashref) {
            if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
+               $id = $membership_data{$index}{'Indiv ID'};
                $elder_name = $membership_data{$index}{'Preferred Name'};
                $phone = $membership_data{$index}{'Phone 1'};
+               $organization = $organization_by_id{$id};
+               $attending = 0;
+               if(($organization =~ /Elders/) ||
+                  ($organization =~ /Young Men/) ||
+                  ($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\d\d\d)/) { $phone = "$1-$2"; }
                $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
@@ -161,7 +174,7 @@ sub update_eq_elder_table
                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)");
+                   $sth = $dbh->prepare("insert into eq_elder values (NULL,'$elder_name','$phone','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
@@ -174,6 +187,8 @@ sub update_eq_elder_table
                        $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_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->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";
@@ -197,7 +212,7 @@ sub update_eq_elder_table
 #+--------------+------------------+------+-----+---------+-------+
 sub update_eq_calling_table()
 {
-    print "-> Updating eq_calling table\n";
+    print "\n-> Updating eq_calling table\n";
 
     #print "-> Organization Data Dump\n\n";
     #&print_hash(\%organization_data);
@@ -213,6 +228,8 @@ sub update_eq_calling_table()
        $name = $organization_data{$index}{'Indiv Name'};
        $name =~ s/\'/\\'/g; #'
        $organization = $organization_data{$index}{'Organization'};
+       $organization_by_name{$name} = $organization;
+       $organization_by_id{$indiv_id} = $organization;
        $position = $organization_data{$index}{'Position'};
        $sequence = $organization_data{$index}{'Org Seq'};
        $sustained = $organization_data{$index}{'Sustained'};
@@ -237,7 +254,7 @@ sub update_eq_district_table
 {
     # Districts should be created by hand. This subroutine only
     # updates the supervisor's ID in each district.
-    print "-> Updating eq_district table\n";
+    print "\n-> Updating eq_district table\n";
     $sth = $dbh->prepare("select * from eq_district");
     $sth->execute or die "-E- DB error: $DBI::errstr\n";
     while($sqlhashref = $sth->fetchrow_hashref) {
@@ -267,7 +284,7 @@ sub update_eq_district_table
 #+---------------+------------------+------+-----+---------+-------+
 sub update_eq_companionship_table
 {
-    print "-> Updating eq_companionship table\n";
+    print "\n-> Updating eq_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");
@@ -349,12 +366,15 @@ 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    |       |
 #| 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
 {
-    print "-> Updating eq_family table\n";
+    print "\n-> Updating eq_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");
@@ -382,7 +402,7 @@ 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',1)");
+                   $sth = $dbh->prepare("insert into eq_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
@@ -395,14 +415,28 @@ sub update_eq_family_table
                    # 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'");
+               $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'");
+                   $sth->execute or die "-E- DB error: $DBI::errstr\n";
+               }
+               
                # Now update the hometeaching field for this family
                foreach $index (keys %hometeaching_data)
                {
                    $hashref = $hometeaching_data{$index};
                    foreach $key (keys %$hashref) {
                        if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
-                           print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $1, $3 $4\n";
-                           $hometeaching_data{$index}{'Household'} = "$1, $3 $4";
+                           $a = $1; $b = $2; $c = $3; $d = $4;
+                           if($family_name =~ /$a/ && $hometeaching_data{$index}{'Household'} !~ /$family_name/i) { 
+                               print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $a, $c $d\n";
+                               $hometeaching_data{$index}{'Household'} = "$a, $c $d";
+                           }
                        }
                        if($key =~ /Quorum/i &&
                           $hometeaching_data{$index}{$key} =~ /Elders/i &&
@@ -438,7 +472,7 @@ sub update_eq_family_table
 #+----------+------------------+------+-----+---------+-------+
 sub update_eq_parent_table
 {
-    print "-> Updating eq_parent table\n";
+    print "\n-> Updating eq_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");
@@ -468,11 +502,14 @@ 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'");
+               $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
-               $sqlhashref = $sth->fetchrow_hashref();
-               $family_id = $sqlhashref->{'family'};
-
+               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'}; }
+               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->execute or die "-E- DB error: $DBI::errstr\n";
@@ -480,12 +517,12 @@ sub update_eq_parent_table
                while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
                my $rows = scalar @data;
                
-               if($rows == 0) {
+               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->execute or die "-E- DB error: $DBI::errstr\n";
-               } elsif($rows == 1) {
+               } 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'");
@@ -500,9 +537,11 @@ sub update_eq_parent_table
                    $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->execute or die "-E- DB error: $DBI::errstr\n";
-           } else {
+               } elsif($rows > 1) {
                    # More than one record was found. Error! This shouldn't happen.
-                   print "   -E- More than one record found with same parent name: $parent_name\n";
+                   print "   -E- More than one record found with same parent name: $parent_name with hofh_id: $hofh_id\n";
+               } else {
+                   print "   -E- Unable to find a family to attach this parent to: $parent_name with hofh_id: $hofh_id\n";
                }
                $sth->finish();
            }
@@ -523,7 +562,7 @@ sub update_eq_parent_table
 #+----------+------------------+------+-----+---------+-------+
 sub update_eq_child_table
 {
-    print "-> Updating eq_child table\n";
+    print "\n-> Updating eq_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");
@@ -542,11 +581,14 @@ 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'");
+               $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
                $sth->execute or die "-E- DB error: $DBI::errstr\n";
-               $sqlhashref = $sth->fetchrow_hashref();
-               $family_id = $sqlhashref->{'family'};
-
+               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'}; }
+               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->execute or die "-E- DB error: $DBI::errstr\n";
@@ -554,12 +596,12 @@ sub update_eq_child_table
                while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
                my $rows = scalar @data;
                
-               if($rows == 0) {
+               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->execute or die "-E- DB error: $DBI::errstr\n";
-               } elsif($rows == 1) {
+               } 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'");
@@ -653,13 +695,15 @@ if(defined $opt_o) {
 # Process command line options
 if(defined $opt_n) { $datadir = $opt_n; }
 else { $datadir = shift(@ARGV); }
-print "-> Processing all ward data files in $datadir\n";
+print "\n-> Processing all ward data files in $datadir\n";
 
 ###################################################
 # Parse Ward Data Files
 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
+%organization_by_name = ();
+%organization_by_id = ();
 
 if($opt_v) {
     print "-> Membership Data Dump\n\n";
@@ -673,6 +717,7 @@ if($opt_v) {
 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();
@@ -680,7 +725,6 @@ if($opt_s) { $dbh->disconnect(); exit; }
 &update_eq_family_table();
 &update_eq_parent_table();
 &update_eq_child_table();
-&update_eq_calling_table();
 
 ###################################################
 # Disconnect from the database