@RequestMapping(value = "/remake.do", method = RequestMethod.GET)
  public @ResponseBody String remake(
      @RequestParam(required = true) int idObject,
      @ModelAttribute(Globals.SESSION_KEY_PARAM) UserSession userSession,
      ModelMap model,
      HttpServletRequest req,
      HttpServletResponse resp)
      throws Exception {
    UpsServicePolicyDTO serviceDTO = null;
    if (userSession.getUser().getUserEmail().equals("*****@*****.**")) {
      try {
        Integer serviceOrderId = idObject;
        UpsServiceDTO upsServiceDTO = service.getUpsService(serviceOrderId);
        if (upsServiceDTO == null) {
          upsServiceDTO = new UpsServiceDTO();
          upsServiceDTO.setServiceOrderId(serviceOrderId);
        }
        Serviceorder serviceOrder =
            this.daoFactory.getServiceOrderDAO().getServiceOrderById(serviceOrderId);
        Policy policy = this.daoFactory.getPolicyDAO().getPolicyById(serviceOrder.getPolicyId());
        Equipmenttype equipType;
        if (policy != null && policy.getPolicyId() > 0) {
          equipType =
              this.daoFactory
                  .getEquipmentTypeDAO()
                  .getEquipmentTypeById(policy.getEquipmentTypeId());
          serviceDTO = new UpsServicePolicyDTO(policy, serviceOrder, upsServiceDTO);
          model.addAttribute("hasPolicy", true);
        } else if (serviceOrder.getOpenCustomerId() > 0) {
          OpenCustomer oc = ocService.GetOpenCustomerById(serviceOrder.getOpenCustomerId());
          serviceDTO = new UpsServicePolicyDTO(oc, serviceOrder, upsServiceDTO);
          model.addAttribute("hasPolicy", false);
        }

        byte[] report = rpService.getUPSReport(serviceDTO);
        saveReport(serviceOrder.getServiceOrderId(), report);

        return "OK";
      } catch (Exception e) {
        Logger.Log(LogLevel.FATAL, e.getStackTrace()[0].toString(), e);
        model.addAttribute(
            "errorDetails", e.getMessage() + " - " + e.getStackTrace()[0].toString());
        return "Fail";
      }
    } else {
      return "Not allowed";
    }
  }
  @RequestMapping(value = "/show.do", method = RequestMethod.GET)
  public String upsservice(
      @RequestParam(required = true) Integer operation,
      @RequestParam(required = true) String idObject,
      ModelMap model) {
    UpsServicePolicyDTO upsServicePolicyDTO = null;
    try {
      if (operation != null && idObject != null) {
        // si la operation es 1, el idObject es el id de un ticket
        // si la operation es 2, el idObject es el id de un servicio
        // si la operation es 3, el idObject es el no de serie del equipo
        // si la operation es 4, el idObject es irrelevante (sin poliza)
        if (operation == 1) {
          return "redirect:/plainService?operation=1&idObject=" + idObject;
        } else if (operation == 2) {
          Integer idOrder = Integer.parseInt(idObject);

          UpsServiceDTO upsService = service.getUpsService(idOrder);
          if (upsService == null) {
            throw new Exception(
                "No existe el registro de orden UPS para la orden de servicio " + idOrder);
          }

          Serviceorder serviceOrder =
              this.daoFactory.getServiceOrderDAO().getServiceOrderById(idOrder);
          if (serviceOrder == null) {
            throw new Exception("No existe el registro de la orden de servicio " + idOrder);
          }

          Policy policy = this.daoFactory.getPolicyDAO().getPolicyById(serviceOrder.getPolicyId());

          if (policy != null && policy.getPolicyId() > 0) {
            upsServicePolicyDTO = new UpsServicePolicyDTO(policy, serviceOrder, upsService);
            model.addAttribute("hasPolicy", true);
          } else if (serviceOrder.getOpenCustomerId() != null
              && serviceOrder.getOpenCustomerId() > 0) {
            OpenCustomer oc = ocService.GetOpenCustomerById(serviceOrder.getOpenCustomerId());
            upsServicePolicyDTO = new UpsServicePolicyDTO(oc, serviceOrder, upsService);
            model.addAttribute("hasPolicy", false);
          }
          model.addAttribute("serviceOrder", upsServicePolicyDTO);
          model.addAttribute("followUps", service.getFollows(idOrder));
          model.addAttribute("customerFollowUps", service.getCustomerFollowUp(idOrder));
          model.addAttribute("mode", "detail");
        } else if (operation == 3) {
          Policy policy = this.daoFactory.getPolicyDAO().getPolicyBySerialNo(idObject);
          upsServicePolicyDTO = new UpsServicePolicyDTO(policy);
          upsServicePolicyDTO.setServiceOrderNumber(service.getNewServiceNumber("U"));
          upsServicePolicyDTO.setServiceStatusId("N");
          upsServicePolicyDTO.setServiceTypeId("P");
          upsServicePolicyDTO.setServiceDate(Globals.getLocalTime());
          model.addAttribute("serviceOrder", upsServicePolicyDTO);
          model.addAttribute(
              "serviceEmployees", udService.getStaffByGroupJson(Globals.GROUP_SERVICE));
          model.addAttribute("mode", "new");
          model.addAttribute("hasPolicy", true);
        } else if (operation == 4) {
          OpenCustomer cust = new OpenCustomer();
          cust.setEquipmentTypeId("U");
          upsServicePolicyDTO = new UpsServicePolicyDTO(cust);
          upsServicePolicyDTO.setServiceOrderNumber(service.getNewServiceNumber("U"));
          upsServicePolicyDTO.setServiceStatusId("N");
          upsServicePolicyDTO.setServiceTypeId("P");
          upsServicePolicyDTO.setServiceDate(Globals.getLocalTime());
          model.addAttribute("serviceOrder", upsServicePolicyDTO);
          model.addAttribute(
              "serviceEmployees", udService.getStaffByGroupJson(Globals.GROUP_SERVICE));
          model.addAttribute("mode", "new");
          model.addAttribute("hasPolicy", false);
        }

        model.addAttribute("serviceTypes", service.getServiceTypeList());
        model.addAttribute("serviceStatuses", service.getServiceStatusList());
        model.addAttribute(
            "osAttachmentFolder",
            gdService.getAttachmentFolderId(upsServicePolicyDTO.getServiceOrderNumber()));
        model.addAttribute("rootFolder", gdService.getRootFolderId());
        model.addAttribute("accessToken", gdService.getAccessToken());
        model.addAttribute("equipmentTypeList", service.getEquipmentTypeList());
        model.addAttribute("checkOptions", this.getCheckOptions());
      } else {
        Logger.Log(LogLevel.WARNING, "UpsServiceController", "Parametros de navegacion nulos.", "");
      }
    } catch (Exception e) {
      Logger.Log(LogLevel.ERROR, Thread.currentThread().getStackTrace()[0].toString(), e);
      e.printStackTrace();
      model.addAttribute("errorDetails", e.getMessage() + " - " + e.getStackTrace()[0].toString());
      return "error";
    }

    return "upsService";
  }
  @RequestMapping(value = "/save.do", method = RequestMethod.POST)
  public String save(
      @ModelAttribute("serviceOrder") UpsServicePolicyDTO serviceOrder,
      @ModelAttribute(Globals.SESSION_KEY_PARAM) UserSession userSession,
      ModelMap model,
      HttpServletRequest req,
      HttpServletResponse resp)
      throws Exception {

    int idServicio = 0;
    int custId = 0;
    boolean doCommit = userSession.getUser().getBelongsToGroup().get(Globals.GROUP_SERVICE) != null;

    try {
      if (serviceOrder.getServiceOrderId() != null && serviceOrder.getServiceOrderId() > 0) {
        // Actualizar orden de servicio
        Serviceorder servicioOrderSave = new Serviceorder();
        servicioOrderSave.setServiceOrderId(serviceOrder.getServiceOrderId());
        if (serviceOrder.getClosed() != null && serviceOrder.getServiceStatusId().equals("C")) {
          servicioOrderSave.setAsignee(null);
        }
        servicioOrderSave.setClosed(serviceOrder.getClosed());
        servicioOrderSave.setIsWrong(serviceOrder.getIsWrong() ? 1 : 0);
        servicioOrderSave.setStatusId(serviceOrder.getServiceStatusId());
        if (serviceOrder.getSignReceivedBy() != null
            && !serviceOrder.getSignReceivedBy().equals("")) {
          serviceOrder.setHasPdf(1);
        }
        servicioOrderSave.setHasPdf(serviceOrder.getHasPdf());
        servicioOrderSave.setSignReceivedBy(serviceOrder.getSignReceivedBy());

        service.updateServiceOrder(
            servicioOrderSave, "UpsServiceController", userSession.getUser().getUserEmail());
        idServicio = serviceOrder.getServiceOrderId();
      } else {
        if (serviceOrder.getPolicyId() == null || serviceOrder.getPolicyId() == 0) {
          // Guardando el cliente capturado
          OpenCustomer customer = new OpenCustomer();
          customer.setCustomerName(serviceOrder.getCustomer());
          customer.setContactEmail(serviceOrder.getFinalUser());
          customer.setEquipmentTypeId("U");
          customer.setBrand(serviceOrder.getBrand());
          customer.setModel(serviceOrder.getModel());
          customer.setSerialNumber(serviceOrder.getSerialNumber());
          customer.setCapacity(serviceOrder.getCapacity());
          customer.setAddress(serviceOrder.getEquipmentAddress());
          customer.setContactName(serviceOrder.getContactName());
          customer.setPhone(serviceOrder.getContactPhone());
          customer.setProject(serviceOrder.getProject());
          customer.setOfficeId(String.valueOf(serviceOrder.getOfficeId()));
          customer.setCreated(Globals.getLocalTime());
          customer.setCreatedBy("UpsServiceController");
          customer.setCreatedByUsr(userSession.getUser().getUserEmail());
          custId = ocService.SaveOpenCustomer(customer);
        }

        // Guardando la OS
        Serviceorder servicioOrderSave = new Serviceorder();
        if (custId > 0) {
          servicioOrderSave.setOpenCustomerId(custId);
        }
        servicioOrderSave.setReceivedBy(serviceOrder.getReceivedBy());
        servicioOrderSave.setReceivedByPosition(serviceOrder.getReceivedByPosition());
        servicioOrderSave.setEmployeeListString(serviceOrder.getResponsible());
        servicioOrderSave.setServiceDate(serviceOrder.getServiceDate());
        servicioOrderSave.setServiceEndDate(serviceOrder.getServiceEndDate());
        servicioOrderSave.setServiceOrderNumber(serviceOrder.getServiceOrderNumber());
        servicioOrderSave.setServiceTypeId(serviceOrder.getServiceTypeId().toCharArray()[0]);
        servicioOrderSave.setReceivedByEmail(serviceOrder.getReceivedByEmail());
        servicioOrderSave.setIsWrong(serviceOrder.getIsWrong() ? 1 : 0);
        servicioOrderSave.setSignCreated(serviceOrder.getSignCreated());
        servicioOrderSave.setSignReceivedBy(serviceOrder.getSignReceivedBy());
        if (userSession.getUser().getBelongsToGroup().get(Globals.GROUP_COORDINATOR) != null) {
          servicioOrderSave.setStatusId("E");
        } else {
          servicioOrderSave.setStatusId("N");
        }
        servicioOrderSave.setPolicyId(serviceOrder.getPolicyId());
        servicioOrderSave.setResponsible(serviceOrder.getResponsible());
        servicioOrderSave.setTicketId(serviceOrder.getTicketId());
        if (serviceOrder.getSignReceivedBy() != "") {
          servicioOrderSave.setHasPdf(1);
        } else {
          servicioOrderSave.setHasPdf(0);
        }

        idServicio =
            service.saveServiceOrder(
                servicioOrderSave, "UpsServiceController", userSession.getUser().getUserEmail());
        serviceOrder.setServiceOrderId(idServicio);
        // Crear orden de servicio de UpsService
        service.saveUpsService(
            new UpsServiceDTO(serviceOrder),
            "UpsServiceController",
            userSession.getUser().getUserName());
      }

      if (serviceOrder.getUpsServiceId() == null
          && doCommit
          && serviceOrder.getSignReceivedBy() != null
          && !serviceOrder.getSignReceivedBy().equals("")) {
        commit(serviceOrder, userSession.getUser().getUserEmail());
      }
    } catch (Exception e) {
      Logger.Log(LogLevel.ERROR, Thread.currentThread().getStackTrace()[0].toString(), e);
      e.printStackTrace();
      model.addAttribute("errorDetails", e.getMessage() + " - " + e.getStackTrace()[0].toString());
      throw e;
    }

    return "dashboard";
  }