/** * Record rent arrears for a particular month in the event * that no amont was paid as rent * @param int $tenant_id The ID used to identify the tenant * @param string $start Date string specifying start of the period * @param string $end Date string specifying end of the period * @return boolean */ public function recordArrears($tenant_id, $start, $end) { $db = Database::getInstance(); $mysqli = $db->getConnection(); // Get monthly rental amount $rent_pm = Room::findByTenantId($tenant_id)->getRent(); $rent_pm = (int) str_replace(',', '', $rent_pm); // Create a new arrears object to hold arrears information //$arrears = new Arrears(); $this->setTenantId($tenant_id); $this->setAmountOwed($rent_pm); $this->setStartPeriod($start); $this->setEndPeriod($end); // Payment Status set to 0 - denoting incompletion $pst = new PaymentStatus(); $pst->setTenantId($tenant_id); $pst->setStartPeriod($start); $pst->setEndPeriod($end); $pst->setStatus(0); $mysqli->autocommit(false); $this->save(); $pst->save(); if (!$mysqli->commit()) { $mysqli->rollback(); $mysqli->autocommit(true); return false; } else { $mysqli->autocommit(true); return true; } }
/** * Pay the rent of tenant's premises for a specified period * @param object $rentObj * @param string $start The start period of the month * @param string $end The end period of the month * @return boolean */ public function payRent(Rent $rentObj, $start, $end) { $db = Database::getInstance(); $mysqli = $db->getConnection(); $monthly_rent = Room::findByTenantId($this->id)->getRent(); $monthly_rent = $this->_sanitizeMoneyString($monthly_rent); $rent_paid = $rentObj->getPaymentAmount(); $rent_paid = $this->_sanitizeMoneyString($rent_paid); if ($monthly_rent == $rent_paid) { // No outstanding arrears $pst = new PaymentStatus(); $pst->setTenantId($this->id); $pst->setStartPeriod($start); $pst->setEndPeriod($end); $pst->setStatus(1); $mysqli->autocommit(false); $rentObj->save(); $pst->save(); if (!$mysqli->commit()) { $mysqli->rollback(); $mysqli->autocommit(true); return false; } else { $mysqli->autocommit(true); return true; } } elseif ($rent_paid < $monthly_rent) { // Tenant has arrears $bal = $monthly_rent - $rent_paid; $arrears = new Arrears(); $arrears->setTenantId($this->id); $arrears->setAmountOwed($bal); $arrears->setStartPeriod($start); $arrears->setEndPeriod($end); // Payment Status set to 0 - denoting incompletion $pst = new PaymentStatus(); $pst->setTenantId($this->id); $pst->setStartPeriod($start); $pst->setEndPeriod($end); $pst->setStatus(0); // Save changes to database $mysqli->autocommit(false); $rentObj->save(); $arrears->save(); $pst->save(); if (!$mysqli->commit()) { $mysqli->rollback(); $mysqli->autocommit(true); return false; } else { $mysqli->autocommit(true); return true; } } }