-# 3RD_PARENT
-#+----------+------------------+------+-----+---------+-------+
-#| Field | Type | Null | Key | Default | Extra |
-#+----------+------------------+------+-----+---------+-------+
-#| parent | int(16) unsigned | | PRI | 0 | A |
-#| family | int(16) unsigned | YES | | NULL | |
-#| name | varchar(30) | YES | | NULL | |
-#| birthday | date | YES | | NULL | |
-#| phone | varchar(12) | YES | | NULL | |
-#| address | varchar(255) | YES | | NULL | |
-#| indiv_id | int(16) unsigned | YES | UNI | NULL | |
-#| valid | tinyint(1) | YES | | NULL | |
-#+----------+------------------+------+-----+---------+-------+
-sub update_tc_parent_table
-{
- print "\n-> Updating tc_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 tc_parent 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 =~ /HH Position/i &&
- $membership_data{$index}{$key} =~ /Head of Household/i ||
- $membership_data{$index}{$key} =~ /Spouse/i
- ) {
- # Get some information from the hash about this parent
- $parent_name = $membership_data{$index}{'Preferred Name'};
- $parent_name =~ s/\'/\\'/g; #'
- $birthday = $membership_data{$index}{'Birth'};
- $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}{'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 "") {
- $address .= " " . $membership_data{$index}{'Street 2'};
- }
-
- # Find the family id for this parent's HofH_ID.
- $sth = $dbh->prepare("select * from tc_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); }
- 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 tc_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); }
- my $rows = scalar @data;
-
- 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 tc_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 tc_parent set family='$family_id' where name='$parent_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_parent set birthday='$year-$month-$day' where name='$parent_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_parent set phone='$phone' where name='$parent_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_parent set address='$address' where name='$parent_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_parent set valid=1 where name='$parent_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_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.
- 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();
- }
- }
- }
-}
-
-# 3RD_CHILD
-#+----------+------------------+------+-----+---------+-------+
-#| Field | Type | Null | Key | Default | Extra |
-#+----------+------------------+------+-----+---------+-------+
-#| child | int(16) unsigned | | PRI | 0 | A |
-#| family | int(16) unsigned | YES | | NULL | |
-#| name | varchar(30) | YES | | NULL | |
-#| birthday | date | YES | | NULL | |
-#| indiv_id | int(16) unsigned | YES | UNI | NULL | |
-#| valid | tinyint(1) | YES | | NULL | |
-#+----------+------------------+------+-----+---------+-------+
-sub update_tc_child_table
-{
- print "\n-> Updating tc_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 tc_child 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 =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
- $child_name = $membership_data{$index}{'Full Name'};
- $child_name =~ s/\'/\\'/g; #'
- $birthday = $membership_data{$index}{'Birth'};
- $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
- $id = $membership_data{$index}{'Indiv ID'};
- $hofh_id = $membership_data{$index}{'HofH ID'};
-
- # Find the family id for this child's HofH_ID.
- $sth = $dbh->prepare("select * from tc_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); }
- 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 tc_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); }
- my $rows = scalar @data;
-
- 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 tc_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 tc_child set family='$family_id' where name='$child_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_child set birthday='$year-$month-$day' where name='$child_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_child set valid=1 where name='$child_name'");
- $sth->execute or die "-E- DB error: $DBI::errstr\n";
- $sth = $dbh->prepare("update tc_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.
- print " -E- More than one record found ($rows) with same child name: $child_name\n";
- }
- $sth->finish();
- }
- }
- }
-}
-