Exemplo n.º 1
0
        //obtener por id
        public SPSubCentroCosto GetById(int id)
        {
            var subcentro = new SPSubCentroCosto();

            try
            {
                using (var ctx = new PresupuestoContext())
                {
                    subcentro = ctx.SPSubCentroCosto.Where(x => x.Id == id).SingleOrDefault();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(subcentro);
        }
Exemplo n.º 2
0
        //metodo para guardar lote y detalle lote
        public string Save(CreateSubcentroDto model)
        {
            var rpta = "";

            try
            {
                using (var ctx = new PresupuestoContext())
                {
                    using (DbContextTransaction transaction = ctx.Database.BeginTransaction())
                    {
                        try
                        {
                            var subCentroCosto = new SPSubCentroCosto
                            {
                                Id         = model.Id,
                                fincaId    = model.fincaId,
                                nombre     = model.nombre,
                                codigo_nav = model.codigo_nav,
                                codigo     = model.codigo,
                                estado     = "A"
                            };

                            if (subCentroCosto.Id > 0)
                            {
                                ctx.Entry(subCentroCosto).State = EntityState.Modified;
                            }
                            else
                            {
                                ctx.SPSubCentroCosto.Add(subCentroCosto);
                            }
                            ctx.SaveChanges();

                            int fincaId = subCentroCosto.Id;

                            //eliminamos los existentes para sustituirlos
                            var detalles = ctx.SPCCostoActividad.Where(x => x.fincaId == fincaId).ToList();
                            ctx.SPCCostoActividad.RemoveRange(detalles);
                            ctx.SaveChanges();

                            if (model.actividades != null)
                            {
                                foreach (var d in model.actividades)
                                {
                                    var detalle = new SPCCostoActividad
                                    {
                                        fincaId     = fincaId,
                                        actividadId = d
                                    };

                                    ctx.SPCCostoActividad.Add(detalle);
                                    ctx.SaveChanges();
                                }
                            }
                            transaction.Commit();
                            rpta = "sub centro de costo registado correctamente";
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            throw ex;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(rpta);
        }