public bool CreateAutomaticReport(AutomaticReportDTO report)
        {
            try
            {
                return(new AutomaticReportDAL().CreateAutomaticReport(report));
            }

            catch (DbEntityValidationException exDb)
            {
                Utility.WriteErrorLog(exDb);
                foreach (var item in exDb.EntityValidationErrors)
                {
                    Utility.WriteLog(item.ValidationErrors.FirstOrDefault().ErrorMessage);
                }
                return(false);
            }
            catch (Exception ex)
            {
                Utility.WriteErrorLog(ex);

                return(false);
            }
        }
        public bool CreateAutomaticReport(AutomaticReportDTO report)
        {
            var res = false;

            try
            {
                var entity = new AutomaticReports()
                {
                    CreatedBy   = report.CurrentUserId,
                    DateCreated = DateTime.Now,
                    IsArabic    = report.IsArabic,
                    IsDeleted   = false,
                    IsEmail     = report.IsEmail,
                    IsEnglish   = report.IsEnglish,
                    IsSMS       = report.IsSMS,
                    ReportName  = report.ReportName,
                    TemplateId  = report.TemplateId
                };

                if (report.Diminsion != null)
                {
                    entity.AutomaticReportDiminsion.Add(new AutomaticReportDiminsion
                    {
                        DiminsionId    = 1,
                        RelativeTypeId = report.Diminsion.RelativeTypeId,
                        ExactValue     = report.Diminsion.ExactValue,
                        IsDeleted      = false,
                        IsStaticValue  = report.Diminsion.IsStaticValue,
                        RelativeValue  = report.Diminsion.RelativeValue,
                    });
                }

                if (report.Schedule != null)
                {
                    entity.ReportSchedule = new ReportSchedule
                    {
                        ScheduleTypeId = report.Schedule.ScheduleTypeId,
                        ScheduleValues = PrepareScheduleValues(report.Schedule),
                        SendHour       = report.Schedule.SendHour
                    };
                }

                if (report.UserGroupIds != null && report.UserGroupIds.Any())
                {
                    foreach (var item in report.UserGroupIds)
                    {
                        entity.AutomaticReportGroups.Add(new AutomaticReportGroups
                        {
                            GroupId   = item,
                            IsDeleted = false,
                        });
                    }
                }

                _operationalDataContext.AutomaticReports.Add(entity);

                res = _operationalDataContext.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(res);
        }
        public bool EditAutomaticReport(AutomaticReportDTO report)
        {
            var res = false;

            try
            {
                var entity = _operationalDataContext.AutomaticReports.FirstOrDefault(x => x.ReportId == report.ReportId);

                if (entity != null)
                {
                    entity.IsArabic         = report.IsArabic;
                    entity.IsDeleted        = report.IsDeleted;
                    entity.IsEmail          = report.IsEmail;
                    entity.IsEnglish        = report.IsEnglish;
                    entity.IsSMS            = report.IsSMS;
                    entity.ReportName       = report.ReportName;
                    entity.TemplateId       = report.TemplateId;
                    entity.LastModifiedBy   = report.CurrentUserId;
                    entity.LastModifiedDate = DateTime.Now;

                    if (entity.AutomaticReportDiminsion.Any())
                    {
                        foreach (var item in entity.AutomaticReportDiminsion)
                        {
                            item.IsDeleted = true;
                        }
                    }

                    if (report.Diminsion != null)
                    {
                        entity.AutomaticReportDiminsion.Add(new AutomaticReportDiminsion
                        {
                            DiminsionId    = 1,
                            RelativeTypeId = report.Diminsion.RelativeTypeId,
                            ExactValue     = report.Diminsion.ExactValue,
                            IsDeleted      = false,
                            IsStaticValue  = report.Diminsion.IsStaticValue,
                            RelativeValue  = report.Diminsion.RelativeValue,
                        });
                    }

                    if (report.Schedule != null)
                    {
                        entity.ReportSchedule.ScheduleTypeId = report.Schedule.ScheduleTypeId;
                        entity.ReportSchedule.ScheduleValues = PrepareScheduleValues(report.Schedule);
                        entity.ReportSchedule.SendHour       = report.Schedule.SendHour;
                    }

                    if (report.UserGroupIds != null && report.UserGroupIds.Any())
                    {
                        var toBeDeleted = entity.AutomaticReportGroups.Where(x => !report.UserGroupIds.Any(y => x.GroupId == y));
                        var toBeAdded   = report.UserGroupIds.Where(x => !entity.AutomaticReportGroups.Any(y => y.GroupId == x));

                        if (toBeDeleted.Any())
                        {
                            foreach (var item in toBeDeleted)
                            {
                                item.IsDeleted = true;
                            }
                        }

                        if (toBeAdded.Any())
                        {
                            foreach (var item in toBeAdded)
                            {
                                entity.AutomaticReportGroups.Add(new AutomaticReportGroups
                                {
                                    GroupId   = item,
                                    IsDeleted = false,
                                });
                            }
                        }
                    }
                    res = _operationalDataContext.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(res);
        }