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;
40 @data = split /\",/, $line;
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 sub optional_csv_to_hash
60 my ($filename, $hashref) = @_;
62 my $opened = open(FILE,$filename);
65 my $found_header = 0; my $index = 0;
68 @data = split /\",/, $line;
73 foreach $i (0..$#data-1) {
75 $header[$i] =~ s/\"//g;
76 $hashref->{$index}{$header[$i]} = $data[$i];
77 #print "$index: $i: $header[$i]: $data[$i]\n";
87 print "-W- could not open optional csv file $filename\n";
91 ######################################################################
96 foreach $key (sort {$a <=> $b} keys %$hashref) {
97 print "Index: $key\n";
98 foreach $field (keys %{$hashref->{$key}}) {
99 print "$field: $hashref->{$key}{$field}\n";
105 ######################################################################
108 #+-------------+------------------+------+-----+---------+----------------+
109 #| Field | Type | Null | Key | Default | Extra |
110 #+-------------+------------------+------+-----+---------+----------------+
111 #| indiv | int(16) unsigned | | PRI | NULL | auto_increment |
112 #| indiv_id | int(16) unsigned | | | NULL | |
113 #| name | varchar(60) | YES | | NULL | |
114 #| phone | varchar(12) | YES | | NULL | |
115 #| email | varchar(120) | YES | | NULL | |
116 #| priesthood | enum | YES | | NULL | |
117 #| ppi_pri | int(10) unsigned | YES | | 1 | |
118 #| ppi_notes | varchar(128) | YES | | NULL | |
119 #| int_pri | int(10) unsigned | YES | | 1 | |
120 #| int_notes | varchar(128) | YES | | NULL | |
121 #| attending | tinyint(1) | YES | | 0 | |
122 #| valid | tinyint(1) | YES | | NULL | |
123 #+-------------+------------------+------+-----+---------+----------------+
124 sub update_tc_indiv_table
126 print "\n-> Updating tc_indiv table\n";
128 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
129 $sth = $dbh->prepare("update tc_indiv set valid=0");
130 $sth->execute or die "-E- DB error: $DBI::errstr\n";
132 foreach $index (keys %membership_data)
134 $hashref = $membership_data{$index};
135 $id = $membership_data{$index}{'Indiv ID'};
136 $indiv_name = $membership_data{$index}{'Preferred Name'};
137 $birthday = $membership_data{$index}{'Birth'};
138 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
139 $address = $membership_data{$index}{'Street 1'};
140 if($membership_data{$index}{'Street 2'} ne "") {
141 $address .= " " . $membership_data{$index}{'Street 2'};
143 $phone = $membership_data{$index}{'Household Phone'};
144 $priesthood = $membership_data{$index}{'Priesthood'};
145 $hhposition = $membership_data{$index}{'HH Position'};
146 $organization = $organization_by_id{$id};
148 if(($organization =~ /Elders/) ||
149 ($organization =~ /Young Men/) ||
150 ($organization =~ /Sunday School/) ||
151 ($organization =~ /Primary/)
152 ) { $attending = 1; }
153 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "$areacode-$1"; }
154 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
155 $email = $membership_data{$index}{'indiv E-mail'};
156 if ($email eq "") { $email = $membership_data{$index}{'Household E-mail'}; }
157 $sth = $dbh->prepare("select * from tc_indiv where name=\"$indiv_name\"");
158 $sth->execute or die "-E- DB error: $DBI::errstr\n";
160 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
161 my $rows = scalar @data;
163 # No existing records found for this indiv, make a new entry
164 print " Adding new indiv: $indiv_name\n";
165 $sth = $dbh->prepare("insert into tc_indiv values (NULL,'$id',\"$indiv_name\",'$year-$month-$day','$address','$phone','$email','','$hhposition','$priesthood','','$default_interview_pri','','$default_interview_pri','',$attending,1)");
166 $sth->execute or die "-E- DB error: $DBI::errstr\n";
167 } elsif($rows == 1) {
168 # An existing record was found for this indiv, update it
169 print " Updating existing indiv: $indiv_name\n";
170 $sth = $dbh->prepare("update tc_indiv set valid=1 where name=\"$indiv_name\"");
171 $sth->execute or die "-E- DB error: $DBI::errstr\n";
173 $sth = $dbh->prepare("update tc_indiv set phone='$phone' where name=\"$indiv_name\"");
175 $sth = $dbh->prepare("update tc_indiv set phone=NULL where name=\"$indiv_name\"");
177 $sth->execute or die "-E- DB error: $DBI::errstr\n";
178 if($birthday ne "") {
179 $sth = $dbh->prepare("update tc_indiv set birthday='$year-$month-$day' where name=\"$indiv_name\"");
181 $sth = $dbh->prepare("update tc_indiv set birthday=NULL where name=\"$indiv_name\"");
183 $sth->execute or die "-E- DB error: $DBI::errstr\n";
185 $sth = $dbh->prepare("update tc_indiv set address='$address' where name=\"$indiv_name\"");
187 $sth = $dbh->prepare("update tc_indiv set address=NULL where name=\"$indiv_name\"");
189 $sth->execute or die "-E- DB error: $DBI::errstr\n";
190 $sth = $dbh->prepare("update tc_indiv set attending='$attending' where name=\"$indiv_name\"");
191 $sth->execute or die "-E- DB error: $DBI::errstr\n";
192 $sth = $dbh->prepare("update tc_indiv set indiv_id='$id' where name=\"$indiv_name\"");
193 $sth->execute or die "-E- DB error: $DBI::errstr\n";
194 $sth = $dbh->prepare("update tc_indiv set priesthood='$priesthood' where name=\"$indiv_name\"");
195 $sth->execute or die "-E- DB error: $DBI::errstr\n";
196 $sth = $dbh->prepare("update tc_indiv set email='$email' where name=\"$indiv_name\"");
197 $sth->execute or die "-E- DB error: $DBI::errstr\n";
198 $sth = $dbh->prepare("update tc_indiv set hh_position='$hhposition' where name=\"$indiv_name\"");
199 $sth->execute or die "-E- DB error: $DBI::errstr\n";
201 # More than one record was found. Error! This shouldn't happen.
202 print " -E- More than one record found ($rows) for indiv: $indiv_name\n";
209 #+--------------+------------------+------+-----+---------+-------+
210 #| Field | Type | Null | Key | Default | Extra |
211 #+--------------+------------------+------+-----+---------+-------+
212 #| indiv_id | int(16) unsigned | YES | | NULL | |
213 #| name | varchar(30) | YES | | NULL | |
214 #| organization | varchar(30) | YES | | NULL | |
215 #| position | varchar(30) | YES | | NULL | |
216 #| sequence | int(16) unsigned | YES | | NULL | |
217 #| sustained | date | YES | | NULL | |
218 #+--------------+------------------+------+-----+---------+-------+
219 sub update_tc_calling_table()
221 print "\n-> Updating tc_calling table\n";
223 #print "-> Organization Data Dump\n\n";
224 #&print_hash(\%organization_data);
226 # Delete all records from the calling table. We have no history to
227 # save here. Just re-populate with the latest calling information.
228 $sth = $dbh->prepare("delete from tc_calling ");
229 $sth->execute or die "-E- DB error: $DBI::errstr\n";
231 foreach $index (keys %organization_data)
233 $indiv_id = $organization_data{$index}{'Indiv ID'};
234 $name = $organization_data{$index}{'Indiv Name'};
235 $name =~ s/\'/\\'/g; #'
236 $organization = $organization_data{$index}{'Organization'};
237 $organization_by_name{$name} = $organization;
238 $organization_by_id{$indiv_id} = $organization;
239 $position = $organization_data{$index}{'Position'};
240 $sequence = $organization_data{$index}{'Org Seq'};
241 $sustained = $organization_data{$index}{'Sustained'};
242 $sustained =~ /(\S+) (\d+)/; $month=$1; $year=$2;
243 if($name eq "") { next; }
244 print " Adding new Calling: $name -> $position\n";
245 $sth = $dbh->prepare("insert into tc_calling values ('$indiv_id','$name','$organization','$position','$sequence','$month $year')");
246 $sth->execute or die "-E- DB error: $DBI::errstr\n";
251 #+------------+------------------+------+-----+---------+-------+
252 #| Field | Type | Null | Key | Default | Extra |
253 #+------------+------------------+------+-----+---------+-------+
254 #| district | int(16) unsigned | | PRI | 0 | |
255 #| name | varchar(30) | YES | | NULL | |
256 #| supervisor | int(16) unsigned | YES | | NULL | |
257 #| valid | tinyint(1) | YES | | NULL | |
258 #+------------+------------------+------+-----+---------+-------+
259 sub update_tc_district_table
261 # Districts should be created by hand. This subroutine only
262 # updates the supervisor's ID in each district.
263 print "\n-> Updating tc_district table\n";
264 $sth = $dbh->prepare("select * from tc_district");
265 $sth->execute or die "-E- DB error: $DBI::errstr\n";
266 while($sqlhashref = $sth->fetchrow_hashref) {
267 $supervisor_name = $sqlhashref->{name};
268 $district = $sqlhashref->{district};
269 $sth2 = $dbh->prepare("select * from tc_indiv where name='$supervisor_name'");
270 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
271 $sqlhashref2 = $sth2->fetchrow_hashref;
272 $supervisor_id = $sqlhashref2->{indiv};
274 $sth2 = $dbh->prepare("update tc_district set supervisor='$supervisor_id' where district='$district'");
275 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
282 #+---------------+------------------+------+-----+---------+-------+
283 #| Field | Type | Null | Key | Default | Extra |
284 #+---------------+------------------+------+-----+---------+-------+
285 #| companionship | int(16) unsigned | | | 0 | |
286 #| indiv | int(16) unsigned | YES | | NULL | |
287 #| district | int(16) unsigned | YES | | NULL | |
288 #| valid | tinyint(1) | YES | | NULL | |
289 #+---------------+------------------+------+-----+---------+-------+
290 sub update_tc_companionship_table
292 print "\n-> Updating tc_companionship table\n";
294 # First, mark all existing companionships as invalid in case they have been dissolved
295 $sth = $dbh->prepare("update tc_companionship set valid=0");
296 $sth->execute or die "-E- DB error: $DBI::errstr\n";
298 foreach $index (keys %hometeaching_data)
300 $hashref = $hometeaching_data{$index};
301 foreach $key (keys %$hashref) {
302 if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
303 foreach $field ("Home Teacher 1","Home Teacher 2") {
304 $indiv_name = $hometeaching_data{$index}{$field};
305 if($indiv_name eq "") { next; }
306 $sth2 = $dbh->prepare("select * from tc_indiv where name='$indiv_name'");
307 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
308 $sqlhashref2 = $sth2->fetchrow_hashref;
309 $indiv = $sqlhashref2->{indiv};
310 $id = $hometeaching_data{$index}{'Comp ID'};
311 $district = $hometeaching_data{$index}{'HT District'};
312 $sth = $dbh->prepare("select * from tc_companionship where indiv='$indiv' and companionship='$id'");
313 $sth->execute or die "-E- DB error: $DBI::errstr\n";
315 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
316 my $rows = scalar @data;
318 # No existing records found for this companionship, make a new entry
319 print " Adding Companion to companionship: $indiv_name -> $id\n";
320 $sth = $dbh->prepare("insert into tc_companionship values ($id,'$indiv','$district',1)");
321 $sth->execute or die "-E- DB error: $DBI::errstr\n";
323 # An existing companionship was found for this companionship, update it
324 $sth2 = $dbh->prepare("select * from tc_companionship where district='$district' and companionship='$id'");
325 $sth2->execute or die "-E- DB error: $DBI::errstr\n";
326 print " Updating Companionship with indiv: $indiv_name ($indiv) -> $id\n";
327 $sth = $dbh->prepare("update tc_companionship set district='$district' where indiv='$indiv' and companionship='$id'");
328 $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
329 $sth = $dbh->prepare("update tc_companionship set indiv='$indiv' where indiv='$indiv' and companionship='$id'");
330 $sth->execute or die "-E- DB error 'indiv': $DBI::errstr\n";
331 $sth = $dbh->prepare("update tc_companionship set valid=1 where indiv='$indiv' and companionship='$id'");
332 $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
343 #+---------------+------------------+------+-----+---------+-------+
344 #| Field | Type | Null | Key | Default | Extra |
345 #+---------------+------------------+------+-----+---------+-------+
346 #| family | int(16) unsigned | | PRI | 0 | A |
347 #| hofh_id | int(16) unsigned | YES | | NULL | |
348 #| name | varchar(30) | YES | | NULL | |
349 #| name_id | varchar(30) | YES | | NULL | |
350 #| indiv_id | int(16) unsigned | YES | | NULL | |
351 #| companionship | int(16) unsigned | YES | | NULL | |
352 #| visit_pri | int(10) unsigned | YES | | 1 | |
353 #| visit_notes | varchar(128) | YES | | NULL | |
354 #| valid | tinyint(1) | YES | | NULL | |
355 #+---------------+------------------+------+-----+---------+-------+
356 sub update_tc_family_table
358 print "\n-> Updating tc_family table\n";
360 # Set all records to be invalid. Only mark them as valid if they appear on the new list.
361 $sth = $dbh->prepare("update tc_family set valid=0");
362 $sth->execute or die "-E- DB error: $DBI::errstr\n";
363 $sth = $dbh->prepare("update tc_family set companionship=0");
364 $sth->execute or die "-E- DB error: $DBI::errstr\n";
366 foreach $index (keys %membership_data)
368 $hashref = $membership_data{$index};
369 foreach $key (keys %$hashref) {
370 if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
371 $family_name = $membership_data{$index}{'Preferred Name'};
372 $family_name =~ s/\'/\\'/g; #'
373 $id = $membership_data{$index}{'HofH ID'};
374 $name_id = uc($family_name);
376 # Find out how many families match this family's name
377 $sth = $dbh->prepare("select * from tc_family where name_id='$name_id'");
378 $sth->execute or die "-E- DB error: $DBI::errstr\n";
380 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
381 my $rows = scalar @data;
384 # No existing records found for this family, make a new entry
385 print " Adding new Family: $family_name\n";
386 $sth = $dbh->prepare("insert into tc_family values (NULL,$id,'$family_name','$name_id','0','0','$default_visit_pri','',1)");
387 $sth->execute or die "-E- DB error: $DBI::errstr\n";
388 } elsif($rows == 1) {
389 # An existing record was found for this family, update it
390 print " Updating existing family: $family_name\n";
391 $sth = $dbh->prepare("update tc_family set hofh_id=$id where name_id='$name_id'");
392 $sth->execute or die "-E- DB error: $DBI::errstr\n";
393 $sth = $dbh->prepare("update tc_family set valid=1 where name_id='$name_id'");
394 $sth->execute or die "-E- DB error: $DBI::errstr\n";
396 # More than one record was found. Error! This shouldn't happen.
397 print " -E- More than one record found ($rows) for family name: $family_name\n";
400 # Now update the indiv_id field for this family
401 $sth = $dbh->prepare("select * from tc_indiv WHERE name='$family_name'");
402 $sth->execute or die "-E- DB error: $DBI::errstr\n";
403 while($sqlhashref = $sth->fetchrow_hashref) {
404 $indiv_id = $sqlhashref->{indiv};
405 print " Updating family indiv_id: $family_name -> $indiv_id\n";
406 $sth = $dbh->prepare("update tc_family set indiv_id=$indiv_id where name_id='$name_id'");
407 $sth->execute or die "-E- DB error: $DBI::errstr\n";
410 # Now update the hometeaching field for this family
411 foreach $index (keys %hometeaching_data)
413 $hashref = $hometeaching_data{$index};
414 foreach $key (keys %$hashref) {
415 if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
416 $a = $1; $b = $2; $c = $3; $d = $4;
417 if($family_name =~ /$a/ && $hometeaching_data{$index}{'Household'} !~ /$family_name/i) {
418 print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $a, $c $d\n";
419 $hometeaching_data{$index}{'Household'} = "$a, $c $d";
422 if($key =~ /Quorum/i &&
423 $hometeaching_data{$index}{$key} =~ /Elders/i &&
424 $hometeaching_data{$index}{'Household'} =~ /$family_name/i &&
425 $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
428 print " Updating hometeaching assignment for $family_name family\n";
429 $companionship = $hometeaching_data{$index}{'Comp ID'};
430 $sth = $dbh->prepare("update tc_family set companionship='$companionship' where name_id='$name_id'");
431 $sth->execute or die "-E- DB error: $DBI::errstr\n";
442 #+----------------+------------------+------+-----+---------+-------+
443 #| Field | Type | Null | Key | Default | Extra |
444 #+----------------+------------------+------+-----+---------+-------+
445 #| visit | int(16) unsigned | | PRI | 0 | A |
446 #| family | int(16) unsigned | YES | UNI | NULL | |
447 #| companionship | int(16) unsigned | YES | | NULL | |
448 #| date | date | YES | | NULL | |
449 #| notes | varchar(128) | YES | | NULL | |
450 #| visited | varchar(1) | YES | | NULL | |
451 #+----------------+------------------+------+-----+---------+-------+
452 sub update_tc_visit_table
454 print "\n-> updating tc_visit table\n";
456 my $month_header_retrieved = 0;
459 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);
460 ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
461 my %visit_status = ('X', 'y', '-', 'n', '', '');
463 foreach $index (keys %hometeaching_stats_data)
465 $hashref = $hometeaching_stats_data{$index};
466 #foreach $key (keys %$hashref) {print "$key\n";}
468 $family_name = $hometeaching_stats_data{$index}{"Preferred Name"};
469 print " Updating visit data: $family_name\n";
471 # get family id from tc_family
472 $sth = $dbh->prepare("select * from tc_family where name=\"$family_name\" and valid=1");
473 $sth->execute or die "-E- DB error: $DBI::errstr\n";
474 my @family_data = ();
475 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
476 my $family_rows = scalar @family_data;
477 if($family_rows > 0) {
478 $family_id = $family_data[0]->{'family'};
479 $comp_id = $family_data[0]->{'companionship'};
482 #print "family_id = $family_id\n";
483 #print "comp_id = $comp_id\n";
485 # ignore visits that weren't done by the EQ
486 if ($comp_id == 0) { next; }
488 # retrieve the month header if not already done
489 if ($month_header_retrieved == 0)
491 foreach $key (keys %$hashref)
493 if (($key ne "Preferred Name") && ($key ne "Home Teachers"))
495 $month_header = $key;
496 @data_months = split /\t/, $key;
499 $month_header_retrieved = 1;
502 # loop through history data
503 @history = split /\t/, $hometeaching_stats_data{$index}{$month_header};
504 my $data_year = 1900 + $yearOffset;
505 my $data_month = $months{$data_months[-1]};
506 #print "$month_header\n";
507 #print $hometeaching_stats_data{$index}{$month_header};
509 foreach $i (reverse(0..$#history)) {
510 # went back a calendar year, decrement $data_year
511 if ($months{$data_months[$i]} > $data_month)
515 $data_month = $months{$data_months[$i]};
516 my $visit_date = sprintf("%4d-%02d-01\n", $data_year, $data_month);
517 #print "$visit_date\n";
518 my $importing_status = $visit_status{$history[$i]};
519 #print "importing_status = $importing_status\n";
520 #print "select * from tc_visit where family=$family_id and companionship=$comp_id and date='$visit_date'\n";
521 $sth = $dbh->prepare("select * from tc_visit where family=$family_id and companionship=$comp_id and date='$visit_date'");
522 $sth->execute or die "-E- DB error: $DBI::errstr\n";
524 while($sqlhashref = $sth->fetchrow_hashref) { push(@visit_data, $sqlhashref); }
525 my $visit_rows = scalar @visit_data;
526 if($visit_rows > 0) {
527 my $visited = $visit_data[0]->{'visited'};
528 #print "visited = $visited\n";
529 # update visit if data is different in tc_visit
530 if ($visited ne $importing_status)
532 #print "importing_status = $importing_status\n";
533 $sth = $dbh->prepare("update tc_visit set visited='$importing_status' where family='$family_id' and date='$visit_date' and companionship='$comp_id'");
534 $sth->execute or die "-E- DB error: $DBI::errstr\n";
537 if ($importing_status ne '')
539 # add visit if it doesn't exist in tc_visit
540 $sth = $dbh->prepare("insert into tc_visit values (NULL, '$family_id', '$comp_id', '', '', '$visit_date', '', '$importing_status', 'hometeaching')");
541 $sth->execute or die "-E- DB error: $DBI::errstr\n";
548 ######################################################################
549 sub check_for_changed_ids
551 # If the Indiv ID & HofH ID has changed between data sets, we could have problems
552 my ($oldhashref, $newhashref) = @_;
553 my $found_problem = 0;
555 foreach $oldindex (keys %$oldhashref)
557 $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
558 $hofh_id = $oldhashref->{$oldindex}{'HofH ID'};
559 $full_name = $oldhashref->{$oldindex}{'Full Name'};
560 $hh_position = $oldhashref->{$oldindex}{'HH Position'};
561 if($hh_position =~ /Other/i) { next; }
563 foreach $newindex (keys %$newhashref)
565 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
566 $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
568 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
572 if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
573 $hofh_id != $newhashref->{$newindex}{'HofH ID'})
575 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
581 return $found_problem;
584 sub update_family_in_tc_indiv_table
586 print "\n-> Updating family info in tc_indiv table\n";
588 foreach $index (keys %membership_data)
590 $hashref = $membership_data{$index};
592 # get some information from hash about this individual
593 $name = $membership_data{$index}{'Preferred Name'};
594 $hofh_id = $membership_data{$index}{'HofH ID'};
596 # Find the family id for this individual's HofH_id
597 $sth = $dbh->prepare("select * from tc_family where hofh_id=$hofh_id and valid=1");
598 $sth->execute or die "-E- DB error: $DBI::errstr\n";
599 my @family_data = ();
600 while($sqlhashref = $sth->fetchrow_hashref) { push(@family_data, $sqlhashref); }
601 my $family_rows = scalar @family_data;
602 if($family_rows > 0) {
603 $family_id = $family_data[0]->{'family'};
605 print " Updating family data for: $name\n";
607 # write the family id to the individual's data in tc_indiv
608 $sth = $dbh->prepare("update tc_indiv set family='$family_id' where name=\"$name\"");
609 $sth->execute or die "-E- DB error: $DBI::errstr\n";
616 sub update_organization_class_data
618 print "\n-> Updating organization class info in tc_indiv table\n";
620 foreach $index (keys %organization_class_data)
622 # get name and organization info for each individual
623 $name = $organization_class_data{$index}{'Preferred Name'};
624 $org_class = $organization_class_data{$index}{'Organization Class'};
626 if ($org_class =~ m/Elder/i) {
627 #print " $name: Elder\n";
628 $sth = $dbh->prepare("update tc_indiv set steward='Elder' where name=\"$name\"");
629 $sth->execute or die "-E- DB error: $DBI::errstr\n";
631 if ($org_class =~ m/High Priest/i) {
632 #print " $name: High Priest\n";
633 $sth = $dbh->prepare("update tc_indiv set steward='High Priest' where name=\"$name\"");
634 $sth->execute or die "-E- DB error: $DBI::errstr\n";
639 ######################################################################
641 ######################################################################
643 ###################################################
644 # Open a connection to the database
645 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
647 PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
649 ###################################################
650 # Check old directory against new directory to ensure
651 # that the Indiv ID & HofH ID have not changed between updates
653 print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
654 my %old_membership_data = ();
655 my %new_membership_data = ();
656 &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
657 &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
659 $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
663 print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
664 print " $opt_o and $opt_n data sets.\n";
665 print " This script is not currently setup to handle this properly.\n";
667 print " Exiting without updating...\n\n";
672 ###################################################
673 # Process command line options
674 if(defined $opt_n) { $datadir = $opt_n; }
675 else { $datadir = shift(@ARGV); }
676 print "\n-> Processing all ward data files in $datadir\n";
678 ###################################################
679 # Parse Ward Data Files
680 &csv_to_hash("$datadir/Organization\ class\ per\ member.csv", \%organization_class_data);
681 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
682 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
683 &csv_to_hash("$datadir/Organization.csv",\%organization_data);
684 &optional_csv_to_hash("$datadir/Home\ Teacher\ per\ Companionship.csv", \%hometeaching_stats_data);
685 %organization_by_name = ();
686 %organization_by_id = ();
689 print "-> Membership Data Dump\n\n";
690 &print_hash(\%membership_data);
691 print "-> HomeTeaching Data Dump\n\n";
692 &print_hash(\%hometeaching_data);
693 print "-> Organization Data Dump\n\n";
694 &print_hash(\%organization_data);
695 print "-> HomeTeaching Stats Data Dump\n\n";
696 &print_hash(\%hometeaching_stats_data);
699 if($opt_s) { $dbh->disconnect(); exit; }
701 # Now update the various eq DB tables
702 &update_tc_calling_table();
703 &update_tc_indiv_table();
704 &update_tc_district_table();
705 &update_tc_companionship_table();
706 &update_tc_family_table();
707 &update_tc_visit_table();
708 &update_family_in_tc_indiv_table();
709 &update_organization_class_data();
711 print "\n-> Import Successful! DONE...\n";
713 ###################################################
714 # Disconnect from the database
717 ######################################################################