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 $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
203 $sth->execute or die "-E- DB error: $DBI::errstr\n";
205 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
206 my $rows = scalar @data;
208 # No existing records found for this elder, make a new entry
209 print " Adding new Elder: $elder_name\n";
210 $sth = $dbh->prepare("insert into eq_elder values (NULL,'$id','$elder_name','$phone','','1','','1','',$attending,1)");
211 $sth->execute or die "-E- DB error: $DBI::errstr\n";
212 } elsif($rows == 1) {
213 # An existing record was found for this elder, update it
214 print " Updating existing Elder: $elder_name\n";
215 $sth = $dbh->prepare("update eq_elder set valid=1 where name='$elder_name'");
216 $sth->execute or die "-E- DB error: $DBI::errstr\n";
218 $sth = $dbh->prepare("update eq_elder set phone='$phone' where name='$elder_name'");
220 $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_name'");
222 $sth->execute or die "-E- DB error: $DBI::errstr\n";
223 $sth = $dbh->prepare("update eq_elder set attending='$attending' where name='$elder_name'");
224 $sth->execute or die "-E- DB error: $DBI::errstr\n";
225 $sth = $dbh->prepare("update eq_elder set indiv_id='$id' where name='$elder_name'");
226 $sth->execute or die "-E- DB error: $DBI::errstr\n";
228 # More than one record was found. Error! This shouldn't happen.
229 print " -E- More than one record found ($rows) for Elder: $elder_name\n";
238 #+--------------+------------------+------+-----+---------+-------+
239 #| Field | Type | Null | Key | Default | Extra |
240 #+--------------+------------------+------+-----+---------+-------+
241 #| indiv_id | int(16) unsigned | YES | | NULL | |
242 #| name | varchar(30) | YES | | NULL | |
243 #| organization | varchar(30) | YES | | NULL | |
244 #| position | varchar(30) | YES | | NULL | |
245 #| sequence | int(16) unsigned | YES | | NULL | |
246 #| sustained | date | YES | | NULL | |
247 #+--------------+------------------+------+-----+---------+-------+
248 sub update_eq_calling_table()
250 print "\n-> Updating eq_calling table\n";
252 #print "-> Organization Data Dump\n\n";
253 #&print_hash(\%organization_data);
255 # Delete all records from the calling table. We have no history to
256 # save here. Just re-populate with the latest calling information.
257 $sth = $dbh->prepare("delete from eq_calling ");
258 $sth->execute or die "-E- DB error: $DBI::errstr\n";
260 foreach $index (keys %organization_data)
262 $indiv_id = $organization_data{$index}{'Indiv ID'};
263 $name = $organization_data{$index}{'Indiv Name'};
264 $name =~ s/\'/\\'/g; #'
265 $organization = $organization_data{$index}{'Organization'};
266 $organization_by_name{$name} = $organization;
267 $organization_by_id{$indiv_id} = $organization;
268 $position = $organization_data{$index}{'Position'};
269 $sequence = $organization_data{$index}{'Org Seq'};
270 $sustained = $organization_data{$index}{'Sustained'};
271 $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
272 if($name eq "") { next; }
273 print " Adding new Calling: $name -> $position\n";
274 $sth = $dbh->prepare("insert into eq_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
275 $sth->execute or die "-E- DB error: $DBI::errstr\n";
280 #+------------+------------------+------+-----+---------+-------+
281 #| Field | Type | Null | Key | Default | Extra |
282 #+------------+------------------+------+-----+---------+-------+
283 #| district | int(16) unsigned | | PRI | 0 | |
284 #| name | varchar(30) | YES | | NULL | |
285 #| supervisor | int(16) unsigned | YES | | NULL | |
286 #| valid | tinyint(1) | YES | | NULL | |
287 #+------------+------------------+------+-----+---------+-------+
288 sub update_eq_district_table
290 # Districts should be created by hand. This subroutine only
291 # updates the supervisor's ID in each district.
292 print "\n-> Updating eq_district table\n";
293 $sth = $dbh->prepare("select * from eq_district");
294 $sth->execute or die "-E- DB error: $DBI::errstr\n";
295 while($sqlhashref = $sth->fetchrow_hashref) {
296 $supervisor_name = $sqlhashref->{name};
297 $district = $sqlhashref->{district};
298 $sth2 = $dbh->prepare("select * from eq_elder where name='$supervisor_name'");
299 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
300 $sqlhashref2 = $sth2->fetchrow_hashref;
301 $supervisor_id = $sqlhashref2->{elder};
303 $sth2 = $dbh->prepare("update eq_district set supervisor='$supervisor_id' where district='$district'");
304 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
311 #+---------------+------------------+------+-----+---------+-------+
312 #| Field | Type | Null | Key | Default | Extra |
313 #+---------------+------------------+------+-----+---------+-------+
314 #| companionship | int(16) unsigned | | | 0 | |
315 #| elder | int(16) unsigned | YES | | NULL | |
316 #| aaronic | int(16) unsigned | YES | | NULL | |
317 #| district | int(16) unsigned | YES | | NULL | |
318 #| valid | tinyint(1) | YES | | NULL | |
319 #+---------------+------------------+------+-----+---------+-------+
320 sub update_eq_companionship_table
322 print "\n-> Updating eq_companionship table\n";
324 # First, mark all existing companionships as invalid in case they have been dissolved
325 $sth = $dbh->prepare("update eq_companionship set valid=0");
326 $sth->execute or die "-E- DB error: $DBI::errstr\n";
327 # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
328 $sth = $dbh->prepare("update eq_aaronic set valid=0");
329 $sth->execute or die "-E- DB error: $DBI::errstr\n";
331 foreach $index (keys %hometeaching_data)
333 $hashref = $hometeaching_data{$index};
334 foreach $key (keys %$hashref) {
335 if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
336 foreach $field ("Home Teacher 1","Home Teacher 2") {
337 $elder_name = $hometeaching_data{$index}{$field};
338 if($elder_name eq "") { next; }
339 $sth2 = $dbh->prepare("select * from eq_elder where name='$elder_name'");
340 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
341 $sqlhashref2 = $sth2->fetchrow_hashref;
342 $elder = $sqlhashref2->{elder};
345 $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_name'");
346 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
347 $sqlhashref2 = $sth2->fetchrow_hashref;
348 $aaronic = $sqlhashref2->{aaronic};
350 if($aaronic eq "") { print "-W- Unable to find $elder_name in eq_elder or eq_aaronic tables\n"; next; }
352 $id = $hometeaching_data{$index}{'Comp ID'};
353 $district = $hometeaching_data{$index}{'HT District'};
354 $sth = $dbh->prepare("select * from eq_companionship where elder='$elder' and aaronic='$aaronic' and companionship='$id'");
355 $sth->execute or die "-E- DB error: $DBI::errstr\n";
357 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
358 my $rows = scalar @data;
360 # No existing records found for this companionship, make a new entry
361 print " Adding Companion to companionship: $elder_name -> $id\n";
362 $sth = $dbh->prepare("insert into eq_companionship values ($id,'$elder','$aaronic','$district',1)");
363 $sth->execute or die "-E- DB error: $DBI::errstr\n";
365 # An existing companionship was found for this companionship, update it
366 $sth2 = $dbh->prepare("select * from eq_companionship where district='$district' and companionship='$id'");
367 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
368 if($elder ne "NULL") {
369 print " Updating Companionship with Elder: $elder_name ($elder) -> $id\n";
370 $sth = $dbh->prepare("update eq_companionship set district='$district' where elder='$elder' and companionship='$id'");
371 $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
372 $sth = $dbh->prepare("update eq_companionship set elder='$elder' where elder='$elder' and companionship='$id'");
373 $sth->execute or die "-E- DB error 'elder': $DBI::errstr\n";
374 $sth = $dbh->prepare("update eq_companionship set valid=1 where elder='$elder' and companionship='$id'");
375 $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
377 print " Updating Companionship with Aaronic: $elder_name ($aaronic) -> $id\n";
378 $sth = $dbh->prepare("update eq_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
379 $sth->execute or die "-E- DB error: $DBI::errstr\n";
380 $sth = $dbh->prepare("update eq_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
381 $sth->execute or die "-E- DB error: $DBI::errstr\n";
382 $sth = $dbh->prepare("update eq_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
383 $sth->execute or die "-E- DB error: $DBI::errstr\n";
384 $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
385 $sth->execute or die "-E- DB error: $DBI::errstr\n";
397 #+---------------+------------------+------+-----+---------+-------+
398 #| Field | Type | Null | Key | Default | Extra |
399 #+---------------+------------------+------+-----+---------+-------+
400 #| family | int(16) unsigned | | PRI | 0 | A |
401 #| hofh_id | int(16) unsigned | YES | | NULL | |
402 #| name | varchar(30) | YES | | NULL | |
403 #| name_id | varchar(30) | YES | | NULL | |
404 #| elder_id | int(16) unsigned | YES | | NULL | |
405 #| companionship | int(16) unsigned | YES | | NULL | |
406 #| visit_pri | int(10) unsigned | YES | | 1 | |
407 #| visit_notes | varchar(128) | YES | | NULL | |
408 #| valid | tinyint(1) | YES | | NULL | |
409 #+---------------+------------------+------+-----+---------+-------+
410 sub update_eq_family_table
412 print "\n-> Updating eq_family table\n";
414 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
415 $sth = $dbh->prepare("update eq_family set valid=0");
416 $sth->execute or die "-E- DB error: $DBI::errstr\n";
417 $sth = $dbh->prepare("update eq_family set companionship=0");
418 $sth->execute or die "-E- DB error: $DBI::errstr\n";
420 foreach $index (keys %membership_data)
422 $hashref = $membership_data{$index};
423 foreach $key (keys %$hashref) {
424 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
425 $family_name = $membership_data{$index}{'Preferred Name'};
426 $family_name =~ s/\'/\\'/g; #'
427 $id = $membership_data{$index}{'HofH ID'};
428 $name_id = uc($family_name);
430 # Find out how many families match this family's name
431 $sth = $dbh->prepare("select * from eq_family where name_id='$name_id'");
432 $sth->execute or die "-E- DB error: $DBI::errstr\n";
434 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
435 my $rows = scalar @data;
438 # No existing records found for this family, make a new entry
439 print " Adding new Family: $family_name\n";
440 $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','$name_id','0','0','1','',1)");
441 $sth->execute or die "-E- DB error: $DBI::errstr\n";
442 } elsif($rows == 1) {
443 # An existing record was found for this family, update it
444 print " Updating existing family: $family_name\n";
445 $sth = $dbh->prepare("update eq_family set hofh_id=$id where name_id='$name_id'");
446 $sth->execute or die "-E- DB error: $DBI::errstr\n";
447 $sth = $dbh->prepare("update eq_family set valid=1 where name_id='$name_id'");
448 $sth->execute or die "-E- DB error: $DBI::errstr\n";
450 # More than one record was found. Error! This shouldn't happen.
451 print " -E- More than one record found ($rows) for family name: $family_name\n";
454 # Now update the elder_id field for this family
455 $sth = $dbh->prepare("select * from eq_elder WHERE name='$family_name'");
456 $sth->execute or die "-E- DB error: $DBI::errstr\n";
457 while($sqlhashref = $sth->fetchrow_hashref) {
458 $elder_id = $sqlhashref->{elder};
459 print " Updating family elder_id: $family_name -> $elder_id\n";
460 $sth = $dbh->prepare("update eq_family set elder_id=$elder_id where name_id='$name_id'");
461 $sth->execute or die "-E- DB error: $DBI::errstr\n";
464 # Now update the hometeaching field for this family
465 foreach $index (keys %hometeaching_data)
467 $hashref = $hometeaching_data{$index};
468 foreach $key (keys %$hashref) {
469 if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
470 $a = $1; $b = $2; $c = $3; $d = $4;
471 if($family_name =~ /$a/ && $hometeaching_data{$index}{'Household'} !~ /$family_name/i) {
472 print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $a, $c $d\n";
473 $hometeaching_data{$index}{'Household'} = "$a, $c $d";
476 if($key =~ /Quorum/i &&
477 $hometeaching_data{$index}{$key} =~ /Elders/i &&
478 $hometeaching_data{$index}{'Household'} =~ /$family_name/i &&
479 $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
482 print " Updating hometeaching assignment for $family_name family\n";
483 $companionship = $hometeaching_data{$index}{'Comp ID'};
484 $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name_id='$name_id'");
485 $sth->execute or die "-E- DB error: $DBI::errstr\n";
496 #+----------+------------------+------+-----+---------+-------+
497 #| Field | Type | Null | Key | Default | Extra |
498 #+----------+------------------+------+-----+---------+-------+
499 #| parent | int(16) unsigned | | PRI | 0 | A |
500 #| family | int(16) unsigned | YES | | NULL | |
501 #| name | varchar(30) | YES | | NULL | |
502 #| birthday | date | YES | | NULL | |
503 #| phone | varchar(12) | YES | | NULL | |
504 #| address | varchar(255) | YES | | NULL | |
505 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
506 #| valid | tinyint(1) | YES | | NULL | |
507 #+----------+------------------+------+-----+---------+-------+
508 sub update_eq_parent_table
510 print "\n-> Updating eq_parent table\n";
512 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
513 $sth = $dbh->prepare("update eq_parent set valid=0");
514 $sth->execute or die "-E- DB error: $DBI::errstr\n";
516 foreach $index (keys %membership_data)
518 $hashref = $membership_data{$index};
519 foreach $key (keys %$hashref) {
520 if($key =~ /HH Position/i &&
521 $membership_data{$index}{$key} =~ /Head of Household/i ||
522 $membership_data{$index}{$key} =~ /Spouse/i
524 # Get some information from the hash about this parent
525 $parent_name = $membership_data{$index}{'Preferred Name'};
526 $parent_name =~ s/\'/\\'/g; #'
527 $birthday = $membership_data{$index}{'Birth'};
528 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
529 $hofh_id = $membership_data{$index}{'HofH ID'};
530 $id = $membership_data{$index}{'Indiv ID'};
531 $phone = $membership_data{$index}{'Household Phone'};
532 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
533 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
534 $address = $membership_data{$index}{'Street 1'};
535 if($membership_data{$index}{'Street 2'} ne "") {
536 $address .= " " . $membership_data{$index}{'Street 2'};
539 # Find the family id for this parent's HofH_ID.
540 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
541 $sth->execute or die "-E- DB error: $DBI::errstr\n";
542 my @family_data = ();
543 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
544 my $family_rows = scalar @family_data;
545 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
546 else { $family_id = 0; }
548 # Find out how many parents match this parent's name
549 $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
550 $sth->execute or die "-E- DB error: $DBI::errstr\n";
552 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
553 my $rows = scalar @data;
555 if($rows == 0 && $family_rows > 0) {
556 # No existing records found for this parent, make a new entry
557 print " Adding new Parent: $parent_name\n";
558 $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
559 $sth->execute or die "-E- DB error: $DBI::errstr\n";
560 } elsif($rows == 1 && $family_rows > 0) {
561 # An existing record was found for this parent, update it
562 print " Updating existing parent: $parent_name\n";
563 $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
564 $sth->execute or die "-E- DB error: $DBI::errstr\n";
565 $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
566 $sth->execute or die "-E- DB error: $DBI::errstr\n";
567 $sth = $dbh->prepare("update eq_parent set phone='$phone' where name='$parent_name'");
568 $sth->execute or die "-E- DB error: $DBI::errstr\n";
569 $sth = $dbh->prepare("update eq_parent set address='$address' where name='$parent_name'");
570 $sth->execute or die "-E- DB error: $DBI::errstr\n";
571 $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
572 $sth->execute or die "-E- DB error: $DBI::errstr\n";
573 $sth = $dbh->prepare("update eq_parent set indiv_id='$id' where name='$parent_name'");
574 $sth->execute or die "-E- DB error: $DBI::errstr\n";
576 # More than one record was found. Error! This shouldn't happen.
577 print " -E- More than one record found with same parent name: $parent_name with hofh_id: $hofh_id\n";
579 print " -E- Unable to find a family to attach this parent to: $parent_name with hofh_id: $hofh_id\n";
588 #+----------+------------------+------+-----+---------+-------+
589 #| Field | Type | Null | Key | Default | Extra |
590 #+----------+------------------+------+-----+---------+-------+
591 #| child | int(16) unsigned | | PRI | 0 | A |
592 #| family | int(16) unsigned | YES | | NULL | |
593 #| name | varchar(30) | YES | | NULL | |
594 #| birthday | date | YES | | NULL | |
595 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
596 #| valid | tinyint(1) | YES | | NULL | |
597 #+----------+------------------+------+-----+---------+-------+
598 sub update_eq_child_table
600 print "\n-> Updating eq_child table\n";
602 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
603 $sth = $dbh->prepare("update eq_child set valid=0");
604 $sth->execute or die "-E- DB error: $DBI::errstr\n";
606 foreach $index (keys %membership_data)
608 $hashref = $membership_data{$index};
609 foreach $key (keys %$hashref) {
610 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
611 $child_name = $membership_data{$index}{'Full Name'};
612 $child_name =~ s/\'/\\'/g; #'
613 $birthday = $membership_data{$index}{'Birth'};
614 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
615 $id = $membership_data{$index}{'Indiv ID'};
616 $hofh_id = $membership_data{$index}{'HofH ID'};
618 # Find the family id for this child's HofH_ID.
619 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
620 $sth->execute or die "-E- DB error: $DBI::errstr\n";
621 my @family_data = ();
622 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
623 my $family_rows = scalar @family_data;
624 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
625 else { $family_id = 0; }
627 # Find out how many children have the same name for the same family
628 $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
629 $sth->execute or die "-E- DB error: $DBI::errstr\n";
631 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
632 my $rows = scalar @data;
634 if($rows == 0 && $family_rows > 0) {
635 # No existing records found for this child, make a new entry
636 print " Adding new Child: $child_name\n";
637 $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
638 $sth->execute or die "-E- DB error: $DBI::errstr\n";
639 } elsif($rows == 1 && $family_rows > 0) {
640 # An existing record was found for this child, update it
641 print " Updating existing child: $child_name\n";
642 $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
643 $sth->execute or die "-E- DB error: $DBI::errstr\n";
644 $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
645 $sth->execute or die "-E- DB error: $DBI::errstr\n";
646 $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
647 $sth->execute or die "-E- DB error: $DBI::errstr\n";
648 $sth = $dbh->prepare("update eq_child set indiv_id='$id' where name='$child_name'");
649 $sth->execute or die "-E- DB error: $DBI::errstr\n";
651 # More than one record was found. Error! This shouldn't happen.
652 print " -E- More than one record found ($rows) with same child name: $child_name\n";
661 #+----------------+------------------+------+-----+---------+-------+
662 #| Field | Type | Null | Key | Default | Extra |
663 #+----------------+------------------+------+-----+---------+-------+
664 #| visit | int(16) unsigned | | PRI | 0 | A |
665 #| family | int(16) unsigned | YES | UNI | NULL | |
666 #| companionship | int(16) unsigned | YES | | NULL | |
667 #| date | date | YES | | NULL | |
668 #| notes | varchar(128) | YES | | NULL | |
669 #| visited | varchar(1) | YES | | NULL | |
670 #+----------------+------------------+------+-----+---------+-------+
671 sub update_eq_visit_table
673 print "\n-> updating eq_visit table\n";
675 my $month_header_retrieved = 0;
678 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);
679 ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
680 my %visit_status = ('X', 'y', '-', 'n', '', '');
682 foreach $index (keys %hometeaching_stats_data)
684 $hashref = $hometeaching_stats_data{$index};
685 #foreach $key (keys %$hashref) {print "$key\n";}
687 $family_name = $hometeaching_stats_data{$index}{"Preferred Name"};
688 print " Updating visit data: $family_name\n";
690 # get family id from eq_family
691 $sth = $dbh->prepare("select * from eq_family where name=\"$family_name\" and valid=1");
692 $sth->execute or die "-E- DB error: $DBI::errstr\n";
693 my @family_data = ();
694 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
695 my $family_rows = scalar @family_data;
696 if($family_rows > 0) {
697 $family_id = $family_data[0]->{'family'};
698 $comp_id = $family_data[0]->{'companionship'};
701 #print "family_id = $family_id\n";
702 #print "comp_id = $comp_id\n";
704 # ignore visits that weren't done by the EQ
705 if ($comp_id == 0) { next; }
707 # retrieve the month header if not already done
708 if ($month_header_retrieved == 0)
710 foreach $key (keys %$hashref)
712 if (($key ne "Preferred Name") && ($key ne "Home Teachers"))
714 $month_header = $key;
715 @data_months = split /\t/, $key;
718 $month_header_retrieved = 1;
721 # loop through history data
722 @history = split /\t/, $hometeaching_stats_data{$index}{$month_header};
723 my $data_year = 1900 + $yearOffset;
724 my $data_month = $months{$data_months[-1]};
725 #print "$month_header\n";
726 #print $hometeaching_stats_data{$index}{$month_header};
728 foreach $i (reverse(0..$#history)) {
729 # went back a calendar year, decrement $data_year
730 if ($months{$data_months[$i]} > $data_month)
734 $data_month = $months{$data_months[$i]};
735 my $visit_date = sprintf("%4d-%02d-01\n", $data_year, $data_month);
736 #print "$visit_date\n";
737 my $importing_status = $visit_status{$history[$i]};
738 #print "importing_status = $importing_status\n";
739 #print "select * from eq_visit where family=$family_id and companionship=$comp_id and date='$visit_date'\n";
740 $sth = $dbh->prepare("select * from eq_visit where family=$family_id and companionship=$comp_id and date='$visit_date'");
741 $sth->execute or die "-E- DB error: $DBI::errstr\n";
743 while($sqlhashref = $sth->fetchrow_hashref) { push(@visit_data, $sqlhashref); }
744 my $visit_rows = scalar @visit_data;
745 if($visit_rows > 0) {
746 my $visited = $visit_data[0]->{'visited'};
747 #print "visited = $visited\n";
748 # update visit if data is different in eq_visit
749 if ($visited ne $importing_status)
751 #print "importing_status = $importing_status\n";
752 $sth = $dbh->prepare("update eq_visit set visited='$importing_status' where family='$family_id' and date='$visit_date' and companionship='$comp_id'");
753 $sth->execute or die "-E- DB error: $DBI::errstr\n";
756 if ($importing_status ne '')
758 # add visit if it doesn't exist in eq_visit
759 $sth = $dbh->prepare("insert into eq_visit values (NULL, '$family_id', '$comp_id', '$visit_date', '', '$importing_status')");
760 $sth->execute or die "-E- DB error: $DBI::errstr\n";
767 ######################################################################
768 sub check_for_changed_ids
770 # If the Indiv ID & HofH ID has changed between data sets, we could have problems
771 my ($oldhashref, $newhashref) = @_;
772 my $found_problem = 0;
774 foreach $oldindex (keys %$oldhashref)
776 $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
777 $hofh_id = $oldhashref->{$oldindex}{'HofH ID'};
778 $full_name = $oldhashref->{$oldindex}{'Full Name'};
779 $hh_position = $oldhashref->{$oldindex}{'HH Position'};
780 if($hh_position =~ /Other/i) { next; }
782 foreach $newindex (keys %$newhashref)
784 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
785 $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
787 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
791 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
792 $hofh_id != $newhashref->{$newindex}{'HofH ID'})
794 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
800 return $found_problem;
803 ######################################################################
805 ######################################################################
807 ###################################################
808 # Open a connection to the database
809 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
811 PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
813 ###################################################
814 # Check old directory against new directory to ensure
815 # that the Indiv ID & HofH ID have not changed between updates
817 print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
818 my %old_membership_data = ();
819 my %new_membership_data = ();
820 &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
821 &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
823 $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
827 print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
828 print " $opt_o and $opt_n data sets.\n";
829 print " This script is not currently setup to handle this properly.\n";
831 print " Exiting without updating...\n\n";
836 ###################################################
837 # Process command line options
838 if(defined $opt_n) { $datadir = $opt_n; }
839 else { $datadir = shift(@ARGV); }
840 print "\n-> Processing all ward data files in $datadir\n";
842 ###################################################
843 # Parse Ward Data Files
844 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
845 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
846 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
847 &optional_csv_to_hash("$datadir/Home\ Teacher\ per\ Companionship.csv", \%hometeaching_stats_data);
848 %organization_by_name = ();
849 %organization_by_id = ();
852 print "-> Membership Data Dump\n\n";
853 &print_hash(\%membership_data);
854 print "-> HomeTeaching Data Dump\n\n";
855 &print_hash(\%hometeaching_data);
856 print "-> Organization Data Dump\n\n";
857 &print_hash(\%organization_data);
858 print "-> HomeTeaching Stats Data Dump\n\n";
859 &print_hash(\%hometeaching_stats_data);
862 if($opt_s) { $dbh->disconnect(); exit; }
864 # Now update the various eq DB tables
865 &update_eq_calling_table();
866 &update_eq_elder_table();
867 &update_eq_aaronic_table();
868 &update_eq_district_table();
869 &update_eq_companionship_table();
870 &update_eq_family_table();
871 &update_eq_parent_table();
872 &update_eq_child_table();
873 &update_eq_visit_table();
875 print "\n-> Import Successful! DONE...\n";
877 ###################################################
878 # Disconnect from the database
881 ######################################################################