6 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir);
7 unshift @INC,("$mydir/../setup");
8 if( -f "$mydir/../setup/db_config.local") { require "db_config.local"; }
9 else { require "db_config"; }
11 %hometeaching_data = ();
12 %membership_data = ();
15 $monthname2num{'Jan'} = '01';
16 $monthname2num{'Feb'} = '02';
17 $monthname2num{'Mar'} = '03';
18 $monthname2num{'Apr'} = '04';
19 $monthname2num{'May'} = '05';
20 $monthname2num{'Jun'} = '06';
21 $monthname2num{'Jul'} = '07';
22 $monthname2num{'Aug'} = '08';
23 $monthname2num{'Sep'} = '09';
24 $monthname2num{'Oct'} = '10';
25 $monthname2num{'Nov'} = '11';
26 $monthname2num{'Dec'} = '12';
28 ######################################################################
30 ######################################################################
33 my ($filename, $hashref) = @_;
35 open(FILE,$filename) || die "-E- Could not open $filename for reading\n";
37 my $found_header = 0; my $index = 0;
41 @data = split /\",/, $line;
42 if(!$found_header) { @header = @data; $found_header = 1; }
44 foreach $i (0..$#data-1) {
46 $header[$i] =~ s/\"//g;
47 $hashref->{$index}{$header[$i]} = $data[$i];
48 #print "$index: $i: $header[$i]: $data[$i]\n";
57 sub optional_csv_to_hash
59 my ($filename, $hashref) = @_;
61 my $opened = open(FILE,$filename);
64 my $found_header = 0; my $index = 0;
68 @data = split /\",/, $line;
69 if(!$found_header) { @header = @data; $found_header = 1; }
71 foreach $i (0..$#data-1) {
73 $header[$i] =~ s/\"//g;
74 $hashref->{$index}{$header[$i]} = $data[$i];
75 #print "$index: $i: $header[$i]: $data[$i]\n";
85 print "-W- could not open optional csv file $filename\n";
89 ######################################################################
94 foreach $key (sort {$a <=> $b} keys %$hashref) {
95 print "Index: $key\n";
96 foreach $field (keys %{$hashref->{$key}}) {
97 print "$field: $hashref->{$key}{$field}\n";
103 ######################################################################
106 #+-------+--------------------+------+-----+---------+-------+
107 #| Field | Type | Null | Key | Default | Extra |
108 #+-------+--------------------+------+-----+---------+-------+
109 #| aaronic | int(16) unsigned | | PRI | 0 | A |
110 #| name | varchar(60) | YES | | NULL | |
111 #| phone | varchar(12) | YES | | NULL | |
112 #| email | varchar(120) | YES | | NULL | |
113 #| valid | tinyint(1) | YES | | NULL | |
114 #+-------+--------------------+------+-----+---------+-------+
115 sub update_tc_aaronic_table
117 print "\n-> Updating tc_aaronic table\n";
119 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
120 $sth = $dbh->prepare("update tc_aaronic set valid=0");
121 $sth->execute or die "-E- DB error: $DBI::errstr\n";
123 foreach $index (keys %membership_data)
125 $hashref = $membership_data{$index};
126 foreach $key (keys %$hashref) {
127 if($key =~ /Priesthood/i &&
128 ($membership_data{$index}{$key} =~ /^Deacon\s*$/i ||
129 $membership_data{$index}{$key} =~ /^Teacher\s*$/i ||
130 $membership_data{$index}{$key} =~ /^Priest\s*$/i)) {
131 $aaronic_name = $membership_data{$index}{'Preferred Name'};
132 $phone = $membership_data{$index}{'Household Phone'};
133 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
134 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
135 $email = $membership_data{$index}{'Individual E-mail'};
136 if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
137 $sth = $dbh->prepare("select * from tc_aaronic where name='$aaronic_name'");
138 $sth->execute or die "-E- DB error: $DBI::errstr\n";
140 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
141 my $rows = scalar @data;
143 # No existing records found for this aaronic, make a new entry
144 print " Adding new Aaronic: $aaronic_name\n";
145 $sth = $dbh->prepare("insert into tc_aaronic values (NULL,'$aaronic_name','$phone','$email',1)");
146 $sth->execute or die "-E- DB error: $DBI::errstr\n";
147 } elsif($rows == 1) {
148 # An existing record was found for this aaronic, update it, mark it valid!
149 print " Updating existing aaronic: $aaronic_name\n";
150 $sth = $dbh->prepare("update tc_aaronic set phone='$phone' where name='$aaronic_name'");
151 $sth->execute or die "-E- DB error: $DBI::errstr\n";
152 $sth = $dbh->prepare("update tc_aaronic set email='$email' where name='$aaronic_name'");
153 $sth->execute or die "-E- DB error: $DBI::errstr\n";
154 $sth = $dbh->prepare("update tc_aaronic set valid=1 where name='$aaronic_name'");
155 $sth->execute or die "-E- DB error: $DBI::errstr\n";
157 # More than one record was found. Error! This shouldn't happen.
158 print " -E- More than one record found ($rows) for aaronic name: $aaronic_name\n";
167 #+-------------+------------------+------+-----+---------+----------------+
168 #| Field | Type | Null | Key | Default | Extra |
169 #+-------------+------------------+------+-----+---------+----------------+
170 #| indiv | int(16) unsigned | | PRI | NULL | auto_increment |
171 #| indiv_id | int(16) unsigned | | | NULL | |
172 #| name | varchar(60) | YES | | NULL | |
173 #| phone | varchar(12) | YES | | NULL | |
174 #| email | varchar(120) | YES | | NULL | |
175 #| priesthood | enum | YES | | NULL | |
176 #| ppi_pri | int(10) unsigned | YES | | 1 | |
177 #| ppi_notes | varchar(128) | YES | | NULL | |
178 #| int_pri | int(10) unsigned | YES | | 1 | |
179 #| int_notes | varchar(128) | YES | | NULL | |
180 #| attending | tinyint(1) | YES | | 0 | |
181 #| valid | tinyint(1) | YES | | NULL | |
182 #+-------------+------------------+------+-----+---------+----------------+
183 sub update_tc_indiv_table
185 print "\n-> Updating tc_indiv table\n";
187 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
188 $sth = $dbh->prepare("update tc_indiv set valid=0");
189 $sth->execute or die "-E- DB error: $DBI::errstr\n";
191 foreach $index (keys %membership_data)
193 $hashref = $membership_data{$index};
194 foreach $key (keys %$hashref) {
195 if($key =~ /Priesthood/i &&
196 ($membership_data{$index}{$key} =~ /Deacon/i ||
197 $membership_data{$index}{$key} =~ /Teacher/i ||
198 $membership_data{$index}{$key} =~ /Priest/i ||
199 $membership_data{$index}{$key} =~ /Elder/i ||
200 $membership_data{$index}{$key} =~ /Unordained/i)) {
202 # check if this is a prospective elder under the stewardship of the EQ
203 if($membership_data{$index}{$key} !~ /Elder/i) {
205 foreach $i (keys %prospective_elder_data) {
206 #print "$membership_data{$index}{'Full Name'}\n";
207 #print "$prospective_elder_data{$i}{'Full Name'}\n";
208 if($membership_data{$index}{'Full Name'} eq $prospective_elder_data{$i}{'Full Name'}) {
213 if($found == 0) { next;}
216 $id = $membership_data{$index}{'Indiv ID'};
217 $indiv_name = $membership_data{$index}{'Preferred Name'};
218 $phone = $membership_data{$index}{'Household Phone'};
219 $priesthood = $membership_data{$index}{'Priesthood'};
220 $organization = $organization_by_id{$id};
222 if(($organization =~ /Elders/) ||
223 ($organization =~ /Young Men/) ||
224 ($organization =~ /Sunday School/) ||
225 ($organization =~ /Primary/)
226 ) { $attending = 1; }
227 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
228 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
229 $email = $membership_data{$index}{'indiv E-mail'};
230 if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
231 $sth = $dbh->prepare("select * from tc_indiv where name='$indiv_name'");
232 $sth->execute or die "-E- DB error: $DBI::errstr\n";
234 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
235 my $rows = scalar @data;
237 # No existing records found for this indiv, make a new entry
238 print " Adding new indiv: $indiv_name\n";
239 $sth = $dbh->prepare("insert into tc_indiv values (NULL,'$id','$indiv_name','$phone','$email','$priesthood','n','1','','1','',$attending,1)");
240 $sth->execute or die "-E- DB error: $DBI::errstr\n";
241 } elsif($rows == 1) {
242 # An existing record was found for this indiv, update it
243 print " Updating existing indiv: $indiv_name\n";
244 $sth = $dbh->prepare("update tc_indiv set valid=1 where name='$indiv_name'");
245 $sth->execute or die "-E- DB error: $DBI::errstr\n";
247 $sth = $dbh->prepare("update tc_indiv set phone='$phone' where name='$indiv_name'");
249 $sth = $dbh->prepare("update tc_indiv set phone=NULL where name='$indiv_name'");
251 $sth->execute or die "-E- DB error: $DBI::errstr\n";
252 $sth = $dbh->prepare("update tc_indiv set attending='$attending' where name='$indiv_name'");
253 $sth->execute or die "-E- DB error: $DBI::errstr\n";
254 $sth = $dbh->prepare("update tc_indiv set indiv_id='$id' where name='$indiv_name'");
255 $sth->execute or die "-E- DB error: $DBI::errstr\n";
256 $sth = $dbh->prepare("update tc_indiv set priesthood='$priesthood' where name='$indiv_name'");
257 $sth->execute or die "-E- DB error: $DBI::errstr\n";
258 $sth = $dbh->prepare("update tc_indiv set email='$email' where name='$indiv_name'");
259 $sth->execute or die "-E- DB error: $DBI::errstr\n";
261 # More than one record was found. Error! This shouldn't happen.
262 print " -E- More than one record found ($rows) for indiv: $indiv_name\n";
271 #+--------------+------------------+------+-----+---------+-------+
272 #| Field | Type | Null | Key | Default | Extra |
273 #+--------------+------------------+------+-----+---------+-------+
274 #| indiv_id | int(16) unsigned | YES | | NULL | |
275 #| name | varchar(30) | YES | | NULL | |
276 #| organization | varchar(30) | YES | | NULL | |
277 #| position | varchar(30) | YES | | NULL | |
278 #| sequence | int(16) unsigned | YES | | NULL | |
279 #| sustained | date | YES | | NULL | |
280 #+--------------+------------------+------+-----+---------+-------+
281 sub update_tc_calling_table()
283 print "\n-> Updating tc_calling table\n";
285 #print "-> Organization Data Dump\n\n";
286 #&print_hash(\%organization_data);
288 # Delete all records from the calling table. We have no history to
289 # save here. Just re-populate with the latest calling information.
290 $sth = $dbh->prepare("delete from tc_calling ");
291 $sth->execute or die "-E- DB error: $DBI::errstr\n";
293 foreach $index (keys %organization_data)
295 $indiv_id = $organization_data{$index}{'Indiv ID'};
296 $name = $organization_data{$index}{'Indiv Name'};
297 $name =~ s/\'/\\'/g; #'
298 $organization = $organization_data{$index}{'Organization'};
299 $organization_by_name{$name} = $organization;
300 $organization_by_id{$indiv_id} = $organization;
301 $position = $organization_data{$index}{'Position'};
302 $sequence = $organization_data{$index}{'Org Seq'};
303 $sustained = $organization_data{$index}{'Sustained'};
304 $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
305 if($name eq "") { next; }
306 print " Adding new Calling: $name -> $position\n";
307 $sth = $dbh->prepare("insert into tc_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
308 $sth->execute or die "-E- DB error: $DBI::errstr\n";
313 #+------------+------------------+------+-----+---------+-------+
314 #| Field | Type | Null | Key | Default | Extra |
315 #+------------+------------------+------+-----+---------+-------+
316 #| district | int(16) unsigned | | PRI | 0 | |
317 #| name | varchar(30) | YES | | NULL | |
318 #| supervisor | int(16) unsigned | YES | | NULL | |
319 #| valid | tinyint(1) | YES | | NULL | |
320 #+------------+------------------+------+-----+---------+-------+
321 sub update_tc_district_table
323 # Districts should be created by hand. This subroutine only
324 # updates the supervisor's ID in each district.
325 print "\n-> Updating tc_district table\n";
326 $sth = $dbh->prepare("select * from tc_district");
327 $sth->execute or die "-E- DB error: $DBI::errstr\n";
328 while($sqlhashref = $sth->fetchrow_hashref) {
329 $supervisor_name = $sqlhashref->{name};
330 $district = $sqlhashref->{district};
331 $sth2 = $dbh->prepare("select * from tc_indiv where name='$supervisor_name'");
332 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
333 $sqlhashref2 = $sth2->fetchrow_hashref;
334 $supervisor_id = $sqlhashref2->{indiv};
336 $sth2 = $dbh->prepare("update tc_district set supervisor='$supervisor_id' where district='$district'");
337 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
344 #+---------------+------------------+------+-----+---------+-------+
345 #| Field | Type | Null | Key | Default | Extra |
346 #+---------------+------------------+------+-----+---------+-------+
347 #| companionship | int(16) unsigned | | | 0 | |
348 #| indiv | int(16) unsigned | YES | | NULL | |
349 #| aaronic | int(16) unsigned | YES | | NULL | |
350 #| district | int(16) unsigned | YES | | NULL | |
351 #| valid | tinyint(1) | YES | | NULL | |
352 #+---------------+------------------+------+-----+---------+-------+
353 sub update_tc_companionship_table
355 print "\n-> Updating tc_companionship table\n";
357 # First, mark all existing companionships as invalid in case they have been dissolved
358 $sth = $dbh->prepare("update tc_companionship set valid=0");
359 $sth->execute or die "-E- DB error: $DBI::errstr\n";
360 # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
361 $sth = $dbh->prepare("update tc_aaronic set valid=0");
362 $sth->execute or die "-E- DB error: $DBI::errstr\n";
364 foreach $index (keys %hometeaching_data)
366 $hashref = $hometeaching_data{$index};
367 foreach $key (keys %$hashref) {
368 if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
369 foreach $field ("Home Teacher 1","Home Teacher 2") {
370 $indiv_name = $hometeaching_data{$index}{$field};
371 if($indiv_name eq "") { next; }
372 $sth2 = $dbh->prepare("select * from tc_indiv where name='$indiv_name'");
373 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
374 $sqlhashref2 = $sth2->fetchrow_hashref;
375 $indiv = $sqlhashref2->{indiv};
378 $sth2 = $dbh->prepare("select * from tc_aaronic where name='$indiv_name'");
379 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
380 $sqlhashref2 = $sth2->fetchrow_hashref;
381 $aaronic = $sqlhashref2->{aaronic};
383 if($aaronic eq "") { print "-W- Unable to find $indiv_name in tc_indiv or tc_aaronic tables\n"; next; }
385 $id = $hometeaching_data{$index}{'Comp ID'};
386 $district = $hometeaching_data{$index}{'HT District'};
387 $sth = $dbh->prepare("select * from tc_companionship where indiv='$indiv' and aaronic='$aaronic' and companionship='$id'");
388 $sth->execute or die "-E- DB error: $DBI::errstr\n";
390 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
391 my $rows = scalar @data;
393 # No existing records found for this companionship, make a new entry
394 print " Adding Companion to companionship: $indiv_name -> $id\n";
395 $sth = $dbh->prepare("insert into tc_companionship values ($id,'$indiv','$aaronic','$district',1)");
396 $sth->execute or die "-E- DB error: $DBI::errstr\n";
398 # An existing companionship was found for this companionship, update it
399 $sth2 = $dbh->prepare("select * from tc_companionship where district='$district' and companionship='$id'");
400 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
401 if($indiv ne "NULL") {
402 print " Updating Companionship with indiv: $indiv_name ($indiv) -> $id\n";
403 $sth = $dbh->prepare("update tc_companionship set district='$district' where indiv='$indiv' and companionship='$id'");
404 $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
405 $sth = $dbh->prepare("update tc_companionship set indiv='$indiv' where indiv='$indiv' and companionship='$id'");
406 $sth->execute or die "-E- DB error 'indiv': $DBI::errstr\n";
407 $sth = $dbh->prepare("update tc_companionship set valid=1 where indiv='$indiv' and companionship='$id'");
408 $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
410 print " Updating Companionship with Aaronic: $indiv_name ($aaronic) -> $id\n";
411 $sth = $dbh->prepare("update tc_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
412 $sth->execute or die "-E- DB error: $DBI::errstr\n";
413 $sth = $dbh->prepare("update tc_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
414 $sth->execute or die "-E- DB error: $DBI::errstr\n";
415 $sth = $dbh->prepare("update tc_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
416 $sth->execute or die "-E- DB error: $DBI::errstr\n";
417 $sth = $dbh->prepare("update tc_aaronic set valid=1 where aaronic='$aaronic'");
418 $sth->execute or die "-E- DB error: $DBI::errstr\n";
430 #+---------------+------------------+------+-----+---------+-------+
431 #| Field | Type | Null | Key | Default | Extra |
432 #+---------------+------------------+------+-----+---------+-------+
433 #| family | int(16) unsigned | | PRI | 0 | A |
434 #| hofh_id | int(16) unsigned | YES | | NULL | |
435 #| name | varchar(30) | YES | | NULL | |
436 #| name_id | varchar(30) | YES | | NULL | |
437 #| indiv_id | int(16) unsigned | YES | | NULL | |
438 #| companionship | int(16) unsigned | YES | | NULL | |
439 #| visit_pri | int(10) unsigned | YES | | 1 | |
440 #| visit_notes | varchar(128) | YES | | NULL | |
441 #| valid | tinyint(1) | YES | | NULL | |
442 #+---------------+------------------+------+-----+---------+-------+
443 sub update_tc_family_table
445 print "\n-> Updating tc_family table\n";
447 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
448 $sth = $dbh->prepare("update tc_family set valid=0");
449 $sth->execute or die "-E- DB error: $DBI::errstr\n";
450 $sth = $dbh->prepare("update tc_family set companionship=0");
451 $sth->execute or die "-E- DB error: $DBI::errstr\n";
453 foreach $index (keys %membership_data)
455 $hashref = $membership_data{$index};
456 foreach $key (keys %$hashref) {
457 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
458 $family_name = $membership_data{$index}{'Preferred Name'};
459 $family_name =~ s/\'/\\'/g; #'
460 $id = $membership_data{$index}{'HofH ID'};
461 $name_id = uc($family_name);
463 # Find out how many families match this family's name
464 $sth = $dbh->prepare("select * from tc_family where name_id='$name_id'");
465 $sth->execute or die "-E- DB error: $DBI::errstr\n";
467 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
468 my $rows = scalar @data;
471 # No existing records found for this family, make a new entry
472 print " Adding new Family: $family_name\n";
473 $sth = $dbh->prepare("insert into tc_family values (NULL,$id,'$family_name','$name_id','0','0','1','',1)");
474 $sth->execute or die "-E- DB error: $DBI::errstr\n";
475 } elsif($rows == 1) {
476 # An existing record was found for this family, update it
477 print " Updating existing family: $family_name\n";
478 $sth = $dbh->prepare("update tc_family set hofh_id=$id where name_id='$name_id'");
479 $sth->execute or die "-E- DB error: $DBI::errstr\n";
480 $sth = $dbh->prepare("update tc_family set valid=1 where name_id='$name_id'");
481 $sth->execute or die "-E- DB error: $DBI::errstr\n";
483 # More than one record was found. Error! This shouldn't happen.
484 print " -E- More than one record found ($rows) for family name: $family_name\n";
487 # Now update the indiv_id field for this family
488 $sth = $dbh->prepare("select * from tc_indiv WHERE name='$family_name'");
489 $sth->execute or die "-E- DB error: $DBI::errstr\n";
490 while($sqlhashref = $sth->fetchrow_hashref) {
491 $indiv_id = $sqlhashref->{indiv};
492 print " Updating family indiv_id: $family_name -> $indiv_id\n";
493 $sth = $dbh->prepare("update tc_family set indiv_id=$indiv_id where name_id='$name_id'");
494 $sth->execute or die "-E- DB error: $DBI::errstr\n";
497 # Now update the hometeaching field for this family
498 foreach $index (keys %hometeaching_data)
500 $hashref = $hometeaching_data{$index};
501 foreach $key (keys %$hashref) {
502 if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
503 $a = $1; $b = $2; $c = $3; $d = $4;
504 if($family_name =~ /$a/ && $hometeaching_data{$index}{'Household'} !~ /$family_name/i) {
505 print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $a, $c $d\n";
506 $hometeaching_data{$index}{'Household'} = "$a, $c $d";
509 if($key =~ /Quorum/i &&
510 $hometeaching_data{$index}{$key} =~ /Elders/i &&
511 $hometeaching_data{$index}{'Household'} =~ /$family_name/i &&
512 $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
515 print " Updating hometeaching assignment for $family_name family\n";
516 $companionship = $hometeaching_data{$index}{'Comp ID'};
517 $sth = $dbh->prepare("update tc_family set companionship='$companionship' where name_id='$name_id'");
518 $sth->execute or die "-E- DB error: $DBI::errstr\n";
529 #+----------+------------------+------+-----+---------+-------+
530 #| Field | Type | Null | Key | Default | Extra |
531 #+----------+------------------+------+-----+---------+-------+
532 #| parent | int(16) unsigned | | PRI | 0 | A |
533 #| family | int(16) unsigned | YES | | NULL | |
534 #| name | varchar(30) | YES | | NULL | |
535 #| birthday | date | YES | | NULL | |
536 #| phone | varchar(12) | YES | | NULL | |
537 #| address | varchar(255) | YES | | NULL | |
538 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
539 #| valid | tinyint(1) | YES | | NULL | |
540 #+----------+------------------+------+-----+---------+-------+
541 sub update_tc_parent_table
543 print "\n-> Updating tc_parent table\n";
545 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
546 $sth = $dbh->prepare("update tc_parent set valid=0");
547 $sth->execute or die "-E- DB error: $DBI::errstr\n";
549 foreach $index (keys %membership_data)
551 $hashref = $membership_data{$index};
552 foreach $key (keys %$hashref) {
553 if($key =~ /HH Position/i &&
554 $membership_data{$index}{$key} =~ /Head of Household/i ||
555 $membership_data{$index}{$key} =~ /Spouse/i
557 # Get some information from the hash about this parent
558 $parent_name = $membership_data{$index}{'Preferred Name'};
559 $parent_name =~ s/\'/\\'/g; #'
560 $birthday = $membership_data{$index}{'Birth'};
561 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
562 $hofh_id = $membership_data{$index}{'HofH ID'};
563 $id = $membership_data{$index}{'Indiv ID'};
564 $phone = $membership_data{$index}{'Household Phone'};
565 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
566 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
567 $address = $membership_data{$index}{'Street 1'};
568 if($membership_data{$index}{'Street 2'} ne "") {
569 $address .= " " . $membership_data{$index}{'Street 2'};
572 # Find the family id for this parent's HofH_ID.
573 $sth = $dbh->prepare("select * from tc_family where hofh_id='$hofh_id' and valid=1");
574 $sth->execute or die "-E- DB error: $DBI::errstr\n";
575 my @family_data = ();
576 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
577 my $family_rows = scalar @family_data;
578 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
579 else { $family_id = 0; }
581 # Find out how many parents match this parent's name
582 $sth = $dbh->prepare("select * from tc_parent where name='$parent_name'");
583 $sth->execute or die "-E- DB error: $DBI::errstr\n";
585 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
586 my $rows = scalar @data;
588 if($rows == 0 && $family_rows > 0) {
589 # No existing records found for this parent, make a new entry
590 print " Adding new Parent: $parent_name\n";
591 $sth = $dbh->prepare("insert into tc_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
592 $sth->execute or die "-E- DB error: $DBI::errstr\n";
593 } elsif($rows == 1 && $family_rows > 0) {
594 # An existing record was found for this parent, update it
595 print " Updating existing parent: $parent_name\n";
596 $sth = $dbh->prepare("update tc_parent set family='$family_id' where name='$parent_name'");
597 $sth->execute or die "-E- DB error: $DBI::errstr\n";
598 $sth = $dbh->prepare("update tc_parent set birthday='$year-$month-$day' where name='$parent_name'");
599 $sth->execute or die "-E- DB error: $DBI::errstr\n";
600 $sth = $dbh->prepare("update tc_parent set phone='$phone' where name='$parent_name'");
601 $sth->execute or die "-E- DB error: $DBI::errstr\n";
602 $sth = $dbh->prepare("update tc_parent set address='$address' where name='$parent_name'");
603 $sth->execute or die "-E- DB error: $DBI::errstr\n";
604 $sth = $dbh->prepare("update tc_parent set valid=1 where name='$parent_name'");
605 $sth->execute or die "-E- DB error: $DBI::errstr\n";
606 $sth = $dbh->prepare("update tc_parent set indiv_id='$id' where name='$parent_name'");
607 $sth->execute or die "-E- DB error: $DBI::errstr\n";
609 # More than one record was found. Error! This shouldn't happen.
610 print " -E- More than one record found with same parent name: $parent_name with hofh_id: $hofh_id\n";
612 print " -E- Unable to find a family to attach this parent to: $parent_name with hofh_id: $hofh_id\n";
621 #+----------+------------------+------+-----+---------+-------+
622 #| Field | Type | Null | Key | Default | Extra |
623 #+----------+------------------+------+-----+---------+-------+
624 #| child | int(16) unsigned | | PRI | 0 | A |
625 #| family | int(16) unsigned | YES | | NULL | |
626 #| name | varchar(30) | YES | | NULL | |
627 #| birthday | date | YES | | NULL | |
628 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
629 #| valid | tinyint(1) | YES | | NULL | |
630 #+----------+------------------+------+-----+---------+-------+
631 sub update_tc_child_table
633 print "\n-> Updating tc_child table\n";
635 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
636 $sth = $dbh->prepare("update tc_child set valid=0");
637 $sth->execute or die "-E- DB error: $DBI::errstr\n";
639 foreach $index (keys %membership_data)
641 $hashref = $membership_data{$index};
642 foreach $key (keys %$hashref) {
643 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
644 $child_name = $membership_data{$index}{'Full Name'};
645 $child_name =~ s/\'/\\'/g; #'
646 $birthday = $membership_data{$index}{'Birth'};
647 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
648 $id = $membership_data{$index}{'Indiv ID'};
649 $hofh_id = $membership_data{$index}{'HofH ID'};
651 # Find the family id for this child's HofH_ID.
652 $sth = $dbh->prepare("select * from tc_family where hofh_id='$hofh_id' and valid=1");
653 $sth->execute or die "-E- DB error: $DBI::errstr\n";
654 my @family_data = ();
655 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
656 my $family_rows = scalar @family_data;
657 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
658 else { $family_id = 0; }
660 # Find out how many children have the same name for the same family
661 $sth = $dbh->prepare("select * from tc_child where name='$child_name'");
662 $sth->execute or die "-E- DB error: $DBI::errstr\n";
664 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
665 my $rows = scalar @data;
667 if($rows == 0 && $family_rows > 0) {
668 # No existing records found for this child, make a new entry
669 print " Adding new Child: $child_name\n";
670 $sth = $dbh->prepare("insert into tc_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
671 $sth->execute or die "-E- DB error: $DBI::errstr\n";
672 } elsif($rows == 1 && $family_rows > 0) {
673 # An existing record was found for this child, update it
674 print " Updating existing child: $child_name\n";
675 $sth = $dbh->prepare("update tc_child set family='$family_id' where name='$child_name'");
676 $sth->execute or die "-E- DB error: $DBI::errstr\n";
677 $sth = $dbh->prepare("update tc_child set birthday='$year-$month-$day' where name='$child_name'");
678 $sth->execute or die "-E- DB error: $DBI::errstr\n";
679 $sth = $dbh->prepare("update tc_child set valid=1 where name='$child_name'");
680 $sth->execute or die "-E- DB error: $DBI::errstr\n";
681 $sth = $dbh->prepare("update tc_child set indiv_id='$id' where name='$child_name'");
682 $sth->execute or die "-E- DB error: $DBI::errstr\n";
684 # More than one record was found. Error! This shouldn't happen.
685 print " -E- More than one record found ($rows) with same child name: $child_name\n";
694 #+----------------+------------------+------+-----+---------+-------+
695 #| Field | Type | Null | Key | Default | Extra |
696 #+----------------+------------------+------+-----+---------+-------+
697 #| visit | int(16) unsigned | | PRI | 0 | A |
698 #| family | int(16) unsigned | YES | UNI | NULL | |
699 #| companionship | int(16) unsigned | YES | | NULL | |
700 #| date | date | YES | | NULL | |
701 #| notes | varchar(128) | YES | | NULL | |
702 #| visited | varchar(1) | YES | | NULL | |
703 #+----------------+------------------+------+-----+---------+-------+
704 sub update_tc_visit_table
706 print "\n-> updating tc_visit table\n";
708 my $month_header_retrieved = 0;
711 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);
712 ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
713 my %visit_status = ('X', 'y', '-', 'n', '', '');
715 foreach $index (keys %hometeaching_stats_data)
717 $hashref = $hometeaching_stats_data{$index};
718 #foreach $key (keys %$hashref) {print "$key\n";}
720 $family_name = $hometeaching_stats_data{$index}{"Preferred Name"};
721 print " Updating visit data: $family_name\n";
723 # get family id from tc_family
724 $sth = $dbh->prepare("select * from tc_family where name=\"$family_name\" and valid=1");
725 $sth->execute or die "-E- DB error: $DBI::errstr\n";
726 my @family_data = ();
727 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
728 my $family_rows = scalar @family_data;
729 if($family_rows > 0) {
730 $family_id = $family_data[0]->{'family'};
731 $comp_id = $family_data[0]->{'companionship'};
734 #print "family_id = $family_id\n";
735 #print "comp_id = $comp_id\n";
737 # ignore visits that weren't done by the EQ
738 if ($comp_id == 0) { next; }
740 # retrieve the month header if not already done
741 if ($month_header_retrieved == 0)
743 foreach $key (keys %$hashref)
745 if (($key ne "Preferred Name") && ($key ne "Home Teachers"))
747 $month_header = $key;
748 @data_months = split /\t/, $key;
751 $month_header_retrieved = 1;
754 # loop through history data
755 @history = split /\t/, $hometeaching_stats_data{$index}{$month_header};
756 my $data_year = 1900 + $yearOffset;
757 my $data_month = $months{$data_months[-1]};
758 #print "$month_header\n";
759 #print $hometeaching_stats_data{$index}{$month_header};
761 foreach $i (reverse(0..$#history)) {
762 # went back a calendar year, decrement $data_year
763 if ($months{$data_months[$i]} > $data_month)
767 $data_month = $months{$data_months[$i]};
768 my $visit_date = sprintf("%4d-%02d-01\n", $data_year, $data_month);
769 #print "$visit_date\n";
770 my $importing_status = $visit_status{$history[$i]};
771 #print "importing_status = $importing_status\n";
772 #print "select * from tc_visit where family=$family_id and companionship=$comp_id and date='$visit_date'\n";
773 $sth = $dbh->prepare("select * from tc_visit where family=$family_id and companionship=$comp_id and date='$visit_date'");
774 $sth->execute or die "-E- DB error: $DBI::errstr\n";
776 while($sqlhashref = $sth->fetchrow_hashref) { push(@visit_data, $sqlhashref); }
777 my $visit_rows = scalar @visit_data;
778 if($visit_rows > 0) {
779 my $visited = $visit_data[0]->{'visited'};
780 #print "visited = $visited\n";
781 # update visit if data is different in tc_visit
782 if ($visited ne $importing_status)
784 #print "importing_status = $importing_status\n";
785 $sth = $dbh->prepare("update tc_visit set visited='$importing_status' where family='$family_id' and date='$visit_date' and companionship='$comp_id'");
786 $sth->execute or die "-E- DB error: $DBI::errstr\n";
789 if ($importing_status ne '')
791 # add visit if it doesn't exist in tc_visit
792 $sth = $dbh->prepare("insert into tc_visit values (NULL, '$family_id', '$comp_id', '', '', '$visit_date', '', '$importing_status', 'hometeaching')");
793 $sth->execute or die "-E- DB error: $DBI::errstr\n";
800 ######################################################################
801 sub check_for_changed_ids
803 # If the Indiv ID & HofH ID has changed between data sets, we could have problems
804 my ($oldhashref, $newhashref) = @_;
805 my $found_problem = 0;
807 foreach $oldindex (keys %$oldhashref)
809 $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
810 $hofh_id = $oldhashref->{$oldindex}{'HofH ID'};
811 $full_name = $oldhashref->{$oldindex}{'Full Name'};
812 $hh_position = $oldhashref->{$oldindex}{'HH Position'};
813 if($hh_position =~ /Other/i) { next; }
815 foreach $newindex (keys %$newhashref)
817 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
818 $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
820 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
824 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
825 $hofh_id != $newhashref->{$newindex}{'HofH ID'})
827 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
833 return $found_problem;
836 ######################################################################
838 ######################################################################
840 ###################################################
841 # Open a connection to the database
842 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
844 PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
846 ###################################################
847 # Check old directory against new directory to ensure
848 # that the Indiv ID & HofH ID have not changed between updates
850 print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
851 my %old_membership_data = ();
852 my %new_membership_data = ();
853 &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
854 &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
856 $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
860 print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
861 print " $opt_o and $opt_n data sets.\n";
862 print " This script is not currently setup to handle this properly.\n";
864 print " Exiting without updating...\n\n";
869 ###################################################
870 # Process command line options
871 if(defined $opt_n) { $datadir = $opt_n; }
872 else { $datadir = shift(@ARGV); }
873 print "\n-> Processing all ward data files in $datadir\n";
875 ###################################################
876 # Parse Ward Data Files
877 &optional_csv_to_hash("$datadir/EQ\ Prospective\ Elders.csv", \%prospective_elder_data);
878 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
879 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
880 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
881 &optional_csv_to_hash("$datadir/Home\ Teacher\ per\ Companionship.csv", \%hometeaching_stats_data);
882 %organization_by_name = ();
883 %organization_by_id = ();
886 print "-> Membership Data Dump\n\n";
887 &print_hash(\%membership_data);
888 print "-> HomeTeaching Data Dump\n\n";
889 &print_hash(\%hometeaching_data);
890 print "-> Organization Data Dump\n\n";
891 &print_hash(\%organization_data);
892 print "-> HomeTeaching Stats Data Dump\n\n";
893 &print_hash(\%hometeaching_stats_data);
896 if($opt_s) { $dbh->disconnect(); exit; }
898 # Now update the various eq DB tables
899 &update_tc_calling_table();
900 &update_tc_indiv_table();
901 &update_tc_aaronic_table();
902 &update_tc_district_table();
903 &update_tc_companionship_table();
904 &update_tc_family_table();
905 &update_tc_parent_table();
906 &update_tc_child_table();
907 &update_tc_visit_table();
909 print "\n-> Import Successful! DONE...\n";
911 ###################################################
912 # Disconnect from the database
915 ######################################################################