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