public async Task <ApiResponse> Handle(EditHolidayDetailCommand request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); try { var financialyear = await _dbContext.FinancialYearDetail.FirstOrDefaultAsync(x => x.IsDefault == true); if (request.HolidayType == (int)HolidayType.REPEATWEEKLYDAY) { var existrecord = await _dbContext.HolidayWeeklyDetails.Where(x => x.IsDeleted == false && x.OfficeId == request.OfficeId && x.FinancialYearId == financialyear.FinancialYearId).ToListAsync(); _dbContext.RemoveRange(existrecord); await _dbContext.SaveChangesAsync(); List <HolidayWeeklyDetails> holidayweeklylist = new List <HolidayWeeklyDetails>(); foreach (var hweeklylist in request.RepeatWeeklyDay) { HolidayWeeklyDetails list = new HolidayWeeklyDetails(); list.Day = hweeklylist.Day; list.OfficeId = (int)request.OfficeId; list.FinancialYearId = financialyear.FinancialYearId; list.ModifiedById = request.ModifiedById; list.ModifiedDate = request.ModifiedDate; list.IsDeleted = false; holidayweeklylist.Add(list); } await _dbContext.HolidayWeeklyDetails.AddRangeAsync(holidayweeklylist); await _dbContext.SaveChangesAsync(); List <HolidayDetails> holidaylist = new List <HolidayDetails>(); var hlist = await _dbContext.HolidayDetails.Where(x => x.IsDeleted == false && x.FinancialYearId == financialyear.FinancialYearId && x.OfficeId == request.OfficeId && x.HolidayType == (int)HolidayType.REPEATWEEKLYDAY).ToListAsync(); foreach (var h in hlist) { h.IsDeleted = true; h.ModifiedById = request.ModifiedById; h.ModifiedDate = request.ModifiedDate; holidaylist.Add(h); } //_uow.GetDbContext().HolidayDetails.UpdateRange(holidaylist); _dbContext.RemoveRange(holidaylist); await _dbContext.SaveChangesAsync(); List <HolidayDetails> holidaylist1 = new List <HolidayDetails>(); for (DateTime todaydate = financialyear.StartDate; todaydate <= financialyear.EndDate;) { HolidayDetails holiday = new HolidayDetails(); string day = todaydate.DayOfWeek.ToString(); foreach (var list in request.RepeatWeeklyDay) { if (list.Day == day) { holiday.HolidayName = "Weekly Off"; holiday.Date = todaydate; holiday.FinancialYearId = financialyear.FinancialYearId; holiday.OfficeId = request.OfficeId; holiday.HolidayType = request.HolidayType; holiday.CreatedById = request.CreatedById; holiday.CreatedDate = request.CreatedDate; holidaylist1.Add(holiday); } } todaydate = todaydate.AddDays(1); } await _dbContext.HolidayDetails.AddRangeAsync(holidaylist1); await _dbContext.SaveChangesAsync(); response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } else { var existrecord = await _dbContext.HolidayDetails.FirstOrDefaultAsync(x => x.HolidayId == request.HolidayId); if (existrecord != null) { existrecord.HolidayName = request.HolidayName; existrecord.Remarks = request.Remarks; existrecord.ModifiedById = request.ModifiedById; existrecord.ModifiedDate = request.ModifiedDate; existrecord.IsDeleted = false; _dbContext.HolidayDetails.Update(existrecord); await _dbContext.SaveChangesAsync(); response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } } } catch (Exception ex) { response.StatusCode = StaticResource.failStatusCode; response.Message = ex.Message; } return(response); }
public async Task <ApiResponse> Handle(AddHolidayDetailCommand request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); try { var financialyear = await _dbContext.FinancialYearDetail.FirstOrDefaultAsync(x => x.IsDefault == true); if (request.HolidayType == (int)HolidayType.REPEATWEEKLYDAY) { List <HolidayWeeklyDetails> holidayweeklylist = new List <HolidayWeeklyDetails>(); foreach (var hlist in request.RepeatWeeklyDay) { HolidayWeeklyDetails list = new HolidayWeeklyDetails(); list.Day = hlist.Day; list.OfficeId = (int)request.OfficeId; list.FinancialYearId = financialyear.FinancialYearId; list.CreatedById = request.CreatedById; list.CreatedDate = request.CreatedDate; list.IsDeleted = false; holidayweeklylist.Add(list); } await _dbContext.HolidayWeeklyDetails.AddRangeAsync(holidayweeklylist); await _dbContext.SaveChangesAsync(); List <HolidayDetails> holidaylist = new List <HolidayDetails>(); for (DateTime todaydate = financialyear.StartDate; todaydate <= financialyear.EndDate;) { HolidayDetails holiday = new HolidayDetails(); string day = todaydate.DayOfWeek.ToString(); foreach (var list in request.RepeatWeeklyDay) { if (list.Day == day) { holiday.HolidayName = "Weekly Off"; holiday.Date = todaydate; holiday.FinancialYearId = financialyear.FinancialYearId; holiday.OfficeId = request.OfficeId; holiday.HolidayType = request.HolidayType; holiday.CreatedById = request.CreatedById; holiday.CreatedDate = request.CreatedDate; holidaylist.Add(holiday); } } todaydate = todaydate.AddDays(1); } await _dbContext.HolidayDetails.AddRangeAsync(holidaylist); await _dbContext.SaveChangesAsync(); response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } else { var existrecord = await _dbContext.HolidayDetails.FirstOrDefaultAsync(x => x.Date.Date == request.Date.Date); if (existrecord == null) { request.FinancialYearId = financialyear.FinancialYearId; request.HolidayType = request.HolidayType; HolidayDetails obj = _mapper.Map <HolidayDetails>(request); obj.IsDeleted = false; await _dbContext.HolidayDetails.AddAsync(obj); await _dbContext.SaveChangesAsync(); response.StatusCode = StaticResource.successStatusCode; response.Message = "Success"; } else { response.StatusCode = StaticResource.MandateNameAlreadyExistCode; response.Message = "Holiday Details already exist for this date."; } } } catch (Exception ex) { response.StatusCode = StaticResource.failStatusCode; response.Message = ex.Message; } return(response); }
public ActionResult Create(DiasFestivoViewModel Omodelo) { var OMensaje = new Mensaje(); try { using (RELOJBIOEntities wdb = new RELOJBIOEntities()) { var OUltimoDiasFestivoCode = wdb.HolidayType.OrderByDescending(a => a.Code).FirstOrDefault(); int wCode = (OUltimoDiasFestivoCode.Code) + 1; var ODiasFestivoNuevo = new HolidayType { Code = wCode, HolidayTypeID = wCode, Name = Omodelo.Name, AuditDateIns = DateTime.Now, AuditStationIns = Environment.MachineName, AuditUserIns = User.Identity.Name }; wdb.HolidayType.Add(ODiasFestivoNuevo); wdb.SaveChanges(); var OUltimoDiasFestivoDetalle = wdb.HolidayDetails.OrderByDescending(a => a.HolidayDetailsID).FirstOrDefault(); int wCodeDetalle = (OUltimoDiasFestivoDetalle.HolidayDetailsID) + 1; var ODiasFestivoDetalle = new HolidayDetails { HolidayDetailsID = wCodeDetalle, HolidayTypeID = ODiasFestivoNuevo.HolidayTypeID, StartDate = Omodelo.StartDate, EndDate = Omodelo.StartDate, AuditDateIns = DateTime.Now, AuditStationIns = Environment.MachineName, AuditUserIns = User.Identity.Name }; wdb.HolidayDetails.Add(ODiasFestivoDetalle); wdb.SaveChanges(); OMensaje.Tipo = "Exito"; OMensaje.Msg = "Dias Festivo Creado con exito"; Session["Mensaje"] = OMensaje; return(RedirectToAction("Index", "DiasFestivo")); } } catch (DbEntityValidationException e) { var errorMessages = e.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("; ", errorMessages); var exceptionMessage = string.Concat(e.Message, " El error de validacion es: ", fullErrorMessage); OMensaje.Tipo = "Error"; OMensaje.Msg = exceptionMessage; Session["Mensaje"] = OMensaje; using (RELOJBIOEntities wdb = new RELOJBIOEntities()) { return(View(Omodelo)); } } }