- # First, mark all existing companionships as invalid in case they have been dissolved
- $sth = $dbh->prepare("update eq_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->execute or die "-E- DB error: $DBI::errstr\n";
-
- foreach $index (keys %hometeaching_data)
- {
- $hashref = $hometeaching_data{$index};
- 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'");
- $sth2->execute or die "-E- DB error: $DBI::errstr\n";
- $sqlhashref2 = $sth2->fetchrow_hashref;
- $elder = $sqlhashref2->{elder};
- $aaronic = "NULL";
- if($elder eq "") {
- $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_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; }
- }
- $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->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)");
- $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->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'");
- $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->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'");
- $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->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->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ # First, mark all existing companionships as invalid in case they have been dissolved
+ $sth = $dbh->prepare("update tc_companionship set valid=0");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+
+ foreach $index (keys %hometeaching_data)
+ {
+ $hashref = $hometeaching_data{$index};
+ foreach $key (keys %$hashref) {
+ if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
+ foreach $field ("Home Teacher 1","Home Teacher 2") {
+ $individual_name = $hometeaching_data{$index}{$field};
+ if($individual_name eq "") { next; }
+ $sth2 = $dbh->prepare("select * from tc_individual where name='$individual_name'");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ $sqlhashref2 = $sth2->fetchrow_hashref;
+ $individual = $sqlhashref2->{individual};
+ $id = $hometeaching_data{$index}{'Comp ID'};
+ $district = $hometeaching_data{$index}{'HT District'};
+ $sth = $dbh->prepare("select * from tc_companionship where individual='$individual' 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: $individual_name -> $id\n";
+ $sth = $dbh->prepare("insert into tc_companionship values ($id,'$individual','$district',NULL,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 tc_companionship where district='$district' and companionship='$id'");
+ $sth2->execute or die "-E- DB error: $DBI::errstr\n";
+ print " Updating Companionship with individual: $individual_name ($individual) -> $id\n";
+ $sth = $dbh->prepare("update tc_companionship set district='$district' where individual='$individual' and companionship='$id'");
+ $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
+ $sth = $dbh->prepare("update tc_companionship set individual='$individual' where individual='$individual' and companionship='$id'");
+ $sth->execute or die "-E- DB error 'individual': $DBI::errstr\n";
+ $sth = $dbh->prepare("update tc_companionship set valid=1 where individual='$individual' and companionship='$id'");
+ $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
+ }
+ $sth->finish();
+ $sth2->finish();
+ }