Beispiel #1
0
        public IActionResult ChangeState(Guid transactionGuid)
        {
            if (transactionGuid == null)
            {
                return(BadRequest());
            }

            var transaction = context.Transaction
                              .Where(x => x.TransactionGuid == transactionGuid)
                              .SingleOrDefault();

            if (transaction == null)
            {
                return(NotFound());
            }

            ChangeTransactionStateViewModel model = new ChangeTransactionStateViewModel()
            {
                Guid      = transaction.TransactionGuid,
                StateGuid = transaction.StateCodeGuid
            };

            ViewBag.TransactionStates = new CodesRepository(context).GetCodesByCodeGroupGuid(CodeGroups.TransactionState);

            return(PartialView(model));
        }
Beispiel #2
0
        public IActionResult ChangeState(ChangeTransactionStateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var transaction = context.Transaction
                                  .Where(x => x.TransactionGuid == model.Guid)
                                  .SingleOrDefault();

                if (transaction == null)
                {
                    NotFound();
                }

                if (transaction.AccountGuid.HasValue)
                {
                    var account = context.Account
                                  .Where(x => x.AccountGuid == transaction.AccountGuid)
                                  .SingleOrDefault();

                    if (account == null)
                    {
                        TempData["ToasterState"]   = ToasterState.Error;
                        TempData["ToasterType"]    = ToasterType.Message;
                        TempData["ToasterMessage"] = Messages.CreateTransactionFailedAccountNotValid;

                        return(RedirectToAction("Index"));
                    }

                    if (transaction.StateCodeGuid != model.StateGuid)
                    {
                        if ((transaction.StateCodeGuid == Codes.WaitingState || transaction.StateCodeGuid == Codes.NotPassedState) && model.StateGuid == Codes.PassedState)
                        {
                            account.Credit       = transaction.TypeCodeGuid == Codes.CreditorType ? account.Credit + transaction.Cost : account.Credit - transaction.Cost;
                            account.ModifiedDate = DateTime.Now;

                            var transactionBefore = context.Transaction
                                                    .Where(x => !x.IsDelete && x.ReceiptDate < transaction.ReceiptDate)
                                                    .OrderByDescending(x => x.ReceiptDate)
                                                    .FirstOrDefault();

                            long transactionBeforeCredit = transactionBefore == null ? 0 : transactionBefore.Credit;
                            transaction.Credit = transaction.TypeCodeGuid == Codes.CreditorType ? transactionBeforeCredit + transaction.Cost : transactionBeforeCredit - transaction.Cost;

                            var transactionsAfter = context.Transaction
                                                    .Where(x => !x.IsDelete && x.ReceiptDate > transaction.ReceiptDate)
                                                    .OrderBy(x => x.ReceiptDate)
                                                    .ToList();

                            if (transactionsAfter.Count > 0)
                            {
                                transactionsAfter[0].Credit = transactionsAfter[0].StateCodeGuid == Codes.PassedState ?
                                                              (transactionsAfter[0].TypeCodeGuid == Codes.CreditorType ? transaction.Credit + transactionsAfter[0].Cost : transaction.Credit - transactionsAfter[0].Cost) :
                                                              (transactionsAfter[0].Credit = transaction.Credit);

                                for (int i = 1; i < transactionsAfter.Count; i++)
                                {
                                    transactionsAfter[i].Credit = transactionsAfter[i].StateCodeGuid == Codes.PassedState ?
                                                                  (transactionsAfter[i].TypeCodeGuid == Codes.CreditorType ? transactionsAfter[i - 1].Credit + transactionsAfter[i].Cost : transactionsAfter[i - 1].Credit - transactionsAfter[i].Cost) :
                                                                  (transactionsAfter[i].Credit = transactionsAfter[i - 1].Credit);
                                }
                            }
                        }
                        else if (transaction.StateCodeGuid == Codes.PassedState)
                        {
                            account.Credit       = transaction.TypeCodeGuid == Codes.CreditorType ? account.Credit - transaction.Cost : account.Credit + transaction.Cost;
                            account.ModifiedDate = DateTime.Now;

                            var transactionBefore = context.Transaction
                                                    .Where(x => !x.IsDelete && x.ReceiptDate < transaction.ReceiptDate)
                                                    .OrderByDescending(x => x.ReceiptDate)
                                                    .FirstOrDefault();

                            long transactionBeforeCredit = transactionBefore == null ? 0 : transactionBefore.Credit;
                            transaction.Credit = transactionBeforeCredit;

                            var transactionsAfter = context.Transaction
                                                    .Where(x => !x.IsDelete && x.ReceiptDate > transaction.ReceiptDate)
                                                    .OrderBy(x => x.ReceiptDate)
                                                    .ToList();

                            if (transactionsAfter.Count > 0)
                            {
                                transactionsAfter[0].Credit = transactionsAfter[0].StateCodeGuid == Codes.PassedState ?
                                                              (transactionsAfter[0].TypeCodeGuid == Codes.CreditorType ? transaction.Credit + transactionsAfter[0].Cost : transaction.Credit - transactionsAfter[0].Cost) :
                                                              (transactionsAfter[0].Credit = transaction.Credit);

                                for (int i = 1; i < transactionsAfter.Count; i++)
                                {
                                    transactionsAfter[i].Credit = transactionsAfter[i].StateCodeGuid == Codes.PassedState ?
                                                                  (transactionsAfter[i].TypeCodeGuid == Codes.CreditorType ? transactionsAfter[i - 1].Credit + transactionsAfter[i].Cost : transactionsAfter[i - 1].Credit - transactionsAfter[i].Cost) :
                                                                  (transactionsAfter[i].Credit = transactionsAfter[i - 1].Credit);
                                }
                            }
                        }

                        transaction.StateCodeGuid = model.StateGuid;
                    }
                }
                else
                {
                    transaction.StateCodeGuid = model.StateGuid;
                }

                transaction.ModifiedDate = DateTime.Now;

                if (Convert.ToBoolean(context.SaveChanges() > 0))
                {
                    TempData["ToasterState"]   = ToasterState.Success;
                    TempData["ToasterType"]    = ToasterType.Message;
                    TempData["ToasterMessage"] = Messages.ChangeTransactionStateSuccessful;
                }
                else
                {
                    TempData["ToasterState"]   = ToasterState.Error;
                    TempData["ToasterType"]    = ToasterType.Message;
                    TempData["ToasterMessage"] = Messages.ChangeTransactionStateFailed;
                }

                return(RedirectToAction("Index"));
            }

            return(BadRequest());
        }