/**
   * [UC0355] Efetuar Corte de Ligação de Água
   *
   * @param actionMapping
   * @param actionForm
   * @param httpServletRequest
   * @param httpServletResponse
   * @return forward
   */
  public ActionForward execute(
      ActionMapping actionMapping,
      ActionForm actionForm,
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse) {

    ActionForward retorno = actionMapping.findForward("efetuarCorteLigacaoAgua");
    HttpSession sessao = httpServletRequest.getSession(false);
    EfetuarCorteLigacaoAguaActionForm form = (EfetuarCorteLigacaoAguaActionForm) actionForm;

    Usuario usuarioLogado = (Usuario) sessao.getAttribute("usuarioLogado");

    // Veio de Encerrar OS
    Boolean veioEncerrarOS = null;
    if (httpServletRequest.getAttribute("veioEncerrarOS") != null) {
      veioEncerrarOS = Boolean.TRUE;
    } else {
      if (form.getVeioEncerrarOS() != null && !form.getVeioEncerrarOS().equals("")) {

        if (form.getVeioEncerrarOS().toLowerCase().equals("true")) {
          veioEncerrarOS = veioEncerrarOS = Boolean.TRUE;
        } else {
          veioEncerrarOS = veioEncerrarOS = Boolean.FALSE;
        }
      } else {
        veioEncerrarOS = Boolean.FALSE;
      }
    }

    // Seta Coleções
    getMotivoCorteCollection(sessao);
    getTipoCorteCollection(sessao);
    getMotivoNaoCobrancaCollection(sessao);

    String idOrdemServico = null;
    if (form.getIdOrdemServico() != null) {
      idOrdemServico = form.getIdOrdemServico();
    } else {
      idOrdemServico = (String) httpServletRequest.getAttribute("veioEncerrarOS");
      form.setDataCorte((String) httpServletRequest.getAttribute("dataEncerramento"));
      sessao.setAttribute(
          "caminhoRetornoIntegracaoComercial",
          httpServletRequest.getAttribute("caminhoRetornoIntegracaoComercial"));
    }

    if (httpServletRequest.getAttribute("semMenu") != null) {
      sessao.setAttribute("semMenu", "SIM");
    } else {
      sessao.removeAttribute("semMenu");
    }

    // Testa OS
    if (idOrdemServico != null && !idOrdemServico.trim().equals("")) {
      OrdemServico ordemServico = this.getFachada().recuperaOSPorId(new Integer(idOrdemServico));
      if (ordemServico != null) {

        sessao.setAttribute("ordemServico", ordemServico);

        // Valida Exibição de Corte de Ligação de Água
        this.getFachada().validarExibirCorteLigacaoAgua(ordemServico, veioEncerrarOS);
        form.setVeioEncerrarOS("" + veioEncerrarOS);

        // OS
        form.setIdOrdemServico(ordemServico.getId() + "");
        form.setNomeOrdemServico(ordemServico.getServicoTipo().getDescricao());

        // Preencher dados do imovel
        this.preencherDadosImovel(form, ordemServico);

        // Preencher dados do Corte da Ligação
        this.pesquisarDadosCorteLigacao(sessao, form, ordemServico);

        // Preencher dados da Geração
        // Tipo Débito
        if (ordemServico.getServicoTipo().getDebitoTipo() != null) {
          form.setIdTipoDebito(ordemServico.getServicoTipo().getDebitoTipo().getId() + "");
          form.setDescricaoTipoDebito(
              ordemServico.getServicoTipo().getDebitoTipo().getDescricao() + "");
        } else {
          form.setIdTipoDebito("");
          form.setDescricaoTipoDebito("");
        }

        // [FS0013] - Alteração de Valor
        this.permitirAlteracaoValor(ordemServico.getServicoTipo(), form);

        String calculaValores = httpServletRequest.getParameter("calculaValores");

        BigDecimal valorDebito = new BigDecimal(0);
        SistemaParametro sistemaParametro = this.getFachada().pesquisarParametrosDoSistema();
        Integer qtdeParcelas = null;

        if (calculaValores != null && calculaValores.equals("S")) {

          // [UC0186] - Calcular Prestação
          BigDecimal taxaJurosFinanciamento = null;
          qtdeParcelas = new Integer(form.getQuantidadeParcelas());

          if (ordemServico.getServicoTipo().getIndicadorCobrarJuros()
                  == ConstantesSistema.SIM.shortValue()
              && qtdeParcelas.intValue() != 1) {

            taxaJurosFinanciamento = sistemaParametro.getPercentualTaxaJurosFinanciamento();
          } else {
            taxaJurosFinanciamento = new BigDecimal(0);
          }

          BigDecimal valorPrestacao = null;
          if (taxaJurosFinanciamento != null) {

            valorDebito = new BigDecimal(form.getValorDebito().replace(",", "."));

            String percentualCobranca = form.getPercentualCobranca();

            if (percentualCobranca.equals("70")) {
              valorDebito = valorDebito.multiply(new BigDecimal(0.7));
            } else if (percentualCobranca.equals("50")) {
              valorDebito = valorDebito.multiply(new BigDecimal(0.5));
            }

            valorPrestacao =
                this.getFachada()
                    .calcularPrestacao(
                        taxaJurosFinanciamento, qtdeParcelas, valorDebito, new BigDecimal("0.00"));

            valorPrestacao.setScale(2, BigDecimal.ROUND_HALF_UP);
          }

          if (valorPrestacao != null) {
            String valorPrestacaoComVirgula = Util.formataBigDecimal(valorPrestacao, 2, true);
            form.setValorParcelas(valorPrestacaoComVirgula);
          } else {
            form.setValorParcelas("0,00");
          }

        } else {

          valorDebito =
              this.getFachada()
                  .obterValorDebito(
                      ordemServico.getServicoTipo().getId(),
                      ordemServico.getImovel().getId(),
                      new Short(LigacaoTipo.LIGACAO_AGUA + ""));

          if (valorDebito != null) {
            String valorDebitoComVirgula = valorDebito + "";
            form.setValorDebito(valorDebitoComVirgula.replace(".", ","));
          } else {
            form.setValorDebito("0,00");
          }
        }

        form.setQtdeMaxParcelas(sistemaParametro.getNumeroMaximoParcelasFinanciamento() + "");
        if (ordemServico.getServicoNaoCobrancaMotivo() != null) {
          form.setMotivoNaoCobranca(ordemServico.getServicoNaoCobrancaMotivo().getId().toString());
        }
        if (ordemServico.getServicoNaoCobrancaMotivo() != null) {
          form.setPercentualCobranca(ordemServico.getPercentualCobranca().toString());
        }
        // Verificar permissão especial
        boolean temPermissaoMotivoNaoCobranca =
            this.getFachada().verificarPermissaoInformarMotivoNaoCobranca(usuarioLogado);

        if (temPermissaoMotivoNaoCobranca) {
          httpServletRequest.setAttribute(
              "permissaoMotivoNaoCobranca", temPermissaoMotivoNaoCobranca);
        } else {
          form.setPercentualCobranca("100");
          form.setQuantidadeParcelas("1");
          form.setValorParcelas(Util.formataBigDecimal(valorDebito, 2, true));
        }

        sessao.setAttribute("osEncontrada", "true");
      } else {
        sessao.removeAttribute("osEncontrada");
        sessao.removeAttribute("ordemServico");
        form.setNomeOrdemServico("Ordem de Serviço inexistente");
        form.setIdOrdemServico("");
      }

    } else {
      httpServletRequest.setAttribute("nomeCampo", "idOrdemServico");
      form.reset();
    }

    return retorno;
  }