/**
  * 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;
 }
 /**
  * Add Client Insurance Process
  */
 public function processAddClientInsuranceAction()
 {
     $buildForm = array();
     $return = array();
     if (!$this->_request->isXmlHttpRequest() || !$this->_request->isPost()) {
         $this->_redirector->gotoRoute(array('controller' => 'index', 'module' => 'clients'), 'admin');
     }
     $flashMessenger = $this->_helper->getHelper('FlashMessenger');
     $buildForm['client_insurance_type'] = $this->_request->getParam('client_insurance_type');
     $buildForm['controller'] = $this->_request->getParam('controller');
     //Pull form name or Close modal and Send Error message
     $clientInsuranceGateway = new Clients_Model_ClientInsuranceGateway();
     if (!($insForm = $clientInsuranceGateway->getFormName($buildForm))) {
         $return = array('refresh' => true, 'callback' => '$("#add-insurance-dialog").dialog("close");');
         $flashMessenger->setNamespace('errors')->addMessage('Error Adding Insurance Plan: Contact Support at support@tools4advisors.com ');
         $this->_helper->json->sendJson($return);
         $this->_redirect('/admin/clients/');
     }
     //Pull form and check validity
     $form = $clientInsuranceGateway->getForm($insForm);
     $validForm = $form->isValid($this->_request->getParams());
     // Check the form for validity
     if (!$validForm) {
         $return['formErrors'] = $form->getMessages();
     } else {
         $clientInsurance = $clientInsuranceGateway->create($form->getValues());
         $clientInsuranceId = $clientInsurance->save();
         $clientInsurance = $clientInsuranceGateway->fetchClientInsurance($clientInsuranceId);
         if (is_object($clientInsurance)) {
             $clientInsurance = $clientInsurance->toArray();
         }
         $flashMessenger->setNamespace('notifications')->addMessage('Client Insurance Added');
         $return = array('append' => array('target' => '#clientInsurance', 'content' => $this->view->partial('partials/_clientInsuranceRow.phtml', 'clients', $clientInsurance)), 'refresh' => true);
         $return['callback'] = '$("#add-insurance-dialog").dialog("close");';
     }
     $this->_helper->json->sendJson($return);
 }
 /**
  * Fetch Non-Qualified Monthly Funds Avail for Investment
  * @param int $id
  */
 public function fetchNonQualifiedMonthlyFundsAvail($id)
 {
     $nonQualifiedMoney = '0';
     $this->checkAcl('read');
     //Get Non-Qualified Money Avail from Client Employment Ret Plan
     $clientEmploymentGateway = new Clients_Model_ClientEmploymentGateway();
     //This function simply returns the matching plus amount with no other details
     //Saving this to determine if I want to use this functionality over the function
     //below
     //        $nonQualRetPlanMoney = $clientEmploymentGateway->fetchClientAndSpouseRetMoneyPlus($id);
     $nonQualRetPlanMoney = $clientEmploymentGateway->fetchClientAndSpouseRetFundsAvailForTx($id, 'Non-Qualified');
     if (is_object($nonQualRetPlanMoney)) {
         $nonQualRetPlanMoney = $nonQualRetPlanMoney->toArray();
     }
     if ($nonQualRetPlanMoney) {
         foreach ($nonQualRetPlanMoney as $key => $value) {
             if ($nonQualRetPlanMoney[$key]['client_employment_yourcontribution'] > $nonQualRetPlanMoney[$key]['client_employment_matchingmax']) {
                 $nonQualifiedMoney += $nonQualRetPlanMoney[$key]['client_employment_yourcontribution'] - $nonQualRetPlanMoney[$key]['client_employment_matchingmax'];
             }
         }
     }
     //Get Non-Qualified Monthly Funds Avail from Client Investments
     //Check if Money is labeled as Emer Fund Money. If so enure that Tx Amount
     //does not exceed Total minus Emer Fund Amount.
     $clientInvestmentGateway = new Clients_Model_ClientInvestmentGateway();
     $nonQualInvestments = $clientInvestmentGateway->fetchClientMonthlyFundsAvail($id, 'Non-Qualified');
     if (is_object($nonQualInvestments)) {
         $nonQualInvestments = $nonQualInvestments->toArray();
     }
     if ($nonQualInvestments) {
         foreach ($nonQualInvestments as $key => $value) {
             //				if($nonQualInvestments[$key]['client_investment_emfund']){
             //					$availFunds = $nonQualInvestments[$key]['client_investment_txamount'] + $nonQualInvestments[$key]['client_investment_emfundamt'];
             //					if($availFunds < $nonQualInvestments[$key]['client_investment_amount']){
             //						$nonQualifiedMoney += ($nonQualInvestments[$key]['client_investment_amount'] - $nonQualInvestments[$key]['client_investment_emfundamt'] );
             //					}else{
             //						$nonQualifiedMoney += $nonQualInvestments[$key]['client_investment_txamount'];
             //					}
             //				}else{
             $nonQualifiedMoney += $nonQualInvestments[$key]['client_investment_monthlycontribute'];
             //				}
         }
     }
     //Get Non-Qualified Monthly Funds Avail from Client Assets
     //Check if Money is labeled as Emer Fund Money.
     $clientAssetGateway = new Clients_Model_ClientAssetGateway();
     $nonQualAssets = $clientAssetGateway->fetchClientSavingsAssetByClientId($id);
     if (is_object($nonQualAssets)) {
         $nonQualAssets = $nonQualAssets->toArray();
     }
     if ($nonQualAssets) {
         foreach ($nonQualAssets as $key => $value) {
             if ($nonQualAssets[$key]['client_asset_emfund'] && $nonQualAssets[$key]['client_asset_emfundamt']) {
                 $nonQualifiedMoney += $nonQualAssets[$key]['client_asset_pmtamount'];
             } else {
                 $nonQualifiedMoney += $nonQualAssets[$key]['client_asset_pmtamount'];
             }
         }
     }
     //Get Monthly Premiums from any existing Term Life Policies
     $clientInsuranceGateway = new Clients_Model_ClientInsuranceGateway();
     $termLifePremiums = $clientInsuranceGateway->fetchClientTermLifePremiumByClientId($id);
     if (is_object($termLifePremiums)) {
         $termLifePremiums = $termLifePremiums->toArray();
     }
     if ($termLifePremiums) {
         foreach ($termLifePremiums as $key => $value) {
             $nonQualifiedMoney += $termLifePremiums[$key]['client_insurance_mthlypremium'];
         }
     }
     return $nonQualifiedMoney;
 }