Example #1
0
        public async Task <IActionResult> Checkout(Orden ord)
        {
            List <Productos> productos = HttpContext.Session.Get <List <Productos> >("productos");

            if (productos != null)
            {
                foreach (var producto in productos)
                {
                    OrdenDetalles ordenDetalles = new OrdenDetalles();
                    ordenDetalles.ProductoID = producto.ID;
                    ord.OrdenDetalles.Add(ordenDetalles);
                }
            }

            if (_SignInManager.IsSignedIn(User))
            {
                ord.Correo = User.Identity.Name;
            }
            ord.numeroOrden = obtenerNumeroOrden();
            ord.Estado      = "En proceso";
            _db.Orden.Add(ord);
            await _db.SaveChangesAsync();

            HttpContext.Session.Set("productos", new List <Productos>());

            return(View());
        }
        private async void Grabar()
        {
            if (this.file == null)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    "Tome la Foto",
                    "Aceptar");

                return;
            }

            foreach (var item in this.trabajo)
            {
                if (item.Check)
                {
                    this.itemseleccionado = 1;
                    break;
                }
                else
                {
                    this.itemseleccionado = 0;
                }
            }

            if (this.itemseleccionado == 0)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Mensaje",
                    "Seleccione un Item de Trabajo",
                    "Aceptar");

                return;
            }

            try
            {
                var location = await Geolocation.GetLastKnownLocationAsync();

                if (location == null)
                {
                    location = await Geolocation.GetLocationAsync(new GeolocationRequest
                    {
                        DesiredAccuracy = GeolocationAccuracy.Medium,
                        Timeout         = TimeSpan.FromSeconds(30)
                    });

                    if (location != null)
                    {
                        this.Latitud  = location.Latitude.ToString();
                        this.Longitud = location.Longitude.ToString();
                    }
                }
                else
                {
                    this.Latitud  = location.Latitude.ToString();
                    this.Longitud = location.Longitude.ToString();
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                this.Latitud  = string.Empty;
                this.Longitud = string.Empty;
            }
            catch (FeatureNotEnabledException fneEx)
            {
                this.Latitud  = string.Empty;
                this.Longitud = string.Empty;
            }
            catch (PermissionException pEx)
            {
                this.Latitud  = string.Empty;
                this.Longitud = string.Empty;
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert(
                    "Error",
                    ex.ToString(),
                    "Aceptar");
            }

            var codigomarca  = this.listamarcas.ElementAt(this.MarcaIndex).CodigoParametro;
            var codigomodelo = this.listamodelos.ElementAt(this.ModeloIndex).CodigoParametro;

            var ordernew = new Orden()
            {
                IdOrden        = Cliente.IdOrden,
                IdEquipo       = Cliente.IdEquipo,
                OrdenEstado    = "PRO",
                Cliente        = Cliente.Cliente,
                Direccion      = Cliente.Direccion,
                Contacto       = Cliente.Contacto,
                Celular        = Cliente.Celular,
                FechaInicioOT  = Cliente.FechaInicioOT,
                FechaFinalOT   = Cliente.FechaFinalOT,
                TipoTrabajo    = Cliente.TipoTrabajo,
                ProblemaEquipo = Cliente.ProblemaEquipo,
                Notas          = Cliente.Notas,
                Equipo         = Cliente.Equipo,
                MarcaId        = codigomarca,
                ModeloId       = codigomodelo,
                Voltaje        = Cliente.Voltaje,
                Amperaje       = Cliente.Amperaje,
                Presion        = Cliente.Presion,
                FechaInicioTR  = fechainiciotr,
                FechaFinalTR   = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                Observacion    = Cliente.Observacion,
                ImagenTR       = FilesHelper.ReadFully(this.file.GetStream()),
                RutaImagen     = Cliente.RutaImagen,
                Latitud        = this.Latitud,
                Logintud       = this.Longitud
            };

            this.dataService.Update(ordernew);

            foreach (var item in this.listatrabajos)
            {
                if (item.Check)
                {
                    var neworderdet = new OrdenDetalles
                    {
                        id_orden        = Cliente.IdOrden,
                        id_listatrabajo = item.Id
                    };
                    this.dataService.Insert(neworderdet);
                }
            }

            var ordencabecera = this.dataAccess.GetOrdenesLocal(Cliente.IdOrden);
            var ordendetalle  = this.dataAccess.GetOrdenDetalleLocal(Cliente.IdOrden);

            IsRunning = true;
            IsEnabled = false;

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

            if (!connection.IsSuccess)
            {
                IsRunning = false;
                isEnabled = true;

                await Application.Current.MainPage.DisplayAlert(
                    "Mensaje",
                    "Sin Conexión, Grabada Localmente",
                    "OK");
            }
            else
            {
                var apiService    = Application.Current.Resources["APIServices"].ToString();
                var ordencabedeta = Converter.ToOrdenCabeDeta(ordencabecera, ordendetalle);

                var response = await this.apiService.GrabarOrden(
                    apiService,
                    "api",
                    "ordencabedeta",
                    ordencabedeta);

                if (response.IsSuccess)
                {
                    this.dataService.Delete(ordencabecera);
                    foreach (var itemorder in ordendetalle)
                    {
                        this.dataService.Delete(itemorder);
                    }
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert(
                        response.Message,
                        "Problema en el Servicio, Grabada Localmente",
                        "OK");
                }
            }

            IsRunning = false;
            IsEnabled = true;

            var mainViewModel = MainViewModel.GetInstance();

            mainViewModel.Orders         = new OrdersViewModel();
            Application.Current.MainPage = new MasterPage();
        }