コード例 #1
0
        public async Task <IActionResult> Create([Bind("IdOrden,FechaPlanificacion,Razon,Estado")] Orden orden)
        {
            var hayPesaje     = (Request.Form["hayPesaje"] == "on");
            var hayInspeccion = (Request.Form["hayInspeccion"] == "on");
            var hayVacunacion = (Request.Form["hayVacunacion"] == "on");


            Abstracciones.OrdenBuilder builder = new Abstracciones.OrdenBuilder(orden.FechaPlanificacion, orden.Razon);
            var seleccionados = (Request.Form["PlanificacionVacunacion"].ToList());

            //Pesaje
            if (hayPesaje)
            {
                builder.WithPesaje(true);
            }
            else
            {
                builder.WithPesaje(false);
            }

            //Inspeccion
            if (hayInspeccion)
            {
                builder.WithInspeccion(true);
            }
            else
            {
                builder.WithInspeccion(false);
            }

            //Vacunacion
            if (hayVacunacion)
            {
                builder.WithVacunacion(true, seleccionados);
            }
            else
            {
                builder.WithVacunacion(false, null);
            }

            orden = builder.GetOrden();
            if (ModelState.IsValid)
            {
                using (var transaccion = _context.Database.BeginTransaction())
                {
                    try
                    {
                        if (orden.FechaPlanificacion < DateTime.Now)
                        {
                            return(View("~/Views/Shared/Error.cshtml"));
                        }
                        _context.Add(orden);
                        await _context.SaveChangesAsync();



                        orden.Conteo.First().IdOrden = orden.IdOrden;
                        Conteo cont = orden.Conteo.First();
                        _context.Add(cont);

                        if (orden.Vacunacion != null)
                        {
                            orden.Vacunacion.First().IdOrden = orden.IdOrden;
                            _context.Add(orden.Vacunacion.First());
                            foreach (PlanificacionVacunacion plan in orden.Vacunacion.First().PlanificacionVacunacion)
                            {
                                plan.IdVacuna = orden.Vacunacion.First().IdVacunacion;
                                _context.Add(plan);
                            }
                        }
                        if (orden.Inspeccion != null)
                        {
                            orden.Inspeccion.First().IdOrden = orden.IdOrden;
                            _context.Add(orden.Inspeccion.First());
                        }
                        if (orden.Pesaje != null)
                        {
                            orden.Pesaje.First().IdOrden = orden.IdOrden;
                            _context.Add(orden.Pesaje.First());
                        }
                        transaccion.Commit();
                    }
                    catch
                    {
                        transaccion.Rollback();
                        return(View("~/Views/Shared/Error.cshtml"));

                        throw;
                    }
                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(orden));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("IdOrden,FechaPlanificacion,Razon,Estado")] Orden orden)
        {
            var hayPesaje     = (Request.Form["hayPesaje"] == "on");
            var hayInspeccion = (Request.Form["hayInspeccion"] == "on");
            var hayVacunacion = (Request.Form["hayVacunacion"] == "on");
            var seleccionados = (Request.Form["PlanificacionVacunacion"].ToList());

            if (id != orden.IdOrden)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (orden.FechaPlanificacion < DateTime.Now)
                    {
                        return(View("~/Views/Shared/Error.cshtml"));
                    }
                    Pesaje     psj;
                    Vacunacion vcn;
                    Inspeccion nspccn;
                    Conteo     cnt = _context.Conteo.Where(o => o.IdOrden == orden.IdOrden).First();

                    if (_context.Pesaje.Where(o => o.IdOrden == orden.IdOrden).Count() == 0)
                    {
                        psj = null;
                    }
                    else
                    {
                        psj = _context.Pesaje.Where(o => o.IdOrden == orden.IdOrden).First();
                    }

                    if (_context.Vacunacion.Where(o => o.IdOrden == orden.IdOrden).Count() == 0)
                    {
                        vcn = null;
                    }
                    else
                    {
                        vcn = _context.Vacunacion.Where(o => o.IdOrden == orden.IdOrden).First();
                    }

                    if (_context.Inspeccion.Where(o => o.IdOrden == orden.IdOrden).Count() == 0)
                    {
                        nspccn = null;
                    }
                    else
                    {
                        nspccn = _context.Inspeccion.Where(o => o.IdOrden == orden.IdOrden).First();
                    }



                    Abstracciones.OrdenBuilder builder = new Abstracciones.OrdenBuilder(orden);

                    if (hayPesaje)
                    {
                        builder.WithPesaje(true);
                    }
                    else
                    {
                        if (psj != null)
                        {
                            _context.Remove(psj);
                        }
                        builder.WithPesaje(false);
                    }

                    if (hayInspeccion)
                    {
                        builder.WithInspeccion(true);
                    }
                    else
                    {
                        if (nspccn != null)
                        {
                            _context.Remove(nspccn);
                        }
                        builder.WithInspeccion(false);
                    }

                    if (hayVacunacion)
                    {
                        builder.WithVacunacion(true, seleccionados);
                    }
                    else
                    {
                        builder.WithVacunacion(false, null);
                    }



                    if (vcn != null)
                    {
                        foreach (PlanificacionVacunacion plan in _context.PlanificacionVacunacion.Where(o => o.IdVacunacion == vcn.IdVacunacion))
                        {
                            _context.Remove(plan);
                        }
                        _context.Remove(vcn);
                    }


                    orden = builder.GetOrden();
                    using (var transaccion = _context.Database.BeginTransaction())
                    {
                        try
                        {
                            _context.Update(orden);
                            await _context.SaveChangesAsync();

                            cnt.IdOrden = orden.IdOrden;
                            Conteo cont = cnt;
                            _context.Update(cont);

                            if (orden.Inspeccion != null && orden.Inspeccion.Count() != 0)
                            {
                                orden.Inspeccion.First().IdOrden = orden.IdOrden;
                                if (nspccn == null)
                                {
                                }
                                else
                                {
                                    orden.Inspeccion.First().IdInspeccion = nspccn.IdInspeccion;
                                    _context.Update(orden.Inspeccion.First());
                                }
                            }
                            if (orden.Pesaje != null && orden.Pesaje.Count() != 0)
                            {
                                orden.Pesaje.First().IdOrden = orden.IdOrden;
                                if (psj == null)
                                {
                                }
                                else
                                {
                                    orden.Pesaje.First().IdPesaje = psj.IdPesaje;
                                    _context.Update(orden.Inspeccion.First());
                                }
                            }
                            transaccion.Commit();
                        }
                        catch
                        {
                            transaccion.Rollback();
                            return(View("~/Views/Shared/Error.cshtml"));

                            throw;
                        }
                        await _context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrdenExists(orden.IdOrden))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }