public function home() { $total = $this->User->query("SELECT SUM(amount) as total FROM expenses WHERE house_id='" . $this->Session->read("Auth.User.House") . "' AND user_id='" . $this->Session->read("Auth.User.id") . "' AND active=1"); App::import('Model', 'Expense'); $mExpense = new Expense(); $expenses_id = $mExpense->query("SELECT expense_id FROM expenses_users WHERE user_id='" . $this->Session->read("Auth.User.id") . "'"); $expenses_id = Set::classicExtract($expenses_id, '{n}.expenses_users.expense_id'); $yourExpenses = $mExpense->find('all', array('conditions' => array('Expense.id' => $expenses_id, 'Expense.house_id' => $this->Session->read('Auth.User.House'), 'Expense.active' => 1))); $myTotal = 0.0; foreach ($yourExpenses as $expense) { $myTotal += $expense['Expense']['amount'] / count($expense['Perro']); } $totalP = $this->User->query("SELECT SUM(amount) as total FROM loans WHERE lender_id='" . $this->Session->read("Auth.User.id") . "' AND active=1"); $total2P = $this->User->query("SELECT SUM(amount) as total FROM loans WHERE borrower_id='" . $this->Session->read("Auth.User.id") . "' AND active=1"); $totalPaymentsP = $this->User->query("SELECT SUM(payments.amount) as total FROM payments, loans WHERE loans.id=payments.loan_id AND loans.lender_id='" . $this->Session->read("Auth.User.id") . "' AND loans.active=1 AND payments.active=1"); $totalPayments2P = $this->User->query("SELECT SUM(payments.amount) as total FROM payments, loans WHERE loans.id=payments.loan_id AND loans.borrower_id='" . $this->Session->read("Auth.User.id") . "' AND loans.active=1 AND payments.active=1"); $this->set(array('total' => $total[0][0]['total'], 'myTotal' => $myTotal, 'totalP' => $totalP['0']['0']['total'], 'total2P' => $total2P['0']['0']['total'], 'totalPaymentsP' => $totalPaymentsP['0']['0']['total'], 'totalPayments2P' => $totalPayments2P['0']['0']['total'])); }