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 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 })); } }