<?php
	date_default_timezone_set('America/New_York');

	class UtilitiesClients
	{
		var $username;     
		
		function UtilitiesClients(){

		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getClientData($clientID){
			global $database;
			
			$query = "
				SELECT 
					userid, 	
					social, 	
					client_name, 	
					alias_maidenName,
					spouse_otherName,
					total_assist, 	
					last_results, 	
					food_stamps, 	
					dates_foodGiven, 	
					special_programs, 	
					holiday_programs, 	
					phone,
					work_phone, 	
					phone_comments,	
					ethnicity,
					birthdate, 	
					housing_subsidies, 	
					primary_address, 	
					other_address, 	
					other_address2, 	
					other_address3, 	
					other_address4, 	
					city, 	
					state, 	
					zip, 	
					employer, 	
					drivers_licenseNumber, 	
					extra_box, 	
					extra_box2, 	
					extra_box3, 	
					extra_box4, 	
					extra_box5, 	
					extra_box6, 	
					takehome_pay, 	
					spouse_otherPay, 	
					afdc,
					child_support, 	
					social_securityReceived, 	
					wic, 	
					ssi_dis, 	
					work_first, 	
					food_stampsIncome,	
					total_income, 	
					rent_mortgage, 	
					rm_Type, 	
					water, 	
					cable_tv, 	
					daycare, 	
					electric, 	
					cpl_account, 	
					cost_heat, 	
					type_heat, 	
					other_fixedExpenses, 	
					phone_bill, 	
					total_fixedExpenses, 	
					food, 	
					medical_insurance,	
					number_cars, 	
					makemodel_car1, 	
					car_payment1, 	
					makemodel_car2, 	
					car_payment2, 	
					car_insurance, 	
					total_otherExpenses, 	
					notes_history, 	
					status, 	
					date, 	
					children_info, 	
					email_address, 	
					times_foodGiven,
					childcareScholarship, 	
					mkScholarship, 			
					otherDorcasPrograms,
					educationCredits, 		
					otherExternalPrograms,
					username,
					isactive,
					ReferredTo,
					CreditCardPayments,
					Deficit,
					ChecksIssued,
					ChildSupportExpense,
					NaturalGasExpense,
					CellPhoneExpense,
					OtherMedicalExpense,
					GasExpense,
					OtherTransportationExpense,
					Gender
				FROM 
					".TBLCLIENT." 
				WHERE 
					userid = '$clientID'
			";
			
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			$dbarray = mysql_fetch_array($result);
			return $dbarray;
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getActivity($clientID, $limit){
			global $database;
			
			$query = "
				SELECT 
					indexid,
					clientid,
					activitytype,
					activity,
					username,
					Type,
					datemodified
				FROM 
					".TBLCLIENTACTIVITY." 
				WHERE 
					clientid = '$clientID'
				ORDER BY datemodified ASC
				$limit
			";
			
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			for($i=0; $i<$numRows; $i++){
				$indexid = mysql_result($result, $i, "indexid");
				$clientid = mysql_result($result, $i, "clientid");
				$activitytype = mysql_result($result, $i, "activitytype");
				$activity = mysql_result($result, $i, "activity");
				$username = mysql_result($result, $i, "username");
				$datemodified = mysql_result($result, $i, "datemodified");
				$Type = mysql_result($result, $i, "Type");
				
				$userData = $database->getUserInfo($username);
				
				switch($activitytype){
					case "CLIENT VISIT":
						$clientActivityDesc = "Client came in for visit on " . date("l F j, Y", strtotime($activity)) . " and saw " . $userData["name"];
						break;
					case "CLIENT INFORMATION UPDATED":
						$clientActivityDesc = "Client information was updated by ". $userData["name"] ." on " . date("l F j, Y", strtotime($activity));
						break;
					case CLIENTNOTESUPDATE:
						$clientActivityDesc = "Client Notes update by " . $userData["name"] . " on " . date("l F j, Y", strtotime($activity));
						break;
				}
				
				
				echo "<div class='message $Type'> 
					<h3>$activitytype</h3> 
				
						$clientActivityDesc
					
				</div>";
			}
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getClientNotes($clientID, $sUsername){
			global $database;
			
			$query = "
				SELECT 
					IndexID,
					ClientID,
					Notes,
					DateCreated,
					Type,
					Username
				FROM 
					" . TBLCLIENTNOTES . " 
				WHERE 
					clientid = '$clientID'
				ORDER BY DATECREATED DESC
			";
			
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			for($i=0; $i<$numRows; $i++){
				$indexid = mysql_result($result, $i, "indexid");
				$clientid = mysql_result($result, $i, "clientid");
				$Notes = nl2br(mysql_result($result, $i, "Notes"));
				$DateCreated = mysql_result($result, $i, "DateCreated");
				$Type = mysql_result($result, $i, "Type");
				$Username = mysql_result($result, $i, "Username");
				$NoteDate = date("l F j, Y", strtotime($DateCreated));
				$userData = $database->getUserInfo($Username);
				$NoteDescription = "Note added $NoteDate by " . $userData["name"];

				if($Username == $sUsername)
				{
					echo "<div class='message $Type'> 
						<h3>$NoteDescription</h3> 
					
							$Notes
						<a class='editnotes' href='#editNote.php?noteID=$indexid&clientID=$clientID'>Edit Notes</a>
					</div>";
				}
				else
				{
				
					echo "<div class='message $Type'> 
						<h3>$NoteDescription</h3> 
					
							$Notes
						
					</div>";
				}
			}
		}//END getClientNotes
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getClientStatus($clientID){
			global $database;
			
			$query = "
				SELECT 
					status,
					client_name
				FROM 
					".TBLCLIENT." 
				WHERE 
					userid = '$clientID'
			";
			
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			for($i=0; $i<$numRows; $i++){
				$status = mysql_result($result, $i, "status");
				$clientName = mysql_result($result, $i, "client_name");
				
				switch($status){
			
					case 2:
						$type = "warning";
						$typeDesc = "Use caution when providing assitance to " . $clientName;
						break;
					case 3:
						$type = "warning";
						$typeDesc = "Use caution when providing assitance to " . $clientName;
						break;
					
				}
				
				echo "<div class='message $type'> 
					<h3>$NoteDescription</h3> 
				
						$typeDesc
					
				</div>";
			}
		}//END getClientStatus
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getCurrentClientID(){
			global $database;
			
			$query ="
				SELECT MAX(userid) AS CurrentClientID FROM ".TBLCLIENT;
			
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			for($i=0; $i<$numRows; $i++){
				$CurrentClientID  = mysql_result($result, $i, "CurrentClientID");	
			}	
			return $CurrentClientID;			
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function setClientNotes($insertedClientID, $notesAndHistory, $now, $notesTYPE, $username){
			global $database;
			
			$insertedClientID = mysql_real_escape_string($insertedClientID);
			$notesAndHistory = mysql_real_escape_string($notesAndHistory);
			$now = mysql_real_escape_string($now);
			$notesTYPE = mysql_real_escape_string($notesTYPE);
			$username = mysql_real_escape_string($username);
			
			$query = "
				INSERT INTO ".TBLCLIENTNOTES."
				(
					ClientID, Notes, DateCreated, Type, Username
				)
				VALUES
				(
					'$insertedClientID', '$notesAndHistory', NOW(), '$notesTYPE', '$username'
				)
			";
			
			$result = $database->query($query);
			
			if(!$result){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
				return;
			}
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getSSN(){
			global $database, $encryption;
			$query = "
				SELECT 
					social,
					userid
				FROM 
					client";
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			for($i=0; $i<$numRows; $i++){
				$userid  = mysql_result($result, $i, "userid");	
				$social  = mysql_result($result, $i, "social");	
				echo "User ID: " . $userid;
				
				
				$this->encrypSSN(mysql_real_escape_string($userid), mysql_real_escape_string($encryption->encrypt($social)));
			}
				
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function encrypSSN($userid, $ssn){
			global $database;
			$query="
				UPDATE ".TBLCLIENT."
					SET	social = '$ssn'
				WHERE userid = '$userid';
			";
			
			$result = $database->query($query);
			
			if(!$result){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
				return;
			}
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getNote($sUsername, $noteID, $clientID){
			global $database;
			
			$query="
				SELECT
					IndexID, 
					ClientID, 
					Notes, 
					Username
				FROM
					".TBLCLIENTNOTES."
				WHERE
					username = '$sUsername'
				AND
					IndexID = '$noteID'
				AND
					clientID = '$clientID'
			";

			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			if($numRows == 0)
			{
				$query="
				SELECT
					Username
				FROM
					".TBLCLIENTNOTES."
				WHERE
					IndexID = '$noteID'
				AND
					clientID = '$clientID'
				";

				$result = $database->query($query);
				$numRows = mysql_numrows($result);
				if(!$result || $numRows < 0){
					if(DEBUG == true){
						printf("Error in query: %s", $query);
					}
				}
				for($i=0; $i<$numRows; $i++){
					$Username  = mysql_result($result, $i, "Username");	
					$userData = $database->getUserInfo($Username);
					$noteUser = $userData["name"];
					echo "
						<div class='clearfix'>
							<label for='form-Note' class='form-label'>Note</label>
							<div class='form-input form-textarea'><div class='noteError'>You do not have permission to edit this note. Only $noteUser may edit this note.</div></div>
						</div>

						<div class='form-action clearfix'>
							<a class='button' href='#clientForm.php?clientID=$clientID'>Back To Client Record</a>
						</div>";
				}
			}
			else
			{
			
			
				for($i=0; $i<$numRows; $i++){
					$Username  = mysql_result($result, $i, "Username");	
					$Notes  = mysql_result($result, $i, "Notes");	
					echo"
					<div class='clearfix'>

							<label for='form-Note' class='form-label'>Note</label>
				
							<div class='form-input form-textarea'><textarea id='form-Note' name='Note' rows='5'>$Notes</textarea></div>

						</div>
						
						<div class='form-action clearfix'>
							<input type='hidden' name='updateNote' value='1'>
							<input type='hidden' name='noteID' value='$noteID'>
							<input type='hidden' name='username' value='$sUsername'>
							<input type='hidden' name='clientID' value='$clientID'>
							<button class='button' type='submit'>OK</button>
						</div>";
					
					
				}//END Loop through data result set
			}
		}//END getNote
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		
		
	};

	$clients = new UtilitiesClients;
?>