Пример #1
0
        private bool?CompanyCanAddUsers()
        {
            int companyUserCount = 0;
            int companyUserLimit = 0;

            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
                using (PendingUsersRepository pendingUsersRep = new PendingUsersRepository())
                    using (CompaniesRepository companiesRep = new CompaniesRepository())
                    {
                        try
                        {
                            companyUserCount =
                                usersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId && x.IsActive).Count() +
                                pendingUsersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId).Count();

                            companyUserLimit = companiesRep.GetEntity(CurrentUser.CompanyId).UsersLimit;
                        }
                        catch
                        {
                            return(null);
                        }
                    }

            return(companyUserCount < companyUserLimit);
        }
Пример #2
0
        public ActionResult Index(int page = FIRST_PAGE, string sortby = NO_SORT_BY, string order = DEFAULT_ORDER)
        {
            if (!Authorized(RoleType.UsersManager))
            {
                return(Error(Loc.Dic.error_no_permission));
            }

            AllUsersModel      model = new AllUsersModel();
            IEnumerable <User> activeUsersQuery;

            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
                using (PendingUsersRepository pendingUsersRep = new PendingUsersRepository())
                    using (CompaniesRepository companiesRep = new CompaniesRepository())
                    {
                        activeUsersQuery     = usersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId && x.IsActive).ToList();
                        activeUsersQuery     = Pagination(activeUsersQuery, page, sortby, order).ToList();
                        model.NonActiveUsers = usersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId && !x.IsActive).ToList();
                        Company company = companiesRep.GetEntity(CurrentUser.CompanyId);

                        if (model.NonActiveUsers == null)
                        {
                            return(Error(Loc.Dic.error_users_get_error));
                        }
                        if (activeUsersQuery == null)
                        {
                            return(Error(Loc.Dic.error_users_get_error));
                        }
                        if (company == null)
                        {
                            return(Error(Loc.Dic.error_database_error));
                        }

                        model.ActiveUsers        = activeUsersQuery.ToList();
                        model.ActiveUsersCount   = activeUsersQuery.Count();
                        model.CanceledUsersCount = model.NonActiveUsers.Count();
                        model.UsersLimit         = companiesRep.GetEntity(CurrentUser.CompanyId).UsersLimit;

                        return(View(model));
                    }
        }
Пример #3
0
        public ActionResult OrdersToExport(List<int> selectedOrder = null)
        {
            if (!Authorized(RoleType.SystemManager)) return Error(Loc.Dic.error_no_permission);
            if (selectedOrder == null || selectedOrder.Count == 0) return Error(Loc.Dic.error_no_selected_orders);

            StringBuilder builder = new StringBuilder();

            List<Order> ordersToExport = new List<Order>();
            Company userCompany;

            using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
            using (CompaniesRepository companiesRep = new CompaniesRepository())
            {
                ordersToExport = ordersRep.GetList("Orders_Statuses", "Supplier", "User")
                    .Where(x => selectedOrder.Contains(x.Id))
                    .ToList();

                userCompany = companiesRep.GetEntity(CurrentUser.CompanyId);

                if (ordersToExport == null) return Error(Loc.Dic.error_database_error);
                if (userCompany == null) return Error(Loc.Dic.error_database_error);

                if (String.IsNullOrEmpty(userCompany.ExternalCoinCode) || String.IsNullOrEmpty(userCompany.ExternalExpenseCode))
                    return Error(Loc.Dic.error_insufficient_company_info_for_export);

                int numberOfOrders = 0;
                builder.AppendLine(numberOfOrders.ToString().PadRight(180));

                foreach (var order in ordersToExport)
                {
                    decimal orderPrice;

                    if (order.Price.HasValue)
                    {
                        if (order.Price.Value > 999999999) return Error(String.Format("({0}: {1}) {2}", Loc.Dic.Order, order.OrderNumber, Loc.Dic.error_order_price_too_high));

                        orderPrice = order.Price.Value;
                    }
                    else
                    {
                        orderPrice = 0;
                    }

                    if (String.IsNullOrEmpty(order.Supplier.ExternalId))
                        return Error(String.Format("({0}: {1}) {2}", Loc.Dic.Supplier, order.Supplier.Name, Loc.Dic.error_insufficient_supplier_info_for_export));

                    if (String.IsNullOrEmpty(order.InvoiceNumber) || order.InvoiceDate == null)
                        return Error(String.Format("({0}: {1}) {2}", Loc.Dic.Order, order.OrderNumber, Loc.Dic.error_insufficient_order_info_for_export));

                    List<Orders_OrderToAllocation> orderAllocations = order.Orders_OrderToAllocation.ToList();
                    List<Budgets_Allocations> distinctOrderAllocations = orderAllocations.Select(x => x.Budgets_Allocations).Distinct().ToList();

                    if (!orderAllocations.Any()) return Error(String.Format("({0}: {1}) {2}", Loc.Dic.Order, order.OrderNumber, Loc.Dic.error_order_has_no_allocations));

                    foreach (var allocation in distinctOrderAllocations)
                    {
                        if (String.IsNullOrEmpty(allocation.ExternalId)) return Error(String.Format("({0}: {1}) {2}", Loc.Dic.BudgetAllocation, allocation.DisplayName, Loc.Dic.error_insufficient_allocation_info_for_export));

                        decimal allocationSum = orderAllocations.Where(x => x.AllocationId == allocation.Id).Sum(a => a.Amount);

                        builder.AppendLine(
                            String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}",
                            String.Empty.PadLeft(3),
                            order.InvoiceNumber.Substring(Math.Max(0, order.InvoiceNumber.Length - 5)).PadLeft(5),
                            order.InvoiceDate.Value.ToString("ddMMyy"),
                            String.Empty.PadLeft(5),
                            order.ValueDate.Value.ToString("ddMMyy"),
                            userCompany.ExternalCoinCode.PadLeft(3),
                            String.Empty.PadLeft(22),
                            allocation.ExternalId.ToString().PadLeft(8),
                            String.Empty.PadLeft(8),
                            String.Empty.PadLeft(8), //order.Supplier.ExternalId.ToString().PadLeft(8),
                            String.Empty.PadLeft(8),
                            allocationSum.ToString("0.00").PadLeft(12),
                            String.Empty.PadLeft(12),
                            String.Empty.PadLeft(12),
                            String.Empty.PadLeft(12),
                            String.Empty.PadLeft(12),
                            String.Empty.PadLeft(12),
                            String.Empty.PadLeft(12),
                            String.Empty.PadLeft(12)
                            )
                        );
                    }

                    builder.AppendLine(
                        String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}",
                        String.Empty.PadLeft(3),
                        order.InvoiceNumber.PadLeft(5),
                        order.InvoiceDate.Value.ToString("ddMMyy"),
                        String.Empty.PadLeft(5),
                        order.ValueDate.Value.ToString("ddMMyy"),
                        userCompany.ExternalCoinCode.PadLeft(3),
                        String.Empty.PadLeft(22),
                        String.Empty.PadLeft(8),
                        String.Empty.PadLeft(8),
                        order.Supplier.ExternalId.ToString().PadLeft(8),
                        String.Empty.PadLeft(8),
                        String.Empty.PadLeft(12),
                        String.Empty.PadLeft(12),
                        orderPrice.ToString("0.00").PadLeft(12),
                        String.Empty.PadLeft(12),
                        String.Empty.PadLeft(12),
                        String.Empty.PadLeft(12),
                        String.Empty.PadLeft(12),
                        String.Empty.PadLeft(12)
                        )
                    );

                    order.StatusId = (int)StatusType.InvoiceExportedToFile;
                    if (ordersRep.Update(order) == null) return Error(Loc.Dic.error_database_error);

                    int? historyActionId = null;
                    historyActionId = (int)HistoryActions.ExportedToFile;
                    Orders_History orderHistory = new Orders_History();
                    using (OrdersHistoryRepository ordersHistoryRep = new OrdersHistoryRepository(CurrentUser.CompanyId, CurrentUser.UserId, order.Id))
                        if (historyActionId.HasValue) ordersHistoryRep.Create(orderHistory, historyActionId.Value);
                }

                //FileStream fileStream = new FileStream(//SystemFile.("", FileMode.Open());
                byte[] fileBytes = Encoding.UTF8.GetBytes(builder.ToString());
                string fileName = "MOVEIN.DAT";
                //Stream stream = new MemoryStream(fileBytes);

                SendNotifications.OrdersExported(CurrentUser, Url, ordersToExport.Count, fileBytes);

                Response.AppendHeader("Refresh", "1");
                Response.AppendHeader("Location", Url.Action("OrdersToExport", "Orders", null, "http"));

                return File(fileBytes, "text/plain", fileName);
            }
        }
Пример #4
0
        public ActionResult Create(User user, string[] roleNames)
        {
            if (!ModelState.IsValid)
            {
                List <string>           allRoleNames       = GetRoleNames();
                List <SelectListItemDB> ApprovalRoutesList = new List <SelectListItemDB>()
                {
                    new SelectListItemDB()
                    {
                        Id = -1, Name = Loc.Dic.NoApprovalRoute
                    }
                };
                SelectList languagesList;

                using (ApprovalRoutesRepository routesRep = new ApprovalRoutesRepository(CurrentUser.CompanyId))
                    using (LanguagesRepository languagesRep = new LanguagesRepository())
                    {
                        ApprovalRoutesList.AddRange(
                            routesRep.GetList()
                            .Select(x => new SelectListItemDB()
                        {
                            Id = x.Id, Name = x.Name
                        })
                            );

                        languagesList = new SelectList(languagesRep.GetList().ToList(), "Id", "Name");
                    }

                ViewBag.RolesList     = allRoleNames;
                ViewBag.RoutesList    = new SelectList(ApprovalRoutesList, "Id", "Name");
                ViewBag.LanguagesList = languagesList;

                return(View(user));
            }

            if (user.DefaultApprovalRouteId == -1)
            {
                user.DefaultApprovalRouteId = null;
            }

            if (!Authorized(RoleType.SystemManager))
            {
                return(Error(Loc.Dic.error_no_permission));
            }

            int companyUserCount = 0;
            int companyUserLimit = 0;

            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
                using (ApprovalRoutesRepository routesRep = new ApprovalRoutesRepository(CurrentUser.CompanyId))
                    using (PendingUsersRepository pendingUsersRep = new PendingUsersRepository())
                        using (CompaniesRepository companiesRep = new CompaniesRepository())
                        {
                            if (user.DefaultApprovalRouteId.HasValue)
                            {
                                var route = routesRep.GetEntity(user.DefaultApprovalRouteId.Value);
                                if (route == null)
                                {
                                    return(Error(Loc.Dic.error_invalid_form));
                                }
                            }

                            try
                            {
                                companyUserCount =
                                    usersRep.GetList().Where(x => x.IsActive).Count() +
                                    pendingUsersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId).Count();

                                companyUserLimit = companiesRep.GetEntity(CurrentUser.CompanyId).UsersLimit;
                            }
                            catch
                            {
                                return(Error(Loc.Dic.error_database_error));
                            }

                            bool userExists        = usersRep.GetList().Any(x => x.CompanyId == CurrentUser.CompanyId && x.Email == user.Email);
                            bool pendingUserExists = pendingUsersRep.GetList().Any(x => x.CompanyId == CurrentUser.CompanyId && x.Email == user.Email);

                            if (userExists || pendingUserExists)
                            {
                                return(Error(Loc.Dic.error_users_exist_error));
                            }
                        }

            if (companyUserCount >= companyUserLimit)
            {
                return(Error(Loc.Dic.error_users_limit_reached));
            }

            user.CompanyId    = CurrentUser.CompanyId;
            user.CreationTime = DateTime.Now;

            RoleType        combinedRoles  = RoleType.None;
            List <RoleType> forbiddenRoles = GetForbiddenRoles();

            if (roleNames == null || roleNames.Count() == 0)
            {
                return(Error(Loc.Dic.error_invalid_form));
            }

            foreach (string roleName in roleNames)
            {
                RoleType role;
                if (!Enum.TryParse(roleName, out role) || forbiddenRoles.Contains(role))
                {
                    return(Error(Loc.Dic.error_invalid_form));
                }
                combinedRoles = Roles.CombineRoles(combinedRoles, role);
            }

            user.Roles = (int)combinedRoles;
            user.DefaultApprovalRouteId = user.DefaultApprovalRouteId.HasValue && user.DefaultApprovalRouteId.Value == -1 ? null : user.DefaultApprovalRouteId;

            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
            {
                if (!usersRep.Create(user))
                {
                    return(Error(Loc.Dic.error_users_create_error));
                }
            }

            return(RedirectToAction("Index"));
        }
Пример #5
0
        private bool? CompanyCanAddUsers()
        {
            int companyUserCount = 0;
            int companyUserLimit = 0;
            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
            using (PendingUsersRepository pendingUsersRep = new PendingUsersRepository())
            using (CompaniesRepository companiesRep = new CompaniesRepository())
            {
                try
                {
                    companyUserCount =
                        usersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId && x.IsActive).Count() +
                        pendingUsersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId).Count();

                    companyUserLimit = companiesRep.GetEntity(CurrentUser.CompanyId).UsersLimit;
                }
                catch
                {
                    return null;
                }
            }

            return companyUserCount < companyUserLimit;
        }
Пример #6
0
        public ActionResult Index(int page = FIRST_PAGE, string sortby = NO_SORT_BY, string order = DEFAULT_ORDER)
        {
            if (!Authorized(RoleType.UsersManager)) return Error(Loc.Dic.error_no_permission);

            AllUsersModel model = new AllUsersModel();
            IEnumerable<User> activeUsersQuery;
            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
            using (PendingUsersRepository pendingUsersRep = new PendingUsersRepository())
            using (CompaniesRepository companiesRep = new CompaniesRepository())
            {
                activeUsersQuery = usersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId && x.IsActive).ToList();
                activeUsersQuery = Pagination(activeUsersQuery, page, sortby, order).ToList();
                model.NonActiveUsers = usersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId && !x.IsActive).ToList();
                Company company = companiesRep.GetEntity(CurrentUser.CompanyId);

                if (model.NonActiveUsers == null) return Error(Loc.Dic.error_users_get_error);
                if (activeUsersQuery == null) return Error(Loc.Dic.error_users_get_error);
                if (company == null) return Error(Loc.Dic.error_database_error);

                model.ActiveUsers = activeUsersQuery.ToList();
                model.ActiveUsersCount = activeUsersQuery.Count();
                model.CanceledUsersCount = model.NonActiveUsers.Count();
                model.UsersLimit = companiesRep.GetEntity(CurrentUser.CompanyId).UsersLimit;

                return View(model);
            }
        }
Пример #7
0
        public ActionResult Create(User user, string[] roleNames)
        {
            if (!ModelState.IsValid)
            {
                List<string> allRoleNames = GetRoleNames();
                List<SelectListItemDB> ApprovalRoutesList = new List<SelectListItemDB>() { new SelectListItemDB() { Id = -1, Name = Loc.Dic.NoApprovalRoute } };
                SelectList languagesList;

                using (ApprovalRoutesRepository routesRep = new ApprovalRoutesRepository(CurrentUser.CompanyId))
                using (LanguagesRepository languagesRep = new LanguagesRepository())
                {
                    ApprovalRoutesList.AddRange(
                            routesRep.GetList()
                            .Select(x => new SelectListItemDB() { Id = x.Id, Name = x.Name })
                            );

                    languagesList = new SelectList(languagesRep.GetList().ToList(), "Id", "Name");
                }

                ViewBag.RolesList = allRoleNames;
                ViewBag.RoutesList = new SelectList(ApprovalRoutesList, "Id", "Name");
                ViewBag.LanguagesList = languagesList;

                return View(user);
            }

            if (user.DefaultApprovalRouteId == -1) user.DefaultApprovalRouteId = null;

            if (!Authorized(RoleType.SystemManager)) return Error(Loc.Dic.error_no_permission);

            int companyUserCount = 0;
            int companyUserLimit = 0;

            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
            using (ApprovalRoutesRepository routesRep = new ApprovalRoutesRepository(CurrentUser.CompanyId))
            using (PendingUsersRepository pendingUsersRep = new PendingUsersRepository())
            using (CompaniesRepository companiesRep = new CompaniesRepository())
            {
                if (user.DefaultApprovalRouteId.HasValue)
                {
                    var route = routesRep.GetEntity(user.DefaultApprovalRouteId.Value);
                    if (route == null) return Error(Loc.Dic.error_invalid_form);
                }

                try
                {
                    companyUserCount =
                        usersRep.GetList().Where(x => x.IsActive).Count() +
                        pendingUsersRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId).Count();

                    companyUserLimit = companiesRep.GetEntity(CurrentUser.CompanyId).UsersLimit;
                }
                catch
                {
                    return Error(Loc.Dic.error_database_error);
                }

                bool userExists = usersRep.GetList().Any(x => x.CompanyId == CurrentUser.CompanyId && x.Email == user.Email);
                bool pendingUserExists = pendingUsersRep.GetList().Any(x => x.CompanyId == CurrentUser.CompanyId && x.Email == user.Email);

                if (userExists || pendingUserExists)
                    return Error(Loc.Dic.error_users_exist_error);
            }

            if (companyUserCount >= companyUserLimit) return Error(Loc.Dic.error_users_limit_reached);

            user.CompanyId = CurrentUser.CompanyId;
            user.CreationTime = DateTime.Now;

            RoleType combinedRoles = RoleType.None;
            List<RoleType> forbiddenRoles = GetForbiddenRoles();

            if (roleNames == null || roleNames.Count() == 0) return Error(Loc.Dic.error_invalid_form);

            foreach (string roleName in roleNames)
            {
                RoleType role;
                if (!Enum.TryParse(roleName, out role) || forbiddenRoles.Contains(role)) return Error(Loc.Dic.error_invalid_form);
                combinedRoles = Roles.CombineRoles(combinedRoles, role);
            }

            user.Roles = (int)combinedRoles;
            user.DefaultApprovalRouteId = user.DefaultApprovalRouteId.HasValue && user.DefaultApprovalRouteId.Value == -1 ? null : user.DefaultApprovalRouteId;

            using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId))
            {
                if (!usersRep.Create(user)) return Error(Loc.Dic.error_users_create_error);
            }

            return RedirectToAction("Index");
        }