Skip to content

Commit

Permalink
Merge pull request #56 from medhatelmasry/debug/8-income-report
Browse files Browse the repository at this point in the history
Debug/GithubIssue#8-income-report
  • Loading branch information
medhatelmasry authored Dec 3, 2024
2 parents 402b263 + 270435e commit 546b813
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 68 deletions.
41 changes: 26 additions & 15 deletions src/AccountGoWeb/Controllers/FinancialsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,38 @@ public async System.Threading.Tasks.Task<IActionResult> BalanceSheet()

public async Task<IActionResult> 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<System.Collections.Generic.List<Models.IncomeStatement>>(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<System.Collections.Generic.List<Models.IncomeStatement>>(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<Models.IncomeStatement>());
}


public IActionResult Banks()
{
ViewBag.PageContentHeader = "Cash/Banks";
Expand Down
106 changes: 59 additions & 47 deletions src/AccountGoWeb/Views/Financials/IncomeStatement.cshtml
Original file line number Diff line number Diff line change
@@ -1,58 +1,70 @@
@model ICollection<AccountGoWeb.Models.IncomeStatement>

@{
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;
}

<h2>Income Statement</h2>

<div style="text-align: center">
<h3>Income Statement</h3>
</div>
<div>
<table class="table">
<tr style="font-weight: bold;">
<td>Account Code</td>
<td>Account Name</td>
<td style="text-align: right">Amount</td>
</tr>
@foreach (var asset in Model.Where(a => a.IsExpense == false))
{
@if (ViewBag.Error != null)
{
<div class="alert alert-danger">@ViewBag.Error</div>
}
else if (Model == null || !Model.Any())
{
<p>No data available to display.</p>
}
else
{
<div>
<h3>Revenues</h3>
<table class="table">
<tr>
<td>@Html.ActionLink((string)string.Format("{0}", asset.AccountCode), "account", new { id = asset.AccountId })</td>
<td>@asset.AccountName</td>
<td style="text-align: right">@asset.Amount</td>
<th>Account Code</th>
<th>Account Name</th>
<th style="text-align: right;">Amount</th>
</tr>
}
<tr style="font-weight: bold;">
<td colspan="2">Total Revenues</td>
<td style="text-align: right">@Model.Where(a => a.IsExpense == false).Sum(a => a.Amount)</td>
</tr>
</table>
</div>
<div>
<table class="table">
<tr style="font-weight: bold;">
<td>Account Code</td>
<td>Account Name</td>
<td style="text-align: right">Amount</td>
</tr>
@foreach (var asset in Model.Where(a => a.IsExpense == true))
{
@foreach (var item in Model.Where(a => !a.IsExpense))
{
<tr>
<td>@Html.ActionLink(item.AccountCode, "Account", new { id = item.AccountId })</td>
<td>@item.AccountName</td>
<td style="text-align: right;">@item.Amount</td>
</tr>
}
<tr>
<td>@Html.ActionLink((string)string.Format("{0}", asset.AccountCode), "account", new { id = asset.AccountId })</td>
<td>@asset.AccountName</td>
<td style="text-align: right">@asset.Amount</td>
<td colspan="2" style="text-align: right; font-weight: bold;">Total Revenues</td>
<td style="text-align: right;">@Model.Where(a => !a.IsExpense).Sum(a => a.Amount)</td>
</tr>
}
<tr style="font-weight: bold;">
<td colspan="2">Total Expenses</td>
<td style="text-align: right">@Model.Where(a => a.IsExpense == true).Sum(a => a.Amount)</td>
</tr>
</table>
</div>
<div style="text-align: right; font-weight: bold; font-size: large">
Net Income: @netIncome
</div>
</table>
</div>
<div>
<h3>Expenses</h3>
<table class="table">
<tr>
<th>Account Code</th>
<th>Account Name</th>
<th style="text-align: right;">Amount</th>
</tr>
@foreach (var item in Model.Where(a => a.IsExpense))
{
<tr>
<td>@Html.ActionLink(item.AccountCode, "Account", new { id = item.AccountId })</td>
<td>@item.AccountName</td>
<td style="text-align: right;">@item.Amount</td>
</tr>
}
<tr>
<td colspan="2" style="text-align: right; font-weight: bold;">Total Expenses</td>
<td style="text-align: right;">@Model.Where(a => a.IsExpense).Sum(a => a.Amount)</td>
</tr>
</table>
</div>
<div style="text-align: right; font-weight: bold; font-size: large;">
Net Income: @netIncome
</div>
}
28 changes: 22 additions & 6 deletions src/Api/Controllers/FinancialsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IncomeStatement>();
}

return Ok(incomeStatementData);
}
catch (Exception ex)
{
return BadRequest(new { message = "Error fetching income statement", details = ex.Message });
}
}




#region Private Methods
private IList<Dto.Financial.Account> BuildAccountGrouping(IList<Core.Domain.Financials.Account> allAccounts,
Expand Down
4 changes: 4 additions & 0 deletions src/Api/Data/ApplicationIdentityDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}




}
}

0 comments on commit 546b813

Please sign in to comment.