Skip to content

Commit

Permalink
Merge pull request #166 from xdorro/v2
Browse files Browse the repository at this point in the history
Merge V2.0.3
  • Loading branch information
anhnmt authored Aug 27, 2021
2 parents 5afba4f + 3a6f441 commit 1a4406c
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 188 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;
}
}
}
40 changes: 37 additions & 3 deletions Backend/Areas/Admin/Controllers/CurrenciesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,52 @@ public ActionResult PutData(Currencies c)
// GET: Admin/Currencies/Delete/5
public ActionResult Delete(int id)
{
if (currencies.Delete(id))
try
{
var currency = currencies.Get(id);
if (currency == null)
{
return Json(new
{
statusCode = 400,
data = "This currency does not exist",
message = "Error"
}, JsonRequestBehavior.AllowGet);
}

if (currency.BankAccounts.Count > 0)
{
return Json(new
{
statusCode = 400,
data = "This currency have bank account link to it, cannot delete!",
message = "Error"
}, JsonRequestBehavior.AllowGet);
}

if (currencies.Delete(currency))
{
return Json(new
{
statusCode = 200,
message = "Success"
}, JsonRequestBehavior.AllowGet);
}
}
catch (System.Exception)
{
return Json(new
{
statusCode = 200,
message = "Success"
statusCode = 400,
data = "Something error happen",
message = "Error"
}, JsonRequestBehavior.AllowGet);
}

return Json(new
{
statusCode = 400,
data = "Something error happen",
message = "Error"
}, JsonRequestBehavior.AllowGet);
}
Expand Down
17 changes: 12 additions & 5 deletions Backend/Areas/Admin/Views/Currencies/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@
if (res.statusCode === 200) {
$("#modifyModal").modal("hide");
$('#datatables').DataTable().ajax.reload();
toastr.success('Created Successfully');
notifySuccess('Success', 'Created Successfully');
} else {
validator.showErrors(res.data);
notifyError('Error', 'Created Fail');
}
}
Expand All @@ -134,9 +135,10 @@
if (res.statusCode === 200) {
$("#modifyModal").modal("hide");
$('#datatables').DataTable().ajax.reload();
toastr.success('Updated Successfully');
notifySuccess('Success', 'Updated Successfully');
} else {
validator.showErrors(res.data);
notifyError('Error', 'Updated Fail');
}
}
})
Expand All @@ -156,9 +158,14 @@
$.ajax({
type: "GET",
url: `/Admin/Currencies/Delete/${id}`,
success: function () {
$('#datatables').DataTable().ajax.reload();
toastr.success('Deleted Successfully');
success: function (res) {
if (res.statusCode == 200) {
$('#datatables').DataTable().ajax.reload();
notifySuccess('Success', 'Deleted Successfully');
} else {
notifyError('Error', res.data);
}
}
})
}
Expand Down
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 1a4406c

Please sign in to comment.