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