Skip to content

Commit

Permalink
Added bonus payment to management console
Browse files Browse the repository at this point in the history
  • Loading branch information
murlodev committed Jan 20, 2025
1 parent 1493d63 commit a55ecf3
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ public void InitializeAccountOnHolder(NetEntity holder)
var msg = new EconomyManagementConsoleInitAccountOnHolderMessage(holder);
SendMessage(msg);
}

public void PayBonus(string payer, float bonusPercent, List<string> accounts)
{
var msg = new EconomyManagementConsolePayBonusMessage(payer, bonusPercent, accounts);
SendMessage(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public EconomyManagementConsoleMenu(EconomyManagementConsoleBoundUserInterface o
AccountHolderTab.OnBlockAccountPressed += Owner.BlockAccountToggle;
AccountHolderTab.OnChangeAccountPressed += Owner.ChangeAccountHolderID;
AccountHolderTab.OnInitializeAccountPressed += Owner.InitializeAccountOnHolder;

BonusTab.OnPayBonusPressed += Owner.PayBonus;
}

public void UpdateState(EconomyManagementConsoleUserInterfaceState state)
{
UpdateAccountManagement(state.Priveleged);
UpdateHolder(state);
UpdateBonus(state.Priveleged);
}

private void UpdateAccountManagement(bool priveleged)
Expand All @@ -53,4 +56,9 @@ private void UpdateHolder(EconomyManagementConsoleUserInterfaceState state)
AccountHolderTab.Priveleged = state.Priveleged;
AccountHolderTab.OnUpdateState(state.AccountID, state.AccountName, state.Balance, state.Penalty, state.Blocked, state.CanReachPayDay);
}

private void UpdateBonus(bool priveleged)
{
BonusTab.OnUpdateState(priveleged);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ public void Initialize()
AccountList.Clear();
var bankAccountSystem = _entityManager.System<EconomyBankAccountSystemShared>();
var tags = new List<BankAccountTag>() { BankAccountTag.Department };
_accounts = bankAccountSystem.GetAccounts(EconomyBankAccountMask.ByTags, tags).Values.ToList();
var accounts = bankAccountSystem.GetAccounts(EconomyBankAccountMask.ByTags, tags);
_accounts = accounts.Values.ToList();

// Update selected account if we had any
if (_currentAccount is not null && accounts.TryGetValue(_currentAccount.AccountID, out var currentAccount))
{
_currentAccount = currentAccount;
FillAccountInfo(_currentAccount);
}

FillList();
}
Expand Down
41 changes: 14 additions & 27 deletions Content.Client/AWS/Economy/UI/ManagementConsole/Tabs/BonusTab.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,41 @@
<BoxContainer Orientation="Horizontal" HorizontalExpand="True" VerticalExpand="True">
<!-- Left: Account List -->
<BoxContainer Orientation="Vertical" Margin="5" MinWidth="300">
<Label Text="{Loc 'economybanksystem-management-console-accounts-title'}" HorizontalExpand="True" Align="Center"/>
<LineEdit Name="FindAccount" HorizontalExpand="True" />
<ScrollContainer VerticalExpand="True">
<ItemList Name="AccountList" />
</ScrollContainer>
</BoxContainer>

<!-- Right: Account Management -->
<!-- Right: Bonus Management -->
<BoxContainer Orientation="Vertical" Margin="5" VerticalExpand="True" HorizontalExpand="True">
<Label Text="{Loc 'economybanksystem-management-console-management-title'}" HorizontalExpand="True" Align="Center"/>
<Label Text="{Loc 'economybanksystem-management-console-bonus-tab'}" HorizontalExpand="True" Align="Center"/>
<PanelContainer StyleClasses="LowDivider" />
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<LineEdit Name="FindAccountBox"
HorizontalExpand="True"/>
<Button Name="FindAccountButton"
Text="{Loc 'economybanksystem-management-console-management-find-account'}"
Disabled="True"/>
</BoxContainer>
<ScrollContainer VerticalExpand="True">
<ItemList Name="SelectedAccountList" />
</ScrollContainer>
<PanelContainer StyleClasses="LowDivider" />
<GridContainer Columns="2"
HorizontalExpand="True"
Margin="0 0 0 10">
<Label Text="{Loc 'economy-bank-atm-menu-account-id'}"
<Label Text="{Loc 'economybanksystem-management-console-bonus-payer-account'}"
HorizontalExpand="True"/>
<Label Name="AccountIdLabel"
HorizontalExpand="True"/>
<Label Text="{Loc 'economy-bank-atm-menu-account-owner'}"
HorizontalExpand="True"/>
<Label Name="AccountOwnerLabel"
<Label Name="PayerAccountIDLabel"
HorizontalExpand="True"/>
<Label Text="{Loc 'economy-bank-atm-menu-account-balance'}"
HorizontalExpand="True"/>
<Label Name="AccountBalanceLabel"
<Label Name="PayerAccountBalanceLabel"
HorizontalExpand="True"/>
<Label Text="{Loc 'economybanksystem-management-console-management-blocked'}"
<Label Text="{Loc 'economybanksystem-management-console-bonus-total-bonus'}"
HorizontalExpand="True"/>
<Label Name="AccountBlockedLabel"
<Label Name="TotalBonusLabel"
HorizontalExpand="True"/>
</GridContainer>
<PanelContainer StyleClasses="LowDivider" />
<Button Name="BlockButton" Text="-" Disabled="True" HorizontalAlignment="Left" MaxWidth="150"/>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<LineEdit Name="ChangeNameBox"
HorizontalExpand="True"/>
<Button Name="ChangeNameButton"
Text="{Loc 'economybanksystem-management-console-management-change-name'}"
Disabled="True"/>
</BoxContainer>
<Label Text="{Loc 'economybanksystem-management-console-bonus-percent'}" />
<SpinBox Name="BonusPercentBox" HorizontalExpand="True" Value="10" />
<Button Name="PayBonusButton" Text="{Loc 'economybanksystem-management-console-bonus-pay-button'}" Disabled="True" HorizontalAlignment="Left" MaxWidth="300"/>
<Button Name="ClearSelectedButton" Text="{Loc 'economybanksystem-management-console-bonus-clear-selected-button'}" HorizontalAlignment="Left" MaxWidth="300"/>
</BoxContainer>
</BoxContainer>
</Control>
177 changes: 177 additions & 0 deletions Content.Client/AWS/Economy/UI/ManagementConsole/Tabs/BonusTab.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,192 @@
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.XAML;
using Robust.Client.UserInterface;
using Content.Shared.AWS.Economy;
using System.Linq;
using Robust.Client.UserInterface.Controls;

namespace Content.Client.AWS.Economy.UI.ManagementConsole.Tabs;

[GenerateTypedNameReferences]
public sealed partial class BonusTab : Control
{
[Dependency] private readonly EntityManager _entityManager = default!;

private IReadOnlyList<Entity<EconomyBankAccountComponent>> _cachedAccounts = default!;
private IReadOnlyList<Entity<EconomyBankAccountComponent>> _payerAccounts = default!;
private List<EconomyBankAccountComponent> _selectedAccounts = new();
private EconomyBankAccountComponent? _selectedPayer;

public Action<string, float, List<string>>? OnPayBonusPressed;

public bool Priveleged;

public BonusTab()
{
IoCManager.InjectDependencies(this);
RobustXamlLoader.Load(this);

Initialize();

FindAccount.OnTextEntered += OnTextEnteredAccount;

BonusPercentBox.AddLeftButton(-10, "-10%");
BonusPercentBox.AddLeftButton(-1, "-1%");
BonusPercentBox.AddRightButton(1, "+1%");
BonusPercentBox.AddRightButton(10, "+10%");
BonusPercentBox.ValueChanged += _ =>
{
FillInfo();
};

PayBonusButton.OnPressed += _ =>
{
if (_selectedAccounts.Count <= 0 || _selectedPayer is null)
return;

var accountIDs = new List<string>();
foreach (var account in _selectedAccounts)
accountIDs.Add(account.AccountID);

var bonusPercent = BonusPercentBox.Value / 100f;
OnPayBonusPressed?.Invoke(_selectedPayer.AccountID, bonusPercent, accountIDs);
};
ClearSelectedButton.OnPressed += _ =>
{
if (_selectedAccounts.Count <= 0)
return;

PayBonusButton.Disabled = true;
SelectedAccountList.Clear();
_selectedAccounts.Clear();
FillList();
FillInfo();
};
}

public void Initialize()
{
var bankAccountSystem = _entityManager.System<EconomyBankAccountSystemShared>();
var tags = new List<BankAccountTag>() { BankAccountTag.Personal };
_cachedAccounts = bankAccountSystem.GetAccounts(EconomyBankAccountMask.ByTags, tags).Values.ToList();

var payerTags = new List<BankAccountTag>() { BankAccountTag.Station };
_payerAccounts = bankAccountSystem.GetAccounts(EconomyBankAccountMask.ByTags, payerTags).Values.ToList();
_selectedPayer = _payerAccounts.FirstOrDefault().Comp;

FillList();
FillInfo();
}

public void OnUpdateState(bool priveleged)
{
Priveleged = priveleged;
PayBonusButton.Disabled = !Priveleged ||
_selectedPayer == null ||
_selectedAccounts.Count <= 0 ||
CalculateBonusPayment() > _selectedPayer.Balance;

FillList();
FillInfo();
}

private void FillList()
{
AccountList.Clear();
foreach (var (key, value) in _cachedAccounts)
{
if (_selectedAccounts.Contains(value))
continue;

var field = AccountList.AddItem(FormFieldName(value));
field.Metadata = value;
field.OnSelected += OnSelectAccount;
}

AccountList.SortItemsByText();
}

private string FormFieldName(EconomyBankAccountComponent account)
{
return account.AccountID + " — " + account.AccountName;
}

private void UpdateSelectedAccounts()
{
SelectedAccountList.Clear();
foreach (var account in _selectedAccounts)
{
var field = SelectedAccountList.AddItem(FormFieldName(account));
field.Metadata = account;
field.OnSelected += OnSelectSelectedAccount;
}

SelectedAccountList.SortItemsByText();
}

private void OnSelectAccount(ItemList.Item accountId)
{
if (accountId.Metadata is not EconomyBankAccountComponent account)
return;

PayBonusButton.Disabled = !Priveleged || _selectedPayer == null || CalculateBonusPayment() > _selectedPayer.Balance;

_selectedAccounts.Add(account);
UpdateSelectedAccounts();
FillList();
FillInfo();
}

private void OnSelectSelectedAccount(ItemList.Item accountId)
{
if (accountId.Metadata is not EconomyBankAccountComponent account)
return;

_selectedAccounts.Remove(account);
UpdateSelectedAccounts();
FillList();
FillInfo();
}

private void FillInfo()
{
PayerAccountIDLabel.Text = _selectedPayer?.AccountID;
PayerAccountBalanceLabel.Text = string.Format("{0:N0}", _selectedPayer?.Balance);
TotalBonusLabel.Text = string.Format("{0:N0}", CalculateBonusPayment());
}

private ulong CalculateBonusPayment()
{
if (_selectedAccounts.Count <= 0)
return 0;

var bonusPercent = BonusPercentBox.Value / 100f;
ulong total = 0;
foreach (var account in _selectedAccounts)
total += account.Salary is not null ? (ulong)(account.Salary * bonusPercent) : 0;

return total;
}

private void OnTextEnteredAccount(LineEdit.LineEditEventArgs eventArgs)
{
AccountList.Clear();
var upText = eventArgs.Text.ToUpper();
foreach (var (key, value) in _cachedAccounts)
{
var fieldName = FormFieldName(value);
if (fieldName.Contains(upText))
{
var field = AccountList.AddItem(fieldName);
field.Metadata = value;
field.OnSelected += OnSelectAccount;
}
}
if (AccountList.Count == 0)
{
AccountList.AddItem("No data acquired");
return;
}
AccountList.SortItemsByText();
}
}
Loading

0 comments on commit a55ecf3

Please sign in to comment.