/** * Edit the amount paid as rent for a particular month * @param int $amount The new payment amount * @return boolean */ public function editPayment($amount) { $start = $this->_start_date; $end = $this->_end_date; $tenant_id = $this->_tid; $rent_pm = Room::findByTenantId($tenant_id)->getRent(); $rent_pm = (int) str_replace(',', '', $rent_pm); $amount = (int) str_replace(',', '', $amount); $curr_amount = (int) $this->_amount; $db = Database::getInstance(); $mysqli = $db->getConnection(); if (PaymentStatus::paymentStatusOK($tenant_id, $start, $end) && $amount < $rent_pm) { // Change payment status for tenant during period $statusObj = PaymentStatus::findByPeriod($tenant_id, $start, $end); $statusObj->setStatus(0); // Create a new arrears object $arrears = new Arrears(); $bal_owed = $rent_pm - $amount; $arrears->setTenantId($tenant_id); $arrears->setStartPeriod($start); $arrears->setEndPeriod($end); $arrears->setAmountOwed($bal_owed); // Change Amount $this->setPaymentAmount($amount); $mysqli->autocommit(false); $arrears->save(); $statusObj->save(); $this->save(); if (!$mysqli->commit()) { $mysqli->rollback(); $mysqli->autocommit(true); return false; } else { $mysqli->autocommit(true); return true; } } elseif (!PaymentStatus::paymentStatusOK($tenant_id, $start, $end) && $amount == $rent_pm) { // Change payment status for tenant during period $statusObj = PaymentStatus::findByPeriod($tenant_id, $start, $end); $statusObj->setStatus(1); // Find corresponding arrears $arrears = Arrears::findByPeriodForTenant($tenant_id, $start, $end); // Change Amount $this->setPaymentAmount($amount); $mysqli->autocommit(false); $arrears->delete(); $statusObj->save(); $this->save(); if (!$mysqli->commit()) { $mysqli->rollback(); $mysqli->autocommit(true); return false; } else { $mysqli->autocommit(true); return true; } } elseif (!PaymentStatus::paymentStatusOK($tenant_id, $start, $end) && $amount < $rent_pm) { // reduce amount paid and increase amount owed $arrears = Arrears::findByPeriodForTenant($tenant_id, $start, $end); $new_arrears = $rent_pm - $amount; $arrears->setAmountOwed($new_arrears); $this->setPaymentAmount($amount); $mysqli->autocommit(false); $arrears->save(); $this->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; } } }