/**
  * Add Client Income Process
  */
 public function processAddClientIncomeAction()
 {
     if (!$this->_request->isXmlHttpRequest() || !$this->_request->isPost()) {
         $this->_redirector->gotoRoute(array('controller' => 'index', 'module' => 'clients'), 'admin');
     }
     $return = array();
     $clientIncomeGateway = new Clients_Model_ClientIncomeGateway();
     $form = $clientIncomeGateway->getForm('CreateClientIncome');
     $validForm = $form->isValid($this->_request->getParams());
     // Check the form for validity
     if (!$validForm) {
         $return['formErrors'] = $form->getMessages();
     } else {
         $formData = $form->getValues();
         //set correct date format for Income Start Date
         $formData['client_income_startdate'] = $this->_dateFormatter->saveDateFormat($formData['client_income_startdate'], $formData['altStartDate']);
         //set correct date format for Income End Date
         $formData['client_income_enddate'] = $this->_dateFormatter->saveDateFormat($formData['client_income_enddate'], $formData['altEndDate']);
         unset($formData['altStartDate']);
         unset($formData['altEndDate']);
         $clientIncome = $clientIncomeGateway->create($formData);
         $clientIncomeId = $clientIncome->save();
         $clientIncome = $clientIncomeGateway->fetchClientIncome($clientIncomeId);
         if (is_object($clientIncome)) {
             $clientIncome = $clientIncome->toArray();
         }
         $clientIncome['client_income_startdate'] = $this->_dateFormatter->viewDateFormat($clientIncome['client_income_startdate']);
         $clientIncome['client_income_enddate'] = $this->_dateFormatter->viewDateFormat($clientIncome['client_income_enddate']);
         $flashMessenger = $this->_helper->getHelper('FlashMessenger');
         $flashMessenger->setNamespace('notifications')->addMessage('Client Income Added');
         $return = array('append' => array('target' => '#clientIncome', 'content' => $this->view->partial('partials/_clientIncomeRow.phtml', 'clients', $clientIncome)), 'refresh' => true);
         $return['callback'] = '$("#add-income-dialog").dialog("close");';
     }
     $this->_helper->json->sendJson($return);
 }
 /**
  * Overview Index Action
  */
 public function indexAction()
 {
     $financials = array();
     if ($this->_request->getParam('id', null) === null) {
         $this->_redirect('/admin/clients/');
     }
     $this->view->clientId = array('client_id' => $this->_request->id);
     //Fetch Client & Spouse Details
     $clientGateway = new Clients_Model_ClientGateway();
     $clientContact = $clientGateway->fetchClient($this->_request->id);
     if (is_object($clientContact)) {
         $clientContact = $clientContact->toArray();
     }
     $clientContact['client_dob'] = $this->_dateFormatter->viewDateFormat($clientContact['client_dob']);
     $clientSpouse = $clientGateway->fetchClientSpouse($this->_request->id);
     if (is_object($clientSpouse)) {
         $clientSpouse = $clientSpouse->toArray();
     }
     if ($clientSpouse) {
         $clientSpouse['client_dob'] = $this->_dateFormatter->viewDateFormat($clientSpouse['client_dob']);
     }
     //Calculate Asset Totals
     $clientAssetGateway = new Clients_Model_ClientAssetGateway();
     $financials['assetsTotalEquity'] = $clientAssetGateway->calculateAssetsTotalEquity($this->_request->id);
     $financials['assetsTotalEquity'] = number_format($financials['assetsTotalEquity'], 2);
     //Calculate Investment Totals - Investments and Retirement Plans
     $clientInvestmentGateway = new Clients_Model_ClientInvestmentGateway();
     $financials['investmentsTotal'] = $clientInvestmentGateway->calculateInvestmentsTotal($this->_request->id);
     $clientEmploymentGateway = new Clients_Model_ClientEmploymentGateway();
     $financials['investmentsTotal'] += $clientEmploymentGateway->calculateRetirementPlanTotals($this->_request->id);
     $financials['investmentsTotal'] = number_format($financials['investmentsTotal'], 2);
     //Calculate Incomes Total
     $clientIncomeGateway = new Clients_Model_ClientIncomeGateway();
     $incomeTotal = $clientIncomeGateway->calculateIncomeTotals($this->_request->id);
     $empIncomeTotal = $clientEmploymentGateway->calculateEmploymentIncomeTotal($this->_request->id);
     $financials['incomeTotal'] = $incomeTotal + $empIncomeTotal;
     $financials['incomeTotal'] = number_format($financials['incomeTotal'], 2);
     //Calculate Total Monthly Expenses
     $financials['mthlyExpenses'] = $clientEmploymentGateway->calculateRetirementMonthlyExpense($this->_request->id);
     $clientInsuranceGateway = new Clients_Model_ClientInsuranceGateway();
     $financials['mthlyExpenses'] += $clientInsuranceGateway->calculateMonthlyInsuranceExpense($this->_request->id);
     $financials['mthlyExpenses'] += $clientAssetGateway->calculateAssetsTotalMonthlyExpense($this->_request->id);
     $financials['mthlyExpenses'] += $clientInvestmentGateway->calculateInvestmentsMonthlyExpense($this->_request->id);
     $clientExpenseGateway = new Clients_Model_ClientExpenseGateway();
     $financials['mthlyExpenses'] += $clientExpenseGateway->calculateTotalMonthlyExpenses($this->_request->id);
     $financials['mthlyExpenses'] = number_format($financials['mthlyExpenses'], 2);
     //Calculate Total Accumulated Debt
     $financials['debtTotal'] = $clientExpenseGateway->calculateTotalAccumulatedExpenses($this->_request->id);
     $financials['debtTotal'] += $clientAssetGateway->calculateAssetsTotalDebt($this->_request->id);
     $financials['debtTotal'] = number_format($financials['debtTotal'], 2);
     //Calculate Emergency Funds
     $financials['emergencyFunds'] = $clientAssetGateway->calculateAssetEmergencyFunds($this->_request->id);
     $financials['emergencyFunds'] = $clientInvestmentGateway->calculateInvestmentEmergencyFunds($this->_request->id);
     $financials['emergencyFunds'] = number_format($financials['emergencyFunds'], 2);
     $request = Zend_Controller_Front::getInstance()->getRequest();
     $this->view->module = $request->getModuleName();
     $this->view->controller = $request->getControllerName();
     $this->view->clientContact = $clientContact;
     $this->view->clientSpouse = $clientSpouse;
     $this->view->financials = $financials;
 }