Fixed bug related to dropping family from being hometaught, still showed up under...
[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     $sth = $dbh->prepare("update eq_family set companionship=0");
321     $sth->execute or die "-E- DB error: $DBI::errstr\n";
322     
323     foreach $index (keys %membership_data)
324     {
325         $hashref = $membership_data{$index};
326         foreach $key (keys %$hashref) {
327             if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Head of Household/i) {
328                 $family_name = $membership_data{$index}{'Preferred Name'};
329                 $family_name =~ s/\'/\\'/g; #'
330                 $id = $membership_data{$index}{'HofH ID'};
331
332                 # Find out how many families match this family's name
333                 $sth = $dbh->prepare("select * from eq_family where name='$family_name'");
334                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
335                 my @data = ();
336                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
337                 my $rows = scalar @data;
338                 
339                 if($rows == 0) {
340                     # No existing records found for this family, make a new entry
341                     print "   Adding new Family: $family_name\n";
342                     $sth = $dbh->prepare("insert into eq_family values (NULL,$id,'$family_name','0',1)");
343                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
344                 } elsif($rows == 1) {
345                     # An existing record was found for this family, update it
346                     print "   Updating existing family: $family_name\n";
347                     $sth = $dbh->prepare("update eq_family set hofh_id=$id where name='$family_name'");
348                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
349                     $sth = $dbh->prepare("update eq_family set valid=1 where name='$family_name'");
350                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
351                 } else {
352                     # More than one record was found. Error! This shouldn't happen.
353                     print "   -E- More than one record found ($rows) for family name: $family_name\n";
354                 }
355                 # Now update the hometeaching field for this family
356                 foreach $index (keys %hometeaching_data)
357                 {
358                     $hashref = $hometeaching_data{$index};
359                     foreach $key (keys %$hashref) {
360                         if($hometeaching_data{$index}{'Household'} =~ /(\S+)\s+(\S+),\s+(\S+)\s+(.*)/) {
361                             print "I: Adjusting hometeaching match from: $hometeaching_data{$index}{'Household'} to $1, $3 $4\n";
362                             $hometeaching_data{$index}{'Household'} = "$1, $3 $4";
363                         }
364                         if($key =~ /Quorum/i &&
365                            $hometeaching_data{$index}{$key} =~ /Elders/i &&
366                            $hometeaching_data{$index}{'Household'} =~ /$family_name/ &&
367                            $data[0]->{companionship} != $hometeaching_data{$index}{'Comp ID'}
368                            )
369                         {
370                             print "   Updating hometeaching assignment for $family_name family\n";
371                             $companionship = $hometeaching_data{$index}{'Comp ID'};
372                             $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name='$family_name'");
373                             $sth->execute or die "-E- DB error: $DBI::errstr\n";
374                         }
375                     }
376                 }
377                 $sth->finish();
378             }
379         }
380     }
381 }
382
383 # EQ_PARENT
384 #+----------+------------------+------+-----+---------+-------+
385 #| Field    | Type             | Null | Key | Default | Extra |
386 #+----------+------------------+------+-----+---------+-------+
387 #| parent   | int(16) unsigned |      | PRI | 0       |   A   |
388 #| family   | int(16) unsigned | YES  |     | NULL    |       |
389 #| name     | varchar(30)      | YES  |     | NULL    |       |
390 #| birthday | date             | YES  |     | NULL    |       |
391 #| valid    | tinyint(1)       | YES  |     | NULL    |       |
392 #+----------+------------------+------+-----+---------+-------+
393 sub update_eq_parent_table
394 {
395     print "-> Updating eq_parent table\n";
396
397     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
398     $sth = $dbh->prepare("update eq_parent set valid=0");
399     $sth->execute or die "-E- DB error: $DBI::errstr\n";
400     
401     foreach $index (keys %membership_data)
402     {
403         $hashref = $membership_data{$index};
404         foreach $key (keys %$hashref) {
405             if($key =~ /HH Position/i &&
406                $membership_data{$index}{$key} =~ /Head of Household/i ||
407                $membership_data{$index}{$key} =~ /Spouse/i
408                ) {
409                 # Get some information from the hash about this parent
410                 $parent_name = $membership_data{$index}{'Preferred Name'};
411                 $parent_name =~ s/\'/\\'/g; #'
412                 $birthday = $membership_data{$index}{'Birth'};
413                 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
414                 $hofh_id = $membership_data{$index}{'HofH ID'};
415                 $phone = $membership_data{$index}{'Phone 1'};
416                 if($phone =~ /(\d\d\d-\d\d\d\d)/) { $phone = "970-$1"; }
417                 if($phone =~ /^\(\d\d\d\) (\d\d\d-\d\d\d\d)/) { $phone = "$1-$2"; }
418                 $address = $membership_data{$index}{'Street 1'};
419                 if($membership_data{$index}{'Street 2'} ne "") { 
420                     $address .= " " . $membership_data{$index}{'Street 2'};
421                 }
422
423                 # Find the family id for this parent's HofH_ID.
424                 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id'");
425                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
426                 $sqlhashref = $sth->fetchrow_hashref();
427                 $family_id = $sqlhashref->{'family'};
428
429                 # Find out how many parents match this parent's name
430                 $sth = $dbh->prepare("select * from eq_parent where name='$parent_name'");
431                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
432                 my @data = ();
433                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
434                 my $rows = scalar @data;
435                 
436                 if($rows == 0) {
437                     # No existing records found for this parent, make a new entry
438                     print "   Adding new Parent: $parent_name\n";
439                     $sth = $dbh->prepare("insert into eq_parent values (NULL,$family_id,'$parent_name','$year-$month-$day','$phone','$address',1)");
440                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
441                 } elsif($rows == 1) {
442                     # An existing record was found for this parent, update it
443                     print "   Updating existing parent: $parent_name\n";
444                     $sth = $dbh->prepare("update eq_parent set family='$family_id' where name='$parent_name'");
445                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
446                     $sth = $dbh->prepare("update eq_parent set birthday='$year-$month-$day' where name='$parent_name'");
447                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
448                     $sth = $dbh->prepare("update eq_parent set phone='$phone' where name='$parent_name'");
449                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
450                     $sth = $dbh->prepare("update eq_parent set address='$address' where name='$parent_name'");
451                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
452                     $sth = $dbh->prepare("update eq_parent set valid=1 where name='$parent_name'");
453                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
454             } else {
455                     # More than one record was found. Error! This shouldn't happen.
456                     print "   -E- More than one record found with same parent name: $parent_name\n";
457                 }
458                 $sth->finish();
459             }
460         }
461     }
462 }
463
464 # EQ_CHILD
465 #+----------+------------------+------+-----+---------+-------+
466 #| Field    | Type             | Null | Key | Default | Extra |
467 #+----------+------------------+------+-----+---------+-------+
468 #| child    | int(16) unsigned |      | PRI | 0       |   A   |
469 #| family   | int(16) unsigned | YES  |     | NULL    |       |
470 #| name     | varchar(30)      | YES  |     | NULL    |       |
471 #| birthday | date             | YES  |     | NULL    |       |
472 #| valid    | tinyint(1)       | YES  |     | NULL    |       |
473 #+----------+------------------+------+-----+---------+-------+
474 sub update_eq_child_table
475 {
476     print "-> Updating eq_child table\n";
477
478     # Set all records to be invalid. Only mark them as valid if they appear on the new list.
479     $sth = $dbh->prepare("update eq_child set valid=0");
480     $sth->execute or die "-E- DB error: $DBI::errstr\n";
481     
482     foreach $index (keys %membership_data)
483     {
484         $hashref = $membership_data{$index};
485         foreach $key (keys %$hashref) {
486             if($key =~ /HH Position/i && $membership_data{$index}{$key} =~ /Other/i ) {
487                 $child_name = $membership_data{$index}{'Full Name'};
488                 $child_name =~ s/\'/\\'/g; #'
489                 $birthday = $membership_data{$index}{'Birth'};
490                 $birthday =~ /(\d+) (\S+) (\d+)/; $day=$1; $month=$monthname2num{$2}; $year=$3;
491                 $id = $membership_data{$index}{'Indiv ID'};
492                 $hofh_id = $membership_data{$index}{'HofH ID'};
493
494                 # Find the family id for this child's HofH_ID.
495                 $sth = $dbh->prepare("select * from eq_family where hofh_id='$hofh_id'");
496                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
497                 $sqlhashref = $sth->fetchrow_hashref();
498                 $family_id = $sqlhashref->{'family'};
499
500                 # Find out how many children have the same name for the same family
501                 $sth = $dbh->prepare("select * from eq_child where name='$child_name'");
502                 $sth->execute or die "-E- DB error: $DBI::errstr\n";
503                 my @data = ();
504                 while($sqlhashref = $sth->fetchrow_hashref) { push(@data, $sqlhashref); }
505                 my $rows = scalar @data;
506                 
507                 if($rows == 0) {
508                     # No existing records found for this child, make a new entry
509                     print "   Adding new Child: $child_name\n";
510                     $sth = $dbh->prepare("insert into eq_child values (NULL,$family_id,'$child_name','$year-$month-$day',1)");
511                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
512                 } elsif($rows == 1) {
513                     # An existing record was found for this child, update it
514                     print "   Updating existing child: $child_name\n";
515                     $sth = $dbh->prepare("update eq_child set family='$family_id' where name='$child_name'");
516                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
517                     $sth = $dbh->prepare("update eq_child set birthday='$year-$month-$day' where name='$child_name'");
518                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
519                     $sth = $dbh->prepare("update eq_child set valid=1 where name='$child_name'");
520                     $sth->execute or die "-E- DB error: $DBI::errstr\n";
521                 } else {
522                     # More than one record was found. Error! This shouldn't happen.
523                     print "   -E- More than one record found ($rows) with same child name: $child_name\n";
524                 }
525                 $sth->finish();
526             }
527         }
528     }
529 }
530
531 ######################################################################
532 sub check_for_changed_ids
533 {
534     # If the Indiv ID & HofH ID has changed between data sets, we could have problems
535     my ($oldhashref, $newhashref) = @_;
536     my $found_problem = 0;
537     
538     foreach $oldindex (keys %$oldhashref)
539     {
540         $indiv_id = $oldhashref->{$oldindex}{'Indiv ID'};
541         $hofh_id  = $oldhashref->{$oldindex}{'HofH ID'};
542         $full_name = $oldhashref->{$oldindex}{'Full Name'};
543         $hh_position = $oldhashref->{$oldindex}{'HH Position'};
544         if($hh_position =~ /Other/i) { next; }
545
546         foreach $newindex (keys %$newhashref)
547         {
548             if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
549                $indiv_id != $newhashref->{$newindex}{'Indiv ID'})
550             {
551                 print "-W- Indiv ID for $full_name changed from $indiv_id to $newhashref->{$newindex}{'Indiv ID'}\n";
552                 $found_problem = 1;
553             }
554
555             if($newhashref->{$newindex}{'Full Name'} eq $full_name &&
556                $hofh_id != $newhashref->{$newindex}{'HofH ID'})
557             {
558                 print "-W- HofH ID for $full_name changed from $hofh_id to $newhashref->{$newindex}{'HofH ID'}\n";
559                 $found_problem = 1;
560             }
561         }
562     }
563     
564     return $found_problem;
565 }
566
567 ######################################################################
568 # MAIN
569 ######################################################################
570
571 ###################################################
572 # Open a connection to the database
573 $dbh=DBI->connect("dbi:mysql:dbname=$dbname;host=$dbhost port=$dbport",$dbuser,$dbpass,{
574     AutoCommit=>0,
575     PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
576
577 ###################################################
578 # Check old directory against new directory to ensure
579 # that the Indiv ID & HofH ID have not changed between updates
580 if(defined $opt_o) {
581     print "-> Comparing old data files to new ones: $opt_o => $opt_n\n";
582     my %old_membership_data = ();
583     my %new_membership_data = ();
584     &csv_to_hash("$opt_o/Membership.csv",\%old_membership_data);
585     &csv_to_hash("$opt_n/Membership.csv",\%new_membership_data);
586
587     $changed_ids=&check_for_changed_ids(\%old_membership_data, \%new_membership_data);
588     
589     if($changed_ids) {
590         print "\n";
591         print "-E- Some Indiv IDs and HofH IDs have changed for Head of Households between \n";
592         print "    $opt_o and $opt_n data sets.\n";
593         print "    This script is not currently setup to handle this properly.\n";
594         print "\n";
595         print "    Exiting without updating...\n\n";
596         exit;
597     }
598 }
599
600 ###################################################
601 # Process command line options
602 if(defined $opt_n) { $datadir = $opt_n; }
603 else { $datadir = shift(@ARGV); }
604 print "-> Processing all ward data files in $datadir\n";
605
606 ###################################################
607 # Parse Ward Data Files
608 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
609 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
610
611 if($opt_v) {
612     print "-> Membership Data Dump\n\n";
613     &print_hash(\%membership_data);
614     print "-> HomeTeaching Data Dump\n\n";
615     &print_hash(\%hometeaching_data);
616 }
617
618 if($opt_s) { $dbh->disconnect(); exit; }
619
620 # Now update the various eq DB tables
621 &update_eq_elder_table();
622 &update_eq_aaronic_table();
623 &update_eq_district_table();
624 &update_eq_companionship_table();
625 &update_eq_family_table();
626 &update_eq_parent_table();
627 &update_eq_child_table();
628
629 ###################################################
630 # Disconnect from the database
631 $dbh->disconnect();
632
633 ######################################################################
634
635
636
637
638
639
640
641
642
643
644
645