Added new phone field to eq_elder table.
[eq/.git] / import_ward_data
1 #!/usr/bin/perl
2
3 use DBI;
4 use Getopt::Std;
5 ###################################################
6 # GLOBALS
7 $dbname = "phpgroupware";
8 $dbhost = "192.168.0.2";
9 $dbport = 3306;
10 $dbuser = "phpgroupware";
11 $dbpass = "phpgroupware";
12 %hometeaching_data = ();
13 %membership_data = ();
14 getopts('vsn:o:');
15
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';
28
29 ######################################################################
30 # SUBROUTINES
31 ######################################################################
32 sub csv_to_hash
33 {
34     my ($filename, $hashref) = @_;
35
36     open(FILE,$filename) || die "-E- Could not open $filename for reading\n";
37
38     my $found_header = 0; my $index = 0;
39     while(<FILE>)
40     {
41         $line = $_;
42         @data = split /\",/, $line;
43         if(!$found_header) { @header = @data; $found_header = 1; }
44         else {
45             foreach $i (0..$#data-1) {
46                 $data[$i] =~ s/\"//g;
47                 $header[$i] =~ s/\"//g;
48                 $hashref->{$index}{$header[$i]} = $data[$i];
49                 #print "$index: $i: $header[$i]: $data[$i]\n";
50             }
51             $index++;
52         }
53     }
54     
55     close(FILE);
56 }
57
58 ######################################################################
59 sub print_hash
60 {
61     my ($hashref) = @_;
62
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";
67         }
68         print "\n";
69     }
70 }
71
72 ######################################################################
73
74 # EQ_AARONIC
75 #+-------+--------------------+------+-----+---------+-------+
76 #| Field | Type               | Null | Key | Default | Extra |
77 #+-------+--------------------+------+-----+---------+-------+
78 #| aaronic | int(16) unsigned |      | PRI | 0       |   A   |
79 #| name    | varchar(60)      | YES  |     | NULL    |       |
80 #| valid   | tinyint(1)       | YES  |     | NULL    |       |
81 #+-------+--------------------+------+-----+---------+-------+
82 sub update_eq_aaronic_table
83 {
84     print "-> Updating eq_aaronic table\n";
85     foreach $index (keys %membership_data)
86     {
87         $hashref = $membership_data{$index};
88         foreach $key (keys %$hashref) {
89             if($key =~ /Priesthood/i &&
90                ($membership_data{$index}{$key} =~ /^Teacher\s*$/i ||
91                 $membership_data{$index}{$key} =~ /^Priest\s*$/i)) {
92                 $aaronic_name = $membership_data{$index}{'Preferred Name'};
93                 $sth = $dbh->prepare("select * from eq_aaronic where name='$aaronic_name'");
94                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
95                 my @data = ();
96                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
97                 my $rows = scalar @data;
98                 if($rows == 0) {
99                     # No existing records found for this aaronic, make a new entry
100                     print "   Adding new Aaronic: $aaronic_name\n";
101                     $sth = $dbh->prepare("insert into eq_aaronic values (NULL,'$aaronic_name',1)");
102                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
103                 } elsif($rows == 1) {
104                     # An existing record was found for this aaronic, update it if it is valid
105                     if($data[0]->{valid} == 1) {
106                         print "   Updating existing aaronic: $aaronic_name\n";
107                     }
108                 } else {
109                     # More than one record was found. Error! This shouldn't happen.
110                     print "   -E- More than one record found ($rows) for aaronic name: $aaronic_name\n";
111                 }
112             }
113         }
114     }
115     $sth->finish();
116 }
117
118 # EQ_ELDER
119 #+------------+------------------+------+-----+---------+-------+
120 #| Field      | Type             | Null | Key | Default | Extra |
121 #+------------+------------------+------+-----+---------+-------+
122 #| elder      | int(16) unsigned |      | PRI | 0       |   A   |
123 #| name       | varchar(60)      | YES  |     | NULL    |       |
124 #| phone      | varchar(12)      | YES  |     | NULL    |       |
125 #| valid      | tinyint(1)       | YES  |     | NULL    |       |
126 #+------------+------------------+------+-----+---------+-------+
127 sub update_eq_elder_table
128 {
129     print "-> Updating eq_elder table\n";
130
131     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
132     $sth = $dbh->prepare("update eq_elder set valid=0");
133     $sth->execute or die "-E- DB error: $DBI::errstr\n";
134     
135     foreach $index (keys %membership_data)
136     {
137         $hashref = $membership_data{$index};
138         foreach $key (keys %$hashref) {
139             if($key =~ /Priesthood/i && $membership_data{$index}{$key} =~ /Elder/i) {
140                 $elder_name = $membership_data{$index}{'Preferred Name'};
141                 $phone = $membership_data{$index}{'Phone 1'};
142                 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
143                 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
144                 $sth = $dbh->prepare("select * from eq_elder where name='$elder_name'");
145                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
146                 my @data = ();
147                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
148                 my $rows = scalar @data;
149                 if($rows == 0) {
150                     # No existing records found for this elder, make a new entry
151                     print "   Adding new Elder: $elder_name\n";
152                     $sth = $dbh->prepare("insert into eq_elder values (NULL,'$elder_name','$phone',1)");
153                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
154                 } elsif($rows == 1) {
155                     # An existing record was found for this elder, update it
156                     print "   Updating existing Elder: $elder_name\n";
157                     $sth = $dbh->prepare("update eq_elder set valid=1 where name='$elder_name'");
158                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
159                     if($phone ne "") { 
160                         $sth = $dbh->prepare("update eq_elder set phone='$phone' where name='$elder_name'");
161                     } else {
162                         $sth = $dbh->prepare("update eq_elder set phone=NULL where name='$elder_name'");
163                     }
164                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
165                 } else {
166                     # More than one record was found. Error! This shouldn't happen.
167                     print "   -E- More than one record found ($rows) for Elder: $elder_name\n";
168                 }
169             }
170         }
171     }
172     $sth->finish();
173 }
174
175 # EQ_DISTRICT
176 #+------------+------------------+------+-----+---------+-------+
177 #| Field      | Type             | Null | Key | Default | Extra |
178 #+------------+------------------+------+-----+---------+-------+
179 #| district   | int(16) unsigned |      | PRI | 0       |       |
180 #| name       | varchar(30)      | YES  |     | NULL    |       |
181 #| supervisor | int(16) unsigned | YES  |     | NULL    |       |
182 #| valid      | tinyint(1)       | YES  |     | NULL    |       |
183 #+------------+------------------+------+-----+---------+-------+
184 sub update_eq_district_table
185 {
186     # Districts should be created by hand. This subroutine only
187     # updates the supervisor's ID in each district.
188     print "-> Updating eq_district table\n";
189     $sth = $dbh->prepare("select * from eq_district");
190     $sth->execute or die "-E- DB error: $DBI::errstr\n";
191     while($sqlhashref = $sth->fetchrow_hashref) {
192         $supervisor_name = $sqlhashref->{name};
193         $district = $sqlhashref->{district};
194         $sth2 = $dbh->prepare("select * from eq_elder where name='$supervisor_name'");
195         $sth2->execute or die "-E- DB error: $DBI::errstr\n";
196         $sqlhashref2 = $sth2->fetchrow_hashref;
197         $supervisor_id = $sqlhashref2->{elder};
198         $sth2->finish();
199         $sth2 = $dbh->prepare("update eq_district set supervisor='$supervisor_id' where district='$district'");
200         $sth2->execute or die "-E- DB error: $DBI::errstr\n";
201         $sth2->finish();
202     }
203     $sth->finish();
204 }
205
206 # EQ_COMPANIONSHIP
207 #+---------------+------------------+------+-----+---------+-------+
208 #| Field         | Type             | Null | Key | Default | Extra |
209 #+---------------+------------------+------+-----+---------+-------+
210 #| companionship | int(16) unsigned |      |     | 0       |       |
211 #| elder         | int(16) unsigned | YES  |     | NULL    |       |
212 #| aaronic       | int(16) unsigned | YES  |     | NULL    |       |
213 #| district      | int(16) unsigned | YES  |     | NULL    |       |
214 #| valid         | tinyint(1)       | YES  |     | NULL    |       |
215 #+---------------+------------------+------+-----+---------+-------+
216 sub update_eq_companionship_table
217 {
218     print "-> Updating eq_companionship table\n";
219
220     # First, mark all existing companionships as invalid in case they have been dissolved
221     $sth = $dbh->prepare("update eq_companionship set valid=0");
222     $sth->execute or die "-E- DB error: $DBI::errstr\n";
223     # Second, mark all the aaronic invalid. We'll only mark the ones as valid that are assigned to hometeach
224     $sth = $dbh->prepare("update eq_aaronic set valid=0");
225     $sth->execute or die "-E- DB error: $DBI::errstr\n";
226     
227     foreach $index (keys %hometeaching_data)
228     {
229         $hashref = $hometeaching_data{$index};
230         foreach $key (keys %$hashref) {
231             if($key =~ /Quorum/i && $hometeaching_data{$index}{$key} =~ /Elders/i) {
232                 foreach $field ("Home Teacher 1","Home Teacher 2") {
233                     $elder_name = $hometeaching_data{$index}{$field};
234                     if($elder_name eq "") { next; }
235                     $sth2 = $dbh->prepare("select * from eq_elder where name='$elder_name'");
236                     $sth2->execute or die "-E- DB error: $DBI::errstr\n";
237                     $sqlhashref2 = $sth2->fetchrow_hashref;
238                     $elder = $sqlhashref2->{elder};
239                     $aaronic = "NULL";
240                     if($elder eq "") {
241                         $sth2 = $dbh->prepare("select * from eq_aaronic where name='$elder_name'");
242                         $sth2->execute or die "-E- DB error: $DBI::errstr\n";
243                         $sqlhashref2 = $sth2->fetchrow_hashref;
244                         $aaronic = $sqlhashref2->{aaronic};
245                         $elder = "NULL";
246                         if($aaronic eq "") { print "-W- Unable to find $elder_name in eq_elder or eq_aaronic tables\n"; }
247                     } 
248                     $id = $hometeaching_data{$index}{'Comp ID'};
249                     $district = $hometeaching_data{$index}{'HT District'};
250                     $sth = $dbh->prepare("select * from eq_companionship where elder='$elder' and aaronic='$aaronic' and companionship='$id'");
251                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
252                     my @data = ();
253                     while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
254                     my $rows = scalar @data;
255                     if($rows == 0) {
256                         # No existing records found for this companionship, make a new entry
257                         print "   Adding Elder to companionship: $elder_name -> $id\n";
258                         $sth = $dbh->prepare("insert into eq_companionship values ($id,'$elder','$aaronic','$district',1)");
259                         $sth->execute or die "-E- DB error: $DBI::errstr\n";
260                     } else {
261                         # An existing companionship was found for this companionship, update it
262                         $sth2 = $dbh->prepare("select * from eq_companionship where district='$district' and companionship='$id'");
263                         $sth2->execute or die "-E- DB error: $DBI::errstr\n";
264                         if($elder ne "NULL") {
265                             print "   Updating Companionship with Elder: $elder_name ($elder) -> $id\n";
266                             $sth = $dbh->prepare("update eq_companionship set district='$district' where elder='$elder' and companionship='$id'");
267                             $sth->execute or die "-E- DB error 'district': $DBI::errstr\n";
268                             $sth = $dbh->prepare("update eq_companionship set elder='$elder' where elder='$elder' and companionship='$id'");
269                             $sth->execute or die "-E- DB error 'elder': $DBI::errstr\n";
270                             $sth = $dbh->prepare("update eq_companionship set valid=1 where elder='$elder' and companionship='$id'");
271                             $sth->execute or die "-E- DB error 'valid': $DBI::errstr\n";
272                         } else {
273                             print "   Updating Companionship with Aaronic: $elder_name ($aaronic) -> $id\n";
274                             $sth = $dbh->prepare("update eq_companionship set district='$district' where aaronic='$aaronic' and companionship='$id'");
275                             $sth->execute or die "-E- DB error: $DBI::errstr\n";
276                             $sth = $dbh->prepare("update eq_companionship set aaronic='$aaronic' where aaronic='$aaronic' and companionship='$id'");
277                             $sth->execute or die "-E- DB error: $DBI::errstr\n";
278                             $sth = $dbh->prepare("update eq_companionship set valid=1 where aaronic='$aaronic' and companionship='$id'");
279                             $sth->execute or die "-E- DB error: $DBI::errstr\n";                            
280                             $sth = $dbh->prepare("update eq_aaronic set valid=1 where aaronic='$aaronic'");
281                             $sth->execute or die "-E- DB error: $DBI::errstr\n";
282                         }
283                     }
284                     $sth->finish();
285                     $sth2->finish();                
286                 }
287             }
288         }
289     }
290 }
291
292 # EQ_FAMILY
293 #+---------------+------------------+------+-----+---------+-------+
294 #| Field         | Type             | Null | Key | Default | Extra |
295 #+---------------+------------------+------+-----+---------+-------+
296 #| family        | int(16) unsigned |      | PRI | 0       |   A   |
297 #| hofh_id       | int(16) unsigned | YES  |     | NULL    |       |
298 #| name          | varchar(30)      | YES  |     | NULL    |       |
299 #| companionship | int(16) unsigned | YES  |     | NULL    |       |
300 #| valid         | tinyint(1)       | YES  |     | NULL    |       |
301 #+---------------+------------------+------+-----+---------+-------+
302 sub update_eq_family_table
303 {
304     print "-> Updating eq_family table\n";
305
306     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
307     $sth = $dbh->prepare("update eq_family set valid=0");
308     $sth->execute or die "-E- DB error: $DBI::errstr\n";
309     
310     foreach $index (keys %membership_data)
311     {
312         $hashref = $membership_data{$index};
313         foreach $key (keys %$hashref) {
314             if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
315                 $family_name = $membership_data{$index}{'Preferred Name'};
316                 $family_name =~ s/\'/\\'/g; #'
317                 $id = $membership_data{$index}{'HofH ID'};
318
319                 # Find out how many families match this family's name
320                 $sth = $dbh->prepare("select * from eq_family where name='$family_name'");
321                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
322                 my @data = ();
323                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
324                 my $rows = scalar @data;
325                 
326                 if($rows == 0) {
327                     # No existing records found for this family, make a new entry
328                     print "   Adding new Family: $family_name\n";
329                     $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','0',1)");
330                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
331                 } elsif($rows == 1) {
332                     # An existing record was found for this family, update it
333                     print "   Updating existing family: $family_name\n";
334                     $sth = $dbh->prepare("update eq_family set hofh_id=$id where name='$family_name'");
335                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
336                     $sth = $dbh->prepare("update eq_family set valid=1 where name='$family_name'");
337                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
338                 } else {
339                     # More than one record was found. Error! This shouldn't happen.
340                     print "   -E- More than one record found ($rows) for family name: $family_name\n";
341                 }
342                 # Now update the hometeaching field for this family
343                 foreach $index (keys %hometeaching_data)
344                 {
345                     $hashref = $hometeaching_data{$index};
346                     foreach $key (keys %$hashref) {
347                         if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
348                             print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $1, $3 $4\n";
349                             $hometeaching_data{$index}{'Household'} = "$1, $3 $4";
350                         }
351                         if($key =~ /Quorum/i &&
352                            $hometeaching_data{$index}{$key} =~ /Elders/i &&
353                            $hometeaching_data{$index}{'Household'} =~ /$family_name/ &&
354                            $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
355                            )
356                         {
357                             print "   Updating hometeaching assignment for $family_name family\n";
358                             $companionship = $hometeaching_data{$index}{'Comp ID'};
359                             $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name='$family_name'");
360                             $sth->execute or die "-E- DB error: $DBI::errstr\n";
361                         }
362                     }
363                 }
364                 $sth->finish();
365             }
366         }
367     }
368 }
369
370 # EQ_PARENT
371 #+----------+------------------+------+-----+---------+-------+
372 #| Field    | Type             | Null | Key | Default | Extra |
373 #+----------+------------------+------+-----+---------+-------+
374 #| parent   | int(16) unsigned |      | PRI | 0       |   A   |
375 #| family   | int(16) unsigned | YES  |     | NULL    |       |
376 #| name     | varchar(30)      | YES  |     | NULL    |       |
377 #| birthday | date             | YES  |     | NULL    |       |
378 #| valid    | tinyint(1)       | YES  |     | NULL    |       |
379 #+----------+------------------+------+-----+---------+-------+
380 sub update_eq_parent_table
381 {
382     print "-> Updating eq_parent table\n";
383
384     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
385     $sth = $dbh->prepare("update eq_parent set valid=0");
386     $sth->execute or die "-E- DB error: $DBI::errstr\n";
387     
388     foreach $index (keys %membership_data)
389     {
390         $hashref = $membership_data{$index};
391         foreach $key (keys %$hashref) {
392             if($key =~ /HH Position/i &&
393                $membership_data{$index}{$key} =~ /Head of Household/i ||
394                $membership_data{$index}{$key} =~ /Spouse/i
395                ) {
396                 # Get some information from the hash about this parent
397                 $parent_name = $membership_data{$index}{'Preferred Name'};
398                 $parent_name =~ s/\'/\\'/g; #'
399                 $birthday = $membership_data{$index}{'Birth'};
400                 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
401                 $hofh_id = $membership_data{$index}{'HofH ID'};
402
403                 # Find the family id for this parent's HofH_ID.
404                 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id'");
405                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
406                 $sqlhashref = $sth->fetchrow_hashref();
407                 $family_id = $sqlhashref->{'family'};
408
409                 # Find out how many parents match this parent's name
410                 $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
411                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
412                 my @data = ();
413                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
414                 my $rows = scalar @data;
415                 
416                 if($rows == 0) {
417                     # No existing records found for this parent, make a new entry
418                     print "   Adding new Parent: $parent_name\n";
419                     $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day',1)");
420                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
421                 } elsif($rows == 1) {
422                     # An existing record was found for this parent, update it
423                     print "   Updating existing parent: $parent_name\n";
424                     $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
425                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
426                     $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
427                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
428                     $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
429                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
430             } else {
431                     # More than one record was found. Error! This shouldn't happen.
432                     print "   -E- More than one record found with same parent name: $parent_name\n";
433                 }
434                 $sth->finish();
435             }
436         }
437     }
438 }
439
440 # EQ_CHILD
441 #+----------+------------------+------+-----+---------+-------+
442 #| Field    | Type             | Null | Key | Default | Extra |
443 #+----------+------------------+------+-----+---------+-------+
444 #| child    | int(16) unsigned |      | PRI | 0       |   A   |
445 #| family   | int(16) unsigned | YES  |     | NULL    |       |
446 #| name     | varchar(30)      | YES  |     | NULL    |       |
447 #| birthday | date             | YES  |     | NULL    |       |
448 #| valid    | tinyint(1)       | YES  |     | NULL    |       |
449 #+----------+------------------+------+-----+---------+-------+
450 sub update_eq_child_table
451 {
452     print "-> Updating eq_child table\n";
453
454     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
455     $sth = $dbh->prepare("update eq_child set valid=0");
456     $sth->execute or die "-E- DB error: $DBI::errstr\n";
457     
458     foreach $index (keys %membership_data)
459     {
460         $hashref = $membership_data{$index};
461         foreach $key (keys %$hashref) {
462             if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
463                 $child_name = $membership_data{$index}{'Full Name'};
464                 $child_name =~ s/\'/\\'/g; #'
465                 $birthday = $membership_data{$index}{'Birth'};
466                 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
467                 $id = $membership_data{$index}{'Indiv ID'};
468                 $hofh_id = $membership_data{$index}{'HofH ID'};
469
470                 # Find the family id for this child's HofH_ID.
471                 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id'");
472                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
473                 $sqlhashref = $sth->fetchrow_hashref();
474                 $family_id = $sqlhashref->{'family'};
475
476                 # Find out how many children have the same name for the same family
477                 $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
478                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
479                 my @data = ();
480                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
481                 my $rows = scalar @data;
482                 
483                 if($rows == 0) {
484                     # No existing records found for this child, make a new entry
485                     print "   Adding new Child: $child_name\n";
486                     $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day',1)");
487                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
488                 } elsif($rows == 1) {
489                     # An existing record was found for this child, update it
490                     print "   Updating existing child: $child_name\n";
491                     $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
492                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
493                     $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
494                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
495                     $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
496                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
497                 } else {
498                     # More than one record was found. Error! This shouldn't happen.
499                     print "   -E- More than one record found ($rows) with same child name: $child_name\n";
500                 }
501                 $sth->finish();
502             }
503         }
504     }
505 }
506
507 ######################################################################
508 sub check_for_changed_ids
509 {
510     # If the Indiv ID & HofH ID has changed between data sets, we could have problems
511     my ($oldhashref, $newhashref) = @_;
512     my $found_problem = 0;
513     
514     foreach $oldindex (keys %$oldhashref)
515     {
516         $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
517         $hofh_id  = $oldhashref->{$oldindex}{'HofH ID'};
518         $full_name = $oldhashref->{$oldindex}{'Full Name'};
519         $hh_position = $oldhashref->{$oldindex}{'HH Position'};
520         if($hh_position =~ /Other/i) { next; }
521
522         foreach $newindex (keys %$newhashref)
523         {
524             if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
525                $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
526             {
527                 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
528                 $found_problem = 1;
529             }
530
531             if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
532                $hofh_id != $newhashref->{$newindex}{'HofH ID'})
533             {
534                 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
535                 $found_problem = 1;
536             }
537         }
538     }
539     
540     return $found_problem;
541 }
542
543 ######################################################################
544 # MAIN
545 ######################################################################
546
547 ###################################################
548 # Open a connection to the database
549 $dbh=DBI->connect("dbi:mysql:dbname=$dbname;host=$dbhost port=$dbport",$dbuser,$dbpass,{
550     AutoCommit=>0,
551     PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
552
553 ###################################################
554 # Check old directory against new directory to ensure
555 # that the Indiv ID & HofH ID have not changed between updates
556 if(defined $opt_o) {
557     print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
558     my %old_membership_data = ();
559     my %new_membership_data = ();
560     &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
561     &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
562
563     $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
564     
565     if($changed_ids) {
566         print "\n";
567         print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
568         print "    $opt_o and $opt_n data sets.\n";
569         print "    This script is not currently setup to handle this properly.\n";
570         print "\n";
571         print "    Exiting without updating...\n\n";
572         exit;
573     }
574 }
575
576 ###################################################
577 # Process command line options
578 if(defined $opt_n) { $datadir = $opt_n; }
579 else { $datadir = shift(@ARGV); }
580 print "-> Processing all ward data files in $datadir\n";
581
582 ###################################################
583 # Parse Ward Data Files
584 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
585 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
586
587 if($opt_v) {
588     print "-> Membership Data Dump\n\n";
589     &print_hash(\%membership_data);
590     print "-> HomeTeaching Data Dump\n\n";
591     &print_hash(\%hometeaching_data);
592 }
593
594 if($opt_s) { $dbh->disconnect(); exit; }
595
596 # Now update the various eq DB tables
597 &update_eq_elder_table();
598 &update_eq_aaronic_table();
599 &update_eq_district_table();
600 &update_eq_companionship_table();
601 &update_eq_family_table();
602 &update_eq_parent_table();
603 &update_eq_child_table();
604
605 ###################################################
606 # Disconnect from the database
607 $dbh->disconnect();
608
609 ######################################################################
610
611
612
613
614
615
616
617
618
619
620
621