예제 #1
0
        private async void GuardarRuta()
        {
            if (string.IsNullOrEmpty(this.Nombre))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Favor de ingresar el nombre de la ruta", "Aceptar");

                return;
            }

            if (string.IsNullOrEmpty(this.NombreChofer))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Favor de ingresar nombre de Chofer asignado", "Aceptar");

                return;
            }

            if (string.IsNullOrEmpty(this.NombreChofer))
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Favor de ingresar nombre de Chofer asignado", "Aceptar");

                return;
            }

            if (this.KmSalida <= 0.0m)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Favor de ingresar kilometraje de salida", "Aceptar");

                return;
            }

            if (this.Identifier != 0)
            {
                if (this.KmLlegada <= 0.0m)
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "Favor de ingresar kilometraje de llegada", "Aceptar");

                    return;
                }

                if (this.KmLlegada <= this.KmSalida)
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "Kilometraje de llegada no puede ser menor o igual a kilometraje de salida", "Aceptar");

                    return;
                }
            }


            this.ActualizarRegistroPorKm(this.kmSalidaInicial, KmSalida);
            this.ActualizarRegistroPorKm(this.kmLlegadaInicial, KmLlegada);

            //Al tener el Identifier igual a 0, quiere decir que es creación de Ruta
            if (this.Identifier == 0)
            {
                //Armar el cuerpo para petición
                JObject ruta = new JObject();
                ruta["Nombre"]      = this.Nombre;
                ruta["Fecha"]       = this.Fecha.ToString("yyyy/MM/dd");
                ruta["HoraLLegada"] = string.Format("{0:hh\\:mm\\:ss}", this.TsHoraLlegada);
                ruta["HoraSalida"]  = string.Format("{0:hh\\:mm\\:ss}", this.TsHoraSalida);
                ruta["KmSalida"]    = this.KmSalida;
                ruta["KmLlegada"]   = this.KmLlegada;
                JObject objChofer = new JObject();
                objChofer["Identifier"] = this.Chofer.Identifier;
                ruta["Chofer"]          = objChofer;
                ruta["Status"]          = 1;
                JArray arrPedidos = new JArray();

                //ruta["DetalleRuta"]= new JArray();
                if (this.DetalleRuta.Count > 0)
                {
                    foreach (var item in this.DetalleRuta)
                    {
                        JObject itemPedido = new JObject();
                        itemPedido["Pedido"] = item.Identifier;
                        itemPedido["Status"] = 1;
                        arrPedidos.Add(itemPedido);
                    }
                    ruta["DetalleRuta"] = arrPedidos;
                }

                //await Application.Current.MainPage.DisplayAlert("Info", ruta.ToString(), "Aceptar");
                //Comienza petición para crear Ruta
                this.IsRunning = true;
                this.IsEnabled = false;

                var connection = await this.apiService.CheckConnection();

                if (!connection.IsSuccess)
                {
                    this.IsRunning = false;
                    this.IsEnabled = true;
                    await Application.Current.MainPage.DisplayAlert("Error", connection.Message, "Aceptar");

                    return;
                }

                //Validar que la ruta que se intenta crear no contenga un nombre duplicado
                //var responseRutas = await apiService.GetRutas("https://apps.fussionweb.com/", "/sietest/Mobile", "/Rutas");
                var responseRutas = await apiService.GetRutas("https://apps.fussionweb.com/", "/sie/Mobile", "/Rutas");

                var rutasList             = (List <Ruta>)responseRutas.Result;
                var existeNombreRuta      = false;
                var choferAsignadoConRuta = false;
                if (rutasList.Count > 0)
                {
                    foreach (var item in rutasList)
                    {
                        if (string.Equals(item.Nombre, this.Nombre))
                        {
                            existeNombreRuta = true;
                        }

                        if (string.Equals(this.Chofer.FormattedId, item.Chofer.FormattedId) && string.Equals(item.Status, "1"))
                        {
                            choferAsignadoConRuta = true;
                        }
                    }

                    if (existeNombreRuta)
                    {
                        this.IsRunning = false;
                        this.IsEnabled = true;
                        await Application.Current.MainPage.DisplayAlert("Error", "El nombre de la ruta ya existe " + Environment.NewLine + "Favor de elegir uno nuevo", "Aceptar");

                        return;
                    }

                    if (choferAsignadoConRuta)
                    {
                        this.IsRunning = false;
                        this.IsEnabled = true;
                        await Application.Current.MainPage.DisplayAlert("Error", "El chofer asignado ya cuenta con Rutas Abiertas" + Environment.NewLine + "Favor de elegir uno nuevo y/o cerrar las rutas asignadas a " + this.Chofer.Nombre, "Aceptar");

                        await App.Navigator.PopAsync();

                        return;
                    }
                }


                var response = await apiService.InsertarRuta("https://apps.fussionweb.com/", "sie/Mobile", "/InsertarRuta", ruta);

                //var response = await apiService.InsertarRuta("https://apps.fussionweb.com/", "sietest/Mobile", "/InsertarRuta", ruta);

                if (!response.IsSuccess)
                {
                    this.IsRunning = false;
                    this.IsEnabled = true;
                    await Application.Current.MainPage.DisplayAlert("Error", response.Message, "Aceptar");

                    return;
                }
                this.IsRunning = false;
                this.IsEnabled = true;

                await Application.Current.MainPage.DisplayAlert("Éxito", response.Message, "Aceptar");

                //MainViewModel.GetInstace().Rutas = new RutasViewModel();
                //await App.Navigator.PushAsync(new RutasPage());
                MainViewModel.GetInstace().Choferes = new ChoferesViewModel();
                Application.Current.MainPage = new MasterPage();
            }
            else
            {
                if (this.actualizar)
                {
                    //Mandar peticion

                    this.IsRunning = true;
                    this.IsEnabled = false;

                    var connection = await this.apiService.CheckConnection();

                    if (!connection.IsSuccess)
                    {
                        this.IsRunning = false;
                        this.IsEnabled = true;
                        await Application.Current.MainPage.DisplayAlert("Error", connection.Message, "Aceptar");

                        return;
                    }

                    //string json = "{\r\n\t\"Identifier\": \""+this.Identifier+"\",\r\n\t\"HoraLLegada\":\""+this.TsHoraLlegada.ToString()+"\",\r\n\t\"KmSalida\": "+this.KmSalida+ ",\r\n\t\"KmLlegada\": " + this.KmLlegada + ",\r\n\t\"Status\": "+this.StatusRutaSeleccionado.Id+"\r\n}";
                    JObject ruta = new JObject();
                    ruta["Identifier"]  = this.Identifier;
                    ruta["HoraLLegada"] = string.Format("{0:hh\\:mm\\:ss}", this.TsHoraLlegada);
                    ruta["KmSalida"]    = this.KmSalida;
                    ruta["KmLlegada"]   = this.KmLlegada;
                    ruta["Status"]      = int.Parse(this.StatusRutaSeleccionado.Id);

                    var response = await apiService.ActualizaRuta("https://apps.fussionweb.com/", "/sie/Mobile", "/ActualizarRuta", ruta);

                    //var response = await apiService.ActualizaRuta("https://apps.fussionweb.com/", "/sietest/Mobile", "/ActualizarRuta", ruta);
                    if (!response.IsSuccess)
                    {
                        this.IsRunning = false;
                        this.IsEnabled = true;
                        await Application.Current.MainPage.DisplayAlert("Error", response.Message, "Aceptar");

                        return;
                    }

                    this.IsRunning = false;
                    this.IsEnabled = true;

                    await Application.Current.MainPage.DisplayAlert("Éxito", response.Message, "Aceptar");

                    MainViewModel.GetInstace().Rutas = new RutasViewModel();
                    await App.Navigator.PopAsync();
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("Información", "No ha cambiado la información", "Aceptar");

                    await App.Navigator.PopAsync();
                }
            }



            //this.IsEnabled = false;
        }