array('numeric'), 'name' => array('notempty'), ); var $belongsTo = array( 'Statement', 'PrimaryContact' => array( 'className' => 'Contact', ), ); var $hasMany = array( 'CurrentLease' => array( 'className' => 'Lease', 'conditions' => 'CurrentLease.close_date IS NULL', ), 'Lease', 'LedgerEntry', // Cheat to get Account set as part of this class 'Account', ); var $hasAndBelongsToMany = array( 'Contact', 'Transaction' => array( 'joinTable' => 'ledger_entries', 'foreignKey' => 'customer_id', 'associationForeignKey' => 'transaction_id', ), ); /************************************************************************** ************************************************************************** ************************************************************************** * function: accountId * - Returns the account ID for the given customer */ function accountId($id) { $this->cacheQueries = true; $item = $this->find('first', array('contain' => false, 'fields' => array('account_id'), 'conditions' => compact('id'))); $this->cacheQueries = false; $item = current($item); return $item['account_id']; } function statementID($id) { $this->cacheQueries = true; $item = $this->find('first', array ('recursive' => -1, 'conditions' => compact('id'), )); $this->cacheQueries = false; $item = current($item); return $item['statement_id']; } /************************************************************************** ************************************************************************** ************************************************************************** * function: leaseIds * - Returns the lease IDs for the given customer */ function leaseIds($id) { $this->cacheQueries = true; $customer = $this->find('first', array('contain' => array('Lease' => array('fields' => array('id'))), 'fields' => array(), 'conditions' => array(array('Customer.id' => $id)))); $this->cacheQueries = false; $ids = array(); foreach ($customer['Lease'] AS $lease) $ids[] = $lease['id']; return $ids; } /************************************************************************** ************************************************************************** ************************************************************************** * function: findSecurityDeposits * - Returns an array of security deposit entries */ function findSecurityDeposits($id, $link = null) { return $this->Statement->findEntriesRelatedToAccount ($this->statementId($id), $this->Account->securityDepositAccountID(), null, $link); } /************************************************************************** ************************************************************************** ************************************************************************** * function: findUnreconciledLedgerEntries * - Returns ledger entries that are not yet reconciled * (such as charges not paid). */ function findUnreconciledLedgerEntries($id = null, $fundamental_type = null) { $unreconciled = $this->Account->findUnreconciledLedgerEntries ($this->Account->accountReceivableAccountID(), $fundamental_type, array('LedgerEntry.customer_id' => $id)); return $unreconciled; } /************************************************************************** ************************************************************************** ************************************************************************** * function: reconcileNewLedgerEntry * - Returns which ledger entries a new credit/debit would * reconcile, and how much. * * - REVISIT 20090617 * This should be subject to different algorithms, such * as apply to oldest charges first, newest first, to fees * before rent, etc. Until we get there, I'll hardcode * whatever algorithm is simplest. */ function reconcileNewLedgerEntry($id, $fundamental_type, $amount) { $reconciled = $this->Account->reconcileNewLedgerEntry ($this->Account->accountReceivableAccountID(), $fundamental_type, $amount, array('LedgerEntry.customer_id' => $id)); return $reconciled; } /************************************************************************** ************************************************************************** ************************************************************************** * function: details * - Returns detail information for the customer */ function details($id = null) { // Query the DB for need information. $customer = $this->find ('first', array ('contain' => array (// Models 'Contact' => array(// Models 'ContactPhone', 'ContactEmail', 'ContactAddress', ), 'Lease' => array('Unit' => array('order' => array('sort_order'), 'fields' => array('id', 'name'), ), ), ), 'conditions' => array('Customer.id' => $id), )); // Figure out the outstanding balance for this customer $customer['stats'] = $this->stats($id); // Figure out the total security deposit for the current lease. $customer['deposits'] = $this->findSecurityDeposits($id); return $customer; } /************************************************************************** ************************************************************************** ************************************************************************** * function: stats * - Returns summary data from the requested customer. */ function stats($id = null, $cond = null, $link = null) { if (!$id) return null; return $this->Statement->stats($this->statementID($id), $cond, $link); } } ?>