public ActionResult Create(Budgets_BasketsToAllocation budgets_permissionstoallocation)
 {
     using (BasketsToAllocationsRepository perToAllRep = new BasketsToAllocationsRepository())
     {
         perToAllRep.Create(budgets_permissionstoallocation);
         return(RedirectToAction("PermissionAllocationList", "PermissionsAllocations", new { permissionId = budgets_permissionstoallocation.BasketId, budgetId = budgets_permissionstoallocation.BudgetId }));
     }
 }
 public ActionResult Create(Budgets_BasketsToAllocation budgets_permissionstoallocation)
 {
     using (BasketsToAllocationsRepository perToAllRep = new BasketsToAllocationsRepository())
     {
         perToAllRep.Create(budgets_permissionstoallocation);
         return RedirectToAction("PermissionAllocationList", "PermissionsAllocations", new { permissionId = budgets_permissionstoallocation.BasketId, budgetId = budgets_permissionstoallocation.BudgetId });
     }
 }
        public ActionResult EditAllocations(int id = 0, int budgetId = 0)
        {
            if (!Authorized(RoleType.SystemManager))
            {
                return(Error(Loc.Dic.error_no_permission));
            }

            PermissionAllocationsModel model = new PermissionAllocationsModel();
            Budget budget;

            using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId))
                using (BudgetsPermissionsRepository permissionsRep = new BudgetsPermissionsRepository())
                    using (BasketsToAllocationsRepository permissionsAllocationsRep = new BasketsToAllocationsRepository())
                    {
                        model.Basket = permissionsRep.GetEntity(id);

                        if (model.Basket == null)
                        {
                            return(Error(Loc.Dic.error_permissions_get_error));
                        }

                        if (model.Basket.CompanyId != CurrentUser.CompanyId)
                        {
                            return(Error(Loc.Dic.error_no_permission));
                        }

                        budget = budgetsRep.GetEntity(budgetId);

                        if (budget == null)
                        {
                            return(Error(Loc.Dic.error_database_error));
                        }

                        List <PermissionAllocation> permissionsToAllocations = permissionsAllocationsRep.GetList("Budgets_Allocations", "Budgets_Allocations.Budgets_Incomes", "Budgets_Allocations.Budgets_Expenses")
                                                                               .Where(x => x.BudgetId == budget.Id && x.BasketId == model.Basket.Id)
                                                                               .AsEnumerable()
                                                                               .Select(alloc => new PermissionAllocation()
                        {
                            IsActive = true, Allocation = alloc
                        })
                                                                               .ToList();

                        model.BudgetAllocations = new BudgetAllocations()
                        {
                            Budget                = budget,
                            AllocationsList       = budget.Budgets_Allocations.OrderBy(x => x.SortingCode).ToList(),
                            PermissionAllocations = permissionsToAllocations
                        };

                        return(View(model));
                    }
        }
        public ActionResult PermissionAllocationList(int permissionId, int budgetId)
        {
            using (BasketsToAllocationsRepository perAlloRep = new BasketsToAllocationsRepository())
            {
                List <Budgets_BasketsToAllocation> perAlloList = perAlloRep.GetList("Budgets_Allocations").Where(x => x.BudgetId == budgetId).Where(x => x.BasketId == permissionId).ToList();
                ViewBag.budgetYear     = perAlloList[0].Budget.Year;
                ViewBag.PermissionName = perAlloList[0].Budgets_Baskets.Name;
                ViewBag.BasketId       = permissionId;
                ViewBag.BudgetId       = budgetId;

                return(View(perAlloList));
            }
        }
        public ActionResult Delete(int id = 0)
        {
            return(Error(Loc.Dic.error_no_permission));

            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Allocations allocation;
                using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
                    using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
                        using (BasketsToAllocationsRepository allocationsPermissionsRep = new BasketsToAllocationsRepository())
                        {
                            allocation = allocationsRep.GetEntity(id, "Budgets_Incomes", "Budgets_Expenses");

                            if (allocation != null)
                            {
                                if (allocation.CompanyId == CurrentUser.CompanyId)
                                {
                                    if (false) //if (allocation.Orders.All(x => x.StatusId < (int)StatusType.ApprovedPendingInvoice))
                                    {
                                        return(View(allocation));
                                    }
                                    else
                                    {
                                        return(Error(Loc.Dic.error_allocations_has_approved_orders));
                                    }
                                }
                                else
                                {
                                    return(Error(Loc.Dic.error_no_permission));
                                }
                            }
                            else
                            {
                                return(Error(Loc.Dic.error_income_get_error));
                            }
                        }
            }
            else
            {
                return(Error(Loc.Dic.error_no_permission));
            }
        }
        public List <Budgets_Allocations> GetUserAllocations(int userId, int budgetId, int?excludeOrder = null)
        {
            List <int> userAllocationIds = new List <int>();
            List <Budgets_Allocations> userAllocations = new List <Budgets_Allocations>();

            using (UsersToBasketsRepository usersToBasketsRep = new UsersToBasketsRepository())
                using (BasketsToAllocationsRepository basketsToAllocationRep = new BasketsToAllocationsRepository())
                {
                    List <Budgets_UsersToBaskets> permissions =
                        usersToBasketsRep
                        .GetList()
                        .Where(x => x.UserId == userId)
                        .ToList();

                    foreach (var permission in permissions)
                    {
                        userAllocationIds.AddRange(
                            basketsToAllocationRep.GetList()
                            .Where(x => x.BasketId == permission.Budgets_Baskets.Id && x.BudgetId == budgetId)
                            .Select(x => x.Budgets_Allocations.Id)
                            .ToList()
                            );
                    }

                    userAllocationIds = userAllocationIds.Distinct().ToList();
                }

            var data = this.GetAllocationsData(userAllocationIds.ToArray(), StatusType.Pending, excludeOrder);

            foreach (var allocationData in data)
            {
                foreach (var monthData in allocationData.Months)
                {
                    monthData.OriginalAllocationMonth.Amount = monthData.RemainingAmount;
                }
            }

            return(data.Select(x => x.OriginalAllocation).ToList());
        }
        public ActionResult DeleteConfirmed(int id)
        {
            return(Error(Loc.Dic.error_no_permission));

            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Allocations allocation;
                using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
                    using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
                        using (BasketsToAllocationsRepository allocationsPermissionsRep = new BasketsToAllocationsRepository())
                        {
                            allocation = allocationsRep.GetEntity(id, "Budgets_Incomes", "Budgets_Expenses");

                            if (allocation != null)
                            {
                                if (allocation.CompanyId == CurrentUser.CompanyId)
                                {
                                    if (false) //if (allocation.Orders.All(x => x.StatusId < (int)StatusType.ApprovedPendingInvoice))
                                    {
                                        bool         noErrors         = true;
                                        List <Order> allocationOrders = new List <Order>();//List<Order> allocationOrders = allocation.Orders.ToList();

                                        foreach (var item in allocationOrders)
                                        {
                                            item.StatusId             = (int)StatusType.Declined;
                                            item.LastStatusChangeDate = DateTime.Now;

                                            //item.OrderApproverNotes = YOUR_ALLOCATION_WAS_REVOKED;
                                            item.NextOrderApproverId = null;

                                            if (ordersRep.Update(item) == null)
                                            {
                                                noErrors = false;
                                            }
                                        }

                                        List <int> allocationPermission = allocation.Budgets_BasketsToAllocation.Select(x => x.Id).ToList();
                                        foreach (var itemId in allocationPermission)
                                        {
                                            if (!allocationsPermissionsRep.Delete(itemId))
                                            {
                                                noErrors = false;
                                            }
                                        }

                                        if (!allocationsRep.Delete(allocation.Id))
                                        {
                                            noErrors = false;
                                        }

                                        if (noErrors)
                                        {
                                            return(RedirectToAction("Index"));
                                        }
                                        else
                                        {
                                            return(Error(Loc.Dic.error_allocations_delete_error));
                                        }
                                    }
                                    else
                                    {
                                        return(Error(Loc.Dic.error_allocations_has_approved_orders));
                                    }
                                }
                                else
                                {
                                    return(Error(Loc.Dic.error_no_permission));
                                }
                            }
                            else
                            {
                                return(Error(Loc.Dic.error_income_get_error));
                            }
                        }
            }
            else
            {
                return(Error(Loc.Dic.error_no_permission));
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            return(Error(Loc.Dic.Error_NoPermission));

            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Incomes income;
                using (BudgetsIncomesRepository incomesRep = new BudgetsIncomesRepository())
                    using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
                        using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
                            using (BasketsToAllocationsRepository permissionAllocationsRep = new BasketsToAllocationsRepository())
                            {
                                income = incomesRep.GetEntity(id, "Budget", "Budgets_Incomes_types", "Budgets_Incomes_Institutions");

                                if (income != null)
                                {
                                    if (income.CompanyId == CurrentUser.CompanyId)
                                    {
                                        List <Budgets_Allocations>         incomeAllocations;
                                        List <Budgets_BasketsToAllocation> incomePermissions;
                                        List <Order> incomeOrders = ordersRep.GetList().Where(x => x.CompanyId == income.Id).ToList(); //List<Order> incomeOrders = ordersRep.GetList().Where(x => x.Budgets_Allocations.IncomeId == income.Id).ToList();

                                        if (!incomeOrders.Any(o => o.StatusId >= (int)StatusType.ApprovedPendingInvoice))
                                        {
                                            try
                                            {
                                                incomeAllocations = allocationsRep.GetList().Where(x => x.IncomeId == income.Id).ToList();
                                                incomePermissions = permissionAllocationsRep.GetList().Where(x => x.Budgets_Allocations.IncomeId == income.Id).ToList();

                                                foreach (var item in incomeOrders)
                                                {
                                                    ordersRep.Delete(item.Id);
                                                }

                                                foreach (var item in incomePermissions)
                                                {
                                                    permissionAllocationsRep.Delete(item.Id);
                                                }

                                                foreach (var item in incomeAllocations)
                                                {
                                                    allocationsRep.Delete(item.Id);
                                                }

                                                incomesRep.Delete(income.Id);
                                            }
                                            catch
                                            {
                                                return(Error(Loc.Dic.error_database_error));
                                            }

                                            return(RedirectToAction("Index"));
                                        }
                                        else
                                        {
                                            return(Error(Loc.Dic.error_income_delete_has_approved_orders));
                                        }
                                    }
                                    else
                                    {
                                        return(Error(Loc.Dic.error_no_permission));
                                    }
                                }
                                else
                                {
                                    return(Error(Loc.Dic.error_income_get_error));
                                }
                            }
            }
            else
            {
                return(Error(Loc.Dic.error_no_permission));
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            return Error(Loc.Dic.Error_NoPermission);
            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Expenses expense;
                using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository())
                using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
                using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
                using (BasketsToAllocationsRepository permissionAllocationsRep = new BasketsToAllocationsRepository())
                {
                    expense = expensesRep.GetEntity(id, "Budget", "Budgets_Incomes_types", "Budgets_Incomes_Institutions");

                    if (expense != null)
                    {
                        if (expense.CompanyId == CurrentUser.CompanyId)
                        {
                            List<Budgets_Allocations> expenseAllocations;
                            List<Budgets_BasketsToAllocation> expensePermissions;
                            List<Order> expenseOrders = ordersRep.GetList().Where(x => x.CompanyId == expense.Id).ToList(); //List<Order> expenseOrders = ordersRep.GetList().Where(x => x.Budgets_Allocations.ExpenseId == expense.Id).ToList();

                            if (!expenseOrders.Any(o => o.StatusId >= (int)StatusType.ApprovedPendingInvoice))
                            {
                                try
                                {
                                    expenseAllocations = allocationsRep.GetList().Where(x => x.ExpenseId == expense.Id).ToList();
                                    expensePermissions = permissionAllocationsRep.GetList().Where(x => x.Budgets_Allocations.ExpenseId == expense.Id).ToList();

                                    foreach (var item in expenseOrders)
                                    {
                                        ordersRep.Delete(item.Id);
                                    }

                                    foreach (var item in expensePermissions)
                                    {
                                        permissionAllocationsRep.Delete(item.Id);
                                    }

                                    foreach (var item in expenseAllocations)
                                    {
                                        allocationsRep.Delete(item.Id);
                                    }

                                    expensesRep.Delete(expense.Id);
                                }
                                catch
                                {
                                    return Error(Loc.Dic.error_database_error);
                                }

                                return RedirectToAction("Index");
                            }
                            else
                            {
                                return Error(Loc.Dic.error_expenses_delete_has_approved_orders);
                            }
                        }
                        else
                        {
                            return Error(Loc.Dic.error_no_permission);
                        }
                    }
                    else
                    {
                        return Error(Loc.Dic.error_expenses_get_error);
                    }
                }
            }
            else
            {
                return Error(Loc.Dic.error_no_permission);
            }
        }
        public ActionResult EditAllocations(PermissionAllocationsModel model)
        {
            if (!Authorized(RoleType.SystemManager))
                return Error(Loc.Dic.error_no_permission);

            Budgets_Baskets permissionFromDB;
            List<Budgets_Allocations> existingPermissionAllocations;
            List<Budgets_BasketsToAllocation> existingPermissionToAllocations;

            using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId))
            using (BudgetsPermissionsRepository permissionsRep = new BudgetsPermissionsRepository())
            using (BasketsToAllocationsRepository permissionsAllocationsRep = new BasketsToAllocationsRepository())
            using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
            {
                permissionFromDB = permissionsRep.GetEntity(model.Basket.Id);
                //TODO: Error gets ALL pemissions from DB
                existingPermissionAllocations = permissionsAllocationsRep.GetList().Where(x => x.BasketId == permissionFromDB.Id).Select(y => y.Budgets_Allocations).ToList();
                existingPermissionToAllocations = permissionsAllocationsRep.GetList().Where(x => x.BasketId == permissionFromDB.Id).ToList();

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

                if (permissionFromDB.CompanyId != CurrentUser.CompanyId)
                    return Error(Loc.Dic.error_no_permission);

                Budget budgetFromDB = budgetsRep.GetEntity(model.BudgetAllocations.Budget.Id);

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

                if (budgetFromDB.CompanyId != CurrentUser.CompanyId)
                    return Error(Loc.Dic.error_no_permission);

                foreach (var allocation in model.BudgetAllocations.PermissionAllocations)
                {
                    if (allocation.IsActive)
                    {
                        if (!existingPermissionAllocations.Any(x => x.Id == allocation.Allocation.BudgetsAllocationId))
                        {
                            allocation.Allocation.BudgetId = budgetFromDB.Id;
                            allocation.Allocation.BasketId = permissionFromDB.Id;
                            if(!permissionsAllocationsRep.Create(allocation.Allocation)) return Error(Loc.Dic.error_database_error);
                        }
                    }
                    else
                    {
                        if (existingPermissionAllocations.Any(x => x.Id == allocation.Allocation.BudgetsAllocationId))
                        {
                            permissionsAllocationsRep.Delete(allocation.Allocation.Id);
                        }
                    }
                }

                return RedirectToAction("BudgetBaskets", new { id = budgetFromDB.Id });
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            return Error(Loc.Dic.error_no_permission);
            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Allocations allocation;
                using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
                using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
                using (BasketsToAllocationsRepository allocationsPermissionsRep = new BasketsToAllocationsRepository())
                {
                    allocation = allocationsRep.GetEntity(id, "Budgets_Incomes", "Budgets_Expenses");

                    if (allocation != null)
                    {
                        if (allocation.CompanyId == CurrentUser.CompanyId)
                        {
                            if (false) //if (allocation.Orders.All(x => x.StatusId < (int)StatusType.ApprovedPendingInvoice))
                            {
                                bool noErrors = true;
                                List<Order> allocationOrders = new List<Order>();//List<Order> allocationOrders = allocation.Orders.ToList();

                                foreach (var item in allocationOrders)
                                {
                                    item.StatusId = (int)StatusType.Declined;
                                    item.LastStatusChangeDate = DateTime.Now;

                                    //item.OrderApproverNotes = YOUR_ALLOCATION_WAS_REVOKED;
                                    item.NextOrderApproverId = null;

                                    if (ordersRep.Update(item) == null)
                                        noErrors = false;
                                }

                                List<int> allocationPermission = allocation.Budgets_BasketsToAllocation.Select(x => x.Id).ToList();
                                foreach (var itemId in allocationPermission)
                                {
                                    if (!allocationsPermissionsRep.Delete(itemId))
                                        noErrors = false;
                                }

                                if (!allocationsRep.Delete(allocation.Id))
                                    noErrors = false;

                                if (noErrors)
                                    return RedirectToAction("Index");
                                else
                                    return Error(Loc.Dic.error_allocations_delete_error);
                            }
                            else
                            {
                                return Error(Loc.Dic.error_allocations_has_approved_orders);
                            }
                        }
                        else
                        {
                            return Error(Loc.Dic.error_no_permission);
                        }
                    }
                    else
                    {
                        return Error(Loc.Dic.error_income_get_error);
                    }
                }
            }
            else
            {
                return Error(Loc.Dic.error_no_permission);
            }
        }
        public ActionResult DeleteConfirmed(int id, int budgetId)
        {
            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Baskets permission;

                using (OrdersRepository orderssRep = new OrdersRepository(CurrentUser.CompanyId))
                using (BudgetsPermissionsRepository permissionsRep = new BudgetsPermissionsRepository())
                using (BasketsToAllocationsRepository permissionsAllocationsRep = new BasketsToAllocationsRepository())
                using (UsersToBasketsRepository usersPermissionsRep = new UsersToBasketsRepository())
                {
                    permission = permissionsRep.GetEntity(id);

                    if (permission != null)
                    {
                        if (permission.CompanyId == CurrentUser.CompanyId)
                        {
                            bool noErrors = true;
                            List<int> permissionAllocations = permission.Budgets_BasketsToAllocation.Select(x => x.Id).ToList();
                            List<int> usersPermissions = permission.Budgets_UsersToBaskets.Select(x => x.Id).ToList();

                            foreach (var itemId in permissionAllocations)
                            {
                                if (!permissionsAllocationsRep.Delete(itemId))
                                    noErrors = false;
                            }

                            foreach (var itemId in usersPermissions)
                            {
                                if (!usersPermissionsRep.Delete(itemId))
                                    noErrors = false;
                            }

                            if (!permissionsRep.Delete(permission.Id))
                                noErrors = false;

                            if (noErrors)
                                return RedirectToAction("Index", new { id = budgetId });
                            else
                                return Error(Loc.Dic.error_permissions_delete_error);
                        }
                        else
                        {
                            return Error(Loc.Dic.error_no_permission);
                        }
                    }
                    else
                    {
                        return Error(Loc.Dic.error_projects_get_error);
                    }
                }
            }
            else
            {
                return Error(Loc.Dic.error_no_permission);
            }
        }
        public ActionResult PermissionAllocationList(int permissionId, int budgetId)
        {
            using (BasketsToAllocationsRepository perAlloRep = new BasketsToAllocationsRepository())
            {
                List<Budgets_BasketsToAllocation> perAlloList = perAlloRep.GetList("Budgets_Allocations").Where(x => x.BudgetId == budgetId).Where(x => x.BasketId == permissionId).ToList();
                ViewBag.budgetYear = perAlloList[0].Budget.Year;
                ViewBag.PermissionName = perAlloList[0].Budgets_Baskets.Name;
                ViewBag.BasketId = permissionId;
                ViewBag.BudgetId = budgetId;

                return View(perAlloList);

            }
        }
        public ActionResult EditAllocations(PermissionAllocationsModel model)
        {
            if (!Authorized(RoleType.SystemManager))
            {
                return(Error(Loc.Dic.error_no_permission));
            }

            Budgets_Baskets                    permissionFromDB;
            List <Budgets_Allocations>         existingPermissionAllocations;
            List <Budgets_BasketsToAllocation> existingPermissionToAllocations;

            using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId))
                using (BudgetsPermissionsRepository permissionsRep = new BudgetsPermissionsRepository())
                    using (BasketsToAllocationsRepository permissionsAllocationsRep = new BasketsToAllocationsRepository())
                        using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
                        {
                            permissionFromDB = permissionsRep.GetEntity(model.Basket.Id);
                            //TODO: Error gets ALL pemissions from DB
                            existingPermissionAllocations   = permissionsAllocationsRep.GetList().Where(x => x.BasketId == permissionFromDB.Id).Select(y => y.Budgets_Allocations).ToList();
                            existingPermissionToAllocations = permissionsAllocationsRep.GetList().Where(x => x.BasketId == permissionFromDB.Id).ToList();

                            if (permissionFromDB == null)
                            {
                                return(Error(Loc.Dic.error_database_error));
                            }

                            if (permissionFromDB.CompanyId != CurrentUser.CompanyId)
                            {
                                return(Error(Loc.Dic.error_no_permission));
                            }

                            Budget budgetFromDB = budgetsRep.GetEntity(model.BudgetAllocations.Budget.Id);

                            if (budgetFromDB == null)
                            {
                                return(Error(Loc.Dic.error_database_error));
                            }

                            if (budgetFromDB.CompanyId != CurrentUser.CompanyId)
                            {
                                return(Error(Loc.Dic.error_no_permission));
                            }

                            foreach (var allocation in model.BudgetAllocations.PermissionAllocations)
                            {
                                if (allocation.IsActive)
                                {
                                    if (!existingPermissionAllocations.Any(x => x.Id == allocation.Allocation.BudgetsAllocationId))
                                    {
                                        allocation.Allocation.BudgetId = budgetFromDB.Id;
                                        allocation.Allocation.BasketId = permissionFromDB.Id;
                                        if (!permissionsAllocationsRep.Create(allocation.Allocation))
                                        {
                                            return(Error(Loc.Dic.error_database_error));
                                        }
                                    }
                                }
                                else
                                {
                                    if (existingPermissionAllocations.Any(x => x.Id == allocation.Allocation.BudgetsAllocationId))
                                    {
                                        permissionsAllocationsRep.Delete(allocation.Allocation.Id);
                                    }
                                }
                            }

                            return(RedirectToAction("BudgetBaskets", new { id = budgetFromDB.Id }));
                        }
        }
        public ActionResult EditAllocations(int id = 0, int budgetId = 0)
        {
            if (!Authorized(RoleType.SystemManager))
                return Error(Loc.Dic.error_no_permission);

            PermissionAllocationsModel model = new PermissionAllocationsModel();
            Budget budget;

            using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId))
            using (BudgetsPermissionsRepository permissionsRep = new BudgetsPermissionsRepository())
            using (BasketsToAllocationsRepository permissionsAllocationsRep = new BasketsToAllocationsRepository())
            {
                model.Basket = permissionsRep.GetEntity(id);

                if (model.Basket == null)
                    return Error(Loc.Dic.error_permissions_get_error);

                if (model.Basket.CompanyId != CurrentUser.CompanyId)
                    return Error(Loc.Dic.error_no_permission);

                budget = budgetsRep.GetEntity(budgetId);

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

                List<PermissionAllocation> permissionsToAllocations = permissionsAllocationsRep.GetList("Budgets_Allocations", "Budgets_Allocations.Budgets_Incomes", "Budgets_Allocations.Budgets_Expenses")
                    .Where(x => x.BudgetId == budget.Id && x.BasketId == model.Basket.Id)
                    .AsEnumerable()
                    .Select(alloc => new PermissionAllocation() { IsActive = true, Allocation = alloc })
                    .ToList();

                model.BudgetAllocations = new BudgetAllocations()
                {
                    Budget = budget,
                    AllocationsList = budget.Budgets_Allocations.OrderBy(x=>x.SortingCode).ToList(),
                    PermissionAllocations = permissionsToAllocations
                };

                return View(model);
            }
        }
        public ActionResult DeleteConfirmed(int id, int budgetId)
        {
            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Baskets permission;

                using (OrdersRepository orderssRep = new OrdersRepository(CurrentUser.CompanyId))
                    using (BudgetsPermissionsRepository permissionsRep = new BudgetsPermissionsRepository())
                        using (BasketsToAllocationsRepository permissionsAllocationsRep = new BasketsToAllocationsRepository())
                            using (UsersToBasketsRepository usersPermissionsRep = new UsersToBasketsRepository())
                            {
                                permission = permissionsRep.GetEntity(id);

                                if (permission != null)
                                {
                                    if (permission.CompanyId == CurrentUser.CompanyId)
                                    {
                                        bool       noErrors = true;
                                        List <int> permissionAllocations = permission.Budgets_BasketsToAllocation.Select(x => x.Id).ToList();
                                        List <int> usersPermissions      = permission.Budgets_UsersToBaskets.Select(x => x.Id).ToList();

                                        foreach (var itemId in permissionAllocations)
                                        {
                                            if (!permissionsAllocationsRep.Delete(itemId))
                                            {
                                                noErrors = false;
                                            }
                                        }

                                        foreach (var itemId in usersPermissions)
                                        {
                                            if (!usersPermissionsRep.Delete(itemId))
                                            {
                                                noErrors = false;
                                            }
                                        }

                                        if (!permissionsRep.Delete(permission.Id))
                                        {
                                            noErrors = false;
                                        }

                                        if (noErrors)
                                        {
                                            return(RedirectToAction("Index", new { id = budgetId }));
                                        }
                                        else
                                        {
                                            return(Error(Loc.Dic.error_permissions_delete_error));
                                        }
                                    }
                                    else
                                    {
                                        return(Error(Loc.Dic.error_no_permission));
                                    }
                                }
                                else
                                {
                                    return(Error(Loc.Dic.error_projects_get_error));
                                }
                            }
            }
            else
            {
                return(Error(Loc.Dic.error_no_permission));
            }
        }
        public ActionResult Delete(int id = 0)
        {
            return Error(Loc.Dic.error_no_permission);
            if (Authorized(RoleType.SystemManager))
            {
                Budgets_Allocations allocation;
                using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId))
                using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId))
                using (BasketsToAllocationsRepository allocationsPermissionsRep = new BasketsToAllocationsRepository())
                {
                    allocation = allocationsRep.GetEntity(id, "Budgets_Incomes", "Budgets_Expenses");

                    if (allocation != null)
                    {
                        if (allocation.CompanyId == CurrentUser.CompanyId)
                        {
                            if (false) //if (allocation.Orders.All(x => x.StatusId < (int)StatusType.ApprovedPendingInvoice))
                            {
                                return View(allocation);
                            }
                            else
                            {
                                return Error(Loc.Dic.error_allocations_has_approved_orders);
                            }
                        }
                        else
                        {
                            return Error(Loc.Dic.error_no_permission);
                        }
                    }
                    else
                    {
                        return Error(Loc.Dic.error_income_get_error);
                    }
                }
            }
            else
            {
                return Error(Loc.Dic.error_no_permission);
            }
        }