diff --git a/src/AccountGoWeb/Controllers/FinancialsController.cs b/src/AccountGoWeb/Controllers/FinancialsController.cs index 9588eba9..6701e6db 100644 --- a/src/AccountGoWeb/Controllers/FinancialsController.cs +++ b/src/AccountGoWeb/Controllers/FinancialsController.cs @@ -168,27 +168,38 @@ public async System.Threading.Tasks.Task BalanceSheet() public async Task IncomeStatement() { - ViewBag.PageContentHeader = "Income Statement"; + ViewBag.PageContentHeader = "Income Statement"; - using (var client = new System.Net.Http.HttpClient()) - { - var baseUri = _baseConfig!["ApiUrl"]; - client.BaseAddress = new System.Uri(baseUri!); - client.DefaultRequestHeaders.Accept.Clear(); - var response = await client.GetAsync(baseUri + "financials/incomestatement"); - if (response.IsSuccessStatusCode) + using (var client = new System.Net.Http.HttpClient()) { - var responseJson = await response.Content.ReadAsStringAsync(); - var incomeStatementModel = Newtonsoft.Json.JsonConvert.DeserializeObject>(responseJson); - return View(incomeStatementModel); + var baseUri = _baseConfig!["ApiUrl"]; + client.BaseAddress = new System.Uri(baseUri!); + client.DefaultRequestHeaders.Accept.Clear(); + + try + { + var response = await client.GetAsync(baseUri + "financials/incomestatement"); + if (response.IsSuccessStatusCode) + { + var responseJson = await response.Content.ReadAsStringAsync(); + var incomeStatementModel = Newtonsoft.Json.JsonConvert.DeserializeObject>(responseJson); + return View(incomeStatementModel); + } + else + { + ViewBag.Error = "Failed to fetch income statement data."; + } + } + catch (Exception ex) + { + ViewBag.Error = $"Error: {ex.Message}"; + } } - } - return View(); - //var Dto = _financialService.IncomeStatement(); - //return View(Dto); + return View(new List()); } + public IActionResult Banks() { ViewBag.PageContentHeader = "Cash/Banks"; diff --git a/src/AccountGoWeb/Views/Financials/IncomeStatement.cshtml b/src/AccountGoWeb/Views/Financials/IncomeStatement.cshtml index 2029defb..54cf2ac2 100644 --- a/src/AccountGoWeb/Views/Financials/IncomeStatement.cshtml +++ b/src/AccountGoWeb/Views/Financials/IncomeStatement.cshtml @@ -1,58 +1,70 @@ @model ICollection + @{ - ViewBag.Title = "IncomeStatement"; + ViewBag.Title = "Income Statement"; Layout = "~/Views/Shared/_Layout_bootstrap.cshtml"; - @* var netIncome = Model.Where(a => a.IsExpense == false).Sum(a => a.Amount) - Model.Where(a => a.IsExpense == true).Sum(a => a.Amount); *@ - var netIncome = 100; + + var netIncome = Model?.Where(a => !a.IsExpense).Sum(a => a.Amount) ?? 0 - + Model?.Where(a => a.IsExpense).Sum(a => a.Amount) ?? 0; }

Income Statement

-
-

Income Statement

-
-
- - - - - - - @foreach (var asset in Model.Where(a => a.IsExpense == false)) - { +@if (ViewBag.Error != null) +{ +
@ViewBag.Error
+} +else if (Model == null || !Model.Any()) +{ +

No data available to display.

+} +else +{ +
+

Revenues

+
Account CodeAccount NameAmount
- - - + + + - } - - - - -
@Html.ActionLink((string)string.Format("{0}", asset.AccountCode), "account", new { id = asset.AccountId })@asset.AccountName@asset.AmountAccount CodeAccount NameAmount
Total Revenues@Model.Where(a => a.IsExpense == false).Sum(a => a.Amount)
-
-
- - - - - - - @foreach (var asset in Model.Where(a => a.IsExpense == true)) - { + @foreach (var item in Model.Where(a => !a.IsExpense)) + { + + + + + + } - - - + + - } - - - - -
Account CodeAccount NameAmount
@Html.ActionLink(item.AccountCode, "Account", new { id = item.AccountId })@item.AccountName@item.Amount
@Html.ActionLink((string)string.Format("{0}", asset.AccountCode), "account", new { id = asset.AccountId })@asset.AccountName@asset.AmountTotal Revenues@Model.Where(a => !a.IsExpense).Sum(a => a.Amount)
Total Expenses@Model.Where(a => a.IsExpense == true).Sum(a => a.Amount)
-
-
- Net Income: @netIncome -
\ No newline at end of file + + +
+

Expenses

+ + + + + + + @foreach (var item in Model.Where(a => a.IsExpense)) + { + + + + + + } + + + + +
Account CodeAccount NameAmount
@Html.ActionLink(item.AccountCode, "Account", new { id = item.AccountId })@item.AccountName@item.Amount
Total Expenses@Model.Where(a => a.IsExpense).Sum(a => a.Amount)
+
+
+ Net Income: @netIncome +
+} diff --git a/src/Api/Controllers/FinancialsController.cs b/src/Api/Controllers/FinancialsController.cs index e8824062..a0e7dae9 100644 --- a/src/Api/Controllers/FinancialsController.cs +++ b/src/Api/Controllers/FinancialsController.cs @@ -335,14 +335,30 @@ public ActionResult BalanceSheet() return new ObjectResult(Dto); } - [HttpGet] - [Route("IncomeStatement")] - // TODO: this generates an error a.ContraAccounts' is invalid inside an 'Include' operation - public ActionResult IncomeStatement() + [HttpGet] + [Route("IncomeStatement")] + public IActionResult IncomeStatement() + { + try { - var Dto = _financialService.IncomeStatement(); - return new ObjectResult(Dto); + var incomeStatementData = _financialService.IncomeStatement(); + + // Ensure the data is not null + if (incomeStatementData == null) + { + incomeStatementData = new List(); + } + + return Ok(incomeStatementData); + } + catch (Exception ex) + { + return BadRequest(new { message = "Error fetching income statement", details = ex.Message }); } + } + + + #region Private Methods private IList BuildAccountGrouping(IList allAccounts, diff --git a/src/Api/Data/ApplicationIdentityDbContext.cs b/src/Api/Data/ApplicationIdentityDbContext.cs index b528e99d..ef07f1c9 100644 --- a/src/Api/Data/ApplicationIdentityDbContext.cs +++ b/src/Api/Data/ApplicationIdentityDbContext.cs @@ -14,5 +14,9 @@ protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); } + + + + } }