Inläsning och analys av innehav fungerar
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using StockWebHistory.Models;
|
||||||
using StockWebHistory.Services;
|
using StockWebHistory.Services;
|
||||||
|
|
||||||
namespace StockWebHistory.Controllers
|
namespace StockWebHistory.Controllers
|
||||||
@ -22,6 +23,35 @@ namespace StockWebHistory.Controllers
|
|||||||
{
|
{
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult ShareEquity()
|
||||||
|
{
|
||||||
|
return View(new ShareEquityModel());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult ShareEquity(ShareEquityModel model)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
return View(model);
|
||||||
|
|
||||||
|
// Exempel: skicka vidare till en resultatsida
|
||||||
|
return RedirectToAction(nameof(EquityFindings), new { date = model.SelectedDate });
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult EquityFindings(DateTime date)
|
||||||
|
{
|
||||||
|
(List<EquityTemp> EquityList, decimal TotalValue) stockList = _processingService.LoadStockListAsync(date).Result;
|
||||||
|
|
||||||
|
ViewBag.TotalSum = stockList.TotalValue;
|
||||||
|
|
||||||
|
var vm = new ShareEquityModel
|
||||||
|
{
|
||||||
|
SelectedDate = date,
|
||||||
|
StockEquities = stockList.EquityList
|
||||||
|
};
|
||||||
|
return View(vm);
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
|
|||||||
10
StockWebHistory/Models/EquityTemp.cs
Normal file
10
StockWebHistory/Models/EquityTemp.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace StockWebHistory.Models;
|
||||||
|
|
||||||
|
public class EquityTemp
|
||||||
|
{
|
||||||
|
public string StockCode { get; set; } = string.Empty;
|
||||||
|
public DateTime Bought { get; set; }
|
||||||
|
public decimal BoughtPrice { get; set; }
|
||||||
|
public int Quantity { get; set; }
|
||||||
|
public decimal TotalValue => BoughtPrice * Quantity;
|
||||||
|
}
|
||||||
9
StockWebHistory/Models/ShareEquityModel.cs
Normal file
9
StockWebHistory/Models/ShareEquityModel.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace StockWebHistory.Models;
|
||||||
|
|
||||||
|
public class ShareEquityModel
|
||||||
|
{
|
||||||
|
|
||||||
|
// Du kan även lägga till fler fält om du vill skicka med mer data i formuläret
|
||||||
|
public DateTime? SelectedDate { get; set; }
|
||||||
|
public List<EquityTemp> StockEquities { get; set; } = new List<EquityTemp>();
|
||||||
|
}
|
||||||
@ -12,14 +12,19 @@ public class FileProcessingService : IFileProcessingService
|
|||||||
private readonly StockWebDbContext _db;
|
private readonly StockWebDbContext _db;
|
||||||
private readonly ILogger<FileProcessingService> _logger;
|
private readonly ILogger<FileProcessingService> _logger;
|
||||||
private readonly ITransactionNotesServices _notesServices;
|
private readonly ITransactionNotesServices _notesServices;
|
||||||
|
private readonly IStockEquityServices _equityServices;
|
||||||
|
List<string> stockList = new();
|
||||||
|
|
||||||
|
|
||||||
public FileProcessingService(StockWebDbContext db,
|
public FileProcessingService(StockWebDbContext db,
|
||||||
ILogger<FileProcessingService> logger,
|
ILogger<FileProcessingService> logger,
|
||||||
ITransactionNotesServices notesServices)
|
ITransactionNotesServices notesServices,
|
||||||
|
IStockEquityServices equityServices)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_notesServices = notesServices;
|
_notesServices = notesServices;
|
||||||
|
_equityServices = equityServices;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> ProcessFileAsync(Stream stream, string fileExtension)
|
public async Task<int> ProcessFileAsync(Stream stream, string fileExtension)
|
||||||
@ -142,4 +147,71 @@ public class FileProcessingService : IFileProcessingService
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<(List<EquityTemp> EquityList, decimal TotalValue)> LoadStockListAsync(DateTime equityDate)
|
||||||
|
{
|
||||||
|
stockList = await _notesServices.GetAllExistingStockCodes();
|
||||||
|
|
||||||
|
|
||||||
|
var stocks = await _equityServices.GetAllAsync(equityDate);
|
||||||
|
// var stocks = await _equityServices.GetAllAsync();
|
||||||
|
|
||||||
|
if (stocks == null || stocks.Count == 0)
|
||||||
|
{
|
||||||
|
await _equityServices.GenerateStockVisionAsync();
|
||||||
|
stocks = await _equityServices.GetAllAsync(equityDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
// lwStocks.Items.Clear();
|
||||||
|
var dTotal = stocks.Sum(s => s.BoughtPrice * s.Quantity);
|
||||||
|
// lblTotalValue.Text = dTotal.ToString("C");
|
||||||
|
|
||||||
|
var equityTempList = new List<EquityTemp>();
|
||||||
|
|
||||||
|
EquityTemp equityTemp = new EquityTemp
|
||||||
|
{
|
||||||
|
StockCode = "Total",
|
||||||
|
Bought = DateTime.MinValue,
|
||||||
|
BoughtPrice = 0,
|
||||||
|
Quantity = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var t in stocks.OrderBy(s => s.StockCode + s.Bought).ToList())
|
||||||
|
{
|
||||||
|
if (t.StockCode != equityTemp.StockCode)
|
||||||
|
{
|
||||||
|
stockList.Remove(t.StockCode);
|
||||||
|
|
||||||
|
if (equityTemp.StockCode != "Total")
|
||||||
|
{
|
||||||
|
EquityTemp equityTempSave = new EquityTemp
|
||||||
|
{
|
||||||
|
StockCode = equityTemp.StockCode,
|
||||||
|
Bought = equityTemp.Bought,
|
||||||
|
BoughtPrice = equityTemp.BoughtPrice,
|
||||||
|
Quantity = equityTemp.Quantity
|
||||||
|
};
|
||||||
|
equityTempList.Add(equityTempSave);
|
||||||
|
}
|
||||||
|
equityTemp.StockCode = t.StockCode;
|
||||||
|
equityTemp.Bought = t.Bought;
|
||||||
|
equityTemp.Quantity = t.Quantity;
|
||||||
|
equityTemp.BoughtPrice = t.BoughtPrice;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (t.Bought < equityTemp.Bought)
|
||||||
|
equityTemp.Bought = t.Bought;
|
||||||
|
if (t.BoughtPrice < equityTemp.BoughtPrice || equityTemp.BoughtPrice == 0)
|
||||||
|
equityTemp.BoughtPrice = t.BoughtPrice;
|
||||||
|
equityTemp.Quantity += t.Quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
stockList.Remove(equityTemp.StockCode);
|
||||||
|
|
||||||
|
equityTempList.Add(equityTemp);
|
||||||
|
return (equityTempList, dTotal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
namespace StockWebHistory.Services
|
using StockWebHistory.Models;
|
||||||
|
|
||||||
|
namespace StockWebHistory.Services
|
||||||
{
|
{
|
||||||
public interface IFileProcessingService
|
public interface IFileProcessingService
|
||||||
{
|
{
|
||||||
Task<int> ProcessFileAsync(Stream stream, string fileExtension);
|
Task<int> ProcessFileAsync(Stream stream, string fileExtension);
|
||||||
|
Task<(List<EquityTemp> EquityList, decimal TotalValue)> LoadStockListAsync(DateTime equityDate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ namespace StockWebHistory.Services
|
|||||||
{
|
{
|
||||||
public interface IStockEquityServices
|
public interface IStockEquityServices
|
||||||
{
|
{
|
||||||
Task<List<StockEquity>> GenerateStockVision();
|
Task<List<StockEquity>> GenerateStockVisionAsync();
|
||||||
Task<List<StockEquity>> GetAllAsync();
|
Task<List<StockEquity>> GetAllAsync();
|
||||||
Task<List<StockEquity>> GetLatestSoldInfoAsync();
|
Task<List<StockEquity>> GetLatestSoldInfoAsync();
|
||||||
Task<List<StockEquity>> GetAllAsync(DateTime equityDate);
|
Task<List<StockEquity>> GetAllAsync(DateTime equityDate);
|
||||||
|
|||||||
@ -84,7 +84,7 @@ public class StockEquityServices : IStockEquityServices
|
|||||||
/// utgående från alla befintliga notor skapa en lista med aktieinnehav.
|
/// utgående från alla befintliga notor skapa en lista med aktieinnehav.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<List<StockEquity>> GenerateStockVision()
|
public async Task<List<StockEquity>> GenerateStockVisionAsync()
|
||||||
{
|
{
|
||||||
var oldStock = "OldStock";
|
var oldStock = "OldStock";
|
||||||
var nyAktie = false;
|
var nyAktie = false;
|
||||||
|
|||||||
20
StockWebHistory/StaticAssetsExtensions.cs
Normal file
20
StockWebHistory/StaticAssetsExtensions.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// C#
|
||||||
|
public static class StaticAssetsExtensions
|
||||||
|
{
|
||||||
|
public static IApplicationBuilder MapStaticAssets(this WebApplication app)
|
||||||
|
{
|
||||||
|
if (app == null) throw new ArgumentNullException(nameof(app));
|
||||||
|
var env = app.Environment ?? throw new InvalidOperationException("IWebHostEnvironment not available.");
|
||||||
|
// Ensure static files middleware is present
|
||||||
|
app.UseStaticFiles(); // registers default file provider; safe even if WebRootPath is null
|
||||||
|
// Additional config: guard against null file provider or missing configuration
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEndpointRouteBuilder WithStaticAssets(this IEndpointRouteBuilder endpoints)
|
||||||
|
{
|
||||||
|
if (endpoints == null) throw new ArgumentNullException(nameof(endpoints));
|
||||||
|
// safe usage of environment/config should be via DI, with null checks
|
||||||
|
return endpoints;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="Services\HandleCsvNotes.cs" />
|
<Compile Remove="Services\HandleCsvNotes.cs" />
|
||||||
|
<Compile Remove="StaticAssetsExtensions.cs" />
|
||||||
|
<Compile Remove="Views\DataLoad\ShareEquity.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
39
StockWebHistory/Views/DataLoad/EquityFindings.cshtml
Normal file
39
StockWebHistory/Views/DataLoad/EquityFindings.cshtml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
@{
|
||||||
|
ViewData["Title"] = "Equity Findings";
|
||||||
|
}
|
||||||
|
@model StockWebHistory.Models.ShareEquityModel
|
||||||
|
|
||||||
|
|
||||||
|
<h2>Du valde datumet:</h2>
|
||||||
|
<p>@Model.SelectedDate?.ToString("yyyy-MM-dd")</p>
|
||||||
|
<br />
|
||||||
|
<hr />
|
||||||
|
<h3>Total Stock Value @ViewBag.TotalSum.ToString("F2") SEK</h3>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
<table class="table small">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>StockCode</th>
|
||||||
|
<th>Bought</th>
|
||||||
|
<th>BoughtPrice</th>
|
||||||
|
<th>Quantity</th>
|
||||||
|
<th>TotalValue</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@{
|
||||||
|
foreach (var note in Model.StockEquities)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td>@note.StockCode</td>
|
||||||
|
<td>@note.Bought.ToString("yyyy-MM-dd")</td>
|
||||||
|
<td>@note.BoughtPrice.ToString("F2")</td>
|
||||||
|
<td>@note.Quantity.ToString("F")</td>
|
||||||
|
<td>@(note.TotalValue == null ? "" : ((decimal)note.TotalValue).ToString("F2", System.Globalization.CultureInfo.InvariantCulture))</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
12
StockWebHistory/Views/DataLoad/EquityFindings.cshtml.cs
Normal file
12
StockWebHistory/Views/DataLoad/EquityFindings.cshtml.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
|
namespace StockWebHistory.Views.DataLoad
|
||||||
|
{
|
||||||
|
public class EquityFindingsModel : PageModel
|
||||||
|
{
|
||||||
|
public void OnGet()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
@page
|
@{
|
||||||
|
ViewData["Title"] = "File Upload";
|
||||||
|
}
|
||||||
|
|
||||||
@model FileUploadViewModel
|
@model FileUploadViewModel
|
||||||
|
|
||||||
|
|||||||
@ -5,5 +5,5 @@
|
|||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<h1 class="display-4">Welcome to Data Loading</h1>
|
<h1 class="display-4">Welcome to Data Loading</h1>
|
||||||
<a class="btn btn-primary" asp-area="" asp-controller="DataLoad" asp-action="Files">File Types</a>
|
<a class="btn btn-primary" asp-area="" asp-controller="DataLoad" asp-action="Files">File Types</a>
|
||||||
<a class="btn btn-secondary" asp-area="" asp-controller="DataLoad" asp-action="History">History</a>
|
<a class="btn btn-secondary" asp-area="" asp-controller="DataLoad" asp-action="ShareEquity">Share Equity</a>
|
||||||
</div>
|
</div>
|
||||||
6
StockWebHistory/Views/DataLoad/ShareEquity.cs
Normal file
6
StockWebHistory/Views/DataLoad/ShareEquity.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace StockWebHistory.Views.DataLoad
|
||||||
|
{
|
||||||
|
public class ShareEquity
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
59
StockWebHistory/Views/DataLoad/ShareEquity.cshtml
Normal file
59
StockWebHistory/Views/DataLoad/ShareEquity.cshtml
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
@{
|
||||||
|
ViewData["Title"] = "Share Equity";
|
||||||
|
}
|
||||||
|
|
||||||
|
@model ShareEquityModel
|
||||||
|
|
||||||
|
<div class="text-center">
|
||||||
|
<h1 class="display-4">Analyse stock notes</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="container mt-4">
|
||||||
|
<h2>Välj ett datum</h2>
|
||||||
|
|
||||||
|
<form asp-action="ShareEquity" method="post">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="SelectedDate">Datum</label>
|
||||||
|
<input asp-for="SelectedDate" class="form-control" id="datePicker" autocomplete="off" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-primary mt-3">Skicka</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
<script>
|
||||||
|
console.log("Flatpickr init start");
|
||||||
|
|
||||||
|
flatpickr("#datePicker", {
|
||||||
|
dateFormat: "Y-m-d",
|
||||||
|
defaultDate: "today",
|
||||||
|
onChange: function(selectedDates, dateStr, instance) {
|
||||||
|
console.log("Datum valt:", dateStr);
|
||||||
|
|
||||||
|
window.location.href = '/DataLoad/EquityFindings?date=' + dateStr;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@* <script>
|
||||||
|
$(function () {
|
||||||
|
$('#datePicker').datepicker({
|
||||||
|
format: 'yyyy-mm-dd',
|
||||||
|
autoclose: true,
|
||||||
|
todayHighlight: true
|
||||||
|
}).on('change', function (e) {
|
||||||
|
alert("Datum triggat")
|
||||||
|
// Här triggar du vidare logik
|
||||||
|
console.log("Datum valt:", e.date);
|
||||||
|
|
||||||
|
// Exempel: automatisk redirect
|
||||||
|
window.location.href = '/DataLoad/EquityFindings??date=' +
|
||||||
|
e.date.toISOString().substring(0, 10);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
*@
|
||||||
12
StockWebHistory/Views/DataLoad/ShareEquity.cshtml.cs
Normal file
12
StockWebHistory/Views/DataLoad/ShareEquity.cshtml.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
|
namespace StockWebHistory.Views.DataLoad
|
||||||
|
{
|
||||||
|
public class ShareEquityModel : PageModel
|
||||||
|
{
|
||||||
|
public void OnGet()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
@ -9,6 +10,7 @@
|
|||||||
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
|
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
|
||||||
<link rel="stylesheet" href="~/StockWebHistory.styles.css" asp-append-version="true" />
|
<link rel="stylesheet" href="~/StockWebHistory.styles.css" asp-append-version="true" />
|
||||||
</head>
|
</head>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
|
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
|
||||||
|
|||||||
Reference in New Issue
Block a user