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