Corregge i rilievi della revisione della finanza completa (ADR 0019). - Rating: il margine resta neutro finché la finestra non è piena e ha meno di finance.ratingMinRevenueMonths (3) mesi con ricavi, così il primo incasso piccolo dopo mesi di soli costi non fa perdere tre o quattro gradini (e non fissa uno spread pessimo per dieci anni); senza ricavi e in perdita il margine è il minimo. Corretta l'affermazione dell'ADR. - Avvisi di cassa negativa dopo finance.negativeCashNoticeDays (3) giorni di fila e notizia di rientro solo dopo quel primo avviso: una cassa che oscilla intorno a zero non riempie le notizie. - Spiegazione del rating: se il punteggio vale già il gradino sopra lo dice (NextAlreadyMet, ScoreRating) senza obiettivi negativi né leve infinite sul filo; il rimborso indicato è segnato come non possibile oggi se supera la cassa o il residuo dei prestiti (RepayAffordable). - Nuovi concorrenti: la quota dei trasporti usa Company.TransportRevenue (tariffe, noli, contratti), non dividendi incassati né commercio. - Client: barra del conto alla rovescia solo con lo scoperto, frasi intere con segnaposto in strings.csv, testo distinto per bancarotta e amministrazione controllata; ClimbOutlook.LoansAllowed allineato. - PROTOCOL.md: tabella dei valori di LedgerEntry e campi nuovi di FinanceReport; ServerTests non dipende più dal cambio di mese. Refs #39. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
286 lines
13 KiB
C#
286 lines
13 KiB
C#
using RailEnterprise.Core.Common;
|
||
|
||
namespace RailEnterprise.Protocol.Messages;
|
||
|
||
// Messaggi della fase F6: pannello «Finanze» definitivo. Il quadro arriva su richiesta (RequestDetail con
|
||
// DetailKind.Finance) ed è calcolato dal server con FinanceOutlook (ADR 0019): il client lo mostra com'è,
|
||
// senza calcolare utili, rating o rate.
|
||
|
||
/// <summary>Voce del conto economico in ordine di presentazione: Kind come LedgerEntry del Core, Section 0 ricavi, 1 costi, 2 investimenti e finanziamenti.</summary>
|
||
public sealed class FinanceLineInfo
|
||
{
|
||
public const byte SectionRevenue = 0, SectionCost = 1, SectionCapital = 2;
|
||
|
||
public byte Kind { get; init; }
|
||
public byte Section { get; init; }
|
||
|
||
public void Write(ref SpanWriter w)
|
||
{
|
||
w.U8(Kind); w.U8(Section);
|
||
w.Flags(0);
|
||
}
|
||
|
||
public static FinanceLineInfo Read(ref SpanReader r)
|
||
{
|
||
var m = new FinanceLineInfo { Kind = r.U8(), Section = r.U8() };
|
||
r.Flags();
|
||
return m;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Colonna del conto economico: Kind 0 mese in corso, 1 mese chiuso, 2 anno in corso, 3 anno chiuso;
|
||
/// Month 1–12 (0 per gli anni); Values con le sole voci diverse da zero (Kind = LedgerEntry); utile
|
||
/// operativo e flusso di cassa del motore.
|
||
/// </summary>
|
||
public sealed class IncomeColumnInfo
|
||
{
|
||
public const byte CurrentMonth = 0, ClosedMonth = 1, CurrentYear = 2, ClosedYear = 3;
|
||
|
||
public byte Kind { get; init; }
|
||
public int Year { get; init; }
|
||
public byte Month { get; init; }
|
||
public List<AmountInfo> Values { get; init; } = [];
|
||
public Money Profit { get; init; }
|
||
public Money CashFlow { get; init; }
|
||
|
||
/// <summary>Valore di una voce (zero se assente).</summary>
|
||
public Money Value(byte entry)
|
||
{
|
||
foreach (var v in Values)
|
||
{
|
||
if (v.Kind == entry) return v.Value;
|
||
}
|
||
return Money.Zero;
|
||
}
|
||
|
||
public void Write(ref SpanWriter w)
|
||
{
|
||
w.U8(Kind); w.I32(Year); w.U8(Month);
|
||
w.Count(Values.Count);
|
||
foreach (var v in Values) v.Write(ref w);
|
||
w.Money(Profit); w.Money(CashFlow);
|
||
w.Flags(0);
|
||
}
|
||
|
||
public static IncomeColumnInfo Read(ref SpanReader r)
|
||
{
|
||
byte kind = r.U8();
|
||
int year = r.I32();
|
||
byte month = r.U8();
|
||
var values = new List<AmountInfo>();
|
||
for (int n = r.Count(), i = 0; i < n; i++) values.Add(AmountInfo.Read(ref r));
|
||
var m = new IncomeColumnInfo { Kind = kind, Year = year, Month = month, Values = values, Profit = r.Money(), CashFlow = r.Money() };
|
||
r.Flags();
|
||
return m;
|
||
}
|
||
}
|
||
|
||
/// <summary>Soglia di un rating: punteggio minimo e tasso di un nuovo prestito con quel rating.</summary>
|
||
public sealed class RatingThresholdInfo
|
||
{
|
||
public string Rating { get; init; } = "";
|
||
public float MinScore { get; init; }
|
||
public float LoanRate { get; init; }
|
||
|
||
public void Write(ref SpanWriter w)
|
||
{
|
||
w.String(Rating); w.F32(MinScore); w.F32(LoanRate);
|
||
w.Flags(0);
|
||
}
|
||
|
||
public static RatingThresholdInfo Read(ref SpanReader r)
|
||
{
|
||
var m = new RatingThresholdInfo { Rating = r.String(), MinScore = r.F32(), LoanRate = r.F32() };
|
||
r.Flags();
|
||
return m;
|
||
}
|
||
}
|
||
|
||
/// <summary>Prestito in corso: capitale, residuo, tasso fisso, mesi restanti, rata, interessi della prossima rata, scadenza.</summary>
|
||
public sealed class FinanceLoanInfo
|
||
{
|
||
public int Loan { get; init; }
|
||
public Money Principal { get; init; }
|
||
public Money Remaining { get; init; }
|
||
public float AnnualRate { get; init; }
|
||
public int MonthsLeft { get; init; }
|
||
public Money Payment { get; init; }
|
||
public Money NextInterest { get; init; }
|
||
public long MaturityDay { get; init; }
|
||
|
||
public void Write(ref SpanWriter w)
|
||
{
|
||
w.I32(Loan); w.Money(Principal); w.Money(Remaining); w.F32(AnnualRate); w.I32(MonthsLeft); w.Money(Payment); w.Money(NextInterest); w.I64(MaturityDay);
|
||
w.Flags(0);
|
||
}
|
||
|
||
public static FinanceLoanInfo Read(ref SpanReader r)
|
||
{
|
||
var m = new FinanceLoanInfo
|
||
{
|
||
Loan = r.I32(), Principal = r.Money(), Remaining = r.Money(), AnnualRate = r.F32(), MonthsLeft = r.I32(),
|
||
Payment = r.Money(), NextInterest = r.Money(), MaturityDay = r.I64(),
|
||
};
|
||
r.Flags();
|
||
return m;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Quadro delle finanze della compagnia della sessione (F6, ADR 0019): conto economico per voce (mese in
|
||
/// corso, mesi chiusi, anno in corso, anni chiusi), spiegazione del rating, prestiti, fido e conto alla
|
||
/// rovescia della bancarotta.
|
||
/// </summary>
|
||
public sealed class FinanceReport : IMessage
|
||
{
|
||
public MessageId Id => MessageId.FinanceReport;
|
||
public long Day { get; init; }
|
||
public int Company { get; init; }
|
||
public Money Cash { get; init; }
|
||
public Money NetWorth { get; init; }
|
||
public Money Loans { get; init; }
|
||
public Money Overdraft { get; init; }
|
||
|
||
/// <summary>Voci in ordine di presentazione e colonne del conto economico.</summary>
|
||
public List<FinanceLineInfo> Lines { get; init; } = [];
|
||
public List<IncomeColumnInfo> Columns { get; init; } = [];
|
||
|
||
/// <summary>Rating e sua spiegazione: misure dell'ultimo calcolo, componenti, punteggio.</summary>
|
||
public string Rating { get; init; } = "";
|
||
/// <summary>Prima del primo calcolo mensile: rating iniziale dei dati, misure di oggi.</summary>
|
||
public bool RatingInitial { get; init; }
|
||
public byte RatingMonths { get; init; }
|
||
/// <summary>
|
||
/// Mesi della finestra con ricavi; con <see cref="MarginNeutral"/> il margine vale 0 finché non ce ne
|
||
/// sono <see cref="MinRevenueMonths"/> (o la finestra non è piena).
|
||
/// </summary>
|
||
public byte RevenueMonths { get; init; }
|
||
public bool MarginNeutral { get; init; }
|
||
public byte MinRevenueMonths { get; init; }
|
||
public float Margin { get; init; }
|
||
/// <summary>Debiti / patrimonio netto; con <see cref="LeverageUnbounded"/> il patrimonio non è positivo e la leva è infinita.</summary>
|
||
public float Leverage { get; init; }
|
||
public bool LeverageUnbounded { get; init; }
|
||
public float MarginWeight { get; init; }
|
||
public float LeverageWeight { get; init; }
|
||
public float MarginComponent { get; init; }
|
||
public float LeverageComponent { get; init; }
|
||
public float Score { get; init; }
|
||
public List<RatingThresholdInfo> Thresholds { get; init; } = [];
|
||
/// <summary>
|
||
/// Gradino sopra ("" ad AAA) con la soglia, il margine che basterebbe, la leva massima e il debito da
|
||
/// rimborsare; con <see cref="NextAlreadyMet"/> il punteggio basta già (rating <see cref="ScoreRating"/>
|
||
/// al prossimo calcolo mensile) e gli obiettivi non si calcolano.
|
||
/// </summary>
|
||
public string NextRating { get; init; } = "";
|
||
public float NextThreshold { get; init; }
|
||
public bool NextAlreadyMet { get; init; }
|
||
public string ScoreRating { get; init; } = "";
|
||
public float MarginNeeded { get; init; }
|
||
public bool MarginReachable { get; init; }
|
||
public float LeverageMax { get; init; }
|
||
public bool LeverageReachable { get; init; }
|
||
public Money DebtToRepay { get; init; }
|
||
/// <summary>Il rimborso indicato non supera la cassa né il residuo dei prestiti (lo scoperto non si rimborsa).</summary>
|
||
public bool RepayAffordable { get; init; }
|
||
|
||
/// <summary>Prestiti, fido e condizioni di un nuovo prestito (base della difficoltà + spread del rating).</summary>
|
||
public List<FinanceLoanInfo> LoanList { get; init; } = [];
|
||
public Money MaxLoan { get; init; }
|
||
public Money LoanAvailable { get; init; }
|
||
public float NewLoanRate { get; init; }
|
||
public float BaseRate { get; init; }
|
||
public float Spread { get; init; }
|
||
public int LoanTermMonths { get; init; }
|
||
public Money LoanStep { get; init; }
|
||
public bool LoansAllowed { get; init; } = true;
|
||
|
||
/// <summary>Giorni di cassa negativa e limite della difficoltà prima della bancarotta.</summary>
|
||
public int NegativeCashDays { get; init; }
|
||
public int BankruptcyDays { get; init; }
|
||
public bool Bankrupt { get; init; }
|
||
public bool Receivership { get; init; }
|
||
|
||
public void Write(ref SpanWriter w)
|
||
{
|
||
w.I64(Day); w.I32(Company); w.Money(Cash); w.Money(NetWorth); w.Money(Loans); w.Money(Overdraft);
|
||
w.Count(Lines.Count);
|
||
foreach (var l in Lines) l.Write(ref w);
|
||
w.Count(Columns.Count);
|
||
foreach (var c in Columns) c.Write(ref w);
|
||
w.String(Rating); w.Bool(RatingInitial); w.U8(RatingMonths); w.U8(RevenueMonths); w.Bool(MarginNeutral); w.U8(MinRevenueMonths); w.F32(Margin); w.F32(Leverage); w.Bool(LeverageUnbounded);
|
||
w.F32(MarginWeight); w.F32(LeverageWeight); w.F32(MarginComponent); w.F32(LeverageComponent); w.F32(Score);
|
||
w.Count(Thresholds.Count);
|
||
foreach (var t in Thresholds) t.Write(ref w);
|
||
w.String(NextRating); w.F32(NextThreshold); w.Bool(NextAlreadyMet); w.String(ScoreRating);
|
||
w.F32(MarginNeeded); w.Bool(MarginReachable); w.F32(LeverageMax); w.Bool(LeverageReachable);
|
||
w.Money(DebtToRepay); w.Bool(RepayAffordable);
|
||
w.Count(LoanList.Count);
|
||
foreach (var l in LoanList) l.Write(ref w);
|
||
w.Money(MaxLoan); w.Money(LoanAvailable); w.F32(NewLoanRate); w.F32(BaseRate); w.F32(Spread); w.I32(LoanTermMonths); w.Money(LoanStep);
|
||
w.Bool(LoansAllowed);
|
||
w.I32(NegativeCashDays); w.I32(BankruptcyDays); w.Bool(Bankrupt); w.Bool(Receivership);
|
||
w.Flags(0);
|
||
}
|
||
|
||
public static FinanceReport Read(ref SpanReader r)
|
||
{
|
||
long day = r.I64();
|
||
int company = r.I32();
|
||
var cash = r.Money();
|
||
var worth = r.Money();
|
||
var loans = r.Money();
|
||
var overdraft = r.Money();
|
||
var lines = new List<FinanceLineInfo>();
|
||
for (int n = r.Count(), i = 0; i < n; i++) lines.Add(FinanceLineInfo.Read(ref r));
|
||
var columns = new List<IncomeColumnInfo>();
|
||
for (int n = r.Count(), i = 0; i < n; i++) columns.Add(IncomeColumnInfo.Read(ref r));
|
||
string rating = r.String();
|
||
bool initial = r.Bool();
|
||
byte months = r.U8(), revenueMonths = r.U8();
|
||
bool neutral = r.Bool();
|
||
byte minRevenueMonths = r.U8();
|
||
float margin = r.F32(), leverage = r.F32();
|
||
bool unbounded = r.Bool();
|
||
float marginWeight = r.F32(), leverageWeight = r.F32(), marginComponent = r.F32(), leverageComponent = r.F32(), score = r.F32();
|
||
var thresholds = new List<RatingThresholdInfo>();
|
||
for (int n = r.Count(), i = 0; i < n; i++) thresholds.Add(RatingThresholdInfo.Read(ref r));
|
||
string next = r.String();
|
||
float nextThreshold = r.F32();
|
||
bool alreadyMet = r.Bool();
|
||
string scoreRating = r.String();
|
||
float marginNeeded = r.F32();
|
||
bool marginReachable = r.Bool();
|
||
float leverageMax = r.F32();
|
||
bool leverageReachable = r.Bool();
|
||
var repay = r.Money();
|
||
bool affordable = r.Bool();
|
||
var loanList = new List<FinanceLoanInfo>();
|
||
for (int n = r.Count(), i = 0; i < n; i++) loanList.Add(FinanceLoanInfo.Read(ref r));
|
||
var maxLoan = r.Money();
|
||
var available = r.Money();
|
||
float newRate = r.F32(), baseRate = r.F32(), spread = r.F32();
|
||
int term = r.I32();
|
||
var step = r.Money();
|
||
bool allowed = r.Bool();
|
||
int negDays = r.I32(), limit = r.I32();
|
||
bool bankrupt = r.Bool(), receivership = r.Bool();
|
||
r.Flags();
|
||
return new FinanceReport
|
||
{
|
||
Day = day, Company = company, Cash = cash, NetWorth = worth, Loans = loans, Overdraft = overdraft,
|
||
Lines = lines, Columns = columns,
|
||
Rating = rating, RatingInitial = initial, RatingMonths = months, RevenueMonths = revenueMonths, MarginNeutral = neutral,
|
||
MinRevenueMonths = minRevenueMonths, Margin = margin, Leverage = leverage, LeverageUnbounded = unbounded,
|
||
MarginWeight = marginWeight, LeverageWeight = leverageWeight, MarginComponent = marginComponent, LeverageComponent = leverageComponent,
|
||
Score = score, Thresholds = thresholds,
|
||
NextRating = next, NextThreshold = nextThreshold, NextAlreadyMet = alreadyMet, ScoreRating = scoreRating, MarginNeeded = marginNeeded, MarginReachable = marginReachable,
|
||
LeverageMax = leverageMax, LeverageReachable = leverageReachable, DebtToRepay = repay, RepayAffordable = affordable,
|
||
LoanList = loanList, MaxLoan = maxLoan, LoanAvailable = available, NewLoanRate = newRate, BaseRate = baseRate, Spread = spread,
|
||
LoanTermMonths = term, LoanStep = step, LoansAllowed = allowed,
|
||
NegativeCashDays = negDays, BankruptcyDays = limit, Bankrupt = bankrupt, Receivership = receivership,
|
||
};
|
||
}
|
||
}
|