public IActionResult CreateAccountTransaction(CreateAccountTransactionViewModel model) { if (ModelState.IsValid) { model.AccountGuid = Constants.DefaultAccountGuid; long cost = Convert.ToInt64(model.Cost); Transaction transaction = new Transaction { AccountGuid = model.AccountGuid, TypeCodeGuid = model.TypeGuid, StateCodeGuid = model.StateGuid, Title = model.Title, Cost = cost, AccountSide = model.AccountSide, Description = model.Description, ReceiptDate = PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate) }; if (model.AccountGuid.HasValue) { if (model.StateGuid == Codes.PassedState) { var account = context.Account .Where(x => x.AccountGuid == model.AccountGuid) .SingleOrDefault(); if (account == null) { TempData["ToasterState"] = ToasterState.Error; TempData["ToasterType"] = ToasterType.Message; TempData["ToasterMessage"] = Messages.CreateTransactionFailedAccountNotValid; return(RedirectToAction("Index")); } account.Credit = model.TypeGuid == Codes.CreditorType ? account.Credit + cost : account.Credit - cost; account.ModifiedDate = DateTime.Now; var transactionBefore = context.Transaction .Where(x => !x.IsDelete && x.ReceiptDate < PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate)) .OrderByDescending(x => x.ReceiptDate) .FirstOrDefault(); if (transactionBefore == null) { transaction.Credit = model.TypeGuid == Codes.CreditorType ? transaction.Credit + cost : transaction.Credit - cost; } else { transaction.Credit = model.TypeGuid == Codes.CreditorType ? transactionBefore.Credit + cost : transactionBefore.Credit - cost; } var transactionsAfter = context.Transaction .Where(x => !x.IsDelete && x.ReceiptDate > PersianDateExtensionMethods.ToGeorgianDateTime(model.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 { var transactionBefore = context.Transaction .Where(x => !x.IsDelete && x.ReceiptDate < PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate)) .OrderByDescending(x => x.ReceiptDate) .FirstOrDefault(); transaction.Credit = transactionBefore == null ? 0 : transactionBefore.Credit; } } context.Transaction.Add(transaction); if (Convert.ToBoolean(context.SaveChanges() > 0)) { TempData["ToasterState"] = ToasterState.Success; TempData["ToasterType"] = ToasterType.Message; TempData["ToasterMessage"] = Messages.CreateTransactionSuccessful; } else { TempData["ToasterState"] = ToasterState.Error; TempData["ToasterType"] = ToasterType.Message; TempData["ToasterMessage"] = Messages.CreateTransactionFailed; } return(RedirectToAction("Index")); } return(BadRequest()); }
public IActionResult EditCheckTransaction(EditCheckTransactionViewModel model) { if (ModelState.IsValid) { var checkTransaction = context.CheckTransaction .Where(x => x.TransactionGuid == model.TransactionGuid) .SingleOrDefault(); if (checkTransaction == null) { return(NotFound()); } var transaction = context.Transaction .Where(x => x.TransactionGuid == model.TransactionGuid) .SingleOrDefault(); var account = context.Account .Where(x => x.AccountGuid == transaction.AccountGuid) .SingleOrDefault(); 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; checkTransaction.Serial = model.Serial; transaction.Description = model.Description; checkTransaction.IssueDate = PersianDateExtensionMethods.ToGeorgianDateTime(model.IssueDate); 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()); }