예제 #1
0
        public async Task <ActionResult> Debit(CreateDebitViewModel model)
        {
            var user = await _userManager.FindByIdAsync(_userManager.GetUserId(User));

            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("Index", "Expenditure"));
            }

            var account = _db.TransitAccounts.FirstOrDefault();

            if (account == null)
            {
                return(RedirectToAction("Index", "Expenditure"));
            }

            if (!model.IsEditBalance)
            {
                user.Balance -= model.Debit.Amount;
            }

            account.Balance += model.Debit.Amount;

            model.Debit.Account           = account;
            model.Debit.OperationDateTime = DateTime.Now;

            using (var transaction = await _db.Database.BeginTransactionAsync())
            {
                if (!model.IsEditBalance)
                {
                    var collection = new Collection
                    {
                        Amount      = model.Debit.Amount,
                        ProviderId  = user.Id,
                        CollectorId = office.Id
                    };

                    var operation = new CollectionOperation
                    {
                        Collection        = collection,
                        OperationDateTime = DateTime.Now,
                        OperationTypeId   = CollectionOperationType.COType.Accepted
                    };

                    _db.CollectionOperations.Add(operation);
                }

                _db.TransitAccountDebits.Add(model.Debit);
                _db.SetUserContext(user.Id);
                await _db.SaveChangesAsync();

                transaction.Commit();
            }

            return(RedirectToAction("Index"));
        }
예제 #2
0
        public IActionResult Debit(bool isEdit = false)
        {
            var model = new CreateDebitViewModel
            {
                Debit         = new TransitAccountDebit(),
                IsEditBalance = isEdit
            };

            return(PartialView(model));
        }