public ActionResult CreateExpenditure() { var model = new CreateExpenditureModel() { DeskGroups = from d in _db.DeskGroups.Where(g => g.IsActive).OrderBy(g => g.Name) select new SelectListItem { Value = d.DeskGroupId.ToString(), Text = d.Name }, Types = from t in _db.ExpenditureTypes.Where(t => t.IsActive).OrderBy(t => t.Description) select new SelectListItem { Value = t.ExpenditureTypeId.ToString(), Text = t.Description }, Objects = from o in _db.ExpenditureObjects.Where(o => o.IsActive).OrderBy(o => o.Description) select new SelectListItem { Value = o.ExpenditureObjectId.ToString(), Text = o.Description } }; return(PartialView(model)); }
public async Task <ActionResult> CreateExpenditure(CreateExpenditureModel model) { var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone(); nfi.NumberGroupSeparator = " "; var officeRole = _db.Roles.SingleOrDefault(r => r.Name.Contains("Офис")); var office = _db.Users.FirstOrDefault(u => u.Roles.Any(r => r.RoleId == officeRole.Id)); if (office == null) { return(RedirectToAction("IssuedExpenditures")); } var otherSum = decimal.Parse(_db.SettingsValues.FirstOrDefault(sv => sv.Key == "OtherSum").Value .Replace(" ", string.Empty), CultureInfo.InvariantCulture); var remainder = model.Amount; if (otherSum > 0) { _db.SettingsValues.FirstOrDefault(sv => sv.Key == "OtherSum").Value = otherSum >= model.Amount ? (otherSum - model.Amount).ToString("#,0", nfi) : (0).ToString("#,0", nfi); } office.Balance -= model.Amount; var expenditure = new Expenditure { Name = model.Name, Amount = model.Amount, DeskGroupId = model.SelectedDeskGroupId, TypeId = model.SelectedTypeId, ObjectId = model.SelectedObjectId, PaymentTypeId = PaymentTypes.Cash }; var operation = new ExpenditureOperation { Expenditure = expenditure, OperationDateTime = DateTime.Now, OperationTypeId = ExpenditureOperation.EOType.New }; var user = await _userManager.FindByIdAsync(_userManager.GetUserId(User)); using (var transaction = await _db.Database.BeginTransactionAsync()) { _db.ExpenditureOperations.Add(operation); _db.SetUserContext(user.Id); await _db.SaveChangesAsync(); transaction.Commit(); } return(RedirectToAction("IssuedExpenditures")); }