<?php
require_once 'MU_database2024.php';
$dbObject = new MySQLDB;
// what's happening here.
// we're going to get a GUID, a method, an answer, and one or two parameters.
// from this, we'll either set a bunch of data, 
// or set some data and send an email (move out)
// or just send an email (move out)
/*

case "OnTime":
var params = { result: ConfirmType, "PaymentIDs":_paymentIDs, "Method":"<?php print $Method ?>", "PaymentID":_paymentID, "TenantID":_tenantID};

case "NotPaid":
var params = { result: ConfirmType, "PaymentIDs":_paymentIDs, "Method":"<?php print $Method ?>", "PaymentID":_paymentID, "TenantID":_tenantID};

case "Late":
var params = { result: ConfirmType, lateAmount:_lateAmount, "PaymentIDs":_paymentIDs, "Method":"<?php print $Method ?>" , "PaymentID":_paymentID, "TenantID":_tenantID};

case "MoveOut":
var params = { result: ConfirmType, "moveOutDate":_moveOutDate, "moveOutStatus":_moveOutStatus , "leaseIDs":_leaseIDs, "Method":"<?php print $Method ?>", "PaymentID":_paymentID, "TenantID":_tenantID};

case "Complicated":
var params = { result: ConfirmType, "complicatedAnswer":_textBox,  "PropertyManagerID":_PropertyManagerID,"leaseIDs":_leaseIDs, "PaymentIDs":_paymentIDs, "Method":"<?php print $Method ?>", "PaymentID":_paymentID, "TenantID":_tenantID };


so in all cases, we get:
MessageID
PaymentID
ConfirmType {OnTime | NotPaid | Late | MoveOut | Complicated}, 
Method (how they clicked) = { SMS | Email | Phone}

and we might get:
lateAmount (how late they were)
moveOutDate
moveOutStatus
Long Form Message

           
*/

try
{
    $json = file_get_contents('php://input');

    // Converts it into a PHP object
    $postedData = json_decode($json);
    $dbObject->logger->logDebug($_SERVER["SCRIPT_FILENAME"]." - json parsed!  # of elements in array: ".count($postedData));
    $dbObject->logger->logDebug($_SERVER["SCRIPT_FILENAME"]." - ".print_r($postedData, true));
    $MonthCount = count($postedData); // element 0 is not meaningful for us.
	$returnMessasge = "";
    for ($i=0; $i < $MonthCount; $i++) {
        
        $thisMonth = $postedData[$i];        
        $MonthLabel = $thisMonth->MonthLabel;
        $PaymentID = $thisMonth->PaymentID;
        $MessageID = $thisMonth->MessageID;                
        $ConfirmType = $thisMonth->result;
        $responseMethod = $thisMonth->Method;
        
        $tenantID = $thisMonth->tenantID;
        
        $dbObject->logger->logDebug($_SERVER["SCRIPT_FILENAME"]." - looping through iteration ".$i." of ".count($postedData).".  confirm type: ".$ConfirmType);

        switch ($ConfirmType)
        {
        case "OnTime":
            $retval = MarkPaymentPaid($PaymentID, $responseMethod, $MonthLabel, $dbObject);
            break;
        case "NotPaid":
            $retval = SavePaymentNotPaidEvent($PaymentID, $responseMethod, $MonthLabel, $dbObject);
            break;
        case "Late":
            if ($lateAmount == "30") {
                $retval = MarkPaymentPaid($PaymentID, $responseMethod, $MonthLabel, $dbObject);
            }
            else {
                $lateAmount = $thisMonth->lateAmount;
                $retval = MarkPaymentLate($PaymentID, $lateAmount, $responseMethod, $MonthLabel, $dbObject);
            }
            break;
        case "MoveOut":
            $moveOutDate = $thisMonth->moveOutDate;
            $moveOutStatus = $thisMonth->moveOutStatus;
            $moveOutStatusText = $thisMonth->moveOutStatusText;
            $retval = SavePaymentMovedOut($PaymentID, $moveOutDate, $moveOutStatus, $moveOutStatusText, $responseMessage, $responseMethod, $MonthLabel, $dbObject);
            break;
        case "Complicated":
            $responseMessage = $thisMonth->complicatedAnswer;
            $retval = SavePaymentComplicated($PaymentID, $responseMessage, $responseMethod, $MonthLabel, $dbObject);
            break;
        default:
            $retval = "There was a problem figuring out what you wanted to do for ".$MonthLabel."  Please contact support@rentalkharma.com and we'll figure this out.";
            SavePaymentComplicated($PaymentID, "Error processing response.", $responseMethod, $dbObject);
        }
		
		$returnMessage = $returnMessage.$retval.PHP_EOL;
    }
    
}
   
catch (Exception $e)
{
    $myMessage = $e->getMessage();
	
    $dbObject->logger->logError("Error updating lease payment info: ".$myMessage);
    
}
echo json_encode($returnMessage);


function SavePaymentComplicated($PaymentID, $Message, $Method, $MonthLabel, $dbObj)
{

    require_once "mandrill/src/Mandrill.php"; // check paths
    $RecipientEmail = "support@rentalkharma.com";
    /*
    $q0 = "select L.leaseID, L.TenantID, L.PropertyManagerID, concat(GetMonthAbbrevFromID(LP.MonthID),' ',GetYearFromID(LP.MonthID)) UpdateMonth, ";
    $q0=$q0." LP.MonthID, concat(T.FirstName, ' ', T.LastName) TenantName, T.Email, PM_Name(PM.PropertyManagerID) PMName,  ";
    $q0=$q0." PM.Phone, PM.PreferredContact, PM.Email    ";
    $q0 = $q0." from lease L inner join lease_payment LP on LP.LeaseID  = L.LeaseID  ";
    $q0 = $q0." inner join tenant T on T.UserID = L.TenantID inner join propertymanager PM on PM.PropertyManagerID = L.PropertyManagerID ";
    $q0 = $q0." inner join Custom_VOR_GUID_LeasePayment VLP on VLP.LeasePaymentID = LP.PaymentID ";
    $q0 = $q0." inner join Custom_VOR_GUID V on V.PropertyManagerID = L.PropertyManagerID ";
    $q0 = $q0."  where V.GUID = ? ";
    */
    $q0 = "select L.leaseID, L.TenantID, L.PropertyManagerID, concat(GetMonthAbbrevFromID(LP.MonthID),' ',GetYearFromID(LP.MonthID)) UpdateMonth, ";
    $q0=$q0." LP.MonthID, concat(T.FirstName, ' ', T.LastName) TenantName, T.Email, PM_Name(PM.PropertyManagerID) PMName,  ";
    $q0=$q0." PM.Phone, PM.PreferredContact, PM.Email    ";
    $q0 = $q0." from lease L inner join lease_payment LP on LP.LeaseID  = L.LeaseID  ";
    $q0 = $q0." inner join tenant T on T.UserID = L.TenantID inner join propertymanager PM on PM.PropertyManagerID = L.PropertyManagerID ";
    $q0 = $q0."  where LP.PaymentID = ? ";
    
    $LeaseQuery = $dbObj->connection->prepare($q0);
    $LeaseQuery->bindValue(1, $PaymentID);
    $success = $LeaseQuery->execute();
    $dbObj->logger->logDebug("result of q0: ".$success.", guid: ".$PaymentID);
    $LeaseIDs = $LeaseQuery->fetchAll();
    
    if (count($LeaseIDs) > 0) {
        $ThisLease = $LeaseIDs[0]; // this is a bit of a hack -- the query should only return one row ever.
        
        $LeaseID = $ThisLease[0];
        $TenantID = $ThisLease[1];
        $LandlordID = $ThisLease[2];
        $MonthName = $ThisLease[3];
        $TenantName = $ThisLease[5];
        $TenantEmail = $ThisLease[6];
        $LandlordName = $ThisLease[7];
        $LandlordPhone = $ThisLease[8];
        $LandlordPreferredContact = $ThisLease[9];
        $LandlordEmail = $ThisLease[10];
    }
    
    $TemplateID = "MU-complicated";
    
    $mandrill = new Mandrill(MANDRILL_API_KEY);
    $vars = array();
    array_push($vars, array('name' => 'TENANTNAME', 'content' => $TenantName));
    array_push($vars, array('name' => 'LEASEID', 'content' => $LeaseID));
    array_push($vars, array('name' => 'TENANTEMAIL', 'content' => $TenantEmail));
    array_push($vars, array('name' => 'TENANTID', 'content' => $TenantID));
    array_push($vars, array('name' => 'ROOMMATENAME', 'content' => ""));
    array_push($vars, array('name' => 'ROOMMATEID', 'content' => ""));
    array_push($vars, array('name' => 'EXTRAINFO', 'content' => $Message));
    array_push($vars, array('name' => 'LANDLORDID', 'content' => $LandlordID));
    array_push($vars, array('name' => 'LANDLORDNAME', 'content' => $LandlordName));
    array_push($vars, array('name' => 'LANDLORDEMAIL', 'content' => $LandlordEmail));
    array_push($vars, array('name' => 'LANDLORDPHONE', 'content' => $LandlordPhone));
    array_push($vars, array('name' => 'LANDLORDCONTACT', 'content' => $LandlordPreferredContact));

    
    $message = array(
        'to' => array(array('email' => $RecipientEmail, 'name' => $RecipientEmail)),
        'recipient_metadata'=> array(array('rcpt'=> $RecipientEmail, 'values' => array('UserID'=> $RecipientEmail))),
        'merge_vars' => array(array(

            'rcpt' => $RecipientEmail,
            'vars' => $vars
            )));


    $template_content = array();
    $response = $mandrill->messages->sendTemplate($TemplateID, $template_content, $message);
    
    if (!$response) {
        $dbObj->logger->logError("ERROR in mandrill complicated content email.  No error data was received.");
        return "There was an error setting your tenants' rental payment history for ".$MonthLabel.".  We're on it.";
    } else {
        if (!($response[0]["status"] == "sent" ||$response[0]["status"] == "queued" )) {
            $dbObj->logger->logError("ERROR in mandrill complicated MU email!  Mandrill response: ". print_r($response, true));
            return "There was an error setting your tenants' rental payment history for ".$MonthLabel.".  We're on it.";
        } else {
            $dbObj->logger->logError("mandrill complicated MU email sent successfully.");
            $LogMessage = "LL/PM reported complicated MU for ".$MonthName.": ".$Message;
            $dbObj->logUserEvent($LogMessage, $TenantID, "MU");
            if ($RoommateID != "") {
                $dbObj->logUserEvent($LogMessage, $RoommateID, "MU");
            }
            $dbObj->logPropertymanagerEvent($LogMessage, $LandlordID, "MU");
            return "Thank you for the information! We'll be in touch soon to get all the details straight.";
        }    
    }
    return "There was an error setting your tenants' rental payment history for ".$MonthLabel.".  We're on it.";
}


function SavePaymentMovedOut($PaymentID, $MoveOutDate, $Status, $StatusText, $Message, $Method, $MonthLabel, $dbObj)
{
    // grab the contents, save an event to both parties, and email support

    require_once "mandrill/src/Mandrill.php"; // check paths
    $RecipientEmail = "support@rentalkharma.com";
    /*
    $q0 = "select L.leaseID, L.TenantID, L.PropertyManagerID, concat(GetMonthAbbrevFromID(LP.MonthID),' ',GetYearFromID(LP.MonthID)) UpdateMonth, ";
    $q0 = $q0." LP.MonthID, concat(T.FirstName, ' ', T.LastName) TenantName, T.Email, PM_Name(PM.PropertyManagerID) PMName,  ";
    $q0 = $q0." PM.Phone, PM.PreferredContact, PM.Email    from  ";
    $q0 = $q0." lease L inner join lease_payment LP on LP.LeaseID  = L.LeaseID  ";
    $q0 = $q0." inner join tenant T on T.UserID = L.TenantID inner join propertymanager PM on PM.PropertyManagerID = L.PropertyManagerID ";
    $q0 = $q0." inner join Custom_VOR_GUID_LeasePayment VLP on VLP.LeasePaymentID = LP.PaymentID ";
    $q0 = $q0." inner join Custom_VOR_GUID V on V.PropertyManagerID = L.PropertyManagerID ";
    $q0 = $q0."  where V.GUID = ? ";
    */
    $q0 = "select L.leaseID, L.TenantID, L.PropertyManagerID, concat(GetMonthAbbrevFromID(LP.MonthID),' ',GetYearFromID(LP.MonthID)) UpdateMonth, ";
    $q0 = $q0." LP.MonthID, concat(T.FirstName, ' ', T.LastName) TenantName, T.Email, PM_Name(PM.PropertyManagerID) PMName,  ";
    $q0 = $q0." PM.Phone, PM.PreferredContact, PM.Email    from  ";
    $q0 = $q0." lease L inner join lease_payment LP on LP.LeaseID  = L.LeaseID  ";
    $q0 = $q0." inner join tenant T on T.UserID = L.TenantID inner join propertymanager PM on PM.PropertyManagerID = L.PropertyManagerID ";
    $q0 = $q0."  where LP.PaymentID = ? ";
    
    $LeaseQuery = $dbObj->connection->prepare($q0);
    $LeaseQuery->bindValue(1, $PaymentID);
    $success = $LeaseQuery->execute();
    $dbObj->logger->logDebug("result of q0: ".$success.", payment ID: ".$PaymentID);
    $LeaseIDs = $LeaseQuery->fetchAll();
    
    if (count($LeaseIDs) > 0) {
        $ThisLease = $LeaseIDs[0]; // this is a bit of a hack -- the query should only return one row ever.
        
        $LeaseID = $ThisLease[0];
        $TenantID = $ThisLease[1];
        $LandlordID = $ThisLease[2];
        $MonthName = $ThisLease[3];
        $TenantName = $ThisLease[5];
        $TenantEmail = $ThisLease[6];
        $LandlordName = $ThisLease[7];
        $LandlordPhone = $ThisLease[8];
        $LandlordPreferredContact = $ThisLease[9];
        $LandlordEmail = $ThisLease[10];
    }
    
    $TemplateID = "move-out-notification";
    
    $MessageText = "Move Out status: ".$Status."/".$StatusText.PHP_EOL."PM Email:".$LandlordEmail.PHP_EOL."PM Phone: ".$LandlordPhone.PHP_EOL."Preferred Contact Method:".$LandlordPreferredContact;
    if ($Message !="")
        $MessageText = $MessageText.PHP_EOL." Extra Info: ".$Message;
    
    $mandrill = new Mandrill(MANDRILL_API_KEY);
    $vars = array();
    array_push($vars, array('name' => 'TENANTNAME', 'content' => $TenantName));
    array_push($vars, array('name' => 'LEASEID', 'content' => $LeaseID));
    array_push($vars, array('name' => 'TENANTEMAIL', 'content' => $TenantEmail));
    array_push($vars, array('name' => 'TENANTID', 'content' => $TenantID));
    array_push($vars, array('name' => 'ROOMMATENAME', 'content' => ""));
    array_push($vars, array('name' => 'ROOMMATEID', 'content' => ""));
    array_push($vars, array('name' => 'MOVEOUTDATE', 'content' => $MoveOutDate));
    array_push($vars, array('name' => 'EXTRAINFO', 'content' => $MessageText));
    array_push($vars, array('name' => 'LANDLORDID', 'content' => $LandlordID));

    
    $message = array(
        'to' => array(array('email' => $RecipientEmail, 'name' => $RecipientEmail)),
        'recipient_metadata'=> array(array('rcpt'=> $RecipientEmail, 'values' => array('UserID'=> $RecipientEmail))),
        'merge_vars' => array(array(

            'rcpt' => $RecipientEmail,
            'vars' => $vars
            )));


    $template_content = array();
    $response = $mandrill->messages->sendTemplate($TemplateID, $template_content, $message);
    
    if (!$response) {
        $dbObj->logger->logError("ERROR in mandrill complicated content email.  No error data was received.");
        return "There was an error setting your tenants' rental payment history for ".$MonthLabel.".  We're on it.";
    } else {
        if (!($response[0]["status"] == "sent" ||$response[0]["status"] == "queued" )) {
            $dbObj->logger->logError("ERROR in mandrill complicated MU email!  Mandrill response: ". print_r($response, true));
            return "There was an error setting your tenants' rental payment history for ".$MonthLabel.".  We're on it.";
        } else {
            $dbObj->logger->logError("mandrill complicated MU email sent successfully.");
            $LogMessage = "LL/PM reported moved out ".$MoveOutDate."/".$Status." for ".$MonthName.": ".$Message;
            $dbObj->logUserEvent($LogMessage, $TenantID, "MU");
            if ($RoommateID != "") {
                $dbObj->logUserEvent($LogMessage, $RoommateID, "MU");
            }
            $dbObj->logPropertymanagerEvent($LogMessage, $LandlordID, "MU");
            return "Thank you for the information! We'll be in touch soon to make sure our records are closed properly.";
        }    
    }
    return "There was an error setting your tenants' rental payment history for ".$MonthLabel.".  We're on it.";
}

function MarkPaymentLate($PaymentID, $howLate, $Method, $MonthLabel, $dbObj) 
{
    switch ($howLate)
    {
    case "31":
        $paymentCode = "1";
        $statusCode="71";
        break;
    case "61":
        $paymentCode = "2";
        $statusCode="78";
        break;
    case "91":
        $paymentCode = "3";
        $statusCode="80";
        break;
    case "121":
        $paymentCode = "4";
        $statusCode="82";
        break;
    }
    try
    {

        $q0 = "select L.leaseID, L.TenantID, L.PropertyManagerID, concat(GetMonthAbbrevFromID(LP.MonthID),' ',GetYearFromID(LP.MonthID)) UpdateMonth ";
        $q0 = $q0." from lease L inner join lease_payment LP on LP.LeaseID  = L.LeaseID  ";
        $q0 = $q0."  where LP.PaymentID = ? ";
        
        $LeaseQuery = $dbObj->connection->prepare($q0);
        $LeaseQuery->bindValue(1, $PaymentID);
        $success = $LeaseQuery->execute();
        $dbObj->logger->logDebug("result of q0: ".$success.", payment ID: ".$PaymentID);
        $LeaseIDs = $LeaseQuery->fetchAll();

        if (count($LeaseIDs) > 0) {
            $ThisLease = $LeaseIDs[0]; // this is a bit of a hack -- the query should only return one row ever.
            
            $LeaseID = $ThisLease[0];
            $TenantID = $ThisLease[1];
            $PropertyManagerID = $ThisLease[2];
            $UpdateMonthName = $ThisLease[3];

            $UpdateMessage = "Updated via ".$Method;
            
            $q = " update lease_payment  set LeaseStatusCode=?, PaymentRatingCode=?, VerifiedDate=curdate(), Comment = ? where PaymentID = ? "; 
            
            $MUQuery = $dbObj->connection->prepare($q);
            $MUQuery->bindValue(1, $statusCode);
            $MUQuery->bindValue(2, $paymentCode);
            $MUQuery->bindValue(3, $UpdateMessage); 
            $MUQuery->bindValue(4, $PaymentID);
        
            
            $success = $MUQuery->execute();
            
            $dbObj->logger->logDebug("late payment Update result:".$success);
            if ($success == 1) {
                $LogMessage = "MU marked ".$howLate." days late for month of ".$UpdateMonthName;
                if ($Method != "")
                    $LogMessage = $LogMessage.", via ".$Method;
                foreach ($LeaseIDs as $ThisLease) {
                    $dbObj->logger->logDebug("in LogEvent for loop".$ThisLease);
                    $dbObj->logUserEvent($LogMessage, $TenantID, "MU");
					$dbObj->logger->logDebug("logged ".$LogMessage." to member");
                }
                if (isset($PropertyManagerID)) {
                    $dbObj->logPropertymanagerEvent($LogMessage." for lease ID ".$LeaseID, $PropertyManagerID, "MU");
					$dbObj->logger->logDebug("logged ".$LogMessage." to PM");
				} else {
                    $dbObj->logPropertymanagerEvent($LogMessage, "!", "MU");
					$dbObj->logger->logDebug("Attempted to log ".$LogMessage." to PM, but didn't know the PM ID.");
				}
            }
        }
        
    }
    catch (Exception $e)
    {
        $myMessage = $e->getMessage();
        $dbObj->logger->logError("Error updating lease payment info: ".$myMessage);
        
    }
    if ($success) {
        return $MonthLabel."'s rent payment set to 'Late'.";
    } else {
        return "There was an error setting your tenants' payment late for ".$MonthLabel.".  We're looking into it.";
    }
    
    
}


function SavePaymentNotPaidEvent($PaymentID, $Method, $MonthLabel, $dbObj)
{
    // this one's pretty easy.  We're just going to save an event to both the PM and RR that we got this notification.
    try
    {
        
        
        $q0 = "select L.leaseID, L.TenantID, L.PropertyManagerID, concat(GetMonthAbbrevFromID(LP.MonthID),' ',GetYearFromID(LP.MonthID)) UpdateMonth ";
        $q0 = $q0." from lease L inner join lease_payment LP on LP.LeaseID  = L.LeaseID  ";
        $q0 = $q0."  where LP.PaymentID = ? ";
        
        $LeaseQuery = $dbObj->connection->prepare($q0);
        $LeaseQuery->bindValue(1, $PaymentID);
        $success = $LeaseQuery->execute();
        $dbObj->logger->logDebug("result of q0: ".$success.", payment ID: ".$PaymentID);
        $LeaseIDs = $LeaseQuery->fetchAll();
        
        /*$dbObj->logger->logDebug("made it through first query in SaveNotPaidEvent");
        $dbObj->logger->logDebug("q0 results:".print_r($LeaseIDs,true));*/
        if (count($LeaseIDs) > 0) {
            $ThisLease = $LeaseIDs[0]; // this is a bit of a hack -- the query should only return one row ever.
            
            $LeaseID = $ThisLease[0];
            $TenantID = $ThisLease[1];
            $PropertyManagerID = $ThisLease[2];
            $UpdateMonthName = $ThisLease[3];

            $LogMessage = "PM/LL reported NOT PAID for ".$UpdateMonthName;
            if ($Method != "")
                $LogMessage = $LogMessage.", via ".$Method;

                        
            $dbObj->logUserEvent($LogMessage, $TenantID, "MU");
			$dbObj->logger->logDebug("logged ".$LogMessage." for ".$MonthLabel);
            if (isset($PropertyManagerID)) {
                $dbObj->logPropertymanagerEvent($LogMessage." for tenant ID ".$TenantID, $PropertyManagerID, "MU");
				$dbObj->logger->logDebug("logged ".$LogMessage." to PM");
            } else {
                $dbObj->logPropertymanagerEvent($LogMessage, "!", "MU");
				$dbObj->logger->logDebug("Attempted to log ".$LogMessage." to PM, but didn't know the PM ID.");
			}
        }

    }
    catch (Exception $e)
    {
        $myMessage = $e->getMessage();
        $dbObj->logger->logError("Error updating lease payment info: ".$myMessage);
        
    }
    if ($success) {
        return $MonthLabel."'s rent payment set to 'Not Paid'.";
    } else {
        return "There was an error setting your tenants' payment 'Not Paid' for ".$MonthLabel.".  We're looking into it.";
    }
}


function MarkPaymentPaid($PaymentID, $Method, $MonthLabel, $dbObj)
{
    try
    {
        $UpdateMessage = "Updated via ".$Method;
        $PropertyManagerID = -1;
        
        $q0 = "select L.leaseID, L.TenantID, L.PropertyManagerID, concat(GetMonthAbbrevFromID(LP.MonthID),' ',GetYearFromID(LP.MonthID)) UpdateMonth ";
        $q0 = $q0."    from lease_payment LP inner join lease L on LP.LeaseID = L.LeaseID ";
        $q0 = $q0." where LP.PaymentID = ?"; 
        
        $LeaseQuery = $dbObj->connection->prepare($q0);
        $LeaseQuery->bindValue(1, $PaymentID);
        $success = $LeaseQuery->execute();
        $dbObj->logger->logDebug("result of q0: ".$success.", paymentID: ".$PaymentID);
        $LeaseIDs = $LeaseQuery->fetchAll();
        
        $dbObj->logger->logDebug("made it through first query in AllPaid");
        $dbObj->logger->logDebug("q0 results:".print_r($LeaseIDs, true));
        if (isset($LeaseIDs)) {            
            $dbObj->logger->logDebug("leaseID results array is set");
            $ThisLease = $LeaseIDs[0];
            $TenantID = $ThisLease[1];
            $PropertyManagerID = $ThisLease[2];
            $UpdateMonth = $ThisLease[3];
        }                        

        $q = " update lease_payment LP set LP.LeaseStatusCode='11', LP.PaymentRatingCode='0', LP.VerifiedDate=curdate(), LP.Comment =?"; 
        $q = $q." where LP.PaymentID = ?";


        $MUQuery = $dbObj->connection->prepare($q);
        $MUQuery->bindValue(1, $UpdateMessage);
        $MUQuery->bindValue(2, $PaymentID);
        
        $success = $MUQuery->execute();
        
        $dbObj->logger->logDebug("Update result:".$success);
        if ($success == 1) {
            $LogMessage = "MU marked ON TIME for ".$UpdateMonth." (PaymentID  ".$PaymentID.") ";
            if ($Method != "")
                $LogMessage = $LogMessage.", via ".$Method;
            $dbObj->logUserEvent($LogMessage, $TenantID, "MU");
            $dbObj->logPropertymanagerEvent($LogMessage, $PropertyManagerID, "MU");
        }
    
    }
    catch (Exception $e)
    {
        $myMessage = $e->getMessage();
        $dbObj->logger->logError("Error updating lease payment info: ".$myMessage);
        
    }
    if ($success) {
                return $MonthLabel."'s rent payment set to 'Paid'.";
    } else {
        return "There was an error setting your tenants' payment 'Paid' for ".$MonthLabel.".  We're looking into it.";
    }
}


    

?>
