/**
  * Generate the HTML of the receipt print
  */
 public function buildDepositReceipt()
 {
     $html = '<div id="outerHTML">';
     $html .= '<div id="printArea">';
     $html .= '<h2 align="center">Salesforce</h2>';
     $html .= '<h3 align="center">Official Receipt: ELDOWAS Deposit</h3>';
     $html .= '<div id="receipt-header">';
     $html .= '<p align="center">';
     $html .= 'Kenyatta Street, New Muya House<br />';
     $html .= '2<sup>nd</sup> Flr, Room 105.<br />';
     $html .= 'Tel: + 254 721 156 315 &nbsp; / &nbsp; + 254 720 711 115<br />';
     $html .= 'www.salesforce.co.ke &nbsp; Email: info@salesforce.co.ke';
     $html .= '</p></div>';
     $html .= '<hr align="center" />';
     $html .= '<div id="receipt-body">';
     $html .= '<p>';
     $html .= '<strong>Receipt No:</strong> &nbsp;<span style="color:#F00;">';
     $html .= $this->_receipt_no;
     $html .= '</span></p>';
     $html .= '<p><strong>Tenant:</strong> &nbsp;';
     $html .= $this->_tenant_name . '</p>';
     $html .= '<p><strong>Room No:</strong> &nbsp;';
     $html .= Room::findById($this->_rid)->getRoomLabel();
     $html .= '<p><strong>Payment Amount:</strong> &nbsp;';
     $html .= number_format($this->_amount);
     $html .= '</p><p><strong>Company Agent:</strong> &nbsp;';
     $html .= $this->_agent . '</p>';
     $html .= '<p><strong>Date:</strong> &nbsp;';
     $html .= $this->_date_paid . " &nbsp;&nbsp;" . strftime("%H:%I:%S");
     $html .= '</p></div>';
     $html .= '</div></div>';
     print $html;
 }
    </form> 
    
    <?php 
if (!empty($tenants)) {
    ?>
    <?php 
    foreach ($tenants as $tnt) {
        ?>
    <p><a href="tenant.php?tid=<?php 
        echo $tnt->id;
        ?>
"><?php 
        echo $tnt->getFullName();
        ?>
</a>&nbsp;&nbsp;<?php 
        echo "Room No: " . Room::findById($tnt->getRoomId())->getRoomLabel();
        ?>
</p>
    <?php 
    }
    ?>
    <?php 
} elseif (empty($tenants) && !empty($prop_id)) {
    ?>
    <?php 
    $mesg = "No previous tenants found from the selected property";
    echo output_message($mesg);
    ?>
    <?php 
}
?>
 /**
  * Generate the HTML of the receipt print
  */
 public function buildArrearsReceipt()
 {
     global $session;
     $html = '<div id="outerHTML">';
     $html .= '<div id="printArea">';
     $html .= '<h2 align="center">Salesforce</h2>';
     $html .= '<h3 align="center">Official Receipt: Arrears Payment</h3>';
     $html .= '<div id="receipt-header">';
     $html .= '<p align="center">';
     $html .= 'Kenyatta Street, New Muya House<br />';
     $html .= '2<sup>nd</sup> Flr, Room 105.<br />';
     $html .= 'Tel: + 254 721 156 315 &nbsp; / &nbsp; + 254 720 711 115<br />';
     $html .= 'www.salesforce.co.ke &nbsp; Email: info@salesforce.co.ke';
     $html .= '</p></div>';
     $html .= '<hr align="center" />';
     $html .= '<div id="receipt-body">';
     $html .= '<p><strong>Receipt No:</strong> &nbsp;<span style="color:#F00;">';
     $html .= $this->_receipt_no;
     $html .= '</span></p>';
     $html .= '<p><strong>Tenant:</strong> &nbsp;';
     $tenant = Tenant::findById($this->_tid);
     $html .= $tenant->getFullName() . '</p>';
     $html .= '<p><strong>Room No:</strong> &nbsp;';
     $html .= Room::findById(Tenant::findById($this->_tid)->getRoomId())->getRoomLabel();
     $html .= '</p><p><strong>Payment Amount:</strong> &nbsp;';
     //$html .= number_format($session->sessionVar('amount'));
     $html .= number_format($this->_amount);
     if ($tenant->hasArrears()) {
         $arrears = new Arrears();
         $html .= '</p><p><strong>Arrears</strong>&nbsp;';
         $html .= $arrears->calcTotalArrearsFromTenant($this->_tid);
     }
     $html .= '</p><p><strong>Month:</strong>&nbsp;';
     $html .= $this->_getMonthFromDate($this->_start_date);
     $html .= '</p><p><strong>Company Agent:</strong> &nbsp;';
     $html .= $this->_agent . '</p>';
     $html .= '<p><strong>Date:</strong> &nbsp;';
     $html .= $this->_date_paid . " &nbsp;&nbsp;";
     $html .= '</p></div>';
     $html .= '</div></div>';
     print $html;
 }
        } else {
            // Get the vacant rooms from the property
            $vacant_rooms = $property->getVacantRooms();
        }
    }
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////// PROCESS SUBMIT //////////////////////////////
///////////////////////////////////////////////////////////////////////////
if (isset($_POST['submit'])) {
    //echo var_dump($_POST);
    $room_id = $_POST['room'];
    if (!isset($room_id)) {
        $err = "Choose room to move tenant to";
    } else {
        $new_room = Room::findById($room_id);
        if ($tenant->changeRoom($new_room)) {
            $mesg = "Tenant has been moved to new room";
            $session->message($mesg);
            redirect_to("tenant.php?tid={$tenant->id}");
        } else {
            $err = "An error occured preventing the tenant from being moved into the selected room";
        }
    }
}
include_layout_template("admin_header.php");
?>

	<div id="container">
    <div id="side-bar">
	<?php 
 public function edit($id)
 {
     if (!($room = Room::findById($id))) {
         Flash::set('error', __('Room not found!'));
         redirect(get_url('room'));
     }
     // check if trying to save
     if (get_request_method() == 'POST') {
         return $this->_edit($id);
     }
     $roomgalleries = RoomImage::findByRoomId($id);
     $features = FeatureImage::findByRoomId($id);
     $this->display('room/edit', array('action' => 'edit', 'csrf_token' => SecureToken::generateToken(BASE_URL . 'room/edit/' . $id), 'room' => $room, 'roomgalleries' => $roomgalleries, 'features' => $features, 'id' => $id, 'pages' => Record::findAllFrom('Page', 'parent_id=1 order by parent_id,position')));
 }
if (!empty($email)) {
    ?>
    <p><strong>Email:</strong> <?php 
    echo $tenant->getEmail();
}
?>
</p>
    <p><strong>Tenancy Status:</strong> <?php 
echo $tenant->hasMovedOut() ? '<span style="color:red">Moved Out</span>' : '<span style="color:green">Active Tenant</span>';
?>
</p>
    <br /><br /> 
    
    <h3>Rent Payment</h3>
    <p>Monthly Rent: <?php 
echo $room = Room::findById($tenant->getRoomId())->getRent();
?>
</p><br />
    
    <h3>Arrears</h3>
    <p>Rent Arrears: <?php 
if ($tenant->hasArrears()) {
    $arrears = $tenant->getTotalArrears();
    echo $arrears;
} else {
    echo "Ksh. 0.00";
}
?>
</p><br />
    
    <p><a class="btn-action" id="rent-payment" href="rent_payment.php?tid=<?php 
 /**
  * Change the room occupied by the tenant
  * @todo Check if there are any empty rooms in the
  *  property, then assign a room to the tenant
  * @param object $roomObj
  * @return boolean
  */
 public function changeRoom($roomObj)
 {
     $db = Database::getInstance();
     $mysqli = $db->getConnection();
     $curr_room = Room::findById($this->_rid);
     $mysqli->autocommit(false);
     // Set room's status to vacant
     $curr_room->setRoomStatus(0);
     $curr_room->unsetTenantId();
     if (!$curr_room->save()) {
         $mysqli->rollback();
         $mysqli->autocommit(true);
         return false;
     }
     // Set room_id property to ID of new room and
     // change stauts of new room
     $this->_rid = $roomObj->id;
     $roomObj->setRoomStatus(1);
     $roomObj->setTenantId($this);
     if (!$this->save()) {
         $mysqli->rollback();
         $mysqli->autocommit(true);
         return false;
     }
     if (!$roomObj->save()) {
         $mysqli->rollback();
         $mysqli->autocommit(true);
         return false;
     }
     if (!$mysqli->commit()) {
         $mysqli->rollback();
         $mysqli->autocommit(true);
         return false;
     }
     return true;
 }