public ActionResult Details(int id = 0) { return(Error(Loc.Dic.Error_NoPermission)); if (Authorized(RoleType.SystemManager)) { Budgets_Expenses expense; using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) { expense = expensesRep.GetEntity(id, "Budget", "Projects_ParentProject", "Projects_SubProject"); } if (expense != null) { if (expense.CompanyId == CurrentUser.CompanyId) { return(View(expense)); } 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 Delete(int id = 0) { return(Error(Loc.Dic.Error_NoPermission)); if (Authorized(RoleType.SystemManager)) { Budgets_Expenses expense; using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId)) using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) { expense = expensesRep.GetEntity(id, "Budget", "Projects_ParentProject", "Projects_SubProject"); if (expense != null) { if (expense.CompanyId == CurrentUser.CompanyId) { if ( !ordersRep.GetList() .Where(x => x.CompanyId == expense.Id) //.Where(x => x.Budgets_Allocations.ExpenseId == expense.Id) .Any(o => o.StatusId >= (int)StatusType.ApprovedPendingInvoice) ) { return(View(expense)); } 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 Edit(Budgets_Allocations Budgets_Allocations) { return(Error(Loc.Dic.error_no_permission)); if (Authorized(RoleType.SystemManager)) { if (ModelState.IsValid) { Budgets_Allocations allocation; Budgets_Incomes income; Budgets_Expenses expense; using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId)) using (BudgetsIncomesRepository incomesRep = new BudgetsIncomesRepository()) using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId)) using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId)) { allocation = allocationsRep.GetEntity(Budgets_Allocations.Id); if (allocation != null) { if (allocation.CompanyId == CurrentUser.CompanyId) { income = incomesRep.GetEntity(Budgets_Allocations.IncomeId.Value); expense = expensesRep.GetEntity(Budgets_Allocations.ExpenseId.Value); if (income != null && expense != null) { if (income.BudgetId == allocation.BudgetId && expense.BudgetId == allocation.BudgetId) { decimal?totalUsed; decimal?allocatedToExpense; decimal?allocatedToIncome; totalUsed = 0; //totalUsed = ordersRep.GetList() // .Where(order => order.BudgetAllocationId == Budgets_Allocations.Id && order.StatusId >= (int)StatusType.ApprovedPendingInvoice) // .Sum(x => (decimal?)x.Price); if ((totalUsed ?? 0) > Budgets_Allocations.CompanyId)//if ((totalUsed ?? 0) > Budgets_Allocations.Amount) { return(Error(Loc.Dic.error_allocations_amount_is_used)); } allocatedToIncome = allocationsRep.GetList() .Where(x => x.IncomeId == income.Id && x.Id != Budgets_Allocations.Id) .Sum(alloc => (decimal?)alloc.CompanyId); //.Sum(alloc => (decimal?)alloc.Amount); if ((allocatedToIncome ?? 0) + Budgets_Allocations.CompanyId > income.Amount) //if ((allocatedToIncome ?? 0) + Budgets_Allocations.Amount > income.Amount) { return(Error(Loc.Dic.error_income_full_allocation)); } allocatedToExpense = allocationsRep.GetList() .Where(x => x.ExpenseId == expense.Id && x.Id != Budgets_Allocations.Id) .Sum(alloc => (decimal?)alloc.CompanyId); //.Sum(alloc => (decimal?)alloc.Amount); if ((allocatedToExpense ?? 0) + Budgets_Allocations.CompanyId > expense.Amount) //if ((allocatedToExpense ?? 0) + Budgets_Allocations.Amount > expense.Amount) { return(Error(Loc.Dic.error_expenses_full_allocation)); } allocation.IncomeId = Budgets_Allocations.IncomeId; allocation.ExpenseId = Budgets_Allocations.ExpenseId; allocation.CompanyId = Budgets_Allocations.CompanyId;//allocation.Amount = Budgets_Allocations.Amount; Budgets_Allocations update = allocationsRep.Update(allocation); if (update != null) { return(RedirectToAction("Index")); } else { return(Error(Loc.Dic.error_allocations_get_error)); } } else { return(Error(Loc.Dic.error_invalid_form)); } } else { return(Error(Loc.Dic.error_database_error)); } } else { return(Error(Loc.Dic.error_no_permission)); } } else { return(Error(Loc.Dic.error_allocations_get_error)); } } } else { return(Error(ModelState)); } } else { return(Error(Loc.Dic.error_no_permission)); } }
public ActionResult Create(Budgets_Allocations Budgets_Allocations, int id = 0) { return(Error(Loc.Dic.error_no_permission)); if (Authorized(RoleType.SystemManager)) { if (ModelState.IsValid) { Budget budget; Budgets_Incomes income; Budgets_Expenses expense; using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId)) using (BudgetsIncomesRepository incomesRep = new BudgetsIncomesRepository()) using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId)) { budget = budgetsRep.GetEntity(id); if (budget != null) { if (budget.CompanyId == CurrentUser.CompanyId) { income = incomesRep.GetEntity(Budgets_Allocations.IncomeId.Value); expense = expensesRep.GetEntity(Budgets_Allocations.ExpenseId.Value); if (income != null && expense != null) { if (income.BudgetId == budget.Id && expense.BudgetId == budget.Id) { decimal?allocatedToExpense; decimal?allocatedToIncome; allocatedToIncome = allocationsRep.GetList() .Where(x => x.IncomeId == income.Id) .Sum(allocation => (decimal?)allocation.CompanyId); //.Sum(allocation => (decimal?)allocation.Amount); if ((allocatedToIncome ?? 0) + Budgets_Allocations.CompanyId > income.Amount) //if ((allocatedToIncome ?? 0) + Budgets_Allocations.Amount > income.Amount) { return(Error(Loc.Dic.error_income_full_allocation)); } allocatedToExpense = allocationsRep.GetList() .Where(x => x.ExpenseId == expense.Id) .Sum(allocation => (decimal?)allocation.CompanyId); //.Sum(allocation => (decimal?)allocation.Amount); if ((allocatedToExpense ?? 0) + Budgets_Allocations.CompanyId > expense.Amount) //if ((allocatedToExpense ?? 0) + Budgets_Allocations.Amount > expense.Amount) { return(Error(Loc.Dic.error_expenses_full_allocation)); } Budgets_Allocations.CompanyId = CurrentUser.CompanyId; Budgets_Allocations.BudgetId = budget.Id; Budgets_Allocations.CompanyId = CurrentUser.CompanyId; if (allocationsRep.Create(Budgets_Allocations)) { return(RedirectToAction("Index")); } else { return(Error(Loc.Dic.error_database_error)); } } else { return(Error(Loc.Dic.error_invalid_form)); } } else { return(Error(Loc.Dic.error_database_error)); } } else { return(Error(Loc.Dic.error_no_permission)); } } else { return(Error(Loc.Dic.error_database_error)); } } } else { return(Error(ModelState)); } } else { return(Error(Loc.Dic.error_no_permission)); } }
public ActionResult Edit(Budgets_Expenses budgets_expenses) { return Error(Loc.Dic.Error_NoPermission); if (Authorized(RoleType.SystemManager)) { if (ModelState.IsValid) { Budgets_Expenses expenseFromDB; Budget budget; Projects_ParentProject project; Projects_SubProject subProject; using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (BudgetsRepository budgetRep = new BudgetsRepository(CurrentUser.CompanyId)) using (ParentProjectsRepository projectsRep = new ParentProjectsRepository()) using (SubProjectsRepository subProjectsRep = new SubProjectsRepository()) { expenseFromDB = expensesRep.GetEntity(budgets_expenses.Id); budget = budgetRep.GetEntity(budgets_expenses.BudgetId); project = projectsRep.GetEntity(budgets_expenses.ParentProjectId.Value); subProject = subProjectsRep.GetEntity(budgets_expenses.SubProjectId.Value); if (expenseFromDB != null) { if (budget != null && project != null && subProject != null) { if (budget.CompanyId == CurrentUser.CompanyId && project.CompanyId == CurrentUser.CompanyId && subProject.CompanyId == CurrentUser.CompanyId) { if (project.IsActive && subProject.IsActive) { if (budgets_expenses.Amount < expenseFromDB.Amount) { decimal? allocatedToExpense; using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId)) { allocatedToExpense = allocationsRep.GetList() .Where(x => x.ExpenseId == expenseFromDB.Id) .Sum(allocation => (decimal?)allocation.CompanyId); //.Sum(allocation => (decimal?)allocation.Amount); } if (allocatedToExpense.HasValue && allocatedToExpense > budgets_expenses.Amount) return Error(Loc.Dic.error_expenses_allocations_exeeds_amount); } expenseFromDB.BudgetId = budgets_expenses.BudgetId; expenseFromDB.ParentProjectId = budgets_expenses.ParentProjectId; expenseFromDB.SubProjectId = budgets_expenses.SubProjectId; expenseFromDB.Amount = budgets_expenses.Amount; expenseFromDB.CustomName = budgets_expenses.CustomName; Budgets_Expenses update = expensesRep.Update(expenseFromDB); if (update != null) return RedirectToAction("Index"); else return Error(Loc.Dic.error_expenses_create_error); } else { return Error(Loc.Dic.error_invalid_form); } } else { return Error(Loc.Dic.error_no_permission); } } else { return Error(Loc.Dic.error_database_error); } } else { return Error(Loc.Dic.error_expenses_get_error); } } } else { return Error(ModelState); } } else { return Error(Loc.Dic.error_no_permission); } }
public ActionResult Edit(int id = 0) { return Error(Loc.Dic.Error_NoPermission); if (Authorized(RoleType.SystemManager)) { Budgets_Expenses expense; using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (BudgetsRepository budgetRep = new BudgetsRepository(CurrentUser.CompanyId)) using (ParentProjectsRepository projectsRep = new ParentProjectsRepository()) using (SubProjectsRepository subProjectsRep = new SubProjectsRepository()) { expense = expensesRep.GetEntity(id); if (expense != null) { if (expense.CompanyId == CurrentUser.CompanyId) { List<SelectListItemDB> budgetsList; List<SelectListItemDB> projectsList; List<SelectListItemDB> subProjectsList; try { budgetsList = budgetRep.GetList() .Where(budget => budget.CompanyId == CurrentUser.CompanyId && budget.Year >= (DateTime.Now.Year - 1)) .Select(a => new { Id = a.Id, Name = a.Year }) .AsEnumerable() .Select(x => new SelectListItemDB() { Id = x.Id, Name = x.Name.ToString() }) .ToList(); projectsList = projectsRep.GetList() .Where(type => type.CompanyId == CurrentUser.CompanyId) .Select(x => new SelectListItemDB() { Id = x.Id, Name = x.Name }) .ToList(); subProjectsList = subProjectsRep.GetList() .Where(type => type.CompanyId == CurrentUser.CompanyId) .Select(x => new SelectListItemDB() { Id = x.Id, Name = x.Name }) .ToList(); } catch { return Error(Loc.Dic.error_database_error); } ViewBag.BudgetId = new SelectList(budgetsList, "Id", "Name", expense.BudgetId); ViewBag.ParentProjectId = new SelectList(projectsList, "Id", "Name", expense.ParentProjectId); ViewBag.SubProjectId = new SelectList(subProjectsList, "Id", "Name", expense.SubProjectId); return View(expense); } 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 Details(int id = 0) { return Error(Loc.Dic.Error_NoPermission); if (Authorized(RoleType.SystemManager)) { Budgets_Expenses expense; using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) { expense = expensesRep.GetEntity(id, "Budget", "Projects_ParentProject", "Projects_SubProject"); } if (expense != null) { if (expense.CompanyId == CurrentUser.CompanyId) { return View(expense); } 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 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 Delete(int id = 0) { return Error(Loc.Dic.Error_NoPermission); if (Authorized(RoleType.SystemManager)) { Budgets_Expenses expense; using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId)) using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) { expense = expensesRep.GetEntity(id, "Budget", "Projects_ParentProject", "Projects_SubProject"); if (expense != null) { if (expense.CompanyId == CurrentUser.CompanyId) { if ( !ordersRep.GetList() .Where(x => x.CompanyId == expense.Id) //.Where(x => x.Budgets_Allocations.ExpenseId == expense.Id) .Any(o => o.StatusId >= (int)StatusType.ApprovedPendingInvoice) ) { return View(expense); } 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 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 Edit(Budgets_Expenses budgets_expenses) { return(Error(Loc.Dic.Error_NoPermission)); if (Authorized(RoleType.SystemManager)) { if (ModelState.IsValid) { Budgets_Expenses expenseFromDB; Budget budget; Projects_ParentProject project; Projects_SubProject subProject; using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (BudgetsRepository budgetRep = new BudgetsRepository(CurrentUser.CompanyId)) using (ParentProjectsRepository projectsRep = new ParentProjectsRepository()) using (SubProjectsRepository subProjectsRep = new SubProjectsRepository()) { expenseFromDB = expensesRep.GetEntity(budgets_expenses.Id); budget = budgetRep.GetEntity(budgets_expenses.BudgetId); project = projectsRep.GetEntity(budgets_expenses.ParentProjectId.Value); subProject = subProjectsRep.GetEntity(budgets_expenses.SubProjectId.Value); if (expenseFromDB != null) { if (budget != null && project != null && subProject != null) { if (budget.CompanyId == CurrentUser.CompanyId && project.CompanyId == CurrentUser.CompanyId && subProject.CompanyId == CurrentUser.CompanyId) { if (project.IsActive && subProject.IsActive) { if (budgets_expenses.Amount < expenseFromDB.Amount) { decimal?allocatedToExpense; using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId)) { allocatedToExpense = allocationsRep.GetList() .Where(x => x.ExpenseId == expenseFromDB.Id) .Sum(allocation => (decimal?)allocation.CompanyId); //.Sum(allocation => (decimal?)allocation.Amount); } if (allocatedToExpense.HasValue && allocatedToExpense > budgets_expenses.Amount) { return(Error(Loc.Dic.error_expenses_allocations_exeeds_amount)); } } expenseFromDB.BudgetId = budgets_expenses.BudgetId; expenseFromDB.ParentProjectId = budgets_expenses.ParentProjectId; expenseFromDB.SubProjectId = budgets_expenses.SubProjectId; expenseFromDB.Amount = budgets_expenses.Amount; expenseFromDB.CustomName = budgets_expenses.CustomName; Budgets_Expenses update = expensesRep.Update(expenseFromDB); if (update != null) { return(RedirectToAction("Index")); } else { return(Error(Loc.Dic.error_expenses_create_error)); } } else { return(Error(Loc.Dic.error_invalid_form)); } } else { return(Error(Loc.Dic.error_no_permission)); } } else { return(Error(Loc.Dic.error_database_error)); } } else { return(Error(Loc.Dic.error_expenses_get_error)); } } } else { return(Error(ModelState)); } } else { return(Error(Loc.Dic.error_no_permission)); } }
public ActionResult Edit(int id = 0) { return(Error(Loc.Dic.Error_NoPermission)); if (Authorized(RoleType.SystemManager)) { Budgets_Expenses expense; using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (BudgetsRepository budgetRep = new BudgetsRepository(CurrentUser.CompanyId)) using (ParentProjectsRepository projectsRep = new ParentProjectsRepository()) using (SubProjectsRepository subProjectsRep = new SubProjectsRepository()) { expense = expensesRep.GetEntity(id); if (expense != null) { if (expense.CompanyId == CurrentUser.CompanyId) { List <SelectListItemDB> budgetsList; List <SelectListItemDB> projectsList; List <SelectListItemDB> subProjectsList; try { budgetsList = budgetRep.GetList() .Where(budget => budget.CompanyId == CurrentUser.CompanyId && budget.Year >= (DateTime.Now.Year - 1)) .Select(a => new { Id = a.Id, Name = a.Year }) .AsEnumerable() .Select(x => new SelectListItemDB() { Id = x.Id, Name = x.Name.ToString() }) .ToList(); projectsList = projectsRep.GetList() .Where(type => type.CompanyId == CurrentUser.CompanyId) .Select(x => new SelectListItemDB() { Id = x.Id, Name = x.Name }) .ToList(); subProjectsList = subProjectsRep.GetList() .Where(type => type.CompanyId == CurrentUser.CompanyId) .Select(x => new SelectListItemDB() { Id = x.Id, Name = x.Name }) .ToList(); } catch { return(Error(Loc.Dic.error_database_error)); } ViewBag.BudgetId = new SelectList(budgetsList, "Id", "Name", expense.BudgetId); ViewBag.ParentProjectId = new SelectList(projectsList, "Id", "Name", expense.ParentProjectId); ViewBag.SubProjectId = new SelectList(subProjectsList, "Id", "Name", expense.SubProjectId); return(View(expense)); } 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 Edit(Budgets_Allocations Budgets_Allocations) { return Error(Loc.Dic.error_no_permission); if (Authorized(RoleType.SystemManager)) { if (ModelState.IsValid) { Budgets_Allocations allocation; Budgets_Incomes income; Budgets_Expenses expense; using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId)) using (BudgetsIncomesRepository incomesRep = new BudgetsIncomesRepository()) using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId)) using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId)) { allocation = allocationsRep.GetEntity(Budgets_Allocations.Id); if (allocation != null) { if (allocation.CompanyId == CurrentUser.CompanyId) { income = incomesRep.GetEntity(Budgets_Allocations.IncomeId.Value); expense = expensesRep.GetEntity(Budgets_Allocations.ExpenseId.Value); if (income != null && expense != null) { if (income.BudgetId == allocation.BudgetId && expense.BudgetId == allocation.BudgetId) { decimal? totalUsed; decimal? allocatedToExpense; decimal? allocatedToIncome; totalUsed = 0; //totalUsed = ordersRep.GetList() // .Where(order => order.BudgetAllocationId == Budgets_Allocations.Id && order.StatusId >= (int)StatusType.ApprovedPendingInvoice) // .Sum(x => (decimal?)x.Price); if ((totalUsed ?? 0) > Budgets_Allocations.CompanyId)//if ((totalUsed ?? 0) > Budgets_Allocations.Amount) return Error(Loc.Dic.error_allocations_amount_is_used); allocatedToIncome = allocationsRep.GetList() .Where(x => x.IncomeId == income.Id && x.Id != Budgets_Allocations.Id) .Sum(alloc => (decimal?)alloc.CompanyId);//.Sum(alloc => (decimal?)alloc.Amount); if ((allocatedToIncome ?? 0) + Budgets_Allocations.CompanyId > income.Amount)//if ((allocatedToIncome ?? 0) + Budgets_Allocations.Amount > income.Amount) return Error(Loc.Dic.error_income_full_allocation); allocatedToExpense = allocationsRep.GetList() .Where(x => x.ExpenseId == expense.Id && x.Id != Budgets_Allocations.Id) .Sum(alloc => (decimal?)alloc.CompanyId);//.Sum(alloc => (decimal?)alloc.Amount); if ((allocatedToExpense ?? 0) + Budgets_Allocations.CompanyId > expense.Amount)//if ((allocatedToExpense ?? 0) + Budgets_Allocations.Amount > expense.Amount) return Error(Loc.Dic.error_expenses_full_allocation); allocation.IncomeId = Budgets_Allocations.IncomeId; allocation.ExpenseId = Budgets_Allocations.ExpenseId; allocation.CompanyId = Budgets_Allocations.CompanyId;//allocation.Amount = Budgets_Allocations.Amount; Budgets_Allocations update = allocationsRep.Update(allocation); if (update != null) return RedirectToAction("Index"); else return Error(Loc.Dic.error_allocations_get_error); } else { return Error(Loc.Dic.error_invalid_form); } } else { return Error(Loc.Dic.error_database_error); } } else { return Error(Loc.Dic.error_no_permission); } } else { return Error(Loc.Dic.error_allocations_get_error); } } } else { return Error(ModelState); } } else { return Error(Loc.Dic.error_no_permission); } }
public ActionResult Create(Budgets_Allocations Budgets_Allocations, int id = 0) { return Error(Loc.Dic.error_no_permission); if (Authorized(RoleType.SystemManager)) { if (ModelState.IsValid) { Budget budget; Budgets_Incomes income; Budgets_Expenses expense; using (BudgetsRepository budgetsRep = new BudgetsRepository(CurrentUser.CompanyId)) using (BudgetsIncomesRepository incomesRep = new BudgetsIncomesRepository()) using (BudgetsExpensesRepository expensesRep = new BudgetsExpensesRepository()) using (AllocationRepository allocationsRep = new AllocationRepository(CurrentUser.CompanyId)) { budget = budgetsRep.GetEntity(id); if (budget != null) { if (budget.CompanyId == CurrentUser.CompanyId) { income = incomesRep.GetEntity(Budgets_Allocations.IncomeId.Value); expense = expensesRep.GetEntity(Budgets_Allocations.ExpenseId.Value); if (income != null && expense != null) { if (income.BudgetId == budget.Id && expense.BudgetId == budget.Id) { decimal? allocatedToExpense; decimal? allocatedToIncome; allocatedToIncome = allocationsRep.GetList() .Where(x => x.IncomeId == income.Id) .Sum(allocation => (decimal?)allocation.CompanyId);//.Sum(allocation => (decimal?)allocation.Amount); if ((allocatedToIncome ?? 0) + Budgets_Allocations.CompanyId > income.Amount)//if ((allocatedToIncome ?? 0) + Budgets_Allocations.Amount > income.Amount) return Error(Loc.Dic.error_income_full_allocation); allocatedToExpense = allocationsRep.GetList() .Where(x => x.ExpenseId == expense.Id) .Sum(allocation => (decimal?)allocation.CompanyId);//.Sum(allocation => (decimal?)allocation.Amount); if ((allocatedToExpense ?? 0) + Budgets_Allocations.CompanyId > expense.Amount)//if ((allocatedToExpense ?? 0) + Budgets_Allocations.Amount > expense.Amount) return Error(Loc.Dic.error_expenses_full_allocation); Budgets_Allocations.CompanyId = CurrentUser.CompanyId; Budgets_Allocations.BudgetId = budget.Id; Budgets_Allocations.CompanyId = CurrentUser.CompanyId; if (allocationsRep.Create(Budgets_Allocations)) return RedirectToAction("Index"); else return Error(Loc.Dic.error_database_error); } else { return Error(Loc.Dic.error_invalid_form); } } else { return Error(Loc.Dic.error_database_error); } } else { return Error(Loc.Dic.error_no_permission); } } else { return Error(Loc.Dic.error_database_error); } } } else { return Error(ModelState); } } else { return Error(Loc.Dic.error_no_permission); } }