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 #| valid | tinyint(1) | YES | | NULL | |
113 #+-------+--------------------+------+-----+---------+-------+
114 sub update_eq_aaronic_table
116 print "\n-> Updating eq_aaronic table\n";
118 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
119 $sth = $dbh->prepare("update eq_aaronic set valid=0");
120 $sth->execute or die "-E- DB error: $DBI::errstr\n";
122 foreach $index (keys %membership_data)
124 $hashref = $membership_data{$index};
125 foreach $key (keys %$hashref) {
126 if($key =~ /Priesthood/i &&
127 ($membership_data{$index}{$key} =~ /^Deacon\s*$/i ||
128 $membership_data{$index}{$key} =~ /^Teacher\s*$/i ||
129 $membership_data{$index}{$key} =~ /^Priest\s*$/i)) {
130 $aaronic_name = $membership_data{$index}{'Preferred Name'};
131 $phone = $membership_data{$index}{'Household Phone'};
132 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
133 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
134 $sth = $dbh->prepare("select * from eq_aaronic where name='$aaronic_name'");
135 $sth->execute or die "-E- DB error: $DBI::errstr\n";
137 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
138 my $rows = scalar @data;
140 # No existing records found for this aaronic, make a new entry
141 print " Adding new Aaronic: $aaronic_name\n";
142 $sth = $dbh->prepare("insert into eq_aaronic values (NULL,'$aaronic_name','$phone',1)");
143 $sth->execute or die "-E- DB error: $DBI::errstr\n";
144 } elsif($rows == 1) {
145 # An existing record was found for this aaronic, update it, mark it valid!
146 print " Updating existing aaronic: $aaronic_name\n";
147 $sth = $dbh->prepare("update eq_aaronic set phone='$phone' where name='$aaronic_name'");
148 $sth->execute or die "-E- DB error: $DBI::errstr\n";
149 $sth = $dbh->prepare("update eq_aaronic set valid=1 where name='$aaronic_name'");
150 $sth->execute or die "-E- DB error: $DBI::errstr\n";
152 # More than one record was found. Error! This shouldn't happen.
153 print " -E- More than one record found ($rows) for aaronic name: $aaronic_name\n";
162 #+-------------+------------------+------+-----+---------+----------------+
163 #| Field | Type | Null | Key | Default | Extra |
164 #+-------------+------------------+------+-----+---------+----------------+
165 #| elder | int(16) unsigned | | PRI | NULL | auto_increment |
166 #| indiv_id | int(16) unsigned | | | NULL | |
167 #| name | varchar(60) | YES | | NULL | |
168 #| phone | varchar(12) | YES | | NULL | |
169 #| email | varchar(120) | YES | | NULL | |
170 #| ppi_pri | int(10) unsigned | YES | | 1 | |
171 #| ppi_notes | varchar(128) | YES | | NULL | |
172 #| int_pri | int(10) unsigned | YES | | 1 | |
173 #| int_notes | varchar(128) | YES | | NULL | |
174 #| attending | tinyint(1) | YES | | 0 | |
175 #| valid | tinyint(1) | YES | | NULL | |
176 #+-------------+------------------+------+-----+---------+----------------+
177 sub update_eq_elder_table
179 print "\n-> Updating eq_elder table\n";
181 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
182 $sth = $dbh->prepare("update eq_elder set valid=0");
183 $sth->execute or die "-E- DB error: $DBI::errstr\n";
185 foreach $index (keys %membership_data)
187 $hashref = $membership_data{$index};
188 foreach $key (keys %$hashref) {
189 if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
190 $id = $membership_data{$index}{'Indiv ID'};
191 $elder_name = $membership_data{$index}{'Preferred Name'};
192 $phone = $membership_data{$index}{'Household Phone'};
193 $organization = $organization_by_id{$id};
195 if(($organization =~ /Elders/) ||
196 ($organization =~ /Young Men/) ||
197 ($organization =~ /Sunday School/) ||
198 ($organization =~ /Primary/)
199 ) { $attending = 1; }
200 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
201 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
202 $email = $membership_data{$index}{'Individual E-mail'};
203 if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
204 $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
205 $sth->execute or die "-E- DB error: $DBI::errstr\n";
207 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
208 my $rows = scalar @data;
210 # No existing records found for this elder, make a new entry
211 print " Adding new Elder: $elder_name\n";
212 $sth = $dbh->prepare("insert into eq_elder values (NULL,'$id','$elder_name','$phone','$email','1','','1','',$attending,1)");
213 $sth->execute or die "-E- DB error: $DBI::errstr\n";
214 } elsif($rows == 1) {
215 # An existing record was found for this elder, update it
216 print " Updating existing Elder: $elder_name\n";
217 $sth = $dbh->prepare("update eq_elder set valid=1 where name='$elder_name'");
218 $sth->execute or die "-E- DB error: $DBI::errstr\n";
220 $sth = $dbh->prepare("update eq_elder set phone='$phone' where name='$elder_name'");
222 $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_name'");
224 $sth->execute or die "-E- DB error: $DBI::errstr\n";
225 $sth = $dbh->prepare("update eq_elder set attending='$attending' where name='$elder_name'");
226 $sth->execute or die "-E- DB error: $DBI::errstr\n";
227 $sth = $dbh->prepare("update eq_elder set indiv_id='$id' where name='$elder_name'");
228 $sth->execute or die "-E- DB error: $DBI::errstr\n";
229 $sth = $dbh->prepare("update eq_elder set email='$email' where name='$elder_name'");
230 $sth->execute or die "-E- DB error: $DBI::errstr\n";
232 # More than one record was found. Error! This shouldn't happen.
233 print " -E- More than one record found ($rows) for Elder: $elder_name\n";
242 #+--------------+------------------+------+-----+---------+-------+
243 #| Field | Type | Null | Key | Default | Extra |
244 #+--------------+------------------+------+-----+---------+-------+
245 #| indiv_id | int(16) unsigned | YES | | NULL | |
246 #| name | varchar(30) | YES | | NULL | |
247 #| organization | varchar(30) | YES | | NULL | |
248 #| position | varchar(30) | YES | | NULL | |
249 #| sequence | int(16) unsigned | YES | | NULL | |
250 #| sustained | date | YES | | NULL | |
251 #+--------------+------------------+------+-----+---------+-------+
252 sub update_eq_calling_table()
254 print "\n-> Updating eq_calling table\n";
256 #print "-> Organization Data Dump\n\n";
257 #&print_hash(\%organization_data);
259 # Delete all records from the calling table. We have no history to
260 # save here. Just re-populate with the latest calling information.
261 $sth = $dbh->prepare("delete from eq_calling ");
262 $sth->execute or die "-E- DB error: $DBI::errstr\n";
264 foreach $index (keys %organization_data)
266 $indiv_id = $organization_data{$index}{'Indiv ID'};
267 $name = $organization_data{$index}{'Indiv Name'};
268 $name =~ s/\'/\\'/g; #'
269 $organization = $organization_data{$index}{'Organization'};
270 $organization_by_name{$name} = $organization;
271 $organization_by_id{$indiv_id} = $organization;
272 $position = $organization_data{$index}{'Position'};
273 $sequence = $organization_data{$index}{'Org Seq'};
274 $sustained = $organization_data{$index}{'Sustained'};
275 $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
276 if($name eq "") { next; }
277 print " Adding new Calling: $name -> $position\n";
278 $sth = $dbh->prepare("insert into eq_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
279 $sth->execute or die "-E- DB error: $DBI::errstr\n";
284 #+------------+------------------+------+-----+---------+-------+
285 #| Field | Type | Null | Key | Default | Extra |
286 #+------------+------------------+------+-----+---------+-------+
287 #| district | int(16) unsigned | | PRI | 0 | |
288 #| name | varchar(30) | YES | | NULL | |
289 #| supervisor | int(16) unsigned | YES | | NULL | |
290 #| valid | tinyint(1) | YES | | NULL | |
291 #+------------+------------------+------+-----+---------+-------+
292 sub update_eq_district_table
294 # Districts should be created by hand. This subroutine only
295 # updates the supervisor's ID in each district.
296 print "\n-> Updating eq_district table\n";
297 $sth = $dbh->prepare("select * from eq_district");
298 $sth->execute or die "-E- DB error: $DBI::errstr\n";
299 while($sqlhashref = $sth->fetchrow_hashref) {
300 $supervisor_name = $sqlhashref->{name};
301 $district = $sqlhashref->{district};
302 $sth2 = $dbh->prepare("select * from eq_elder where name='$supervisor_name'");
303 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
304 $sqlhashref2 = $sth2->fetchrow_hashref;
305 $supervisor_id = $sqlhashref2->{elder};
307 $sth2 = $dbh->prepare("update eq_district set supervisor='$supervisor_id' where district='$district'");
308 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
315 #+---------------+------------------+------+-----+---------+-------+
316 #| Field | Type | Null | Key | Default | Extra |
317 #+---------------+------------------+------+-----+---------+-------+
318 #| companionship | int(16) unsigned | | | 0 | |
319 #| elder | int(16) unsigned | YES | | NULL | |
320 #| aaronic | int(16) unsigned | YES | | NULL | |
321 #| district | int(16) unsigned | YES | | NULL | |
322 #| valid | tinyint(1) | YES | | NULL | |
323 #+---------------+------------------+------+-----+---------+-------+
324 sub update_eq_companionship_table
326 print "\n-> Updating eq_companionship table\n";
328 # First, mark all existing companionships as invalid in case they have been dissolved
329 $sth = $dbh->prepare("update eq_companionship set valid=0");
330 $sth->execute or die "-E- DB error: $DBI::errstr\n";
331 # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
332 $sth = $dbh->prepare("update eq_aaronic set valid=0");
333 $sth->execute or die "-E- DB error: $DBI::errstr\n";
335 foreach $index (keys %hometeaching_data)
337 $hashref = $hometeaching_data{$index};
338 foreach $key (keys %$hashref) {
339 if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
340 foreach $field ("Home Teacher 1","Home Teacher 2") {
341 $elder_name = $hometeaching_data{$index}{$field};
342 if($elder_name eq "") { next; }
343 $sth2 = $dbh->prepare("select * from eq_elder where name='$elder_name'");
344 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
345 $sqlhashref2 = $sth2->fetchrow_hashref;
346 $elder = $sqlhashref2->{elder};
349 $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_name'");
350 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
351 $sqlhashref2 = $sth2->fetchrow_hashref;
352 $aaronic = $sqlhashref2->{aaronic};
354 if($aaronic eq "") { print "-W- Unable to find $elder_name in eq_elder or eq_aaronic tables\n"; next; }
356 $id = $hometeaching_data{$index}{'Comp ID'};
357 $district = $hometeaching_data{$index}{'HT District'};
358 $sth = $dbh->prepare("select * from eq_companionship where elder='$elder' and aaronic='$aaronic' and companionship='$id'");
359 $sth->execute or die "-E- DB error: $DBI::errstr\n";
361 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
362 my $rows = scalar @data;
364 # No existing records found for this companionship, make a new entry
365 print " Adding Companion to companionship: $elder_name -> $id\n";
366 $sth = $dbh->prepare("insert into eq_companionship values ($id,'$elder','$aaronic','$district',1)");
367 $sth->execute or die "-E- DB error: $DBI::errstr\n";
369 # An existing companionship was found for this companionship, update it
370 $sth2 = $dbh->prepare("select * from eq_companionship where district='$district' and companionship='$id'");
371 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
372 if($elder ne "NULL") {
373 print " Updating Companionship with Elder: $elder_name ($elder) -> $id\n";
374 $sth = $dbh->prepare("update eq_companionship set district='$district' where elder='$elder' and companionship='$id'");
375 $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
376 $sth = $dbh->prepare("update eq_companionship set elder='$elder' where elder='$elder' and companionship='$id'");
377 $sth->execute or die "-E- DB error 'elder': $DBI::errstr\n";
378 $sth = $dbh->prepare("update eq_companionship set valid=1 where elder='$elder' and companionship='$id'");
379 $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
381 print " Updating Companionship with Aaronic: $elder_name ($aaronic) -> $id\n";
382 $sth = $dbh->prepare("update eq_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
383 $sth->execute or die "-E- DB error: $DBI::errstr\n";
384 $sth = $dbh->prepare("update eq_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
385 $sth->execute or die "-E- DB error: $DBI::errstr\n";
386 $sth = $dbh->prepare("update eq_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
387 $sth->execute or die "-E- DB error: $DBI::errstr\n";
388 $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
389 $sth->execute or die "-E- DB error: $DBI::errstr\n";
401 #+---------------+------------------+------+-----+---------+-------+
402 #| Field | Type | Null | Key | Default | Extra |
403 #+---------------+------------------+------+-----+---------+-------+
404 #| family | int(16) unsigned | | PRI | 0 | A |
405 #| hofh_id | int(16) unsigned | YES | | NULL | |
406 #| name | varchar(30) | YES | | NULL | |
407 #| name_id | varchar(30) | YES | | NULL | |
408 #| elder_id | int(16) unsigned | YES | | NULL | |
409 #| companionship | int(16) unsigned | YES | | NULL | |
410 #| visit_pri | int(10) unsigned | YES | | 1 | |
411 #| visit_notes | varchar(128) | YES | | NULL | |
412 #| valid | tinyint(1) | YES | | NULL | |
413 #+---------------+------------------+------+-----+---------+-------+
414 sub update_eq_family_table
416 print "\n-> Updating eq_family table\n";
418 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
419 $sth = $dbh->prepare("update eq_family set valid=0");
420 $sth->execute or die "-E- DB error: $DBI::errstr\n";
421 $sth = $dbh->prepare("update eq_family set companionship=0");
422 $sth->execute or die "-E- DB error: $DBI::errstr\n";
424 foreach $index (keys %membership_data)
426 $hashref = $membership_data{$index};
427 foreach $key (keys %$hashref) {
428 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
429 $family_name = $membership_data{$index}{'Preferred Name'};
430 $family_name =~ s/\'/\\'/g; #'
431 $id = $membership_data{$index}{'HofH ID'};
432 $name_id = uc($family_name);
434 # Find out how many families match this family's name
435 $sth = $dbh->prepare("select * from eq_family where name_id='$name_id'");
436 $sth->execute or die "-E- DB error: $DBI::errstr\n";
438 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
439 my $rows = scalar @data;
442 # No existing records found for this family, make a new entry
443 print " Adding new Family: $family_name\n";
444 $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','$name_id','0','0','1','',1)");
445 $sth->execute or die "-E- DB error: $DBI::errstr\n";
446 } elsif($rows == 1) {
447 # An existing record was found for this family, update it
448 print " Updating existing family: $family_name\n";
449 $sth = $dbh->prepare("update eq_family set hofh_id=$id where name_id='$name_id'");
450 $sth->execute or die "-E- DB error: $DBI::errstr\n";
451 $sth = $dbh->prepare("update eq_family set valid=1 where name_id='$name_id'");
452 $sth->execute or die "-E- DB error: $DBI::errstr\n";
454 # More than one record was found. Error! This shouldn't happen.
455 print " -E- More than one record found ($rows) for family name: $family_name\n";
458 # Now update the elder_id field for this family
459 $sth = $dbh->prepare("select * from eq_elder WHERE name='$family_name'");
460 $sth->execute or die "-E- DB error: $DBI::errstr\n";
461 while($sqlhashref = $sth->fetchrow_hashref) {
462 $elder_id = $sqlhashref->{elder};
463 print " Updating family elder_id: $family_name -> $elder_id\n";
464 $sth = $dbh->prepare("update eq_family set elder_id=$elder_id where name_id='$name_id'");
465 $sth->execute or die "-E- DB error: $DBI::errstr\n";
468 # Now update the hometeaching field for this family
469 foreach $index (keys %hometeaching_data)
471 $hashref = $hometeaching_data{$index};
472 foreach $key (keys %$hashref) {
473 if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
474 $a = $1; $b = $2; $c = $3; $d = $4;
475 if($family_name =~ /$a/ && $hometeaching_data{$index}{'Household'} !~ /$family_name/i) {
476 print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $a, $c $d\n";
477 $hometeaching_data{$index}{'Household'} = "$a, $c $d";
480 if($key =~ /Quorum/i &&
481 $hometeaching_data{$index}{$key} =~ /Elders/i &&
482 $hometeaching_data{$index}{'Household'} =~ /$family_name/i &&
483 $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
486 print " Updating hometeaching assignment for $family_name family\n";
487 $companionship = $hometeaching_data{$index}{'Comp ID'};
488 $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name_id='$name_id'");
489 $sth->execute or die "-E- DB error: $DBI::errstr\n";
500 #+----------+------------------+------+-----+---------+-------+
501 #| Field | Type | Null | Key | Default | Extra |
502 #+----------+------------------+------+-----+---------+-------+
503 #| parent | int(16) unsigned | | PRI | 0 | A |
504 #| family | int(16) unsigned | YES | | NULL | |
505 #| name | varchar(30) | YES | | NULL | |
506 #| birthday | date | YES | | NULL | |
507 #| phone | varchar(12) | YES | | NULL | |
508 #| address | varchar(255) | YES | | NULL | |
509 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
510 #| valid | tinyint(1) | YES | | NULL | |
511 #+----------+------------------+------+-----+---------+-------+
512 sub update_eq_parent_table
514 print "\n-> Updating eq_parent table\n";
516 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
517 $sth = $dbh->prepare("update eq_parent set valid=0");
518 $sth->execute or die "-E- DB error: $DBI::errstr\n";
520 foreach $index (keys %membership_data)
522 $hashref = $membership_data{$index};
523 foreach $key (keys %$hashref) {
524 if($key =~ /HH Position/i &&
525 $membership_data{$index}{$key} =~ /Head of Household/i ||
526 $membership_data{$index}{$key} =~ /Spouse/i
528 # Get some information from the hash about this parent
529 $parent_name = $membership_data{$index}{'Preferred Name'};
530 $parent_name =~ s/\'/\\'/g; #'
531 $birthday = $membership_data{$index}{'Birth'};
532 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
533 $hofh_id = $membership_data{$index}{'HofH ID'};
534 $id = $membership_data{$index}{'Indiv ID'};
535 $phone = $membership_data{$index}{'Household Phone'};
536 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
537 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
538 $address = $membership_data{$index}{'Street 1'};
539 if($membership_data{$index}{'Street 2'} ne "") {
540 $address .= " " . $membership_data{$index}{'Street 2'};
543 # Find the family id for this parent's HofH_ID.
544 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
545 $sth->execute or die "-E- DB error: $DBI::errstr\n";
546 my @family_data = ();
547 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
548 my $family_rows = scalar @family_data;
549 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
550 else { $family_id = 0; }
552 # Find out how many parents match this parent's name
553 $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
554 $sth->execute or die "-E- DB error: $DBI::errstr\n";
556 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
557 my $rows = scalar @data;
559 if($rows == 0 && $family_rows > 0) {
560 # No existing records found for this parent, make a new entry
561 print " Adding new Parent: $parent_name\n";
562 $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
563 $sth->execute or die "-E- DB error: $DBI::errstr\n";
564 } elsif($rows == 1 && $family_rows > 0) {
565 # An existing record was found for this parent, update it
566 print " Updating existing parent: $parent_name\n";
567 $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
568 $sth->execute or die "-E- DB error: $DBI::errstr\n";
569 $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
570 $sth->execute or die "-E- DB error: $DBI::errstr\n";
571 $sth = $dbh->prepare("update eq_parent set phone='$phone' where name='$parent_name'");
572 $sth->execute or die "-E- DB error: $DBI::errstr\n";
573 $sth = $dbh->prepare("update eq_parent set address='$address' where name='$parent_name'");
574 $sth->execute or die "-E- DB error: $DBI::errstr\n";
575 $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
576 $sth->execute or die "-E- DB error: $DBI::errstr\n";
577 $sth = $dbh->prepare("update eq_parent set indiv_id='$id' where name='$parent_name'");
578 $sth->execute or die "-E- DB error: $DBI::errstr\n";
580 # More than one record was found. Error! This shouldn't happen.
581 print " -E- More than one record found with same parent name: $parent_name with hofh_id: $hofh_id\n";
583 print " -E- Unable to find a family to attach this parent to: $parent_name with hofh_id: $hofh_id\n";
592 #+----------+------------------+------+-----+---------+-------+
593 #| Field | Type | Null | Key | Default | Extra |
594 #+----------+------------------+------+-----+---------+-------+
595 #| child | int(16) unsigned | | PRI | 0 | A |
596 #| family | int(16) unsigned | YES | | NULL | |
597 #| name | varchar(30) | YES | | NULL | |
598 #| birthday | date | YES | | NULL | |
599 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
600 #| valid | tinyint(1) | YES | | NULL | |
601 #+----------+------------------+------+-----+---------+-------+
602 sub update_eq_child_table
604 print "\n-> Updating eq_child table\n";
606 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
607 $sth = $dbh->prepare("update eq_child set valid=0");
608 $sth->execute or die "-E- DB error: $DBI::errstr\n";
610 foreach $index (keys %membership_data)
612 $hashref = $membership_data{$index};
613 foreach $key (keys %$hashref) {
614 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
615 $child_name = $membership_data{$index}{'Full Name'};
616 $child_name =~ s/\'/\\'/g; #'
617 $birthday = $membership_data{$index}{'Birth'};
618 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
619 $id = $membership_data{$index}{'Indiv ID'};
620 $hofh_id = $membership_data{$index}{'HofH ID'};
622 # Find the family id for this child's HofH_ID.
623 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
624 $sth->execute or die "-E- DB error: $DBI::errstr\n";
625 my @family_data = ();
626 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
627 my $family_rows = scalar @family_data;
628 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
629 else { $family_id = 0; }
631 # Find out how many children have the same name for the same family
632 $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
633 $sth->execute or die "-E- DB error: $DBI::errstr\n";
635 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
636 my $rows = scalar @data;
638 if($rows == 0 && $family_rows > 0) {
639 # No existing records found for this child, make a new entry
640 print " Adding new Child: $child_name\n";
641 $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
642 $sth->execute or die "-E- DB error: $DBI::errstr\n";
643 } elsif($rows == 1 && $family_rows > 0) {
644 # An existing record was found for this child, update it
645 print " Updating existing child: $child_name\n";
646 $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
647 $sth->execute or die "-E- DB error: $DBI::errstr\n";
648 $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
649 $sth->execute or die "-E- DB error: $DBI::errstr\n";
650 $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
651 $sth->execute or die "-E- DB error: $DBI::errstr\n";
652 $sth = $dbh->prepare("update eq_child set indiv_id='$id' where name='$child_name'");
653 $sth->execute or die "-E- DB error: $DBI::errstr\n";
655 # More than one record was found. Error! This shouldn't happen.
656 print " -E- More than one record found ($rows) with same child name: $child_name\n";
665 #+----------------+------------------+------+-----+---------+-------+
666 #| Field | Type | Null | Key | Default | Extra |
667 #+----------------+------------------+------+-----+---------+-------+
668 #| visit | int(16) unsigned | | PRI | 0 | A |
669 #| family | int(16) unsigned | YES | UNI | NULL | |
670 #| companionship | int(16) unsigned | YES | | NULL | |
671 #| date | date | YES | | NULL | |
672 #| notes | varchar(128) | YES | | NULL | |
673 #| visited | varchar(1) | YES | | NULL | |
674 #+----------------+------------------+------+-----+---------+-------+
675 sub update_eq_visit_table
677 print "\n-> updating eq_visit table\n";
679 my $month_header_retrieved = 0;
682 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);
683 ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
684 my %visit_status = ('X', 'y', '-', 'n', '', '');
686 foreach $index (keys %hometeaching_stats_data)
688 $hashref = $hometeaching_stats_data{$index};
689 #foreach $key (keys %$hashref) {print "$key\n";}
691 $family_name = $hometeaching_stats_data{$index}{"Preferred Name"};
692 print " Updating visit data: $family_name\n";
694 # get family id from eq_family
695 $sth = $dbh->prepare("select * from eq_family where name=\"$family_name\" and valid=1");
696 $sth->execute or die "-E- DB error: $DBI::errstr\n";
697 my @family_data = ();
698 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
699 my $family_rows = scalar @family_data;
700 if($family_rows > 0) {
701 $family_id = $family_data[0]->{'family'};
702 $comp_id = $family_data[0]->{'companionship'};
705 #print "family_id = $family_id\n";
706 #print "comp_id = $comp_id\n";
708 # ignore visits that weren't done by the EQ
709 if ($comp_id == 0) { next; }
711 # retrieve the month header if not already done
712 if ($month_header_retrieved == 0)
714 foreach $key (keys %$hashref)
716 if (($key ne "Preferred Name") && ($key ne "Home Teachers"))
718 $month_header = $key;
719 @data_months = split /\t/, $key;
722 $month_header_retrieved = 1;
725 # loop through history data
726 @history = split /\t/, $hometeaching_stats_data{$index}{$month_header};
727 my $data_year = 1900 + $yearOffset;
728 my $data_month = $months{$data_months[-1]};
729 #print "$month_header\n";
730 #print $hometeaching_stats_data{$index}{$month_header};
732 foreach $i (reverse(0..$#history)) {
733 # went back a calendar year, decrement $data_year
734 if ($months{$data_months[$i]} > $data_month)
738 $data_month = $months{$data_months[$i]};
739 my $visit_date = sprintf("%4d-%02d-01\n", $data_year, $data_month);
740 #print "$visit_date\n";
741 my $importing_status = $visit_status{$history[$i]};
742 #print "importing_status = $importing_status\n";
743 #print "select * from eq_visit where family=$family_id and companionship=$comp_id and date='$visit_date'\n";
744 $sth = $dbh->prepare("select * from eq_visit where family=$family_id and companionship=$comp_id and date='$visit_date'");
745 $sth->execute or die "-E- DB error: $DBI::errstr\n";
747 while($sqlhashref = $sth->fetchrow_hashref) { push(@visit_data, $sqlhashref); }
748 my $visit_rows = scalar @visit_data;
749 if($visit_rows > 0) {
750 my $visited = $visit_data[0]->{'visited'};
751 #print "visited = $visited\n";
752 # update visit if data is different in eq_visit
753 if ($visited ne $importing_status)
755 #print "importing_status = $importing_status\n";
756 $sth = $dbh->prepare("update eq_visit set visited='$importing_status' where family='$family_id' and date='$visit_date' and companionship='$comp_id'");
757 $sth->execute or die "-E- DB error: $DBI::errstr\n";
760 if ($importing_status ne '')
762 # add visit if it doesn't exist in eq_visit
763 $sth = $dbh->prepare("insert into eq_visit values (NULL, '$family_id', '$comp_id', '$visit_date', '', '$importing_status')");
764 $sth->execute or die "-E- DB error: $DBI::errstr\n";
771 ######################################################################
772 sub check_for_changed_ids
774 # If the Indiv ID & HofH ID has changed between data sets, we could have problems
775 my ($oldhashref, $newhashref) = @_;
776 my $found_problem = 0;
778 foreach $oldindex (keys %$oldhashref)
780 $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
781 $hofh_id = $oldhashref->{$oldindex}{'HofH ID'};
782 $full_name = $oldhashref->{$oldindex}{'Full Name'};
783 $hh_position = $oldhashref->{$oldindex}{'HH Position'};
784 if($hh_position =~ /Other/i) { next; }
786 foreach $newindex (keys %$newhashref)
788 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
789 $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
791 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
795 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
796 $hofh_id != $newhashref->{$newindex}{'HofH ID'})
798 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
804 return $found_problem;
807 ######################################################################
809 ######################################################################
811 ###################################################
812 # Open a connection to the database
813 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
815 PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
817 ###################################################
818 # Check old directory against new directory to ensure
819 # that the Indiv ID & HofH ID have not changed between updates
821 print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
822 my %old_membership_data = ();
823 my %new_membership_data = ();
824 &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
825 &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
827 $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
831 print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
832 print " $opt_o and $opt_n data sets.\n";
833 print " This script is not currently setup to handle this properly.\n";
835 print " Exiting without updating...\n\n";
840 ###################################################
841 # Process command line options
842 if(defined $opt_n) { $datadir = $opt_n; }
843 else { $datadir = shift(@ARGV); }
844 print "\n-> Processing all ward data files in $datadir\n";
846 ###################################################
847 # Parse Ward Data Files
848 #&optional_csv_to_hash("$datadir/EQ\ Prospective\ Elders.csv", \%prospective_elder_data);
849 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
850 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
851 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
852 &optional_csv_to_hash("$datadir/Home\ Teacher\ per\ Companionship.csv", \%hometeaching_stats_data);
853 %organization_by_name = ();
854 %organization_by_id = ();
857 print "-> Membership Data Dump\n\n";
858 &print_hash(\%membership_data);
859 print "-> HomeTeaching Data Dump\n\n";
860 &print_hash(\%hometeaching_data);
861 print "-> Organization Data Dump\n\n";
862 &print_hash(\%organization_data);
863 print "-> HomeTeaching Stats Data Dump\n\n";
864 &print_hash(\%hometeaching_stats_data);
867 if($opt_s) { $dbh->disconnect(); exit; }
869 # Now update the various eq DB tables
870 &update_eq_calling_table();
871 &update_eq_elder_table();
872 &update_eq_aaronic_table();
873 &update_eq_district_table();
874 &update_eq_companionship_table();
875 &update_eq_family_table();
876 &update_eq_parent_table();
877 &update_eq_child_table();
878 &update_eq_visit_table();
880 print "\n-> Import Successful! DONE...\n";
882 ###################################################
883 # Disconnect from the database
886 ######################################################################