- print "\n-> Updating tc_companionship table\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") {
- $indiv_name = $hometeaching_data{$index}{$field};
- if($indiv_name eq "") { next; }
- $sth2 = $dbh->prepare("select * from tc_indiv where name='$indiv_name'");
- $sth2->execute or die "-E- DB error: $DBI::errstr\n";
- $sqlhashref2 = $sth2->fetchrow_hashref;
- $indiv = $sqlhashref2->{indiv};
- $id = $hometeaching_data{$index}{'Comp ID'};
- $district = $hometeaching_data{$index}{'HT District'};
- $sth = $dbh->prepare("select * from tc_companionship where indiv='$indiv' 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: $indiv_name -> $id\n";
- $sth = $dbh->prepare("insert into tc_companionship values ($id,'$indiv','$district',1)");
+ print "\n-> Updating tc_companionship table\n";
+
+ my %companionships = {};
+
+ # build up hash for each access of companionships and what families they teach
+ foreach $index (keys %hometeaching_data) {
+ $hashref = $hometeaching_data{$index};
+ my $compid = $hometeaching_data{$index}{'Comp ID'};
+ my $household = $hometeaching_data{$index}{'Household'};
+ #print "household = $household :: ";
+ if ($household =~ /(.*)\s&.*/) { $household = $1; }
+ #print "household = $household\n";
+ my $district = $hometeaching_data{$index}{'HT District'};
+ my $ht1 = $hometeaching_data{$index}{'Home Teacher 1'};
+ my $ht2 = $hometeaching_data{$index}{'Home Teacher 2'};
+ my $quorum = $hometeaching_data{$index}{'Quorum'};
+ if (($compid ne '') && ($quorum =~ m/($default_stewardship)/i)) {
+ #print "compid=$compid, household=$household, district=$district, ht1=$ht1, ht2=$ht2, quorum=$quorum\n";
+ $companionships->{$compid}->{'families'}->{$household} = 1;
+ $companionships->{$compid}->{'district'} = $district;
+ #print "$district $companionships->{$compid}->{'district'}\n";
+ if ($ht1 ne "") { $companionships->{$compid}->{'companions'}->{$ht1} = 1; }
+ if ($ht2 ne "") { $companionships->{$compid}->{'companions'}->{$ht2} = 1; }
+ }
+ }
+
+ # loop through each mls companionship and see if there are any changes to companions or familes from what we have in 3rd Counselor
+ for my $companionship (keys %$companionships) {
+ #print "companionship = $companionship\n";
+ my $changed = 0;
+ my $tc_companionship_id = 0;
+ my $district = $companionships->{$companionship}->{'district'};
+ #print "district = $district\n";
+ for my $companion (keys %{$companionships->{$companionship}->{'companions'}}) {
+ #print "companion = $companion\n";
+ # see if $companion is in tc_companion
+ $sth = $dbh->prepare("SELECT * FROM tc_companion AS tc JOIN (tc_individual AS ti, tc_companionship AS tp) WHERE tc.individual=ti.individual AND tc.companionship=tp.companionship AND tp.mls_id=$companionship AND ti.name='$companion' COLLATE latin1_general_cs AND tp.type='H' AND tc.valid=1");
+ $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) {
+ print " $companion is not in tc_companion\n";
+ $changed = 1;
+ next;
+ }
+ $db_individual = $data[0]->{'individual'};
+ if ($tc_companionship_id == 0) {
+ # set tc_companionship to what was in database
+ $tc_companionship_id = $data[0]->{'companionship'};
+ } elsif ($tc_companionship_id != $data[0]->{'companionship'}) {
+ # companionship doesn't match what was in the database, we have a change
+ print " companionship doesn't match what is in database, $tc_companionship_id != $data[0]->{'companionship'}\n";
+ $changed = 1;
+ last;
+ }
+
+ # get list of families assigned to this companionship in database
+ #print "SELECT * FROM tc_family AS tf JOIN tc_individual AS ti WHERE tf.individual=ti.individual AND tf.companionship=$tc_companionship_id AND tf.valid=1\n";
+ $sth = $dbh->prepare("SELECT * FROM tc_family AS tf JOIN tc_individual AS ti WHERE tf.individual=ti.individual AND tf.companionship=$tc_companionship_id AND tf.valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ @data = ();
+ while ($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
+ for my $row (@data) {
+ #print "name: $row->{'name'}\n";
+ #print "companionship: $row->{'companionship'}\n";
+ # if family is not assigned, we have a change
+ if (! exists $companionships->{$companionship}->{'families'}->{$row->{'name'}}) {
+ print " family is not assigned, we have a change: $row->{'name'}\n";
+ $changed = 1;
+ last;
+ }
+ # if companionship assigned to family in database isn't this companionship, we have a change
+ if ($tc_companionship_id != $row->{'companionship'}) {
+ "companionship assigned to this family in the database isn't this companionship: $tc_companionship_id, $row->{'companionship'}\n";
+ $changed = 1;
+ last;
+ }
+ }
+
+ # loop through families from HomeTeaching.csv
+ for my $family (keys %{$companionships->{$companionship}->{'families'}}) {
+ #print "SELECT * FROM tc_family AS tf JOIN (tc_companionship AS tc, tc_individual AS ti) WHERE tf.companionship=tc.companionship AND tf.individual=ti.individual AND ti.name=\"$family\" COLLATE latin1_general_cs AND tf.valid=1\n";
+ $sth = $dbh->prepare("SELECT * FROM tc_family AS tf JOIN (tc_companionship AS tc, tc_individual AS ti) WHERE tf.companionship=tc.companionship AND tf.individual=ti.individual AND ti.name=\"$family\" COLLATE latin1_general_cs AND tc.type='H' AND tf.valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ # if family isn't in tc_family table, we have a change
+ # if family in tc_family has a different companionship assigned to it, we have a change
+ # no rows here means we have a change
+ @familydata = ();
+ while ($sqlhashref = $sth->fetchrow_hashref) { push(@familydata, $sqlhashref); }
+ if (scalar @familydata == 0) {
+ print " new family, not in db: $family\n";
+ $changed = 1;
+ last;
+ }
+ }
+
+ # last check to break out if we discovered a change
+ if ($changed == 1) { last; }
+ }
+
+ # we found a change to the companionship
+ if ($changed == 1) {
+ # invalidate existing companionship in database
+ print " invalidating companionship:$tc_companionship_id\n";
+ $sth = $dbh->prepare("UPDATE tc_companionship SET valid=0 WHERE companionship='$tc_companionship_id' AND type='H'");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+
+ # create new companionship in database
+ print " creating a new companionship\n";
+ $sth = $dbh->prepare("INSERT INTO tc_companionship values (NULL,$companionship,$district,'H',1)");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ $comp_id = $dbh->last_insert_id(NULL,NULL,'tc_companionship',NULL);
+
+ # create new companions in database
+ for my $companion (keys %{$companionships->{$companionship}->{'companions'}}) {
+ print " companion=$companion\n";
+ # if companion exists, preserve scheduling_priority
+ #print "SELECT tc.* FROM tc_companion AS tc JOIN (tc_individual AS ti, tc_companionship AS tp) WHERE tc.individual=ti.individual AND tc.companionship=tp.companionship AND tp.mls_id=$companionship AND ti.name=\"$companion\" AND tc.valid=1\n";
+ $sth = $dbh->prepare("SELECT tc.* FROM tc_companion AS tc JOIN (tc_individual AS ti, tc_companionship AS tp) WHERE tc.individual=ti.individual AND tc.companionship=tp.companionship AND tp.mls_id=$companionship AND ti.name=\"$companion\" AND tp.type='H' AND tc.valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ if ($sqlhashref = $sth->fetchrow_hashref) {
+ my $individual = $sqlhashref->{'individual'};
+ my $scheduling_priority = $sqlhashref->{'scheduling_priority'};
+ print " adding new companion $individual\n";
+ #print "INSERT INTO tc_companion values (NULL,$individual,$comp_id,$scheduling_priority,1)\n";
+ $sth = $dbh->prepare("INSERT INTO tc_companion values (NULL,$individual,$comp_id,$scheduling_priority,1)");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ } else {
+ # new companion, create scheduling_priority first
+ print " adding new companion without existing scheduling_priority\n";
+ $sth = $dbh->prepare("INSERT INTO tc_scheduling_priority values (NULL,30,'')");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ my $scheduling_priority = $dbh->last_insert_id(NULL,NULL,'tc_scheduling_priority',NULL);
+
+ #print "SELECT * FROM tc_individual WHERE name=\"$companion\" AND valid='1'\n";
+ $sth = $dbh->prepare("SELECT * FROM tc_individual WHERE name=\"$companion\" COLLATE latin1_general_cs AND valid='1'");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ if ($sqlhashref = $sth->fetchrow_hashref) {
+ my $individual = $sqlhashref->{'individual'};
+ #print "INSERT INTO tc_companion values (NULL,$individual,$comp_id,$scheduling_priority,1)\n";
+ $sth = $dbh->prepare("INSERT INTO tc_companion values (NULL,$individual,$comp_id,$scheduling_priority,1)");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ } else {
+ die "trying to add $companion as a companion but doesn't exist in tc_individual - how is this possible!!!\n";
+ }
+ }
+ }
+
+ # invalidate existing old companions in database
+ #print "UPDATE tc_companion SET valid=0 WHERE companionship=$tc_companionship_id AND valid=1\n";
+ $sth = $dbh->prepare("UPDATE tc_companion SET valid=0 WHERE companionship=$tc_companionship_id AND valid=1");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+
+ # don't worry about linking families, that will be done in update_tc_family_table()
+ } else {
+ # companionship is the same, just update district in case it changed
+ $sth = $dbh->prepare("UPDATE tc_companionship SET district=$district WHERE companionship=$tc_companionship_id");