private void setCheckOut2DB(boolean sent) {

    CheckIn.updateActive(getActivity(), currentCheckin.get("id_checkin"), "noactive");

    String id_checkout = String.valueOf(System.currentTimeMillis());
    String date_time = DatesHelper.getStringDate(new Date());

    if (!sent) {
      // id_checkout = "N" + id_checkout;
      CheckOut.updateInsert(
          getActivity(),
          currentCheckin.get("id_checkin"),
          id_checkout,
          "no_sent",
          visit_id,
          date_time);
    } else {
      CheckOut.updateInsert(
          getActivity(),
          currentCheckin.get("id_checkin"),
          id_checkout,
          "sent",
          visit_id,
          date_time);
    }
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_visit, container, false);

    context = getActivity();

    visit_id = getArguments().getString("visit_id");
    pdv_id = getArguments().getString("pdv_id");
    pdv = getArguments().getString("pdv");
    credit = getArguments().getString("credit_line");
    pdv_rfc = getArguments().getString("pdv_rfc");

    GPSTracker tracker = new GPSTracker(getActivity());
    latitude = tracker.getLatitude();
    longitude = tracker.getLongitude();

    Log.d(LOG_VISIT_FRAGMENT, "PDV VISITADO: " + pdv_id);

    TrackerManager.sharedInstance().addInfoToCurrentUtils("id_pdv", pdv_id);

    TextView txt_header = (TextView) view.findViewById(R.id.txt_header);
    txt_header.setText(pdv);

    lst_step_visit = (RecyclerView) view.findViewById(R.id.lst_step_visit);
    lst_step_visit.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    lst_step_visit.setLayoutManager(llm);

    // checkin     = WorkPlan.getCheckInByVisitID(getActivity(),visit_id);

    currentCheckin = CheckIn.getActiveMap(getActivity(), visit_id);

    if (currentCheckin.size() > 0) checkin = "active";
    else checkin = "noactive";

    if (FormAnswers.getMapByVisitID(context, visit_id).size() > 0) form = "form";
    else form = "noform";

    adapter = new StepVisitsAdapter(createListData(), getActivity(), checkin, form);
    adapter.listener = this;

    lst_step_visit.setAdapter(adapter);

    bundle = new Bundle();

    bundle.putString("pdv_id", pdv_id);
    bundle.putString("pdv", pdv);
    bundle.putString("visit_id", visit_id);
    bundle.putString("latitude", String.valueOf(latitude));
    bundle.putString("longitude", String.valueOf(longitude));
    bundle.putString("pdv_rfc", pdv_rfc);
    bundle.putString("credit_line", credit);

    checkoutFlag = false;

    return view;
  }
  private void setCheckIn2DB(boolean sent, String id_visit_session) {

    String id_checkin = String.valueOf(System.currentTimeMillis());
    String date_time = DatesHelper.getStringDate(new Date());

    if (!sent) {
      // id_checkin = "N" + id_checkin;
      CheckIn.insert(
          getActivity(),
          id_checkin,
          "no_sent",
          latitude,
          longitude,
          visit_id,
          date_time,
          id_visit_session,
          "active");
    } else {
      CheckIn.insert(
          getActivity(),
          id_checkin,
          "sent",
          latitude,
          longitude,
          visit_id,
          date_time,
          id_visit_session,
          "active");
    }

    CheckOut.insert(
        getActivity(),
        id_checkin,
        "pre_checkout" + id_checkin,
        "no_sent",
        visit_id,
        date_time,
        id_visit_session);

    checkin = "active";
  }
  @Override
  public void clickFromList(int position) {
    Log.d(LOG_VISIT_FRAGMENT, "Position: " + position);
    Map<String, String> params = new HashMap<String, String>();

    currentCheckin = CheckIn.getActiveMap(getActivity(), visit_id);

    if (currentCheckin.size() > 0) checkin = "active";
    else checkin = "noactive";

    Log.e("CHECKIN CLICK", checkin);

    if (position == 0) {

      // if(checkin.equalsIgnoreCase("no_checkin")) {
      if (!checkin.equalsIgnoreCase("active")) {
        params.put("latitude", String.valueOf(latitude));
        params.put("longitude", String.valueOf(longitude));
        params.put("visit_id", visit_id);
        params.put("date_time", "" + DatesHelper.getStringDateDays(new Date()));
        prepareRequest(METHOD.SEND_START_VISIT, params, true, true);
      } else if (checkin.equalsIgnoreCase("active")) {
        Toast.makeText(getActivity(), "La visita ya fue iniciada", Toast.LENGTH_SHORT).show();
      }

    } else if (position == 1) {
      if (checkin.equalsIgnoreCase("active")) {
        // prepareRequest(METHOD.GET_PRODUCT,params,true);

        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        FragmentOrder fragment = new FragmentOrder();
        fragment.tag = FRAGMENT_TAG.FRAG_ORDERS.toString();
        fragment.section_index = NavigationDrawerFragment.WORK_PLAN;
        fragment.setArguments(bundle);
        fragmentTransaction.addToBackStack(null);
        // fragmentTransaction.replace(R.id.container, fragment,
        // TrackerFragment.FRAGMENT_TAG.FRAG_HOME.toString());
        fragmentTransaction.replace(R.id.container, fragment, FRAGMENT_TAG.FRAG_ORDERS.toString());

        fragmentTransaction.commit();
        ((MainActivity) getActivity()).depthCounter = 3;
      } else if (!checkin.equalsIgnoreCase("active")) {
        Toast.makeText(getActivity(), "Primero haz Check In", Toast.LENGTH_SHORT).show();
      }

    } else if (position == 2) {
      FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      FragmentPromotions fragment = new FragmentPromotions();
      fragment.tag = FRAGMENT_TAG.FRAG_SALES.toString();
      fragment.section_index = NavigationDrawerFragment.WORK_PLAN;
      fragment.setArguments(bundle);
      fragmentTransaction.addToBackStack(null);
      fragmentTransaction.replace(R.id.container, fragment, FRAGMENT_TAG.FRAG_SALES.toString());
      fragmentTransaction.commit();
      ((MainActivity) getActivity()).depthCounter = 3;
    } else if (position == 3) {
      if (checkin.equalsIgnoreCase("active")) {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        FragmentCashing fragment = new FragmentCashing();
        fragment.tag = FRAGMENT_TAG.FRAG_PAYMENT.toString();
        fragment.section_index = NavigationDrawerFragment.WORK_PLAN;
        fragment.setArguments(bundle);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.container, fragment, FRAGMENT_TAG.FRAG_PAYMENT.toString());
        fragmentTransaction.commit();
        ((MainActivity) getActivity()).depthCounter = 3;
      } else if (!checkin.equalsIgnoreCase("active")) {
        Toast.makeText(getActivity(), "Primero haz Check In", Toast.LENGTH_SHORT).show();
      }
    } else if (position == 4) {
      if (checkin.equalsIgnoreCase("active")) {

        // if (WorkPlan.getAnswersandDatetimeByVisit(context, visit_id).get("answers").equals("")) {
        if (FormAnswers.getAnswers(context, visit_id).equals("")) {

          try {
            id_Form =
                Form.getAllInMaps(context)
                    .get(0)
                    .get("id_form"); // Si no hay formulario se puede causar una excepcion
            // por el ".get("id_form")"
            bundle.putString("id_form", id_Form);

            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            FragmentForms fragment = new FragmentForms();
            fragment.tag = FRAGMENT_TAG.FRAG_FORMS.toString();
            fragment.section_index = NavigationDrawerFragment.WORK_PLAN;
            fragment.setArguments(bundle);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.replace(
                R.id.container, fragment, FRAGMENT_TAG.FRAG_FORMS.toString());
            fragmentTransaction.commit();
            ((MainActivity) getActivity()).depthCounter = 3;

          } catch (Exception e) {
            // No hay formularios para esta visita
            Toast.makeText(getActivity(), "No hay formulario para este cliente", Toast.LENGTH_LONG)
                .show();
          }

        } else {
          Toast.makeText(
                  getActivity(), "Ya se envio un formulario para esta visita", Toast.LENGTH_LONG)
              .show();
        }

      } else if (!checkin.equalsIgnoreCase("active")) {
        Toast.makeText(getActivity(), "Primero haz Check In", Toast.LENGTH_SHORT).show();
      }

    } else if (position == 5) {
      if (!checkin.equalsIgnoreCase("active")) {
        Toast.makeText(getActivity(), "Primero haz Check In", Toast.LENGTH_SHORT).show();
      } else {
        String id_visit_session =
            CheckIn.getIdVisitSession(getActivity(), currentCheckin.get("id_checkin"));

        checkoutFlag = true;

        if (!id_visit_session.equalsIgnoreCase("no_id_visit_session")) {

          params.put("visit_id", visit_id);
          params.put("date_time", "" + DatesHelper.getStringDateDays(new Date()));
          params.put("id_visit_session", "" + id_visit_session);

          prepareRequest(METHOD.END_VISIT, params, true, true);

        } else if (id_visit_session.equalsIgnoreCase("no_id_visit_session")) {

          RequestManager.sharedInstance()
              .showConfirmationDialogWithListener(
                  "No fue posible enviar el Check Out, se registrara en modo OFFLINE",
                  getActivity(),
                  this);
          setCheckOut2DB(false);
          getActivity().onBackPressed();
        }
      }
    }
  }