Example #1
0
        public GencodeDayDTO Save(GencodeDayDTO dto)
        {
            try
            {
                using (SEDESOLEntities db = new SEDESOLEntities())
                {
                    GEN_CODE_DAY day = db.GEN_CODE_DAY.FirstOrDefault(v => v.Id == dto.Id);
                    if (day != null)
                    {
                        GEN_CODE_DAY day2 = db.GEN_CODE_DAY.FirstOrDefault(v => v.Id_Year == dto.Id_Year && v.Id_Month == dto.Id_Month && v.Id != dto.Id);
                        if (day2 != null)
                        {
                            dto.Message = "Se ha ingresado previamente una fecha para el mes y año seleccionados.";
                        }
                        else
                        {
                            day.Id_Month = dto.Id_Month;
                            day.Id_Year  = dto.Id_Year;
                            day.Day      = dto.Day;
                            if (db.SaveChanges() > 0)
                            {
                                dto.Id      = day.Id;
                                dto.Message = "SUCCESS";
                            }
                        }
                    }
                    else
                    {
                        GEN_CODE_DAY day2 = db.GEN_CODE_DAY.FirstOrDefault(v => v.Id_Year == dto.Id_Year && v.Id_Month == dto.Id_Month);
                        if (day2 != null)
                        {
                            dto.Message = "Se ha ingresado previamente una fecha para el mes y año seleccionados.";
                        }
                        else
                        {
                            day          = new GEN_CODE_DAY();
                            day.Id_Month = dto.Id_Month;
                            day.Id_Year  = dto.Id_Year;
                            day.Day      = dto.Day;
                            db.GEN_CODE_DAY.Add(day);

                            if (db.SaveChanges() > 0)
                            {
                                dto.Id      = day.Id;
                                dto.Message = "SUCCESS";
                            }
                        }
                    }
                    return(dto);
                }
            }
            catch (Exception ex)
            {
                return(new GencodeDayDTO());
            }
        }
Example #2
0
        public InspectionCodeDTO Save(InspectionCodeDTO dto)
        {
            try
            {
                using (SEDESOLEntities db = new SEDESOLEntities())
                {
                    INSPECTION_CODE insCode = db.INSPECTION_CODE.FirstOrDefault(v => v.InspectionCode == dto.InspectionCode && v.Id_User == dto.Id_User);
                    if (insCode != null)
                    {
                        dto.Message = "Se ha ingresado previamente el código generado.";
                    }
                    else
                    {
                        INSPECTION_CODE insCode2 = db.INSPECTION_CODE.FirstOrDefault(v => v.Id_Year == dto.Id_Year && v.Id_Month == dto.Id_Month && v.Id_User == dto.Id_User);
                        if (insCode2 != null)
                        {
                            dto.Message = "Se ha generado previamente un código para el año y mes seleccionados.";
                        }
                        else
                        {
                            GEN_CODE_DAY genCode = db.GEN_CODE_DAY.FirstOrDefault(x => x.Id_Month == dto.Id_Month && x.Id_Year == dto.Id_Year);
                            if (genCode == null)
                            {
                                dto.Message = "No se ha habilitado la generación de códigos para el mes y año seleccionados.";
                            }
                            else
                            {
                                DateTime dateToValidate = new DateTime(Convert.ToInt32(genCode.YEAR.Description), genCode.Id_Month, genCode.Day);

                                if (DateTime.Today < dateToValidate)
                                {
                                    dto.Message = "No se ha habilitado la generación de códigos para el mes y año seleccionados.";
                                }
                                else
                                {
                                    INSPECTION_CODE insEntity = new INSPECTION_CODE
                                    {
                                        Id_Month       = dto.Id_Month,
                                        Id_Year        = dto.Id_Year,
                                        Id_User        = dto.Id_User,
                                        InspectionCode = dto.InspectionCode
                                    };

                                    db.INSPECTION_CODE.Add(insEntity);
                                    if (db.SaveChanges() > 0)
                                    {
                                        dto.Id      = insEntity.Id;
                                        dto.Message = "SUCCESS";
                                    }
                                    else
                                    {
                                        dto.Message = "Se ha producido un error al guardar el código de inspección";
                                    }
                                }
                            }
                        }
                    }
                    return(dto);
                }
            }
            catch (Exception ex)
            {
                return(new InspectionCodeDTO());
            }
        }