5 ###################################################
7 $dbname = "phpgroupware";
8 $dbhost = "192.168.0.2";
10 $dbuser = "phpgroupware";
11 $dbpass = "phpgroupware";
12 %hometeaching_data = ();
13 %membership_data = ();
16 $monthname2num{'Jan'} = '01';
17 $monthname2num{'Feb'} = '02';
18 $monthname2num{'Mar'} = '03';
19 $monthname2num{'Apr'} = '04';
20 $monthname2num{'May'} = '05';
21 $monthname2num{'Jun'} = '06';
22 $monthname2num{'Jul'} = '07';
23 $monthname2num{'Aug'} = '08';
24 $monthname2num{'Sep'} = '09';
25 $monthname2num{'Oct'} = '10';
26 $monthname2num{'Nov'} = '11';
27 $monthname2num{'Dec'} = '12';
29 ######################################################################
31 ######################################################################
34 my ($filename, $hashref) = @_;
36 open(FILE,$filename) || die "-E- Could not open $filename for reading\n";
38 my $found_header = 0; my $index = 0;
42 @data = split /\",/, $line;
43 if(!$found_header) { @header = @data; $found_header = 1; }
45 foreach $i (0..$#data-1) {
47 $header[$i] =~ s/\"//g;
48 $hashref->{$index}{$header[$i]} = $data[$i];
49 #print "$index: $i: $header[$i]: $data[$i]\n";
58 ######################################################################
63 foreach $key (sort {$a <=> $b} keys %$hashref) {
64 print "Index: $key\n";
65 foreach $field (keys %{$hashref->{$key}}) {
66 print "$field: $hashref->{$key}{$field}\n";
72 ######################################################################
75 #+-------+--------------------+------+-----+---------+-------+
76 #| Field | Type | Null | Key | Default | Extra |
77 #+-------+--------------------+------+-----+---------+-------+
78 #| aaronic | int(16) unsigned | | PRI | 0 | A |
79 #| name | varchar(60) | YES | | NULL | |
80 #| phone | varchar(12) | YES | | NULL | |
81 #| valid | tinyint(1) | YES | | NULL | |
82 #+-------+--------------------+------+-----+---------+-------+
83 sub update_eq_aaronic_table
85 print "-> Updating eq_aaronic table\n";
87 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
88 $sth = $dbh->prepare("update eq_aaronic set valid=0");
89 $sth->execute or die "-E- DB error: $DBI::errstr\n";
91 foreach $index (keys %membership_data)
93 $hashref = $membership_data{$index};
94 foreach $key (keys %$hashref) {
95 if($key =~ /Priesthood/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 | 0 | A |
134 #| name | varchar(60) | YES | | NULL | |
135 #| phone | varchar(12) | YES | | NULL | |
136 #| valid | tinyint(1) | YES | | NULL | |
137 #+------------+------------------+------+-----+---------+-------+
138 sub update_eq_elder_table
140 print "-> Updating eq_elder table\n";
142 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
143 $sth = $dbh->prepare("update eq_elder set valid=0");
144 $sth->execute or die "-E- DB error: $DBI::errstr\n";
146 foreach $index (keys %membership_data)
148 $hashref = $membership_data{$index};
149 foreach $key (keys %$hashref) {
150 if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
151 $elder_name = $membership_data{$index}{'Preferred Name'};
152 $phone = $membership_data{$index}{'Phone 1'};
153 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
154 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
155 $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
156 $sth->execute or die "-E- DB error: $DBI::errstr\n";
158 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
159 my $rows = scalar @data;
161 # No existing records found for this elder, make a new entry
162 print " Adding new Elder: $elder_name\n";
163 $sth = $dbh->prepare("insert into eq_elder values (NULL,'$elder_name','$phone',1)");
164 $sth->execute or die "-E- DB error: $DBI::errstr\n";
165 } elsif($rows == 1) {
166 # An existing record was found for this elder, update it
167 print " Updating existing Elder: $elder_name\n";
168 $sth = $dbh->prepare("update eq_elder set valid=1 where name='$elder_name'");
169 $sth->execute or die "-E- DB error: $DBI::errstr\n";
171 $sth = $dbh->prepare("update eq_elder set phone='$phone' where name='$elder_name'");
173 $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_name'");
175 $sth->execute or die "-E- DB error: $DBI::errstr\n";
177 # More than one record was found. Error! This shouldn't happen.
178 print " -E- More than one record found ($rows) for Elder: $elder_name\n";
187 #+--------------+------------------+------+-----+---------+-------+
188 #| Field | Type | Null | Key | Default | Extra |
189 #+--------------+------------------+------+-----+---------+-------+
190 #| indiv_id | int(16) unsigned | YES | | NULL | |
191 #| name | varchar(30) | YES | | NULL | |
192 #| organization | varchar(30) | YES | | NULL | |
193 #| position | varchar(30) | YES | | NULL | |
194 #| sequence | int(16) unsigned | YES | | NULL | |
195 #| sustained | date | YES | | NULL | |
196 #+--------------+------------------+------+-----+---------+-------+
197 sub update_eq_calling_table()
199 print "-> Updating eq_calling table\n";
201 #print "-> Organization Data Dump\n\n";
202 #&print_hash(\%organization_data);
204 # Delete all records from the calling table. We have no history to
205 # save here. Just re-populate with the latest calling information.
206 $sth = $dbh->prepare("delete from eq_calling ");
207 $sth->execute or die "-E- DB error: $DBI::errstr\n";
209 foreach $index (keys %organization_data)
211 $indiv_id = $organization_data{$index}{'Indiv ID'};
212 $name = $organization_data{$index}{'Indiv Name'};
213 $name =~ s/\'/\\'/g; #'
214 $organization = $organization_data{$index}{'Organization'};
215 $position = $organization_data{$index}{'Position'};
216 $sequence = $organization_data{$index}{'Org Seq'};
217 $sustained = $organization_data{$index}{'Sustained'};
218 $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
219 if($name eq "") { next; }
220 print " Adding new Calling: $name -> $position\n";
221 $sth = $dbh->prepare("insert into eq_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
222 $sth->execute or die "-E- DB error: $DBI::errstr\n";
227 #+------------+------------------+------+-----+---------+-------+
228 #| Field | Type | Null | Key | Default | Extra |
229 #+------------+------------------+------+-----+---------+-------+
230 #| district | int(16) unsigned | | PRI | 0 | |
231 #| name | varchar(30) | YES | | NULL | |
232 #| supervisor | int(16) unsigned | YES | | NULL | |
233 #| valid | tinyint(1) | YES | | NULL | |
234 #+------------+------------------+------+-----+---------+-------+
235 sub update_eq_district_table
237 # Districts should be created by hand. This subroutine only
238 # updates the supervisor's ID in each district.
239 print "-> Updating eq_district table\n";
240 $sth = $dbh->prepare("select * from eq_district");
241 $sth->execute or die "-E- DB error: $DBI::errstr\n";
242 while($sqlhashref = $sth->fetchrow_hashref) {
243 $supervisor_name = $sqlhashref->{name};
244 $district = $sqlhashref->{district};
245 $sth2 = $dbh->prepare("select * from eq_elder where name='$supervisor_name'");
246 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
247 $sqlhashref2 = $sth2->fetchrow_hashref;
248 $supervisor_id = $sqlhashref2->{elder};
250 $sth2 = $dbh->prepare("update eq_district set supervisor='$supervisor_id' where district='$district'");
251 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
258 #+---------------+------------------+------+-----+---------+-------+
259 #| Field | Type | Null | Key | Default | Extra |
260 #+---------------+------------------+------+-----+---------+-------+
261 #| companionship | int(16) unsigned | | | 0 | |
262 #| elder | int(16) unsigned | YES | | NULL | |
263 #| aaronic | int(16) unsigned | YES | | NULL | |
264 #| district | int(16) unsigned | YES | | NULL | |
265 #| valid | tinyint(1) | YES | | NULL | |
266 #+---------------+------------------+------+-----+---------+-------+
267 sub update_eq_companionship_table
269 print "-> Updating eq_companionship table\n";
271 # First, mark all existing companionships as invalid in case they have been dissolved
272 $sth = $dbh->prepare("update eq_companionship set valid=0");
273 $sth->execute or die "-E- DB error: $DBI::errstr\n";
274 # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
275 $sth = $dbh->prepare("update eq_aaronic set valid=0");
276 $sth->execute or die "-E- DB error: $DBI::errstr\n";
278 foreach $index (keys %hometeaching_data)
280 $hashref = $hometeaching_data{$index};
281 foreach $key (keys %$hashref) {
282 if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
283 foreach $field ("Home Teacher 1","Home Teacher 2") {
284 $elder_name = $hometeaching_data{$index}{$field};
285 if($elder_name eq "") { next; }
286 $sth2 = $dbh->prepare("select * from eq_elder where name='$elder_name'");
287 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
288 $sqlhashref2 = $sth2->fetchrow_hashref;
289 $elder = $sqlhashref2->{elder};
292 $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_name'");
293 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
294 $sqlhashref2 = $sth2->fetchrow_hashref;
295 $aaronic = $sqlhashref2->{aaronic};
297 if($aaronic eq "") { print "-W- Unable to find $elder_name in eq_elder or eq_aaronic tables\n"; next; }
299 $id = $hometeaching_data{$index}{'Comp ID'};
300 $district = $hometeaching_data{$index}{'HT District'};
301 $sth = $dbh->prepare("select * from eq_companionship where elder='$elder' and aaronic='$aaronic' and companionship='$id'");
302 $sth->execute or die "-E- DB error: $DBI::errstr\n";
304 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
305 my $rows = scalar @data;
307 # No existing records found for this companionship, make a new entry
308 print " Adding Companion to companionship: $elder_name -> $id\n";
309 $sth = $dbh->prepare("insert into eq_companionship values ($id,'$elder','$aaronic','$district',1)");
310 $sth->execute or die "-E- DB error: $DBI::errstr\n";
312 # An existing companionship was found for this companionship, update it
313 $sth2 = $dbh->prepare("select * from eq_companionship where district='$district' and companionship='$id'");
314 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
315 if($elder ne "NULL") {
316 print " Updating Companionship with Elder: $elder_name ($elder) -> $id\n";
317 $sth = $dbh->prepare("update eq_companionship set district='$district' where elder='$elder' and companionship='$id'");
318 $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
319 $sth = $dbh->prepare("update eq_companionship set elder='$elder' where elder='$elder' and companionship='$id'");
320 $sth->execute or die "-E- DB error 'elder': $DBI::errstr\n";
321 $sth = $dbh->prepare("update eq_companionship set valid=1 where elder='$elder' and companionship='$id'");
322 $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
324 print " Updating Companionship with Aaronic: $elder_name ($aaronic) -> $id\n";
325 $sth = $dbh->prepare("update eq_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
326 $sth->execute or die "-E- DB error: $DBI::errstr\n";
327 $sth = $dbh->prepare("update eq_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
328 $sth->execute or die "-E- DB error: $DBI::errstr\n";
329 $sth = $dbh->prepare("update eq_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
330 $sth->execute or die "-E- DB error: $DBI::errstr\n";
331 $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
332 $sth->execute or die "-E- DB error: $DBI::errstr\n";
344 #+---------------+------------------+------+-----+---------+-------+
345 #| Field | Type | Null | Key | Default | Extra |
346 #+---------------+------------------+------+-----+---------+-------+
347 #| family | int(16) unsigned | | PRI | 0 | A |
348 #| hofh_id | int(16) unsigned | YES | | NULL | |
349 #| name | varchar(30) | YES | | NULL | |
350 #| name_id | varchar(30) | YES | | NULL | |
351 #| companionship | int(16) unsigned | YES | | NULL | |
352 #| valid | tinyint(1) | YES | | NULL | |
353 #+---------------+------------------+------+-----+---------+-------+
354 sub update_eq_family_table
356 print "-> Updating eq_family table\n";
358 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
359 $sth = $dbh->prepare("update eq_family set valid=0");
360 $sth->execute or die "-E- DB error: $DBI::errstr\n";
361 $sth = $dbh->prepare("update eq_family set companionship=0");
362 $sth->execute or die "-E- DB error: $DBI::errstr\n";
364 foreach $index (keys %membership_data)
366 $hashref = $membership_data{$index};
367 foreach $key (keys %$hashref) {
368 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
369 $family_name = $membership_data{$index}{'Preferred Name'};
370 $family_name =~ s/\'/\\'/g; #'
371 $id = $membership_data{$index}{'HofH ID'};
372 $name_id = uc($family_name);
374 # Find out how many families match this family's name
375 $sth = $dbh->prepare("select * from eq_family where name_id='$name_id'");
376 $sth->execute or die "-E- DB error: $DBI::errstr\n";
378 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
379 my $rows = scalar @data;
382 # No existing records found for this family, make a new entry
383 print " Adding new Family: $family_name\n";
384 $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','$name_id','0',1)");
385 $sth->execute or die "-E- DB error: $DBI::errstr\n";
386 } elsif($rows == 1) {
387 # An existing record was found for this family, update it
388 print " Updating existing family: $family_name\n";
389 $sth = $dbh->prepare("update eq_family set hofh_id=$id where name_id='$name_id'");
390 $sth->execute or die "-E- DB error: $DBI::errstr\n";
391 $sth = $dbh->prepare("update eq_family set valid=1 where name_id='$name_id'");
392 $sth->execute or die "-E- DB error: $DBI::errstr\n";
394 # More than one record was found. Error! This shouldn't happen.
395 print " -E- More than one record found ($rows) for family name: $family_name\n";
397 # Now update the hometeaching field for this family
398 foreach $index (keys %hometeaching_data)
400 $hashref = $hometeaching_data{$index};
401 foreach $key (keys %$hashref) {
402 if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
403 print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $1, $3 $4\n";
404 $hometeaching_data{$index}{'Household'} = "$1, $3 $4";
406 if($key =~ /Quorum/i &&
407 $hometeaching_data{$index}{$key} =~ /Elders/i &&
408 $hometeaching_data{$index}{'Household'} =~ /$family_name/i &&
409 $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
412 print " Updating hometeaching assignment for $family_name family\n";
413 $companionship = $hometeaching_data{$index}{'Comp ID'};
414 $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name_id='$name_id'");
415 $sth->execute or die "-E- DB error: $DBI::errstr\n";
426 #+----------+------------------+------+-----+---------+-------+
427 #| Field | Type | Null | Key | Default | Extra |
428 #+----------+------------------+------+-----+---------+-------+
429 #| parent | int(16) unsigned | | PRI | 0 | A |
430 #| family | int(16) unsigned | YES | | NULL | |
431 #| name | varchar(30) | YES | | NULL | |
432 #| birthday | date | YES | | NULL | |
433 #| phone | varchar(12) | YES | | NULL | |
434 #| address | varchar(255) | YES | | NULL | |
435 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
436 #| valid | tinyint(1) | YES | | NULL | |
437 #+----------+------------------+------+-----+---------+-------+
438 sub update_eq_parent_table
440 print "-> Updating eq_parent table\n";
442 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
443 $sth = $dbh->prepare("update eq_parent set valid=0");
444 $sth->execute or die "-E- DB error: $DBI::errstr\n";
446 foreach $index (keys %membership_data)
448 $hashref = $membership_data{$index};
449 foreach $key (keys %$hashref) {
450 if($key =~ /HH Position/i &&
451 $membership_data{$index}{$key} =~ /Head of Household/i ||
452 $membership_data{$index}{$key} =~ /Spouse/i
454 # Get some information from the hash about this parent
455 $parent_name = $membership_data{$index}{'Preferred Name'};
456 $parent_name =~ s/\'/\\'/g; #'
457 $birthday = $membership_data{$index}{'Birth'};
458 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
459 $hofh_id = $membership_data{$index}{'HofH ID'};
460 $id = $membership_data{$index}{'Indiv ID'};
461 $phone = $membership_data{$index}{'Phone 1'};
462 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
463 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
464 $address = $membership_data{$index}{'Street 1'};
465 if($membership_data{$index}{'Street 2'} ne "") {
466 $address .= " " . $membership_data{$index}{'Street 2'};
469 # Find the family id for this parent's HofH_ID.
470 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id'");
471 $sth->execute or die "-E- DB error: $DBI::errstr\n";
472 $sqlhashref = $sth->fetchrow_hashref();
473 $family_id = $sqlhashref->{'family'};
475 # Find out how many parents match this parent's name
476 $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
477 $sth->execute or die "-E- DB error: $DBI::errstr\n";
479 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
480 my $rows = scalar @data;
483 # No existing records found for this parent, make a new entry
484 print " Adding new Parent: $parent_name\n";
485 $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
486 $sth->execute or die "-E- DB error: $DBI::errstr\n";
487 } elsif($rows == 1) {
488 # An existing record was found for this parent, update it
489 print " Updating existing parent: $parent_name\n";
490 $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
491 $sth->execute or die "-E- DB error: $DBI::errstr\n";
492 $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
493 $sth->execute or die "-E- DB error: $DBI::errstr\n";
494 $sth = $dbh->prepare("update eq_parent set phone='$phone' where name='$parent_name'");
495 $sth->execute or die "-E- DB error: $DBI::errstr\n";
496 $sth = $dbh->prepare("update eq_parent set address='$address' where name='$parent_name'");
497 $sth->execute or die "-E- DB error: $DBI::errstr\n";
498 $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
499 $sth->execute or die "-E- DB error: $DBI::errstr\n";
500 $sth = $dbh->prepare("update eq_parent set indiv_id='$id' where name='$parent_name'");
501 $sth->execute or die "-E- DB error: $DBI::errstr\n";
503 # More than one record was found. Error! This shouldn't happen.
504 print " -E- More than one record found with same parent name: $parent_name\n";
513 #+----------+------------------+------+-----+---------+-------+
514 #| Field | Type | Null | Key | Default | Extra |
515 #+----------+------------------+------+-----+---------+-------+
516 #| child | int(16) unsigned | | PRI | 0 | A |
517 #| family | int(16) unsigned | YES | | NULL | |
518 #| name | varchar(30) | YES | | NULL | |
519 #| birthday | date | YES | | NULL | |
520 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
521 #| valid | tinyint(1) | YES | | NULL | |
522 #+----------+------------------+------+-----+---------+-------+
523 sub update_eq_child_table
525 print "-> Updating eq_child table\n";
527 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
528 $sth = $dbh->prepare("update eq_child set valid=0");
529 $sth->execute or die "-E- DB error: $DBI::errstr\n";
531 foreach $index (keys %membership_data)
533 $hashref = $membership_data{$index};
534 foreach $key (keys %$hashref) {
535 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
536 $child_name = $membership_data{$index}{'Full Name'};
537 $child_name =~ s/\'/\\'/g; #'
538 $birthday = $membership_data{$index}{'Birth'};
539 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
540 $id = $membership_data{$index}{'Indiv ID'};
541 $hofh_id = $membership_data{$index}{'HofH ID'};
543 # Find the family id for this child's HofH_ID.
544 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id'");
545 $sth->execute or die "-E- DB error: $DBI::errstr\n";
546 $sqlhashref = $sth->fetchrow_hashref();
547 $family_id = $sqlhashref->{'family'};
549 # Find out how many children have the same name for the same family
550 $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
551 $sth->execute or die "-E- DB error: $DBI::errstr\n";
553 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
554 my $rows = scalar @data;
557 # No existing records found for this child, make a new entry
558 print " Adding new Child: $child_name\n";
559 $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
560 $sth->execute or die "-E- DB error: $DBI::errstr\n";
561 } elsif($rows == 1) {
562 # An existing record was found for this child, update it
563 print " Updating existing child: $child_name\n";
564 $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
565 $sth->execute or die "-E- DB error: $DBI::errstr\n";
566 $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
567 $sth->execute or die "-E- DB error: $DBI::errstr\n";
568 $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
569 $sth->execute or die "-E- DB error: $DBI::errstr\n";
570 $sth = $dbh->prepare("update eq_child set indiv_id='$id' where name='$child_name'");
571 $sth->execute or die "-E- DB error: $DBI::errstr\n";
573 # More than one record was found. Error! This shouldn't happen.
574 print " -E- More than one record found ($rows) with same child name: $child_name\n";
582 ######################################################################
583 sub check_for_changed_ids
585 # If the Indiv ID & HofH ID has changed between data sets, we could have problems
586 my ($oldhashref, $newhashref) = @_;
587 my $found_problem = 0;
589 foreach $oldindex (keys %$oldhashref)
591 $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
592 $hofh_id = $oldhashref->{$oldindex}{'HofH ID'};
593 $full_name = $oldhashref->{$oldindex}{'Full Name'};
594 $hh_position = $oldhashref->{$oldindex}{'HH Position'};
595 if($hh_position =~ /Other/i) { next; }
597 foreach $newindex (keys %$newhashref)
599 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
600 $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
602 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
606 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
607 $hofh_id != $newhashref->{$newindex}{'HofH ID'})
609 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
615 return $found_problem;
618 ######################################################################
620 ######################################################################
622 ###################################################
623 # Open a connection to the database
624 $dbh=DBI->connect("dbi:mysql:dbname=$dbname;host=$dbhost port=$dbport",$dbuser,$dbpass,{
626 PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
628 ###################################################
629 # Check old directory against new directory to ensure
630 # that the Indiv ID & HofH ID have not changed between updates
632 print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
633 my %old_membership_data = ();
634 my %new_membership_data = ();
635 &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
636 &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
638 $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
642 print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
643 print " $opt_o and $opt_n data sets.\n";
644 print " This script is not currently setup to handle this properly.\n";
646 print " Exiting without updating...\n\n";
651 ###################################################
652 # Process command line options
653 if(defined $opt_n) { $datadir = $opt_n; }
654 else { $datadir = shift(@ARGV); }
655 print "-> Processing all ward data files in $datadir\n";
657 ###################################################
658 # Parse Ward Data Files
659 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
660 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
661 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
664 print "-> Membership Data Dump\n\n";
665 &print_hash(\%membership_data);
666 print "-> HomeTeaching Data Dump\n\n";
667 &print_hash(\%hometeaching_data);
668 print "-> Organization Data Dump\n\n";
669 &print_hash(\%organization_data);
672 if($opt_s) { $dbh->disconnect(); exit; }
674 # Now update the various eq DB tables
675 &update_eq_elder_table();
676 &update_eq_aaronic_table();
677 &update_eq_district_table();
678 &update_eq_companionship_table();
679 &update_eq_family_table();
680 &update_eq_parent_table();
681 &update_eq_child_table();
682 &update_eq_calling_table();
684 ###################################################
685 # Disconnect from the database
688 ######################################################################