Skip to content

Commit

Permalink
Add finance tools
Browse files Browse the repository at this point in the history
  • Loading branch information
strifel committed Jun 29, 2024
1 parent fc70b2d commit 6c72a0f
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 11 deletions.
13 changes: 13 additions & 0 deletions Components/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ protected override void OnInitialized()
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Add
</NavLink>
</div>
@if (nm.Uri.Contains("admin="))
{
<div class="nav-item px-3">
<NavLink class="nav-link" href="@FinanceLink()" Match="NavLinkMatch.All">
<span class="bi bi-bank-nav-menu" aria-hidden="true"></span> Finanzen
</NavLink>
</div>
}
</nav>
</div>

Expand All @@ -53,4 +61,9 @@ protected override void OnInitialized()
{
return _prefix + "/add" + _suffix;
}

private string FinanceLink()
{
return _prefix + "/finance" + _suffix;
}
}
4 changes: 2 additions & 2 deletions Components/Layout/NavMenu.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-plus-square-fill' viewBox='0 0 16 16'%3E%3Cpath d='M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2zm6.5 4.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3a.5.5 0 0 1 1 0z'/%3E%3C/svg%3E");
}

.bi-list-nested-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-list-nested' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M4.5 11.5A.5.5 0 0 1 5 11h10a.5.5 0 0 1 0 1H5a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 3 7h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 1 3h10a.5.5 0 0 1 0 1H1a.5.5 0 0 1-.5-.5z'/%3E%3C/svg%3E");
.bi-bank-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2216%22%20height%3D%2216%22%20fill%3D%22white%22%20class%3D%22bi%20bi-bank%22%20viewBox%3D%220%200%2016%2016%22%3E%0A%20%20%3Cpath%20d%3D%22m8%200%206.61%203h.89a.5.5%200%200%201%20.5.5v2a.5.5%200%200%201-.5.5H15v7a.5.5%200%200%201%20.485.38l.5%202a.498.498%200%200%201-.485.62H.5a.498.498%200%200%201-.485-.62l.5-2A.5.5%200%200%201%201%2013V6H.5a.5.5%200%200%201-.5-.5v-2A.5.5%200%200%201%20.5%203h.89zM3.777%203h8.447L8%201zM2%206v7h1V6zm2%200v7h2.5V6zm3.5%200v7h1V6zm2%200v7H12V6zM13%206v7h1V6zm2-1V4H1v1zm-.39%209H1.39l-.25%201h13.72z%22%2F%3E%0A%3C%2Fsvg%3E");
}

.nav-item {
Expand Down
1 change: 1 addition & 0 deletions Components/Pages/GroupAdd.razor
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
return;
}

Order!.PaymentStatus = PaymentStatus.Unpaid;
Order!.Group = group;
context.Add(Order!);
context.SaveChanges();
Expand Down
127 changes: 127 additions & 0 deletions Components/Pages/GroupFinanze.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
@page "/group/{GroupSlug}/finance"

@using GroupOrder.Data
@using Microsoft.AspNetCore.WebUtilities
@using Microsoft.EntityFrameworkCore
@rendermode InteractiveServer

@inject IDbContextFactory<GroupContext> DbFactory
@inject NavigationManager nm

<PageTitle>Mampf.Link @Group?.GroupName Finances</PageTitle>

@if (Group != null)
{
<h1>Finance Overview: @Group?.GroupName</h1>


<table class="table">
<thead>
<tr>
<th>
Name
</th>
<th>
Price
</th>
<th>
Payment Status
</th>
</tr>
</thead>
<tbody>
@foreach (Order order in Group!.Orders)
{
<tr>
<td>
@order.Name
</td>
<td>
@order.getPrice()
</td>
<td style="padding-top: 3px; padding-bottom: 0">
<InputSelect class="form-select form-select-sm" style="max-width: 200px" @bind-Value="@order.PaymentStatus">
<option value="@PaymentStatus.Unpaid">Unpaid</option>
<option value="@PaymentStatus.PaymentPending">Payment Pending</option>
<option value="@PaymentStatus.Paid">Paid</option>
</InputSelect>
</td>
</tr>
}
</tbody>
</table>

<button class="btn btn-primary" @onclick="Save">Save changes</button>
}

@code {

private Group? Group { get; set; }

private bool Loading { get; set; } = false;
private bool NotFound { get; set; } = false;

[Parameter]
public string? GroupSlug { get; set; }

private string? AdminCode { get; set; }

protected override Task OnInitializedAsync()
{
_context = DbFactory.CreateDbContext();
return base.OnInitializedAsync();
}

protected override async Task OnParametersSetAsync()
{
await LoadGroupAsync();
await base.OnParametersSetAsync();
}

GroupContext? _context;

// Loads the contact.
private async Task LoadGroupAsync()
{
if (nm.Uri.Contains("?") && QueryHelpers.ParseQuery(nm.Uri.Split("?")[1]).TryGetValue("admin", out var code))
{
AdminCode = code;
}
else
{
NotFound = true;
return;
}

if (Loading)
{
return; //avoid concurrent requests.
}

Group = null;
Loading = true;

if (_context!.Groups is not null)
{
Group = await _context!.Groups
.Include(group => group.Orders)
.SingleOrDefaultAsync(
c => c.GroupSlug == GroupSlug && c.AdminCode == AdminCode);

if (Group is null)
{
NotFound = true;
}
}

Loading = false;
}

private async void Save()
{
if (Group == null) return;
if (Group.AdminCode != AdminCode) return; // this theoretically should not happen
await _context!.SaveChangesAsync();
}

}
11 changes: 3 additions & 8 deletions Components/Pages/GroupOverview.razor
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
@order.Food
</td>
<td>
@getPrice(order)
@order.getPrice()
</td>
<td>
@if (Group.PaypalUsername != null)
@if (!string.IsNullOrEmpty(Group.PaypalUsername))
{
<a style="text-decoration: none" target="_blank" href="https://www.paypal.com/paypalme/@Group.PaypalUsername/@getPrice(order)">💳</a>
<a style="text-decoration: none" target="_blank" href="https://www.paypal.com/paypalme/@Group.PaypalUsername/@order.getPrice()">💳</a>
}
</td>
</tr>
Expand Down Expand Up @@ -101,10 +101,5 @@

Loading = false;
}

private String getPrice(Order order)
{
return (order.Price / 100) + "." + System.String.Format("{0:D2}", order.Price % 100);
}

}
17 changes: 17 additions & 0 deletions Data/Order.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

Expand All @@ -20,4 +21,20 @@ public class Order

[Required]
public int? Price { get; set; } // in cent

[Required]
[DefaultValue(PaymentStatus.Unpaid)]
public PaymentStatus PaymentStatus { get; set; }

public String getPrice()
{
return (this.Price / 100) + "." + System.String.Format("{0:D2}", this.Price % 100);
}
}

public enum PaymentStatus
{
Unpaid,
PaymentPending,
Paid
}
111 changes: 111 additions & 0 deletions Migrations/20240629204340_Finance.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Migrations/20240629204340_Finance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace GroupOrder.Migrations
{
/// <inheritdoc />
public partial class Finance : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "PaymentStatus",
table: "Orders",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PaymentStatus",
table: "Orders");
}
}
}
3 changes: 3 additions & 0 deletions Migrations/GroupContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<int>("PaymentStatus")
.HasColumnType("INTEGER");
b.Property<int?>("Price")
.IsRequired()
.HasColumnType("INTEGER");
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

var app = builder.Build();

app.UseExceptionHandler("/Error", createScopeForErrors: true);

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}

app.UseHttpsRedirection();
Expand Down

0 comments on commit 6c72a0f

Please sign in to comment.