+ 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 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;
+ last;
+ }
+ $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 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'");
+ $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,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
+ $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 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";
+ $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,'')");