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