From c57e6e155b11fb24d9e2a9e29763db98b7d4bf21 Mon Sep 17 00:00:00 2001
From: "Alan J. Pippin" <ajp@server.pippins.net>
Date: Wed, 13 Feb 2008 08:06:55 -0700
Subject: [PATCH] Fixed bug related to not being able to edit hometeaching
 history for a family once they had been reassigned to a new companionship.

---
 bin/import_ward_data | 10 ----------
 inc/class.eq.inc.php | 29 ++++++++++++++++++++++++++---
 2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/bin/import_ward_data b/bin/import_ward_data
index 9b830aa..0e04f05 100755
--- a/bin/import_ward_data
+++ b/bin/import_ward_data
@@ -447,16 +447,6 @@ sub update_eq_family_table
 			    $companionship = $hometeaching_data{$index}{'Comp ID'};
 			    $sth = $dbh->prepare("update eq_family set companionship='$companionship' where name_id='$name_id'");
 			    $sth->execute or die "-E- DB error: $DBI::errstr\n";
-			    
-			    # In addition to changing the hometeaching assignment, update the eq_visit table
-			    # so that all previous visits made by the old hometeaching companionship, get
-			    # updated with the id of the new companionship. This will allow us to go in and
-			    # edit the history as needed for the family being hometaught under their new
-			    # companionship. Otherwise, we will not be able to update this history.
-			    #$family_id = $data[0]->{family};
-			    #print "   update eq_visit set companionship='$companionship' where family='$family_id' and companionship!='0'\n";
-			    #$sth = $dbh->prepare("update eq_visit set companionship='$companionship' where companionship='$old_companionship'");
-			    #$sth->execute or die "-E- DB error: $DBI::errstr\n";
 			}
 		    }
 		}
diff --git a/inc/class.eq.inc.php b/inc/class.eq.inc.php
index 2af40b1..4574d4b 100644
--- a/inc/class.eq.inc.php
+++ b/inc/class.eq.inc.php
@@ -294,12 +294,24 @@ class eq
 		$month_start = "$year"."-"."$month"."-"."01";
 		$month_end = "$year"."-"."$month"."-"."31";
 		$month = "$month"."/"."$year";
+		
 		//print "m: $m month: $month year: $year month_start: $month_start month_end: $month_end<br>";
 		// Add this to the query to filter on only visits made by this companionship:
 		// " AND companionship=" . $unique_companionships[$j]['companionship'].
+
+		// First check to see if the currently assigned companionship has visited them
 		$sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
-		       " AND companionship!=0".
- 		       " AND family=". $family_id;
+	           " AND companionship=".$unique_companionships[$j]['companionship'].
+	           " AND family=". $family_id;
+		$query_id = $this->db2->query($sql,__LINE__,__FILE__);
+		if($this->db2->num_rows($query_id) == 0) {
+		  // We did not find any visits made by the currently assigned companionship,
+		  // look for visits made by any other companionship other than 0. (0 == EQ Presidency Visit)
+		  $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
+		     " AND companionship!=0".
+		     " AND family=". $family_id;
+		  $query_id = $this->db2->query($sql,__LINE__,__FILE__);
+		}
 		$this->db2->query($sql,__LINE__,__FILE__);
 		$link_data['menuaction'] = 'eq.eq.ht_update';
 		$link_data['date'] = $month_start;
@@ -507,10 +519,21 @@ class eq
 	    $table_data.="<tr bgcolor=". $this->t->get_var('tr_color') ."><td>$family_name Family</td>";
 	    
 	    $header_row="<th width=$comp_width><font size=-2>Families</th>";
+
+	    // First check to see if the currently assigned companionship has visited them
 	    $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
+	           " AND companionship=".$unique_companionships[$j]['companionship'].
+	           " AND family=". $family_id;
+	    $query_id = $this->db2->query($sql,__LINE__,__FILE__);
+	    if($this->db2->num_rows($query_id) == 0) {
+	      // We did not find any visits made by the currently assigned companionship,
+	      // look for visits made by any other companionship other than 0. (0 == EQ Presidency Visit)
+	      $sql = "SELECT * FROM eq_visit WHERE date >= '$month_start' AND date <= '$month_end' ".
 	           " AND companionship!=0".
 	           " AND family=". $family_id;
-	    $this->db2->query($sql,__LINE__,__FILE__);
+	      $query_id = $this->db2->query($sql,__LINE__,__FILE__);
+	    }
+	    
 	    $value = $family_id . "/" . $unique_companionships[$j]['companionship'] . "/" . $date;
 	    $header_row .= "<th width=$visit_width><font size=-2><a href=$link>$month</a></th>";
 	    if(!$total_visits) { $total_visits = 0; }
-- 
2.34.1