diff --git a/site/app_controller.php b/site/app_controller.php index dc53904..65a6fe7 100644 --- a/site/app_controller.php +++ b/site/app_controller.php @@ -278,8 +278,14 @@ class AppController extends Controller { $this->addSideMenuLink('Unit Summary', array('controller' => 'units', 'action' => 'overview'), null, 'REPORT'); - $this->addSideMenuLink('Monthly Charges', - array('controller' => 'statement_entries', 'action' => 'chargesbymonth'), null, + $this->addSideMenuLink('Monthly Income', + array('controller' => 'statement_entries', 'action' => 'incomebymonth'), null, + 'REPORT'); + $this->addSideMenuLink('Monthly Expenses', + array('controller' => 'statement_entries', 'action' => 'expensebymonth'), null, + 'REPORT'); + $this->addSideMenuLink('Monthly Net', + array('controller' => 'statement_entries', 'action' => 'netbymonth'), null, 'REPORT'); } else { diff --git a/site/controllers/statement_entries_controller.php b/site/controllers/statement_entries_controller.php index 41363b9..60667d1 100644 --- a/site/controllers/statement_entries_controller.php +++ b/site/controllers/statement_entries_controller.php @@ -260,38 +260,65 @@ class StatementEntriesController extends AppController { /************************************************************************** ************************************************************************** ************************************************************************** - * action: chargesbymonth - * - Displays charges by month + * action: incexpbymonth + * - Displays income and/or expenses by month */ - function chargesbymonth($months = 12) { + function incexpbymonth($accts, $security_deposits, $months) { + $datefrom = 'DATE(NOW() - INTERVAL '.($months-1).' MONTH - INTERVAL DAY(NOW())-1 DAY)'; + $dateto = 'NOW()'; + /* $datefrom = '"2009-01-01"'; */ + /* $dateto = '"2011-12-31"'; */ + $result = $this->StatementEntry->find ('all', array('link' => array('Account' => array('fields' => 'name')), 'fields' => array_merge(array('MONTHNAME(effective_date) AS month', 'YEAR(effective_date) AS year'), $this->StatementEntry->chargeDisbursementFields(true)), - 'conditions' => array('Account.type' => 'INCOME', - 'effective_date >= DATE(NOW() - INTERVAL '.($months-1).' MONTH - INTERVAL DAY(NOW())-1 DAY)', - 'effective_date <= NOW()', + 'conditions' => array('Account.type' => $accts, + "effective_date >= $datefrom", + "effective_date <= $dateto", ), 'group' => array('YEAR(effective_date)', 'MONTH(effective_date)', 'Account.id'), - 'order' => array('YEAR(effective_date) DESC', 'MONTH(effective_date) DESC', 'Account.name'), + 'order' => array('YEAR(effective_date) DESC', 'MONTH(effective_date) DESC', 'Account.type', + 'IF(Account.id = '.$this->StatementEntry->Account->rentAccountID().', "---", Account.name)'), )); - $overview = array('months' => array(), 'charges' => 0); - foreach ($result AS $row) { + if ($security_deposits) { + $sdresult = $this->StatementEntry->Transaction->LedgerEntry->find + ('all', + array('link' => array('Transaction' => array('StatementEntry' => array('fields' => 'effective_date'), + 'fields' => array()), + 'Account' => array('fields' => 'name')), + 'fields' => array_merge(array('MONTHNAME(effective_date) AS month', + 'YEAR(effective_date) AS year'), + $this->StatementEntry->Transaction->LedgerEntry->debitCreditFields(true)), + 'conditions' => array('LedgerEntry.account_id' => $this->StatementEntry->Account->securityDepositAccountID(), + "effective_date >= $datefrom", + "effective_date <= $dateto", + 'StatementEntry.id = (SELECT MIN(id) FROM pmgr_statement_entries WHERE transaction_id = `Transaction`.id)' + ), + 'group' => array('YEAR(effective_date)', 'MONTH(effective_date)', 'Account.id'), + 'order' => array('YEAR(effective_date) DESC', 'MONTH(effective_date) DESC', 'Account.type', 'Account.name'), + )); + } else { + $sdresult = array(); + } + + $overview = array('months' => array(), 'amount' => 0); + foreach (array_merge($result, $sdresult) AS $row) { $mname = $row[0]['month'] .', '. $row[0]['year']; if (empty($overview['months'][$mname])) $overview['months'][$mname] = array('name' => $mname, 'subs' => array(), - 'charges' => 0); + 'amount' => 0); $month = &$overview['months'][$mname]; $month['subs'][] = array('name' => $row['Account']['name'], - 'charges' => $row[0]['balance']); + 'amount' => $row[0]['balance']); - $month['charges'] += $row[0]['balance']; - $overview['charges'] += $row[0]['balance']; + $month['amount'] += $row[0]['balance']; + $overview['amount'] += $row[0]['balance']; } // Enable the Reports menu section @@ -299,8 +326,26 @@ class StatementEntriesController extends AppController { // Prepare to render. $this->set('months', $months); - $this->set('title', 'Monthly Charges'); $this->set(compact('overview')); + $this->render('chargesbymonth'); + } + + function incomebymonth($months = 12) { + $this->set('title', 'Monthly Gross Income'); + $this->set('reptype', 'Gross Income'); + $this->incexpbymonth(array('INCOME'), true, $months); + } + + function expensebymonth($months = 12) { + $this->set('title', 'Gross Monthly Expenses'); + $this->set('reptype', 'Gross Expenses'); + $this->incexpbymonth(array('EXPENSE'), false, $months); + } + + function netbymonth($months = 12) { + $this->set('title', 'Net Monthly Income'); + $this->set('reptype', 'Net Income'); + $this->incexpbymonth(array('INCOME', 'EXPENSE'), true, $months); } diff --git a/site/views/statement_entries/chargesbymonth.ctp b/site/views/statement_entries/chargesbymonth.ctp index e13c809..670b08a 100644 --- a/site/views/statement_entries/chargesbymonth.ctp +++ b/site/views/statement_entries/chargesbymonth.ctp @@ -10,11 +10,11 @@ echo '