Esempio n. 1
0
        public IActionResult EditAccountTransaction(Guid transactionGuid)
        {
            if (transactionGuid == null)
            {
                return(BadRequest());
            }

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

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

            EditAccountTransactionViewModel model = new EditAccountTransactionViewModel()
            {
                TransactionGuid = transaction.TransactionGuid,
                AccountGuid     = transaction.AccountGuid,
                TypeGuid        = transaction.TypeCodeGuid,
                StateGuid       = transaction.StateCodeGuid,
                Title           = transaction.Title,
                Cost            = transaction.Cost.ToString(),
                AccountSide     = transaction.AccountSide,
                Description     = transaction.Description,
                ReceiptDate     = transaction.ReceiptDate.ToString()
            };

            CodesRepository codeRepository = new CodesRepository(context);

            ViewBag.Accounts          = new AccountsRepository(context).GetAccounts(userManager.GetUserId(User));
            ViewBag.TransactionTypes  = codeRepository.GetCodesByCodeGroupGuid(CodeGroups.TransactionType);
            ViewBag.TransactionStates = codeRepository.GetCodesByCodeGroupGuid(CodeGroups.TransactionState);

            return(View(model));
        }
Esempio n. 2
0
        public IActionResult EditAccountTransaction(EditAccountTransactionViewModel model)
        {
            if (ModelState.IsValid)
            {
                var transaction = context.Transaction
                                  .Where(x => x.TransactionGuid == model.TransactionGuid)
                                  .SingleOrDefault();

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

                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.AccountGuid.HasValue && transaction.StateCodeGuid == Codes.PassedState)
                {
                    account.Credit       = transaction.TypeCodeGuid == Codes.CreditorType ? account.Credit - transaction.Cost : account.Credit + transaction.Cost;
                    account.ModifiedDate = DateTime.Now;
                }

                model.AccountGuid = Constants.DefaultAccountGuid;
                long     cost = Convert.ToInt64(model.Cost);
                DateTime transactionOldReceiptDate = transaction.ReceiptDate;

                if (model.AccountGuid.HasValue && model.StateGuid == Codes.PassedState)
                {
                    account.Credit       = model.TypeGuid == Codes.CreditorType ? account.Credit + cost : account.Credit - cost;
                    account.ModifiedDate = DateTime.Now;
                }

                transaction.AccountGuid   = model.AccountGuid;
                transaction.TypeCodeGuid  = model.TypeGuid;
                transaction.StateCodeGuid = model.StateGuid;
                transaction.Title         = model.Title;
                transaction.Cost          = cost;
                transaction.AccountSide   = model.AccountSide;
                transaction.Description   = model.Description;
                transaction.ReceiptDate   = PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate);
                transaction.ModifiedDate  = DateTime.Now;

                DateTime dateToUpdateFrom = transactionOldReceiptDate < transaction.ReceiptDate ? transactionOldReceiptDate : transaction.ReceiptDate;

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

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

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

                transactionsAfter.Add(transaction);
                transactionsAfter = transactionsAfter.OrderBy(x => x.ReceiptDate)
                                    .ToList();

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

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

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

                return(RedirectToAction("Index"));
            }

            return(BadRequest());
        }