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