close(FILE);
}
+sub optional_csv_to_hash
+{
+ my ($filename, $hashref) = @_;
+
+ my $opened = open(FILE,$filename);
+
+ if ($opened) {
+ my $found_header = 0; my $index = 0;
+ while(<FILE>)
+ {
+ $line = $_;
+ @data = split /\",/, $line;
+ if(!$found_header) { @header = @data; $found_header = 1; }
+ else {
+ foreach $i (0..$#data-1) {
+ $data[$i] =~ s/\"//g;
+ $header[$i] =~ s/\"//g;
+ $hashref->{$index}{$header[$i]} = $data[$i];
+ #print "$index: $i: $header[$i]: $data[$i]\n";
+ }
+ $index++;
+ }
+ }
+
+ close(FILE);
+ }
+ else
+ {
+ print "-W- could not open optional csv file $filename\n";
+ }
+}
+
######################################################################
sub print_hash
{
#| aaronic | int(16) unsigned | | PRI | 0 | A |
#| name | varchar(60) | YES | | NULL | |
#| phone | varchar(12) | YES | | NULL | |
+#| email | varchar(120) | YES | | NULL | |
#| valid | tinyint(1) | YES | | NULL | |
#+-------+--------------------+------+-----+---------+-------+
sub update_eq_aaronic_table
$membership_data{$index}{$key} =~ /^Teacher\s*$/i ||
$membership_data{$index}{$key} =~ /^Priest\s*$/i)) {
$aaronic_name = $membership_data{$index}{'Preferred Name'};
- $phone = $membership_data{$index}{'Phone 1'};
- if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
+ $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"; }
+ $email = $membership_data{$index}{'Individual E-mail'};
+ if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
$sth = $dbh->prepare("select * from eq_aaronic where name='$aaronic_name'");
$sth->execute or die "-E- DB error: $DBI::errstr\n";
my @data = ();
if($rows == 0) {
# No existing records found for this aaronic, make a new entry
print " Adding new Aaronic: $aaronic_name\n";
- $sth = $dbh->prepare("insert into eq_aaronic values (NULL,'$aaronic_name','$phone',1)");
+ $sth = $dbh->prepare("insert into eq_aaronic values (NULL,'$aaronic_name','$phone','$email',1)");
$sth->execute or die "-E- DB error: $DBI::errstr\n";
} elsif($rows == 1) {
# An existing record was found for this aaronic, update it, mark it valid!
print " Updating existing aaronic: $aaronic_name\n";
$sth = $dbh->prepare("update eq_aaronic set phone='$phone' where name='$aaronic_name'");
$sth->execute or die "-E- DB error: $DBI::errstr\n";
+ $sth = $dbh->prepare("update eq_aaronic set email='$email' where name='$aaronic_name'");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
$sth = $dbh->prepare("update eq_aaronic set valid=1 where name='$aaronic_name'");
$sth->execute or die "-E- DB error: $DBI::errstr\n";
} else {
#| Field | Type | Null | Key | Default | Extra |
#+-------------+------------------+------+-----+---------+----------------+
#| elder | int(16) unsigned | | PRI | NULL | auto_increment |
+#| indiv_id | int(16) unsigned | | | NULL | |
#| name | varchar(60) | YES | | NULL | |
#| phone | varchar(12) | YES | | NULL | |
+#| email | varchar(120) | YES | | NULL | |
+#| priesthood | enum | YES | | NULL | |
#| ppi_pri | int(10) unsigned | YES | | 1 | |
#| ppi_notes | varchar(128) | YES | | NULL | |
#| int_pri | int(10) unsigned | YES | | 1 | |
{
$hashref = $membership_data{$index};
foreach $key (keys %$hashref) {
- if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
+ if($key =~ /Priesthood/i &&
+ ($membership_data{$index}{$key} =~ /Deacon/i ||
+ $membership_data{$index}{$key} =~ /Teacher/i ||
+ $membership_data{$index}{$key} =~ /Priest/i ||
+ $membership_data{$index}{$key} =~ /Elder/i ||
+ $membership_data{$index}{$key} =~ /Unordained/i)) {
+
+ # check if this is a prospective elder under the stewardship of the EQ
+ if($membership_data{$index}{$key} !~ /Elder/i) {
+ $found = 0;
+ foreach $i (keys %prospective_elder_data) {
+ #print "$membership_data{$index}{'Full Name'}\n";
+ #print "$prospective_elder_data{$i}{'Full Name'}\n";
+ if($membership_data{$index}{'Full Name'} eq $prospective_elder_data{$i}{'Full Name'}) {
+ $found = 1;
+ last;
+ }
+ }
+ if($found == 0) { next;}
+ }
+
$id = $membership_data{$index}{'Indiv ID'};
$elder_name = $membership_data{$index}{'Preferred Name'};
- $phone = $membership_data{$index}{'Phone 1'};
+ $phone = $membership_data{$index}{'Household Phone'};
+ $priesthood = $membership_data{$index}{'Priesthood'};
$organization = $organization_by_id{$id};
$attending = 0;
if(($organization =~ /Elders/) ||
($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)/) { $phone = "$areacode-$1"; }
if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
+ $email = $membership_data{$index}{'Individual E-mail'};
+ if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
$sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
$sth->execute or die "-E- DB error: $DBI::errstr\n";
my @data = ();
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','','1','',$attending,1)");
+ $sth = $dbh->prepare("insert into eq_elder values (NULL,'$id','$elder_name','$phone','$email','$priesthood','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
$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";
+ $sth = $dbh->prepare("update eq_elder set indiv_id='$id' where name='$elder_name'");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ $sth = $dbh->prepare("update eq_elder set priesthood='$priesthood' where name='$elder_name'");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ $sth = $dbh->prepare("update eq_elder set email='$email' 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";
$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}{'Phone 1'};
- if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
+ $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 "") {
}
}
+# EQ_VISIT
+#+----------------+------------------+------+-----+---------+-------+
+#| Field | Type | Null | Key | Default | Extra |
+#+----------------+------------------+------+-----+---------+-------+
+#| visit | int(16) unsigned | | PRI | 0 | A |
+#| family | int(16) unsigned | YES | UNI | NULL | |
+#| companionship | int(16) unsigned | YES | | NULL | |
+#| date | date | YES | | NULL | |
+#| notes | varchar(128) | YES | | NULL | |
+#| visited | varchar(1) | YES | | NULL | |
+#+----------------+------------------+------+-----+---------+-------+
+sub update_eq_visit_table
+{
+ print "\n-> updating eq_visit table\n";
+
+ my $month_header_retrieved = 0;
+ my $month_header;
+ my @data_months;
+ my %months = ('Jan', 1, 'Feb', 2, 'Mar', 3, 'Apr', 4, 'May', 5, 'Jun', 6, 'Jul', 7, 'Aug', 8, 'Sep', 9, 'Oct', 10, 'Nov', 11, 'Dec', 12);
+ ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
+ my %visit_status = ('X', 'y', '-', 'n', '', '');
+
+ foreach $index (keys %hometeaching_stats_data)
+ {
+ $hashref = $hometeaching_stats_data{$index};
+ #foreach $key (keys %$hashref) {print "$key\n";}
+
+ $family_name = $hometeaching_stats_data{$index}{"Preferred Name"};
+ print " Updating visit data: $family_name\n";
+
+ # get family id from eq_family
+ $sth = $dbh->prepare("select * from eq_family where name=\"$family_name\" 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'};
+ $comp_id = $family_data[0]->{'companionship'};
+ }
+ else { next; }
+ #print "family_id = $family_id\n";
+ #print "comp_id = $comp_id\n";
+
+ # ignore visits that weren't done by the EQ
+ if ($comp_id == 0) { next; }
+
+ # retrieve the month header if not already done
+ if ($month_header_retrieved == 0)
+ {
+ foreach $key (keys %$hashref)
+ {
+ if (($key ne "Preferred Name") && ($key ne "Home Teachers"))
+ {
+ $month_header = $key;
+ @data_months = split /\t/, $key;
+ }
+ }
+ $month_header_retrieved = 1;
+ }
+
+ # loop through history data
+ @history = split /\t/, $hometeaching_stats_data{$index}{$month_header};
+ my $data_year = 1900 + $yearOffset;
+ my $data_month = $months{$data_months[-1]};
+ #print "$month_header\n";
+ #print $hometeaching_stats_data{$index}{$month_header};
+ #print "\n";
+ foreach $i (reverse(0..$#history)) {
+ # went back a calendar year, decrement $data_year
+ if ($months{$data_months[$i]} > $data_month)
+ {
+ $data_year -= 1;
+ }
+ $data_month = $months{$data_months[$i]};
+ my $visit_date = sprintf("%4d-%02d-01\n", $data_year, $data_month);
+ #print "$visit_date\n";
+ my $importing_status = $visit_status{$history[$i]};
+ #print "importing_status = $importing_status\n";
+ #print "select * from eq_visit where family=$family_id and companionship=$comp_id and date='$visit_date'\n";
+ $sth = $dbh->prepare("select * from eq_visit where family=$family_id and companionship=$comp_id and date='$visit_date'");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ my @visit_data = ();
+ while($sqlhashref = $sth->fetchrow_hashref) { push(@visit_data, $sqlhashref); }
+ my $visit_rows = scalar @visit_data;
+ if($visit_rows > 0) {
+ my $visited = $visit_data[0]->{'visited'};
+ #print "visited = $visited\n";
+ # update visit if data is different in eq_visit
+ if ($visited ne $importing_status)
+ {
+ #print "importing_status = $importing_status\n";
+ $sth = $dbh->prepare("update eq_visit set visited='$importing_status' where family='$family_id' and date='$visit_date' and companionship='$comp_id'");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ }
+ } else {
+ if ($importing_status ne '')
+ {
+ # add visit if it doesn't exist in eq_visit
+ $sth = $dbh->prepare("insert into eq_visit values (NULL, '$family_id', '$comp_id', '$visit_date', '', '$importing_status')");
+ $sth->execute or die "-E- DB error: $DBI::errstr\n";
+ }
+ }
+ }
+ }
+}
+
######################################################################
sub check_for_changed_ids
{
###################################################
# Parse Ward Data Files
+&optional_csv_to_hash("$datadir/EQ\ Prospective\ Elders.csv", \%prospective_elder_data);
&csv_to_hash("$datadir/Membership.csv",\%membership_data);
&csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
&csv_to_hash("$datadir/Organization.csv",\%organization_data);
+&optional_csv_to_hash("$datadir/Home\ Teacher\ per\ Companionship.csv", \%hometeaching_stats_data);
%organization_by_name = ();
%organization_by_id = ();
&print_hash(\%hometeaching_data);
print "-> Organization Data Dump\n\n";
&print_hash(\%organization_data);
+ print "-> HomeTeaching Stats Data Dump\n\n";
+ &print_hash(\%hometeaching_stats_data);
}
if($opt_s) { $dbh->disconnect(); exit; }
&update_eq_family_table();
&update_eq_parent_table();
&update_eq_child_table();
+&update_eq_visit_table();
print "\n-> Import Successful! DONE...\n";