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