private void saveDoc(HttpServletRequest req) {

    if (!req.getParameter("SaveDoc").isEmpty()) {

      PaymentDocument paymentDocument =
          (PaymentDocument) req.getSession(false).getAttribute("currentPaymentDocumentForEdit");

      if (paymentDocument == null) {

        LOG.error("Payment Document not found in session scope.");
        req.getSession(false)
            .setAttribute("errorMessage", "Payment Document not found. Please reload the page.");

      } else {

        try {

          if (paymentDocument.getId() == 0) {
            paymentDocumentService.createDoc(paymentDocument);
          } else {
            paymentDocumentService.updateDoc(paymentDocument);
          }

        } catch (POMServicesException e) {

          LOG.error("Could not save Document: " + e.getMessage(), e);
          req.getSession(false)
              .setAttribute("errorMessage", "Could not save Document:" + e.getMessage());
          return;
        }
      }
    }
  }
  private void updateDocDetail(HttpServletRequest req) {

    String projectId = req.getParameter("project");
    String projectStageId = req.getParameter("projectStage");
    String costItemId = req.getParameter("costItem");
    String currencyString = req.getParameter("currency");
    String sumString = req.getParameter("sum");

    if (!req.getParameter("EditCurrentDetail").isEmpty()) {

      long rowNumber = Long.parseLong(req.getParameter("EditCurrentDetail"));
      PaymentDocument paymentDocument =
          (PaymentDocument) req.getSession(false).getAttribute("currentPaymentDocumentForEdit");

      if (paymentDocument == null) {

        LOG.error("Payment Document not found in session scope.");
        req.getSession(false)
            .setAttribute("errorMessage", "Payment Document not found. Please reload the page.");

      } else {

        PaymentDocumentDetail detail = paymentDocument.getPaymentDetailByRowNumber(rowNumber);

        try {

          if (!((costItemId == null) || costItemId.isEmpty())) {
            CostItem costItemRef = costItemService.retrieveById(Long.parseLong(costItemId));
            detail.setCostItem(costItemRef);
          }
          if (!((projectId == null) || projectId.isEmpty())) {
            Project projectRef = projectService.retrieveById(Long.parseLong(projectId));
            detail.setProject(projectRef);
          }
          if (!((projectStageId == null) || projectStageId.isEmpty())) {
            ProjectStage projectStageRef =
                projectStageService.retrieveById(Long.parseLong(projectStageId));
            detail.setProjectStage(projectStageRef);
          }
          Currency currency = Currency.getInstance(currencyString);
          Money sum = new Money(Double.parseDouble(sumString), currency);
          detail.setSum(sum);

        } catch (IllegalArgumentException | POMServicesException | POMDataModelException e) {

          LOG.error("Could not change Document Details properties: " + e.getMessage(), e);
          req.getSession(false)
              .setAttribute(
                  "errorMessage",
                  "Could not change Document Details properties: " + e.getMessage());
          return;
        }
        req.getSession(false).setAttribute("currentDocDetailForEdit", null);
      }
    }
  }
  private void cancelEditing(HttpServletRequest req) {

    PaymentDocument paymentDocument =
        (PaymentDocument) req.getSession(false).getAttribute("currentPaymentDocumentForEdit");
    if (paymentDocument.getId() == 0) {

      req.getSession(false).setAttribute("currentPaymentDocumentForEdit", new PaymentDocument());

    } else {

      req.setAttribute("EditCurrent", paymentDocument.getId());
      setCurrentDocToSession(req);
    }
  }
  private void editCurrentDocDetail(HttpServletRequest req) {

    if (!req.getParameter("EditCurrentDetail").isEmpty()) {

      long rowNumber = Long.parseLong(req.getParameter("EditCurrentDetail"));
      PaymentDocument paymentDocument =
          (PaymentDocument) req.getSession(false).getAttribute("currentPaymentDocumentForEdit");

      if (paymentDocument == null) {

        LOG.error("Payment Document not found in session scope.");
        req.getSession(false)
            .setAttribute("errorMessage", "Payment Document not found. Please reload the page.");

      } else {

        PaymentDocumentDetail detail = paymentDocument.getPaymentDetailByRowNumber(rowNumber);
        req.getSession(false).setAttribute("currentDocDetailForEdit", detail);
      }
    }
  }
  private void updateDocProperties(HttpServletRequest req) {

    String dateString = req.getParameter("date");
    String bankAccountId = req.getParameter("bankAccount");
    String description = req.getParameter("description");

    PaymentDocument paymentDocument =
        (PaymentDocument) req.getSession(false).getAttribute("currentPaymentDocumentForEdit");

    if (paymentDocument == null) {

      LOG.error("Payment Document not found in session scope.");
      req.getSession(false)
          .setAttribute("errorMessage", "Payment Document not found. Please reload the page.");

    } else {

      try {

        String pattern = (dateString.length() == 10) ? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat dateFormatter = new SimpleDateFormat(pattern);
        paymentDocument.setDate(dateFormatter.parse(dateString));

        if (!((bankAccountId == null) || bankAccountId.isEmpty())) {
          BankAccount bankAccount = bankAccountService.retrieveById(Long.parseLong(bankAccountId));
          paymentDocument.setBankAccount(bankAccount);
        }

        paymentDocument.setDescription(description);

      } catch (ParseException | NumberFormatException | POMServicesException e) {

        LOG.error("Could not update Doc properties: " + e.getMessage(), e);
        req.getSession(false)
            .setAttribute("errorMessage", "Could not update Doc properties: " + e.getMessage());
        return;
      }
    }
  }