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 "\n-> 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} =~ /^Deacon\s*$/i ||
97 $membership_data{$index}{$key} =~ /^Teacher\s*$/i ||
98 $membership_data{$index}{$key} =~ /^Priest\s*$/i)) {
99 $aaronic_name = $membership_data{$index}{'Preferred Name'};
100 $phone = $membership_data{$index}{'Phone 1'};
101 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
102 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
103 $sth = $dbh->prepare("select * from eq_aaronic where name='$aaronic_name'");
104 $sth->execute or die "-E- DB error: $DBI::errstr\n";
106 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
107 my $rows = scalar @data;
109 # No existing records found for this aaronic, make a new entry
110 print " Adding new Aaronic: $aaronic_name\n";
111 $sth = $dbh->prepare("insert into eq_aaronic values (NULL,'$aaronic_name','$phone',1)");
112 $sth->execute or die "-E- DB error: $DBI::errstr\n";
113 } elsif($rows == 1) {
114 # An existing record was found for this aaronic, update it, mark it valid!
115 print " Updating existing aaronic: $aaronic_name\n";
116 $sth = $dbh->prepare("update eq_aaronic set phone='$phone' where name='$aaronic_name'");
117 $sth->execute or die "-E- DB error: $DBI::errstr\n";
118 $sth = $dbh->prepare("update eq_aaronic set valid=1 where name='$aaronic_name'");
119 $sth->execute or die "-E- DB error: $DBI::errstr\n";
121 # More than one record was found. Error! This shouldn't happen.
122 print " -E- More than one record found ($rows) for aaronic name: $aaronic_name\n";
131 #+-------------+------------------+------+-----+---------+----------------+
132 #| Field | Type | Null | Key | Default | Extra |
133 #+-------------+------------------+------+-----+---------+----------------+
134 #| elder | int(16) unsigned | | PRI | NULL | auto_increment |
135 #| name | varchar(60) | YES | | NULL | |
136 #| phone | varchar(12) | YES | | NULL | |
137 #| ppi_pri | int(10) unsigned | YES | | 1 | |
138 #| ppi_notes | varchar(128) | YES | | NULL | |
139 #| int_pri | int(10) unsigned | YES | | 1 | |
140 #| int_notes | varchar(128) | YES | | NULL | |
141 #| attending | tinyint(1) | YES | | 0 | |
142 #| valid | tinyint(1) | YES | | NULL | |
143 #+-------------+------------------+------+-----+---------+----------------+
144 sub update_eq_elder_table
146 print "\n-> Updating eq_elder table\n";
148 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
149 $sth = $dbh->prepare("update eq_elder set valid=0");
150 $sth->execute or die "-E- DB error: $DBI::errstr\n";
152 foreach $index (keys %membership_data)
154 $hashref = $membership_data{$index};
155 foreach $key (keys %$hashref) {
156 if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
157 $id = $membership_data{$index}{'Indiv ID'};
158 $elder_name = $membership_data{$index}{'Preferred Name'};
159 $phone = $membership_data{$index}{'Phone 1'};
160 $organization = $organization_by_id{$id};
162 if(($organization =~ /Elders/) ||
163 ($organization =~ /Young Men/) ||
164 ($organization =~ /Sunday School/) ||
165 ($organization =~ /Primary/)
166 ) { $attending = 1; }
167 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
168 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
169 $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
170 $sth->execute or die "-E- DB error: $DBI::errstr\n";
172 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
173 my $rows = scalar @data;
175 # No existing records found for this elder, make a new entry
176 print " Adding new Elder: $elder_name\n";
177 $sth = $dbh->prepare("insert into eq_elder values (NULL,'$elder_name','$phone','1','','1','',$attending,1)");
178 $sth->execute or die "-E- DB error: $DBI::errstr\n";
179 } elsif($rows == 1) {
180 # An existing record was found for this elder, update it
181 print " Updating existing Elder: $elder_name\n";
182 $sth = $dbh->prepare("update eq_elder set valid=1 where name='$elder_name'");
183 $sth->execute or die "-E- DB error: $DBI::errstr\n";
185 $sth = $dbh->prepare("update eq_elder set phone='$phone' where name='$elder_name'");
187 $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_name'");
189 $sth->execute or die "-E- DB error: $DBI::errstr\n";
190 $sth = $dbh->prepare("update eq_elder set attending='$attending' where name='$elder_name'");
191 $sth->execute or die "-E- DB error: $DBI::errstr\n";
193 # More than one record was found. Error! This shouldn't happen.
194 print " -E- More than one record found ($rows) for Elder: $elder_name\n";
203 #+--------------+------------------+------+-----+---------+-------+
204 #| Field | Type | Null | Key | Default | Extra |
205 #+--------------+------------------+------+-----+---------+-------+
206 #| indiv_id | int(16) unsigned | YES | | NULL | |
207 #| name | varchar(30) | YES | | NULL | |
208 #| organization | varchar(30) | YES | | NULL | |
209 #| position | varchar(30) | YES | | NULL | |
210 #| sequence | int(16) unsigned | YES | | NULL | |
211 #| sustained | date | YES | | NULL | |
212 #+--------------+------------------+------+-----+---------+-------+
213 sub update_eq_calling_table()
215 print "\n-> Updating eq_calling table\n";
217 #print "-> Organization Data Dump\n\n";
218 #&print_hash(\%organization_data);
220 # Delete all records from the calling table. We have no history to
221 # save here. Just re-populate with the latest calling information.
222 $sth = $dbh->prepare("delete from eq_calling ");
223 $sth->execute or die "-E- DB error: $DBI::errstr\n";
225 foreach $index (keys %organization_data)
227 $indiv_id = $organization_data{$index}{'Indiv ID'};
228 $name = $organization_data{$index}{'Indiv Name'};
229 $name =~ s/\'/\\'/g; #'
230 $organization = $organization_data{$index}{'Organization'};
231 $organization_by_name{$name} = $organization;
232 $organization_by_id{$indiv_id} = $organization;
233 $position = $organization_data{$index}{'Position'};
234 $sequence = $organization_data{$index}{'Org Seq'};
235 $sustained = $organization_data{$index}{'Sustained'};
236 $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
237 if($name eq "") { next; }
238 print " Adding new Calling: $name -> $position\n";
239 $sth = $dbh->prepare("insert into eq_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
240 $sth->execute or die "-E- DB error: $DBI::errstr\n";
245 #+------------+------------------+------+-----+---------+-------+
246 #| Field | Type | Null | Key | Default | Extra |
247 #+------------+------------------+------+-----+---------+-------+
248 #| district | int(16) unsigned | | PRI | 0 | |
249 #| name | varchar(30) | YES | | NULL | |
250 #| supervisor | int(16) unsigned | YES | | NULL | |
251 #| valid | tinyint(1) | YES | | NULL | |
252 #+------------+------------------+------+-----+---------+-------+
253 sub update_eq_district_table
255 # Districts should be created by hand. This subroutine only
256 # updates the supervisor's ID in each district.
257 print "\n-> Updating eq_district table\n";
258 $sth = $dbh->prepare("select * from eq_district");
259 $sth->execute or die "-E- DB error: $DBI::errstr\n";
260 while($sqlhashref = $sth->fetchrow_hashref) {
261 $supervisor_name = $sqlhashref->{name};
262 $district = $sqlhashref->{district};
263 $sth2 = $dbh->prepare("select * from eq_elder where name='$supervisor_name'");
264 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
265 $sqlhashref2 = $sth2->fetchrow_hashref;
266 $supervisor_id = $sqlhashref2->{elder};
268 $sth2 = $dbh->prepare("update eq_district set supervisor='$supervisor_id' where district='$district'");
269 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
276 #+---------------+------------------+------+-----+---------+-------+
277 #| Field | Type | Null | Key | Default | Extra |
278 #+---------------+------------------+------+-----+---------+-------+
279 #| companionship | int(16) unsigned | | | 0 | |
280 #| elder | int(16) unsigned | YES | | NULL | |
281 #| aaronic | int(16) unsigned | YES | | NULL | |
282 #| district | int(16) unsigned | YES | | NULL | |
283 #| valid | tinyint(1) | YES | | NULL | |
284 #+---------------+------------------+------+-----+---------+-------+
285 sub update_eq_companionship_table
287 print "\n-> Updating eq_companionship table\n";
289 # First, mark all existing companionships as invalid in case they have been dissolved
290 $sth = $dbh->prepare("update eq_companionship set valid=0");
291 $sth->execute or die "-E- DB error: $DBI::errstr\n";
292 # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
293 $sth = $dbh->prepare("update eq_aaronic set valid=0");
294 $sth->execute or die "-E- DB error: $DBI::errstr\n";
296 foreach $index (keys %hometeaching_data)
298 $hashref = $hometeaching_data{$index};
299 foreach $key (keys %$hashref) {
300 if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
301 foreach $field ("Home Teacher 1","Home Teacher 2") {
302 $elder_name = $hometeaching_data{$index}{$field};
303 if($elder_name eq "") { next; }
304 $sth2 = $dbh->prepare("select * from eq_elder where name='$elder_name'");
305 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
306 $sqlhashref2 = $sth2->fetchrow_hashref;
307 $elder = $sqlhashref2->{elder};
310 $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_name'");
311 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
312 $sqlhashref2 = $sth2->fetchrow_hashref;
313 $aaronic = $sqlhashref2->{aaronic};
315 if($aaronic eq "") { print "-W- Unable to find $elder_name in eq_elder or eq_aaronic tables\n"; next; }
317 $id = $hometeaching_data{$index}{'Comp ID'};
318 $district = $hometeaching_data{$index}{'HT District'};
319 $sth = $dbh->prepare("select * from eq_companionship where elder='$elder' and aaronic='$aaronic' and companionship='$id'");
320 $sth->execute or die "-E- DB error: $DBI::errstr\n";
322 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
323 my $rows = scalar @data;
325 # No existing records found for this companionship, make a new entry
326 print " Adding Companion to companionship: $elder_name -> $id\n";
327 $sth = $dbh->prepare("insert into eq_companionship values ($id,'$elder','$aaronic','$district',1)");
328 $sth->execute or die "-E- DB error: $DBI::errstr\n";
330 # An existing companionship was found for this companionship, update it
331 $sth2 = $dbh->prepare("select * from eq_companionship where district='$district' and companionship='$id'");
332 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
333 if($elder ne "NULL") {
334 print " Updating Companionship with Elder: $elder_name ($elder) -> $id\n";
335 $sth = $dbh->prepare("update eq_companionship set district='$district' where elder='$elder' and companionship='$id'");
336 $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
337 $sth = $dbh->prepare("update eq_companionship set elder='$elder' where elder='$elder' and companionship='$id'");
338 $sth->execute or die "-E- DB error 'elder': $DBI::errstr\n";
339 $sth = $dbh->prepare("update eq_companionship set valid=1 where elder='$elder' and companionship='$id'");
340 $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
342 print " Updating Companionship with Aaronic: $elder_name ($aaronic) -> $id\n";
343 $sth = $dbh->prepare("update eq_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
344 $sth->execute or die "-E- DB error: $DBI::errstr\n";
345 $sth = $dbh->prepare("update eq_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
346 $sth->execute or die "-E- DB error: $DBI::errstr\n";
347 $sth = $dbh->prepare("update eq_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
348 $sth->execute or die "-E- DB error: $DBI::errstr\n";
349 $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
350 $sth->execute or die "-E- DB error: $DBI::errstr\n";
362 #+---------------+------------------+------+-----+---------+-------+
363 #| Field | Type | Null | Key | Default | Extra |
364 #+---------------+------------------+------+-----+---------+-------+
365 #| family | int(16) unsigned | | PRI | 0 | A |
366 #| hofh_id | int(16) unsigned | YES | | NULL | |
367 #| name | varchar(30) | YES | | NULL | |
368 #| name_id | varchar(30) | YES | | NULL | |
369 #| elder_id | int(16) unsigned | YES | | NULL | |
370 #| companionship | int(16) unsigned | YES | | NULL | |
371 #| visit_pri | int(10) unsigned | YES | | 1 | |
372 #| visit_notes | varchar(128) | YES | | NULL | |
373 #| valid | tinyint(1) | YES | | NULL | |
374 #+---------------+------------------+------+-----+---------+-------+
375 sub update_eq_family_table
377 print "\n-> Updating eq_family table\n";
379 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
380 $sth = $dbh->prepare("update eq_family set valid=0");
381 $sth->execute or die "-E- DB error: $DBI::errstr\n";
382 $sth = $dbh->prepare("update eq_family set companionship=0");
383 $sth->execute or die "-E- DB error: $DBI::errstr\n";
385 foreach $index (keys %membership_data)
387 $hashref = $membership_data{$index};
388 foreach $key (keys %$hashref) {
389 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
390 $family_name = $membership_data{$index}{'Preferred Name'};
391 $family_name =~ s/\'/\\'/g; #'
392 $id = $membership_data{$index}{'HofH ID'};
393 $name_id = uc($family_name);
395 # Find out how many families match this family's name
396 $sth = $dbh->prepare("select * from eq_family where name_id='$name_id'");
397 $sth->execute or die "-E- DB error: $DBI::errstr\n";
399 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
400 my $rows = scalar @data;
403 # No existing records found for this family, make a new entry
404 print " Adding new Family: $family_name\n";
405 $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','$name_id','0','0','1','',1)");
406 $sth->execute or die "-E- DB error: $DBI::errstr\n";
407 } elsif($rows == 1) {
408 # An existing record was found for this family, update it
409 print " Updating existing family: $family_name\n";
410 $sth = $dbh->prepare("update eq_family set hofh_id=$id where name_id='$name_id'");
411 $sth->execute or die "-E- DB error: $DBI::errstr\n";
412 $sth = $dbh->prepare("update eq_family set valid=1 where name_id='$name_id'");
413 $sth->execute or die "-E- DB error: $DBI::errstr\n";
415 # More than one record was found. Error! This shouldn't happen.
416 print " -E- More than one record found ($rows) for family name: $family_name\n";
419 # Now update the elder_id field for this family
420 $sth = $dbh->prepare("select * from eq_elder WHERE name='$family_name'");
421 $sth->execute or die "-E- DB error: $DBI::errstr\n";
422 while($sqlhashref = $sth->fetchrow_hashref) {
423 $elder_id = $sqlhashref->{elder};
424 print " Updating family elder_id: $family_name -> $elder_id\n";
425 $sth = $dbh->prepare("update eq_family set elder_id=$elder_id where name_id='$name_id'");
426 $sth->execute or die "-E- DB error: $DBI::errstr\n";
429 # Now update the hometeaching field for this family
430 foreach $index (keys %hometeaching_data)
432 $hashref = $hometeaching_data{$index};
433 foreach $key (keys %$hashref) {
434 if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
435 $a = $1; $b = $2; $c = $3; $d = $4;
436 if($family_name =~ /$a/ && $hometeaching_data{$index}{'Household'} !~ /$family_name/i) {
437 print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $a, $c $d\n";
438 $hometeaching_data{$index}{'Household'} = "$a, $c $d";
441 if($key =~ /Quorum/i &&
442 $hometeaching_data{$index}{$key} =~ /Elders/i &&
443 $hometeaching_data{$index}{'Household'} =~ /$family_name/i &&
444 $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
447 print " Updating hometeaching assignment for $family_name family\n";
448 $companionship = $hometeaching_data{$index}{'Comp ID'};
449 $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name_id='$name_id'");
450 $sth->execute or die "-E- DB error: $DBI::errstr\n";
461 #+----------+------------------+------+-----+---------+-------+
462 #| Field | Type | Null | Key | Default | Extra |
463 #+----------+------------------+------+-----+---------+-------+
464 #| parent | int(16) unsigned | | PRI | 0 | A |
465 #| family | int(16) unsigned | YES | | NULL | |
466 #| name | varchar(30) | YES | | NULL | |
467 #| birthday | date | YES | | NULL | |
468 #| phone | varchar(12) | YES | | NULL | |
469 #| address | varchar(255) | YES | | NULL | |
470 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
471 #| valid | tinyint(1) | YES | | NULL | |
472 #+----------+------------------+------+-----+---------+-------+
473 sub update_eq_parent_table
475 print "\n-> Updating eq_parent table\n";
477 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
478 $sth = $dbh->prepare("update eq_parent set valid=0");
479 $sth->execute or die "-E- DB error: $DBI::errstr\n";
481 foreach $index (keys %membership_data)
483 $hashref = $membership_data{$index};
484 foreach $key (keys %$hashref) {
485 if($key =~ /HH Position/i &&
486 $membership_data{$index}{$key} =~ /Head of Household/i ||
487 $membership_data{$index}{$key} =~ /Spouse/i
489 # Get some information from the hash about this parent
490 $parent_name = $membership_data{$index}{'Preferred Name'};
491 $parent_name =~ s/\'/\\'/g; #'
492 $birthday = $membership_data{$index}{'Birth'};
493 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
494 $hofh_id = $membership_data{$index}{'HofH ID'};
495 $id = $membership_data{$index}{'Indiv ID'};
496 $phone = $membership_data{$index}{'Phone 1'};
497 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
498 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
499 $address = $membership_data{$index}{'Street 1'};
500 if($membership_data{$index}{'Street 2'} ne "") {
501 $address .= " " . $membership_data{$index}{'Street 2'};
504 # Find the family id for this parent's HofH_ID.
505 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
506 $sth->execute or die "-E- DB error: $DBI::errstr\n";
507 my @family_data = ();
508 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
509 my $family_rows = scalar @family_data;
510 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
511 else { $family_id = 0; }
513 # Find out how many parents match this parent's name
514 $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
515 $sth->execute or die "-E- DB error: $DBI::errstr\n";
517 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
518 my $rows = scalar @data;
520 if($rows == 0 && $family_rows > 0) {
521 # No existing records found for this parent, make a new entry
522 print " Adding new Parent: $parent_name\n";
523 $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address','$id',1)");
524 $sth->execute or die "-E- DB error: $DBI::errstr\n";
525 } elsif($rows == 1 && $family_rows > 0) {
526 # An existing record was found for this parent, update it
527 print " Updating existing parent: $parent_name\n";
528 $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
529 $sth->execute or die "-E- DB error: $DBI::errstr\n";
530 $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
531 $sth->execute or die "-E- DB error: $DBI::errstr\n";
532 $sth = $dbh->prepare("update eq_parent set phone='$phone' where name='$parent_name'");
533 $sth->execute or die "-E- DB error: $DBI::errstr\n";
534 $sth = $dbh->prepare("update eq_parent set address='$address' where name='$parent_name'");
535 $sth->execute or die "-E- DB error: $DBI::errstr\n";
536 $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
537 $sth->execute or die "-E- DB error: $DBI::errstr\n";
538 $sth = $dbh->prepare("update eq_parent set indiv_id='$id' where name='$parent_name'");
539 $sth->execute or die "-E- DB error: $DBI::errstr\n";
541 # More than one record was found. Error! This shouldn't happen.
542 print " -E- More than one record found with same parent name: $parent_name with hofh_id: $hofh_id\n";
544 print " -E- Unable to find a family to attach this parent to: $parent_name with hofh_id: $hofh_id\n";
553 #+----------+------------------+------+-----+---------+-------+
554 #| Field | Type | Null | Key | Default | Extra |
555 #+----------+------------------+------+-----+---------+-------+
556 #| child | int(16) unsigned | | PRI | 0 | A |
557 #| family | int(16) unsigned | YES | | NULL | |
558 #| name | varchar(30) | YES | | NULL | |
559 #| birthday | date | YES | | NULL | |
560 #| indiv_id | int(16) unsigned | YES | UNI | NULL | |
561 #| valid | tinyint(1) | YES | | NULL | |
562 #+----------+------------------+------+-----+---------+-------+
563 sub update_eq_child_table
565 print "\n-> Updating eq_child table\n";
567 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
568 $sth = $dbh->prepare("update eq_child set valid=0");
569 $sth->execute or die "-E- DB error: $DBI::errstr\n";
571 foreach $index (keys %membership_data)
573 $hashref = $membership_data{$index};
574 foreach $key (keys %$hashref) {
575 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
576 $child_name = $membership_data{$index}{'Full Name'};
577 $child_name =~ s/\'/\\'/g; #'
578 $birthday = $membership_data{$index}{'Birth'};
579 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
580 $id = $membership_data{$index}{'Indiv ID'};
581 $hofh_id = $membership_data{$index}{'HofH ID'};
583 # Find the family id for this child's HofH_ID.
584 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id' and valid=1");
585 $sth->execute or die "-E- DB error: $DBI::errstr\n";
586 my @family_data = ();
587 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
588 my $family_rows = scalar @family_data;
589 if($family_rows > 0) { $family_id = $family_data[0]->{'family'}; }
590 else { $family_id = 0; }
592 # Find out how many children have the same name for the same family
593 $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
594 $sth->execute or die "-E- DB error: $DBI::errstr\n";
596 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
597 my $rows = scalar @data;
599 if($rows == 0 && $family_rows > 0) {
600 # No existing records found for this child, make a new entry
601 print " Adding new Child: $child_name\n";
602 $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day','$id',1)");
603 $sth->execute or die "-E- DB error: $DBI::errstr\n";
604 } elsif($rows == 1 && $family_rows > 0) {
605 # An existing record was found for this child, update it
606 print " Updating existing child: $child_name\n";
607 $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
608 $sth->execute or die "-E- DB error: $DBI::errstr\n";
609 $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
610 $sth->execute or die "-E- DB error: $DBI::errstr\n";
611 $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
612 $sth->execute or die "-E- DB error: $DBI::errstr\n";
613 $sth = $dbh->prepare("update eq_child set indiv_id='$id' where name='$child_name'");
614 $sth->execute or die "-E- DB error: $DBI::errstr\n";
616 # More than one record was found. Error! This shouldn't happen.
617 print " -E- More than one record found ($rows) with same child name: $child_name\n";
625 ######################################################################
626 sub check_for_changed_ids
628 # If the Indiv ID & HofH ID has changed between data sets, we could have problems
629 my ($oldhashref, $newhashref) = @_;
630 my $found_problem = 0;
632 foreach $oldindex (keys %$oldhashref)
634 $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
635 $hofh_id = $oldhashref->{$oldindex}{'HofH ID'};
636 $full_name = $oldhashref->{$oldindex}{'Full Name'};
637 $hh_position = $oldhashref->{$oldindex}{'HH Position'};
638 if($hh_position =~ /Other/i) { next; }
640 foreach $newindex (keys %$newhashref)
642 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
643 $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
645 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
649 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
650 $hofh_id != $newhashref->{$newindex}{'HofH ID'})
652 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
658 return $found_problem;
661 ######################################################################
663 ######################################################################
665 ###################################################
666 # Open a connection to the database
667 $dbh=DBI->connect("dbi:mysql:dbname=$dbname;host=$dbhost port=$dbport",$dbuser,$dbpass,{
669 PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
671 ###################################################
672 # Check old directory against new directory to ensure
673 # that the Indiv ID & HofH ID have not changed between updates
675 print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
676 my %old_membership_data = ();
677 my %new_membership_data = ();
678 &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
679 &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
681 $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
685 print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
686 print " $opt_o and $opt_n data sets.\n";
687 print " This script is not currently setup to handle this properly.\n";
689 print " Exiting without updating...\n\n";
694 ###################################################
695 # Process command line options
696 if(defined $opt_n) { $datadir = $opt_n; }
697 else { $datadir = shift(@ARGV); }
698 print "\n-> Processing all ward data files in $datadir\n";
700 ###################################################
701 # Parse Ward Data Files
702 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
703 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
704 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
705 %organization_by_name = ();
706 %organization_by_id = ();
709 print "-> Membership Data Dump\n\n";
710 &print_hash(\%membership_data);
711 print "-> HomeTeaching Data Dump\n\n";
712 &print_hash(\%hometeaching_data);
713 print "-> Organization Data Dump\n\n";
714 &print_hash(\%organization_data);
717 if($opt_s) { $dbh->disconnect(); exit; }
719 # Now update the various eq DB tables
720 &update_eq_calling_table();
721 &update_eq_elder_table();
722 &update_eq_aaronic_table();
723 &update_eq_district_table();
724 &update_eq_companionship_table();
725 &update_eq_family_table();
726 &update_eq_parent_table();
727 &update_eq_child_table();
729 ###################################################
730 # Disconnect from the database
733 ######################################################################