/**
   * This method is used to return back to expenses tab
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return mapping forward
   * @throws Exception
   */
  public ActionForward returnToExpenses(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    BudgetForm budgetForm = (BudgetForm) form;
    BudgetDocument budgetDocument = budgetForm.getBudgetDocument();
    Budget budget = budgetDocument.getBudget();
    int selectedBudgetPeriodIndex = budgetForm.getViewBudgetPeriod() - 1;
    int selectedBudgetLineItemIndex = budgetForm.getSelectedBudgetLineItemIndex();
    BudgetLineItem selectedBudgetLineItem =
        budget
            .getBudgetPeriod(selectedBudgetPeriodIndex)
            .getBudgetLineItem(selectedBudgetLineItemIndex);
    int k = 0;
    boolean errorFound = false;
    GlobalVariables.getMessageMap().addToErrorPath("document");
    for (BudgetPersonnelDetails budgetPersonnelDetails :
        selectedBudgetLineItem.getBudgetPersonnelDetailsList()) {
      errorFound =
          errorFound
              || personnelDatesCheck(
                  selectedBudgetLineItem,
                  budgetPersonnelDetails,
                  selectedBudgetPeriodIndex,
                  selectedBudgetLineItemIndex,
                  k);
      k++;
    }
    GlobalVariables.getMessageMap().removeFromErrorPath("document");
    if (errorFound) {
      return mapping.findForward(Constants.MAPPING_BASIC);
    } else {
      getCalculationService().calculateBudgetLineItem(budget, selectedBudgetLineItem);

      BudgetCategoryTypeValuesFinder budgetCategoryTypeValuesFinder =
          new BudgetCategoryTypeValuesFinder();
      List<KeyValue> budgetCategoryTypes = new ArrayList<KeyValue>();
      budgetCategoryTypes = budgetCategoryTypeValuesFinder.getKeyValues();
      for (int i = 0; i < budgetCategoryTypes.size(); i++) {
        budgetForm.getNewBudgetLineItems().add(budget.getNewBudgetLineItem());
      }
      budget.setBudgetCategoryTypeCodes(budgetCategoryTypes);
      request.setAttribute(
          "fromPersonnelBudget"
              + budgetForm.getViewBudgetPeriod()
              + ""
              + selectedBudgetLineItemIndex,
          true);

      return mapping.findForward("expenses");
    }
  }
 @Override
 public ActionForward save(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
     throws Exception {
   BudgetForm budgetForm = (BudgetForm) form;
   BudgetDocument budgetDocument = budgetForm.getBudgetDocument();
   Budget budget = budgetDocument.getBudget();
   int selectedBudgetPeriodIndex = budgetForm.getViewBudgetPeriod() - 1;
   int selectedBudgetLineItemIndex = budgetForm.getSelectedBudgetLineItemIndex();
   BudgetLineItem selectedBudgetLineItem =
       budget
           .getBudgetPeriod(selectedBudgetPeriodIndex)
           .getBudgetLineItem(selectedBudgetLineItemIndex);
   int k = 0;
   boolean errorFound = false;
   GlobalVariables.getMessageMap().addToErrorPath("document");
   for (BudgetPersonnelDetails budgetPersonnelDetails :
       selectedBudgetLineItem.getBudgetPersonnelDetailsList()) {
     errorFound |=
         personnelDatesCheck(
             selectedBudgetLineItem,
             budgetPersonnelDetails,
             selectedBudgetPeriodIndex,
             selectedBudgetLineItemIndex,
             k);
     k++;
   }
   GlobalVariables.getMessageMap().removeFromErrorPath("document");
   if (errorFound) {
     getCalculationService().calculateBudgetLineItem(budget, selectedBudgetLineItem);
     return mapping.findForward(Constants.MAPPING_BASIC);
   } else {
     ActionForward actionForward = super.save(mapping, form, request, response);
     selectedBudgetLineItem.setBudgetPersonnelLineItemDeleted(false);
     return actionForward;
   }
 }
  public ActionForward deleteBudgetPersonnelDetails(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    BudgetForm budgetForm = (BudgetForm) form;
    BudgetDocument budgetDocument = budgetForm.getBudgetDocument();
    Budget budget = budgetDocument.getBudget();
    int selectedBudgetPeriodIndex = budgetForm.getViewBudgetPeriod() - 1;
    int selectedBudgetLineItemIndex = budgetForm.getSelectedBudgetLineItemIndex();
    BudgetPersonnelBudgetService budgetPersonnelBudgetService =
        KraServiceLocator.getService(BudgetPersonnelBudgetService.class);
    budgetPersonnelBudgetService.deleteBudgetPersonnelDetails(
        budget, selectedBudgetPeriodIndex, selectedBudgetLineItemIndex, getLineToDelete(request));
    HashMap uniqueBudgetPersonnelCount = new HashMap();
    int qty = 0;
    for (BudgetPersonnelDetails budgetPersonnelDetails :
        budget
            .getBudgetPeriod(selectedBudgetPeriodIndex)
            .getBudgetLineItem(selectedBudgetLineItemIndex)
            .getBudgetPersonnelDetailsList()) {
      if (!uniqueBudgetPersonnelCount.containsValue(budgetPersonnelDetails.getPersonId())) {
        uniqueBudgetPersonnelCount.put(qty, budgetPersonnelDetails.getPersonId());
        qty = qty + 1;
      }
    }
    budget
        .getBudgetPeriod(selectedBudgetPeriodIndex)
        .getBudgetLineItem(selectedBudgetLineItemIndex)
        .setQuantity(new Integer(qty));

    getCalculationService()
        .calculateBudgetPeriod(budget, budget.getBudgetPeriod(selectedBudgetPeriodIndex));
    return mapping.findForward(Constants.MAPPING_BASIC);
  }
  /**
   * This method is used to add a new Budget Line Item
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return mapping forward
   * @throws Exception
   */
  public ActionForward addBudgetPersonnelDetails(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    BudgetForm budgetForm = (BudgetForm) form;
    BudgetDocument budgetDocument = budgetForm.getBudgetDocument();
    Budget budget = budgetDocument.getBudget();
    int selectedBudgetPeriodIndex = budgetForm.getViewBudgetPeriod() - 1;
    int selectedBudgetLineItemIndex = budgetForm.getSelectedBudgetLineItemIndex();
    BudgetPersonnelDetails newBudgetPersonnelDetails = budgetForm.getNewBudgetPersonnelDetails();

    boolean errorFound = false;
    if (newBudgetPersonnelDetails.getPercentEffort().isGreaterThan(new BudgetDecimal(100))) {
      GlobalVariables.getMessageMap()
          .putError(
              "newBudgetPersonnelDetails.percentEffort",
              KeyConstants.ERROR_PERCENTAGE,
              Constants.PERCENT_EFFORT_FIELD);
      errorFound = true;
    }
    if (newBudgetPersonnelDetails.getPercentCharged().isGreaterThan(new BudgetDecimal(100))) {
      GlobalVariables.getMessageMap()
          .putError(
              "newBudgetPersonnelDetails.percentCharged",
              KeyConstants.ERROR_PERCENTAGE,
              Constants.PERCENT_CHARGED_FIELD);
      errorFound = true;
    }
    if (newBudgetPersonnelDetails
        .getPercentCharged()
        .isGreaterThan(newBudgetPersonnelDetails.getPercentEffort())) {
      GlobalVariables.getMessageMap()
          .putError(
              "newBudgetPersonnelDetails.percentCharged",
              KeyConstants.ERROR_PERCENT_EFFORT_LESS_THAN_PERCENT_CHARGED);
      errorFound = true;
    }
    if (StringUtils.isBlank((budgetForm.getNewBudgetPersonnelDetails().getPeriodTypeCode()))) {
      GlobalVariables.getMessageMap()
          .putError(
              "newBudgetPersonnelDetails.periodTypeCode",
              RiceKeyConstants.ERROR_REQUIRED,
              new String[] {"Period Type (Period Type)"});
      errorFound = true;
    }
    if (budgetForm.getNewBudgetPersonnelDetails().getPersonSequenceNumber() == null) {
      GlobalVariables.getMessageMap()
          .putError(
              "newBudgetPersonnelDetails.personSequenceNumber",
              RiceKeyConstants.ERROR_REQUIRED,
              new String[] {"Person (Person)"});
      errorFound = true;
    }

    HashMap uniqueBudgetPersonnelCount = new HashMap();
    int qty = 0;
    if (!errorFound) {
      BudgetPersonnelBudgetService budgetPersonnelBudgetService =
          KraServiceLocator.getService(BudgetPersonnelBudgetService.class);
      for (BudgetPersonnelDetails budgetPersonnelDetails :
          budget
              .getBudgetPeriod(selectedBudgetPeriodIndex)
              .getBudgetLineItem(selectedBudgetLineItemIndex)
              .getBudgetPersonnelDetailsList()) {
        if (!uniqueBudgetPersonnelCount.containsValue(budgetPersonnelDetails.getPersonId())) {
          uniqueBudgetPersonnelCount.put(qty, budgetPersonnelDetails.getPersonId());
          qty = qty + 1;
        }
      }
      budget
          .getBudgetPeriod(selectedBudgetPeriodIndex)
          .getBudgetLineItem(selectedBudgetLineItemIndex)
          .setQuantity(new Integer(qty));
      updatePersonnelBudgetRate(
          budget
              .getBudgetPeriod(selectedBudgetPeriodIndex)
              .getBudgetLineItem(selectedBudgetLineItemIndex));
      getCalculationService()
          .calculateBudgetPeriod(budget, budget.getBudgetPeriod(selectedBudgetPeriodIndex));
    }
    return mapping.findForward(Constants.MAPPING_BASIC);
  }
 /**
  * This method calculates the salary
  *
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return mapping forward
  * @throws Exception
  */
 public ActionForward calculateSalary(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
     throws Exception {
   BudgetForm budgetForm = (BudgetForm) form;
   BudgetDocument budgetDocument = budgetForm.getBudgetDocument();
   Budget budget = budgetDocument.getBudget();
   int selectedBudgetPeriodIndex = budgetForm.getViewBudgetPeriod() - 1;
   int selectedBudgetLineItemIndex = budgetForm.getSelectedBudgetLineItemIndex();
   BudgetLineItem selectedBudgetLineItem =
       budget
           .getBudgetPeriod(selectedBudgetPeriodIndex)
           .getBudgetLineItem(selectedBudgetLineItemIndex);
   BudgetPersonnelDetails budgetPersonnelDetails =
       selectedBudgetLineItem.getBudgetPersonnelDetailsList().get(getLineToDelete(request));
   boolean errorFound = false;
   GlobalVariables.getMessageMap().addToErrorPath("document");
   if (budgetPersonnelDetails.getPercentEffort().isGreaterThan(new BudgetDecimal(100))) {
     GlobalVariables.getMessageMap()
         .putError(
             "budgetPeriod["
                 + selectedBudgetPeriodIndex
                 + "].budgetLineItems["
                 + selectedBudgetLineItemIndex
                 + "].budgetPersonnelDetailsList["
                 + getLineToDelete(request)
                 + "].percentEffort",
             KeyConstants.ERROR_PERCENTAGE,
             Constants.PERCENT_EFFORT_FIELD);
     errorFound = true;
   }
   if (budgetPersonnelDetails.getPercentCharged().isGreaterThan(new BudgetDecimal(100))) {
     GlobalVariables.getMessageMap()
         .putError(
             "budgetPeriod["
                 + selectedBudgetPeriodIndex
                 + "].budgetLineItems["
                 + selectedBudgetLineItemIndex
                 + "].budgetPersonnelDetailsList["
                 + getLineToDelete(request)
                 + "].percentCharged",
             KeyConstants.ERROR_PERCENTAGE,
             Constants.PERCENT_CHARGED_FIELD);
     errorFound = true;
   }
   if (budgetPersonnelDetails
       .getPercentCharged()
       .isGreaterThan(budgetPersonnelDetails.getPercentEffort())) {
     GlobalVariables.getMessageMap()
         .putError(
             "budgetPeriod["
                 + selectedBudgetPeriodIndex
                 + "].budgetLineItems["
                 + selectedBudgetLineItemIndex
                 + "].budgetPersonnelDetailsList["
                 + getLineToDelete(request)
                 + "].percentCharged",
             KeyConstants.ERROR_PERCENT_EFFORT_LESS_THAN_PERCENT_CHARGED);
     errorFound = true;
   }
   errorFound =
       errorFound
           || personnelDatesCheck(
               selectedBudgetLineItem,
               budgetPersonnelDetails,
               selectedBudgetPeriodIndex,
               selectedBudgetLineItemIndex,
               getLineToDelete(request));
   if (!errorFound) {
     BudgetPersonnelBudgetService budgetPersonnelBudgetService =
         KraServiceLocator.getService(BudgetPersonnelBudgetService.class);
     budgetPersonnelBudgetService.calculateBudgetPersonnelBudget(
         budget, selectedBudgetLineItem, budgetPersonnelDetails, getLineToDelete(request));
   }
   return mapping.findForward(Constants.MAPPING_BASIC);
 }