diff --git a/site/controllers/leases_controller.php b/site/controllers/leases_controller.php index ac0269e..001011f 100644 --- a/site/controllers/leases_controller.php +++ b/site/controllers/leases_controller.php @@ -403,13 +403,16 @@ class LeasesController extends AppController { /************************************************************************** ************************************************************************** ************************************************************************** - * action: assess_rent - * - Assesses the new monthly rent charge, if need be + * action: assess_rent/late + * - Assesses the new monthly rent/late charge, if need be */ function assess_rent($date = null) { $this->Lease->assessMonthlyRentAll($date); } + function assess_late($date = null) { + $this->Lease->assessMonthlyLateAll($date); + } /************************************************************************** diff --git a/site/models/lease.php b/site/models/lease.php index c82d26c..38bd81c 100644 --- a/site/models/lease.php +++ b/site/models/lease.php @@ -168,6 +168,35 @@ class Lease extends AppModel { } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: lateCharges + * - Returns a list of late charges from this lease + */ + + function lateCharges($id) { + $this->prEnter(compact('id')); + $late_account_id = $this->StatementEntry->Account->lateChargeAccountID(); + $entries = $this->StatementEntry->find + ('all', + array('link' => + array(// Models + 'Lease', + ), + + //'fields' => array('id', 'amount', 'effective_date', 'through_date'), + 'conditions' => array(array('Lease.id' => $id), + array('StatementEntry.type' => 'CHARGE'), + array('StatementEntry.account_id' => $late_account_id), + ), + ) + ); + + return $this->prReturn($entries); + } + + /************************************************************************** ************************************************************************** ************************************************************************** @@ -370,6 +399,119 @@ class Lease extends AppModel { return $this->prReturn($ret + array('error' => false)); } + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: assessMonthlyLate + * - Assess late charges for the month, if not already charged. + */ + + function assessMonthlyLate($id, $date = null) { + $this->prFunctionLevel(25); + $this->prEnter(compact('id', 'date')); + $this->id = $id; + + if (empty($date)) + $date = time(); + + if (is_string($date)) + $date = strtotime($date); + + // REVISIT : 20090808 + // Anniversary Billing not supported + $anniversary = 0 && $this->field('anniversary_billing'); + if (empty($anniversary)) { + $date_parts = getdate($date); + $date = mktime(0, 0, 0, $date_parts['mon'], 11, $date_parts['year']); + } + + // Make sure we're not trying to assess late charges on a closed lease + $close_date = $this->field('close_date'); + $this->pr(17, compact('close_date')); + if (!empty($close_date)) + return $this->prReturn(null); + + // Don't assess late charges after customer has moved out + $moveout_date = $this->field('moveout_date'); + $this->pr(17, compact('moveout_date')); + if (!empty($moveout_date) && strtotime($moveout_date) < $date) + return $this->prReturn(null); + + // Determine when the customer has been charged through for rent + // and don't mark them as late if they haven't even been charged rent + $charge_through_date = strtotime($this->rentChargeThrough($id)); + $this->pr(17, compact('date', 'charge_through_date') + + array('date_str' => date('Y-m-d', $date), + 'charge_through_date_str' => date('Y-m-d', $charge_through_date))); + if ($charge_through_date <= $date) + return $this->prReturn(null); + + // Determine if the customer is actually late. This is based on + // when they've paid through, plus 10 days before they're late. + $paid_through_date = strtotime($this->rentPaidThrough($id)); + $this->pr(17, compact('date', 'paid_through_date') + + array('date_str' => date('Y-m-d', $date), + 'paid_through_date_str' => date('Y-m-d', $paid_through_date))); + $date_parts = getdate($paid_through_date); + $paid_through_date = mktime(0, 0, 0, $date_parts['mon'], $date_parts['mday']+10, $date_parts['year']); + if ($paid_through_date >= $date) + return $this->prReturn(null); + + // Determine if the customer has already been charged a late fee + // and, of course, don't charge them if they've already been. + $late_charges = $this->lateCharges($id); + foreach ($late_charges AS $late) { + if (strtotime($late['StatementEntry']['effective_date']) == $date) + return $this->prReturn(null); + } + + // Build the invoice transaction + $invoice = array('Transaction' => array(), 'Entry' => array()); + // REVISIT : 20090808 + // Keeping Transaction.stamp until the existing facility + // is up to date. Then we want the stamp to be now() + // (and so can just delete the next line). + $invoice['Transaction']['stamp'] = date('Y-m-d', $date); + $invoice['Entry'][] = + array('effective_date' => date('Y-m-d', $date), + 'amount' => 10, + 'account_id' => $this->StatementEntry->Account->lateChargeAccountId(), + ); + + // Record the invoice and return the result + $this->pr(21, compact('invoice')); + $result = $this->StatementEntry->Transaction->addInvoice + ($invoice, null, $id); + return $this->prReturn($result); + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: assessMonthlyLateAll + * - Ensures rent has been charged on all open leases + */ + + function assessMonthlyLateAll($date = null) { + $this->prEnter(compact('date')); + $leases = $this->find + ('all', array('contain' => false, + 'conditions' => array('Lease.close_date' => null), + )); + + $ret = array('Lease' => array()); + foreach ($leases AS $lease) { + $result = $this->assessMonthlyLate($lease['Lease']['id'], $date); + $ret['Lease'][$lease['Lease']['id']] = $result; + if ($result['error']) + $ret['error'] = true; + } + return $this->prReturn($ret + array('error' => false)); + } + + /************************************************************************** ************************************************************************** **************************************************************************