Skip to content

Commit

Permalink
Merge pull request #165
Browse files Browse the repository at this point in the history
tuananh
  • Loading branch information
anhnmt authored Aug 27, 2021
2 parents 532271b + b5694ed commit 3a6f441
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 176 deletions.
83 changes: 60 additions & 23 deletions Backend/Areas/Admin/Controllers/ChequesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public ActionResult PostData(Cheques chequeInformation)
var fromBankAccount = _context.BankAccounts.FirstOrDefault(x =>
x.BankAccountId == chequeInformation.FromBankAccountId);

if (fromBankAccount.Status != (int) BankAccountStatus.Actived)
if (fromBankAccount != null && fromBankAccount.Status != (int) BankAccountStatus.Actived)
{
errors.Add("FromBankAccountId", "This bank account is not actived");
return Json(new
Expand All @@ -127,7 +127,7 @@ public ActionResult PostData(Cheques chequeInformation)

var chequeBook =
_context.ChequeBooks.FirstOrDefault(x => x.ChequeBookId == chequeInformation.ChequeBookId);
if (chequeBook.Status != (int) ChequeBookStatus.Opened)
if (chequeBook != null && chequeBook.Status != (int) ChequeBookStatus.Opened)
{
return Json(new
{
Expand All @@ -145,7 +145,7 @@ public ActionResult PostData(Cheques chequeInformation)
chequeInformation.Code = code;
chequeInformation.Status = (int) ChequeStatus.Actived;

if (fromBankAccount.Balance < chequeInformation.Amount)
if (fromBankAccount != null && fromBankAccount.Balance < chequeInformation.Amount)
{
errors.Add("Amount", "Your balance is not enough");
return Json(new
Expand All @@ -171,18 +171,23 @@ public ActionResult PostData(Cheques chequeInformation)
_context.Cheques.Add(chequeInformation);
_context.SaveChanges();

fromBankAccount.Balance -= chequeInformation.Amount;
// bankAccounts.Update(fromBankAccount);
// _context.Entry(fromBankAccount).State = EntityState.Modified;
_context.SaveChanges();
List<Notifications> newNotifications = null;
if (fromBankAccount != null)
{
fromBankAccount.Balance -= chequeInformation.Amount;
// bankAccounts.Update(fromBankAccount);
// _context.Entry(fromBankAccount).State = EntityState.Modified;
_context.SaveChanges();

var message = "Your account balance -" + chequeInformation.Amount +
", available balance: " + fromBankAccount.Balance;

newNotifications = CreateNotification(chequeInformation, message);
}

transaction.Commit();

return Json(new
{
message = "Success",
statusCode = 200,
}, JsonRequestBehavior.AllowGet);
ChatHub.Instance().SendNotifications(newNotifications);
}
catch (Exception ex)
{
Expand All @@ -193,8 +198,13 @@ public ActionResult PostData(Cheques chequeInformation)
message = "error",
statuscode = 404
}, JsonRequestBehavior.AllowGet);
throw;
}

return Json(new
{
message = "Success",
statusCode = 200,
}, JsonRequestBehavior.AllowGet);
}
}
}
Expand Down Expand Up @@ -435,7 +445,7 @@ public ActionResult ChequeExec(ChequesExecViewModel chequeExec)
var newNotifications = CreateNotifications(newTransaction);

transaction.Commit();

ChatHub.Instance().SendNotifications(newNotifications);

return Json(new
Expand Down Expand Up @@ -573,40 +583,67 @@ public ActionResult DeleteData(int chequeId)
var fromBankAccount =
_context.BankAccounts.FirstOrDefault(x => x.BankAccountId == cheque.FromBankAccountId);

List<Notifications> newNotifications = null;

if (fromBankAccount != null)
{
fromBankAccount.Balance += cheque.Amount;
_context.SaveChanges();

// cheques.Delete(cheque);
// bankAccounts.Edit(fromBankAccount);
_context.Cheques.Remove(cheque);
_context.SaveChanges();

var message = "Your account balance +" + cheque.Amount +
", available balance: " + cheque.FromBankAccount.Balance;

newNotifications = CreateNotification(cheque, message);
}

_context.Cheques.Remove(cheque);
_context.SaveChanges();
transaction.Commit();

return Json(new
{
message = "Success",
data = "Delete Successfully",
statusCode = 200,
}, JsonRequestBehavior.AllowGet);
ChatHub.Instance().SendNotifications(newNotifications);
}
catch (Exception ex)
{
transaction.Rollback();

return Json(new
{
data = ex,
message = "error",
statuscode = 404
}, JsonRequestBehavior.AllowGet);
throw;
}

return Json(new
{
message = "Success",
data = "Delete Successfully",
statusCode = 200,
}, JsonRequestBehavior.AllowGet);
}
}
}

private List<Notifications> CreateNotification(Cheques cheque, string message)
{
var lstNotification = new List<Notifications>()
{
new Notifications
{
AccountId = cheque.FromBankAccount.AccountId,
Content = message,
Status = (int) NotificationStatus.Unread,
PkType = (int) NotificationType.Cheque,
PkId = cheque.ChequeBookId,
}
};

_context.Set<Notifications>().AddRange(lstNotification);
_context.SaveChanges();
return lstNotification;
}
}
}
57 changes: 34 additions & 23 deletions Backend/Controllers/BankAccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public ActionResult Index()
[HttpPost]
public ActionResult GetBalance(int bankId)
{
var data = bankAccounts.Get().Where(x => x.BankAccountId == bankId).Select(x => new BalanceViewModels
{
Balance = x.Balance,
BankId = x.BankAccountId,
Currency = x.Currency.Name
});
var data = bankAccounts.Get()
.Where(x => x.BankAccountId == bankId)
.Select(x => new BalanceViewModels
{
Balance = x.Balance,
BankId = x.BankAccountId,
Currency = x.Currency.Name
});

return Json(new
{
Expand All @@ -45,11 +47,14 @@ public ActionResult GetBalance(int bankId)

public ActionResult GetInfoBankAccount(string name)
{
var data = bankAccounts.Get().Where(x => x.Name == name).Select(x => new GetInfoBankAccountViewModels
{
Name = x.Account.Name,
Id = x.BankAccountId
});
var data = bankAccounts
.Get()
.Where(x => x.Name == name)
.Select(x => new GetInfoBankAccountViewModels
{
Name = x.Account.Name,
Id = x.BankAccountId
});

if (data.FirstOrDefault() == null)
{
Expand All @@ -72,17 +77,20 @@ public ActionResult GetInfoBankAccount(string name)
public ActionResult GetData(int Account)
{
ViewBag.Accounts = "active";
var data = bankAccounts.Get().Where(a => a.AccountId == Account).Select(x => new BankAccountsViewModels
{
AccountId = x.AccountId,
BankAccountId = x.BankAccountId,
CurrencyId = x.CurrencyId,
CurrencyName = x.Currency.Name,
Name = x.Name,
Balance = x.Balance,
Status = x.Status,
StatusName = ((BankAccountStatus) x.Status).ToString()
});
var data = bankAccounts
.Get()
.Where(a => a.AccountId == Account)
.Select(x => new BankAccountsViewModels
{
AccountId = x.AccountId,
BankAccountId = x.BankAccountId,
CurrencyId = x.CurrencyId,
CurrencyName = x.Currency.Name,
Name = x.Name,
Balance = x.Balance,
Status = x.Status,
StatusName = ((BankAccountStatus) x.Status).ToString()
});

return Json(new
{
Expand All @@ -95,7 +103,10 @@ public ActionResult GetData(int Account)
// GET: Admin/BankAccounts/Details/5
public ActionResult GetStatus()
{
var data = Enum.GetValues(typeof(BankAccountStatus)).Cast<BankAccountStatus>().Select(v => v.ToString())
var data = Enum
.GetValues(typeof(BankAccountStatus))
.Cast<BankAccountStatus>()
.Select(v => v.ToString())
.ToArray();

return Json(data, JsonRequestBehavior.AllowGet);
Expand Down
Loading

0 comments on commit 3a6f441

Please sign in to comment.