Пример #1
0
        private static IEnumerable <T> GetDocuments <T>(ConnectContext context, ConnectUser connectUser, DateTime?from, DateTime?to) where T : Document
        {
            var result = new List <T>();

            var documentCount = context.Set <T>().Count();

            if (documentCount > 0)
            {
                var applicationUserManager = GetApplicationUserManager();

                List <T> documents = null;
                var      userRoles = applicationUserManager.GetRoles(connectUser.Id);

                if (userRoles.Any(role => string.Equals(ConnectRoles.Admin, role)))
                {
                    documents = (from document in context.Set <T>().SqlQuery(FastQueryHelper.GetSqlQueryFor <T>(true))
                                 where document.Deleted == null
                                 select document).ToList();
                }
                else if (userRoles.Any(role => string.Equals(ConnectRoles.TachographCentre, role)))
                {
                    documents = (from document in context.Set <T>().SqlQuery(FastQueryHelper.GetSqlQueryFor <T>(true))
                                 where document.Deleted == null && document.UserId == connectUser.Id
                                 select document).ToList();
                }
                else if (userRoles.Any(role => string.Equals(ConnectRoles.TachographCentre, role)) && connectUser.CustomerContact != null)
                {
                    documents = (from linkedVehicle in context.LinkedVehicles.Include(x => x.CustomerContact).Where(v => v.CustomerContact.Id == connectUser.CustomerContact.Id).DefaultIfEmpty()
                                 from document in context.Set <T>().Where(d => d.RegistrationNumber == linkedVehicle.VehicleRegistrationNumber).DefaultIfEmpty()
                                 select document).ToList();
                }

                if (from != null && to != null && documents != null)
                {
                    foreach (var document in documents.Where(d => d != null))
                    {
                        var inspectionDate = document.InspectionDate.GetValueOrDefault();
                        if (inspectionDate != default(DateTime))
                        {
                            var nextDueDate = inspectionDate.AddYears(2);
                            if (nextDueDate >= from.GetValueOrDefault() && nextDueDate <= to.GetValueOrDefault())
                            {
                                result.Add(document);
                            }
                        }
                    }
                }

                var users = context.Users.ToList().Where(u => documents.Any(d => d.UserId == u.Id)).ToList();

                documents.ForEach(document =>
                {
                    var user = users.FirstOrDefault(u => u.Id == document.UserId);
                    if (user != null)
                    {
                        document.CompanyName = user.CompanyKey;
                    }
                });
            }

            return(result);
        }
Пример #2
0
 public static IEnumerable <T> GetDocuments <T>(this TachographContext context) where T : Document
 {
     return(from document in context.Set <T>().SqlQuery(FastQueryHelper.GetSqlQueryFor <T>(false))
            where document.Deleted == null
            select document);
 }