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 ######################################################################
62 foreach $key (sort {$a <=> $b} keys %$hashref) {
63 print "Index: $key\n";
64 foreach $field (keys %{$hashref->{$key}}) {
65 print "$field: $hashref->{$key}{$field}\n";
71 ######################################################################
74 #+-------+--------------------+------+-----+---------+-------+
75 #| Field | Type | Null | Key | Default | Extra |
76 #+-------+--------------------+------+-----+---------+-------+
77 #| aaronic | int(16) unsigned | | PRI | 0 | A |
78 #| name | varchar(60) | YES | | NULL | |
79 #| phone | varchar(12) | YES | | NULL | |
80 #| valid | tinyint(1) | YES | | NULL | |
81 #+-------+--------------------+------+-----+---------+-------+
82 sub update_eq_aaronic_table
84 print "\n-> Updating eq_aaronic table\n";
86 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
87 $sth = $dbh->prepare("update eq_aaronic set valid=0");
88 $sth->execute or die "-E- DB error: $DBI::errstr\n";
90 foreach $index (keys %membership_data)
92 $hashref = $membership_data{$index};
93 foreach $key (keys %$hashref) {
94 if($key =~ /Priesthood/i &&
95 ($membership_data{$index}{$key} =~ /^Deacon\s*$/i ||
96 $membership_data{$index}{$key} =~ /^Teacher\s*$/i ||
97 $membership_data{$index}{$key} =~ /^Priest\s*$/i)) {
98 $aaronic_name = $membership_data{$index}{'Preferred Name'};
99 $phone = $membership_data{$index}{'Phone 1'};
100 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
101 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
102 $sth = $dbh->prepare("select * from eq_aaronic where name='$aaronic_name'");
103 $sth->execute or die "-E- DB error: $DBI::errstr\n";
105 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
106 my $rows = scalar @data;
108 # No existing records found for this aaronic, make a new entry
109 print " Adding new Aaronic: $aaronic_name\n";
110 $sth = $dbh->prepare("insert into eq_aaronic values (NULL,'$aaronic_name','$phone',1)");
111 $sth->execute or die "-E- DB error: $DBI::errstr\n";
112 } elsif($rows == 1) {
113 # An existing record was found for this aaronic, update it, mark it valid!
114 print " Updating existing aaronic: $aaronic_name\n";
115 $sth = $dbh->prepare("update eq_aaronic set phone='$phone' where name='$aaronic_name'");
116 $sth->execute or die "-E- DB error: $DBI::errstr\n";
117 $sth = $dbh->prepare("update eq_aaronic set valid=1 where name='$aaronic_name'");
118 $sth->execute or die "-E- DB error: $DBI::errstr\n";
120 # More than one record was found. Error! This shouldn't happen.
121 print " -E- More than one record found ($rows) for aaronic name: $aaronic_name\n";
130 #+-------------+------------------+------+-----+---------+----------------+
131 #| Field | Type | Null | Key | Default | Extra |
132 #+-------------+------------------+------+-----+---------+----------------+
133 #| elder | int(16) unsigned | | PRI | NULL | auto_increment |
134 #| name | varchar(60) | YES | | NULL | |
135 #| phone | varchar(12) | YES | | NULL | |
136 #| ppi_pri | int(10) unsigned | YES | | 1 | |
137 #| ppi_notes | varchar(128) | YES | | NULL | |
138 #| int_pri | int(10) unsigned | YES | | 1 | |
139 #| int_notes | varchar(128) | YES | | NULL | |
140 #| attending | tinyint(1) | YES | | 0 | |
141 #| valid | tinyint(1) | YES | | NULL | |
142 #+-------------+------------------+------+-----+---------+----------------+
143 sub update_eq_elder_table
145 print "\n-> Updating eq_elder table\n";
147 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
148 $sth = $dbh->prepare("update eq_elder set valid=0");
149 $sth->execute or die "-E- DB error: $DBI::errstr\n";
151 foreach $index (keys %membership_data)
153 $hashref = $membership_data{$index};
154 foreach $key (keys %$hashref) {
155 if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
156 $id = $membership_data{$index}{'Indiv ID'};
157 $elder_name = $membership_data{$index}{'Preferred Name'};
158 $phone = $membership_data{$index}{'Phone 1'};
159 $organization = $organization_by_id{$id};
161 if(($organization =~ /Elders/) ||
162 ($organization =~ /Young Men/) ||
163 ($organization =~ /Sunday School/) ||
164 ($organization =~ /Primary/)
165 ) { $attending = 1; }
166 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
167 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
168 $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
169 $sth->execute or die "-E- DB error: $DBI::errstr\n";
171 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
172 my $rows = scalar @data;
174 # No existing records found for this elder, make a new entry
175 print " Adding new Elder: $elder_name\n";
176 $sth = $dbh->prepare("insert into eq_elder values (NULL,'$elder_name','$phone','1','','1','',$attending,1)");
177 $sth->execute or die "-E- DB error: $DBI::errstr\n";
178 } elsif($rows == 1) {
179 # An existing record was found for this elder, update it
180 print " Updating existing Elder: $elder_name\n";
181 $sth = $dbh->prepare("update eq_elder set valid=1 where name='$elder_name'");
182 $sth->execute or die "-E- DB error: $DBI::errstr\n";
184 $sth = $dbh->prepare("update eq_elder set phone='$phone' where name='$elder_name'");
186 $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_name'");
188 $sth->execute or die "-E- DB error: $DBI::errstr\n";
189 $sth = $dbh->prepare("update eq_elder set attending='$attending' where name='$elder_name'");
190 $sth->execute or die "-E- DB error: $DBI::errstr\n";
192 # More than one record was found. Error! This shouldn't happen.
193 print " -E- More than one record found ($rows) for Elder: $elder_name\n";
202 #+--------------+------------------+------+-----+---------+-------+
203 #| Field | Type | Null | Key | Default | Extra |
204 #+--------------+------------------+------+-----+---------+-------+
205 #| indiv_id | int(16) unsigned | YES | | NULL | |
206 #| name | varchar(30) | YES | | NULL | |
207 #| organization | varchar(30) | YES | | NULL | |
208 #| position | varchar(30) | YES | | NULL | |
209 #| sequence | int(16) unsigned | YES | | NULL | |
210 #| sustained | date | YES | | NULL | |
211 #+--------------+------------------+------+-----+---------+-------+
212 sub update_eq_calling_table()
214 print "\n-> Updating eq_calling table\n";
216 #print "-> Organization Data Dump\n\n";
217 #&print_hash(\%organization_data);
219 # Delete all records from the calling table. We have no history to
220 # save here. Just re-populate with the latest calling information.
221 $sth = $dbh->prepare("delete from eq_calling ");
222 $sth->execute or die "-E- DB error: $DBI::errstr\n";
224 foreach $index (keys %organization_data)
226 $indiv_id = $organization_data{$index}{'Indiv ID'};
227 $name = $organization_data{$index}{'Indiv Name'};
228 $name =~ s/\'/\\'/g; #'
229 $organization = $organization_data{$index}{'Organization'};
230 $organization_by_name{$name} = $organization;
231 $organization_by_id{$indiv_id} = $organization;
232 $position = $organization_data{$index}{'Position'};
233 $sequence = $organization_data{$index}{'Org Seq'};
234 $sustained = $organization_data{$index}{'Sustained'};
235 $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
236 if($name eq "") { next; }
237 print " Adding new Calling: $name -> $position\n";
238 $sth = $dbh->prepare("insert into eq_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
239 $sth->execute or die "-E- DB error: $DBI::errstr\n";
244 #+------------+------------------+------+-----+---------+-------+
245 #| Field | Type | Null | Key | Default | Extra |
246 #+------------+------------------+------+-----+---------+-------+
247 #| district | int(16) unsigned | | PRI | 0 | |
248 #| name | varchar(30) | YES | | NULL | |
249 #| supervisor | int(16) unsigned | YES | | NULL | |
250 #| valid | tinyint(1) | YES | | NULL | |
251 #+------------+------------------+------+-----+---------+-------+
252 sub update_eq_district_table
254 # Districts should be created by hand. This subroutine only
255 # updates the supervisor's ID in each district.
256 print "\n-> Updating eq_district table\n";
257 $sth = $dbh->prepare("select * from eq_district");
258 $sth->execute or die "-E- DB error: $DBI::errstr\n";
259 while($sqlhashref = $sth->fetchrow_hashref) {
260 $supervisor_name = $sqlhashref->{name};
261 $district = $sqlhashref->{district};
262 $sth2 = $dbh->prepare("select * from eq_elder where name='$supervisor_name'");
263 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
264 $sqlhashref2 = $sth2->fetchrow_hashref;
265 $supervisor_id = $sqlhashref2->{elder};
267 $sth2 = $dbh->prepare("update eq_district set supervisor='$supervisor_id' where district='$district'");
268 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
275 #+---------------+------------------+------+-----+---------+-------+
276 #| Field | Type | Null | Key | Default | Extra |
277 #+---------------+------------------+------+-----+---------+-------+
278 #| companionship | int(16) unsigned | | | 0 | |
279 #| elder | int(16) unsigned | YES | | NULL | |
280 #| aaronic | int(16) unsigned | YES | | NULL | |
281 #| district | int(16) unsigned | YES | | NULL | |
282 #| valid | tinyint(1) | YES | | NULL | |
283 #+---------------+------------------+------+-----+---------+-------+
284 sub update_eq_companionship_table
286 print "\n-> Updating eq_companionship table\n";
288 # First, mark all existing companionships as invalid in case they have been dissolved
289 $sth = $dbh->prepare("update eq_companionship set valid=0");
290 $sth->execute or die "-E- DB error: $DBI::errstr\n";
291 # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
292 $sth = $dbh->prepare("update eq_aaronic set valid=0");
293 $sth->execute or die "-E- DB error: $DBI::errstr\n";
295 foreach $index (keys %hometeaching_data)
297 $hashref = $hometeaching_data{$index};
298 foreach $key (keys %$hashref) {
299 if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
300 foreach $field ("Home Teacher 1","Home Teacher 2") {
301 $elder_name = $hometeaching_data{$index}{$field};
302 if($elder_name eq "") { next; }
303 $sth2 = $dbh->prepare("select * from eq_elder where name='$elder_name'");
304 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
305 $sqlhashref2 = $sth2->fetchrow_hashref;
306 $elder = $sqlhashref2->{elder};
309 $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_name'");
310 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
311 $sqlhashref2 = $sth2->fetchrow_hashref;
312 $aaronic = $sqlhashref2->{aaronic};
314 if($aaronic eq "") { print "-W- Unable to find $elder_name in eq_elder or eq_aaronic tables\n"; next; }
316 $id = $hometeaching_data{$index}{'Comp ID'};
317 $district = $hometeaching_data{$index}{'HT District'};
318 $sth = $dbh->prepare("select * from eq_companionship where elder='$elder' and aaronic='$aaronic' and companionship='$id'");
319 $sth->execute or die "-E- DB error: $DBI::errstr\n";
321 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
322 my $rows = scalar @data;
324 # No existing records found for this companionship, make a new entry
325 print " Adding Companion to companionship: $elder_name -> $id\n";
326 $sth = $dbh->prepare("insert into eq_companionship values ($id,'$elder','$aaronic','$district',1)");
327 $sth->execute or die "-E- DB error: $DBI::errstr\n";
329 # An existing companionship was found for this companionship, update it
330 $sth2 = $dbh->prepare("select * from eq_companionship where district='$district' and companionship='$id'");
331 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
332 if($elder ne "NULL") {
333 print " Updating Companionship with Elder: $elder_name ($elder) -> $id\n";
334 $sth = $dbh->prepare("update eq_companionship set district='$district' where elder='$elder' and companionship='$id'");
335 $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
336 $sth = $dbh->prepare("update eq_companionship set elder='$elder' where elder='$elder' and companionship='$id'");
337 $sth->execute or die "-E- DB error 'elder': $DBI::errstr\n";
338 $sth = $dbh->prepare("update eq_companionship set valid=1 where elder='$elder' and companionship='$id'");
339 $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
341 print " Updating Companionship with Aaronic: $elder_name ($aaronic) -> $id\n";
342 $sth = $dbh->prepare("update eq_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
343 $sth->execute or die "-E- DB error: $DBI::errstr\n";
344 $sth = $dbh->prepare("update eq_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
345 $sth->execute or die "-E- DB error: $DBI::errstr\n";
346 $sth = $dbh->prepare("update eq_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
347 $sth->execute or die "-E- DB error: $DBI::errstr\n";
348 $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
349 $sth->execute or die "-E- DB error: $DBI::errstr\n";
361 #+---------------+------------------+------+-----+---------+-------+
362 #| Field | Type | Null | Key | Default | Extra |
363 #+---------------+------------------+------+-----+---------+-------+
364 #| family | int(16) unsigned | | PRI | 0 | A |
365 #| hofh_id | int(16) unsigned | YES | | NULL | |
366 #| name | varchar(30) | YES | | NULL | |
367 #| name_id | varchar(30) | YES | | NULL | |
368 #| elder_id | int(16) unsigned | YES | | NULL | |
369 #| companionship | int(16) unsigned | YES | | NULL | |
370 #| visit_pri | int(10) unsigned | YES | | 1 | |
371 #| visit_notes | varchar(128) | YES | | NULL | |
372 #| valid | tinyint(1) | YES | | NULL | |
373 #+---------------+------------------+------+-----+---------+-------+
374 sub update_eq_family_table
376 print "\n-> Updating eq_family table\n";
378 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
379 $sth = $dbh->prepare("update eq_family set valid=0");
380 $sth->execute or die "-E- DB error: $DBI::errstr\n";
381 $sth = $dbh->prepare("update eq_family set companionship=0");
382 $sth->execute or die "-E- DB error: $DBI::errstr\n";
384 foreach $index (keys %membership_data)
386 $hashref = $membership_data{$index};
387 foreach $key (keys %$hashref) {
388 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
389 $family_name = $membership_data{$index}{'Preferred Name'};
390 $family_name =~ s/\'/\\'/g; #'
391 $id = $membership_data{$index}{'HofH ID'};
392 $name_id = uc($family_name);
394 # Find out how many families match this family's name
395 $sth = $dbh->prepare("select * from eq_family where name_id='$name_id'");
396 $sth->execute or die "-E- DB error: $DBI::errstr\n";
398 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
399 my $rows = scalar @data;
402 # No existing records found for this family, make a new entry
403 print " Adding new Family: $family_name\n";
404 $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','$name_id','0','0','1','',1)");
405 $sth->execute or die "-E- DB error: $DBI::errstr\n";
406 } elsif($rows == 1) {
407 # An existing record was found for this family, update it
408 print " Updating existing family: $family_name\n";
409 $sth = $dbh->prepare("update eq_family set hofh_id=$id where name_id='$name_id'");
410 $sth->execute or die "-E- DB error: $DBI::errstr\n";
411 $sth = $dbh->prepare("update eq_family set valid=1 where name_id='$name_id'");
412 $sth->execute or die "-E- DB error: $DBI::errstr\n";
414 # More than one record was found. Error! This shouldn't happen.
415 print " -E- More than one record found ($rows) for family name: $family_name\n";
418 # Now update the elder_id field for this family
419 $sth = $dbh->prepare("select * from eq_elder WHERE name='$family_name'");
420 $sth->execute or die "-E- DB error: $DBI::errstr\n";
421 while($sqlhashref = $sth->fetchrow_hashref) {
422 $elder_id = $sqlhashref->{elder};
423 print " Updating family elder_id: $family_name -> $elder_id\n";
424 $sth = $dbh->prepare("update eq_family set elder_id=$elder_id where name_id='$name_id'");
425 $sth->execute or die "-E- DB error: $DBI::errstr\n";
428 # Now update the hometeaching field for this family
429 foreach $index (keys %hometeaching_data)
431 $hashref = $hometeaching_data{$index};
432 foreach $key (keys %$hashref) {
433 if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
434 $a = $1; $b = $2; $c = $3; $d = $4;
435 if($family_name =~ /$a/ && $hometeaching_data{$index}{'Household'} !~ /$family_name/i) {
436 print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $a, $c $d\n";
437 $hometeaching_data{$index}{'Household'} = "$a, $c $d";
440 if($key =~ /Quorum/i &&
441 $hometeaching_data{$index}{$key} =~ /Elders/i &&
442 $hometeaching_data{$index}{'Household'} =~ /$family_name/i &&
443 $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
446 print " Updating hometeaching assignment for $family_name family\n";
447 $companionship = $hometeaching_data{$index}{'Comp ID'};
448 $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name_id='$name_id'");
449 $sth->execute or die "-E- DB error: $DBI::errstr\n";
460 #+----------+------------------+------+-----+---------+-------+
461 #| Field | Type | Null | Key | Default | Extra |
462 #+----------+------------------+------+-----+---------+-------+
463 #| parent | int(16) unsigned | | PRI | 0 | A |
464 #| family | int(16) unsigned | YES | | NULL | |
465 #| name | varchar(30) | YES | | NULL | |
466 #| birthday | date | YES | | NULL | |
467 #| phone | varchar(12) | YES | | NULL | |
468 #| address | varchar(255) | YES | | NULL | |
469 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
470 #| valid | tinyint(1) | YES | | NULL | |
471 #+----------+------------------+------+-----+---------+-------+
472 sub update_eq_parent_table
474 print "\n-> Updating eq_parent table\n";
476 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
477 $sth = $dbh->prepare("update eq_parent set valid=0");
478 $sth->execute or die "-E- DB error: $DBI::errstr\n";
480 foreach $index (keys %membership_data)
482 $hashref = $membership_data{$index};
483 foreach $key (keys %$hashref) {
484 if($key =~ /HH Position/i &&
485 $membership_data{$index}{$key} =~ /Head of Household/i ||
486 $membership_data{$index}{$key} =~ /Spouse/i
488 # Get some information from the hash about this parent
489 $parent_name = $membership_data{$index}{'Preferred Name'};
490 $parent_name =~ s/\'/\\'/g; #'
491 $birthday = $membership_data{$index}{'Birth'};
492 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
493 $hofh_id = $membership_data{$index}{'HofH ID'};
494 $id = $membership_data{$index}{'Indiv ID'};
495 $phone = $membership_data{$index}{'Phone 1'};
496 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
497 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
498 $address = $membership_data{$index}{'Street 1'};
499 if($membership_data{$index}{'Street 2'} ne "") {
500 $address .= " " . $membership_data{$index}{'Street 2'};
503 # Find the family id for this parent's HofH_ID.
504 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
505 $sth->execute or die "-E- DB error: $DBI::errstr\n";
506 my @family_data = ();
507 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
508 my $family_rows = scalar @family_data;
509 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
510 else { $family_id = 0; }
512 # Find out how many parents match this parent's name
513 $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
514 $sth->execute or die "-E- DB error: $DBI::errstr\n";
516 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
517 my $rows = scalar @data;
519 if($rows == 0 && $family_rows > 0) {
520 # No existing records found for this parent, make a new entry
521 print " Adding new Parent: $parent_name\n";
522 $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
523 $sth->execute or die "-E- DB error: $DBI::errstr\n";
524 } elsif($rows == 1 && $family_rows > 0) {
525 # An existing record was found for this parent, update it
526 print " Updating existing parent: $parent_name\n";
527 $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
528 $sth->execute or die "-E- DB error: $DBI::errstr\n";
529 $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
530 $sth->execute or die "-E- DB error: $DBI::errstr\n";
531 $sth = $dbh->prepare("update eq_parent set phone='$phone' where name='$parent_name'");
532 $sth->execute or die "-E- DB error: $DBI::errstr\n";
533 $sth = $dbh->prepare("update eq_parent set address='$address' where name='$parent_name'");
534 $sth->execute or die "-E- DB error: $DBI::errstr\n";
535 $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
536 $sth->execute or die "-E- DB error: $DBI::errstr\n";
537 $sth = $dbh->prepare("update eq_parent set indiv_id='$id' where name='$parent_name'");
538 $sth->execute or die "-E- DB error: $DBI::errstr\n";
540 # More than one record was found. Error! This shouldn't happen.
541 print " -E- More than one record found with same parent name: $parent_name with hofh_id: $hofh_id\n";
543 print " -E- Unable to find a family to attach this parent to: $parent_name with hofh_id: $hofh_id\n";
552 #+----------+------------------+------+-----+---------+-------+
553 #| Field | Type | Null | Key | Default | Extra |
554 #+----------+------------------+------+-----+---------+-------+
555 #| child | int(16) unsigned | | PRI | 0 | A |
556 #| family | int(16) unsigned | YES | | NULL | |
557 #| name | varchar(30) | YES | | NULL | |
558 #| birthday | date | YES | | NULL | |
559 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
560 #| valid | tinyint(1) | YES | | NULL | |
561 #+----------+------------------+------+-----+---------+-------+
562 sub update_eq_child_table
564 print "\n-> Updating eq_child table\n";
566 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
567 $sth = $dbh->prepare("update eq_child set valid=0");
568 $sth->execute or die "-E- DB error: $DBI::errstr\n";
570 foreach $index (keys %membership_data)
572 $hashref = $membership_data{$index};
573 foreach $key (keys %$hashref) {
574 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
575 $child_name = $membership_data{$index}{'Full Name'};
576 $child_name =~ s/\'/\\'/g; #'
577 $birthday = $membership_data{$index}{'Birth'};
578 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
579 $id = $membership_data{$index}{'Indiv ID'};
580 $hofh_id = $membership_data{$index}{'HofH ID'};
582 # Find the family id for this child's HofH_ID.
583 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
584 $sth->execute or die "-E- DB error: $DBI::errstr\n";
585 my @family_data = ();
586 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
587 my $family_rows = scalar @family_data;
588 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
589 else { $family_id = 0; }
591 # Find out how many children have the same name for the same family
592 $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
593 $sth->execute or die "-E- DB error: $DBI::errstr\n";
595 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
596 my $rows = scalar @data;
598 if($rows == 0 && $family_rows > 0) {
599 # No existing records found for this child, make a new entry
600 print " Adding new Child: $child_name\n";
601 $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
602 $sth->execute or die "-E- DB error: $DBI::errstr\n";
603 } elsif($rows == 1 && $family_rows > 0) {
604 # An existing record was found for this child, update it
605 print " Updating existing child: $child_name\n";
606 $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
607 $sth->execute or die "-E- DB error: $DBI::errstr\n";
608 $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
609 $sth->execute or die "-E- DB error: $DBI::errstr\n";
610 $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
611 $sth->execute or die "-E- DB error: $DBI::errstr\n";
612 $sth = $dbh->prepare("update eq_child set indiv_id='$id' where name='$child_name'");
613 $sth->execute or die "-E- DB error: $DBI::errstr\n";
615 # More than one record was found. Error! This shouldn't happen.
616 print " -E- More than one record found ($rows) with same child name: $child_name\n";
624 ######################################################################
625 sub check_for_changed_ids
627 # If the Indiv ID & HofH ID has changed between data sets, we could have problems
628 my ($oldhashref, $newhashref) = @_;
629 my $found_problem = 0;
631 foreach $oldindex (keys %$oldhashref)
633 $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
634 $hofh_id = $oldhashref->{$oldindex}{'HofH ID'};
635 $full_name = $oldhashref->{$oldindex}{'Full Name'};
636 $hh_position = $oldhashref->{$oldindex}{'HH Position'};
637 if($hh_position =~ /Other/i) { next; }
639 foreach $newindex (keys %$newhashref)
641 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
642 $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
644 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
648 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
649 $hofh_id != $newhashref->{$newindex}{'HofH ID'})
651 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
657 return $found_problem;
660 ######################################################################
662 ######################################################################
664 ###################################################
665 # Open a connection to the database
666 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
668 PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
670 ###################################################
671 # Check old directory against new directory to ensure
672 # that the Indiv ID & HofH ID have not changed between updates
674 print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
675 my %old_membership_data = ();
676 my %new_membership_data = ();
677 &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
678 &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
680 $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
684 print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
685 print " $opt_o and $opt_n data sets.\n";
686 print " This script is not currently setup to handle this properly.\n";
688 print " Exiting without updating...\n\n";
693 ###################################################
694 # Process command line options
695 if(defined $opt_n) { $datadir = $opt_n; }
696 else { $datadir = shift(@ARGV); }
697 print "\n-> Processing all ward data files in $datadir\n";
699 ###################################################
700 # Parse Ward Data Files
701 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
702 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
703 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
704 %organization_by_name = ();
705 %organization_by_id = ();
708 print "-> Membership Data Dump\n\n";
709 &print_hash(\%membership_data);
710 print "-> HomeTeaching Data Dump\n\n";
711 &print_hash(\%hometeaching_data);
712 print "-> Organization Data Dump\n\n";
713 &print_hash(\%organization_data);
716 if($opt_s) { $dbh->disconnect(); exit; }
718 # Now update the various eq DB tables
719 &update_eq_calling_table();
720 &update_eq_elder_table();
721 &update_eq_aaronic_table();
722 &update_eq_district_table();
723 &update_eq_companionship_table();
724 &update_eq_family_table();
725 &update_eq_parent_table();
726 &update_eq_child_table();
728 print "\n-> Import Successful! DONE...\n";
730 ###################################################
731 # Disconnect from the database
734 ######################################################################