The project page opens on a snapshot: revenue, direct costs, the contribution before overhead and its margin of revenue (a segment margin: revenue less every cost traced to the project, fixed and variable; B-37 item 7), in the black or red, and for on-spec work the cost unrecovered if repaid. To date or this fiscal year. By month, revenue and costs with the net to date in its own strip; costs by category; the list colors each net by its sign. books.project carries the same figures, and a parity test runs the viewer and the MCP on the same books to the cent. Tests honor TMPDIR. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
This commit is contained in:
23 files changed
+2231
-246
No files matched your search
+114
-18
@@ -250,19 +250,45 @@ pub fn bar_chart(
|
||||
series: &[(&str, Color32)],
|
||||
height: f32,
|
||||
) -> Option<usize> {
|
||||
bars(ui, groups, series, height, true)
|
||||
bars(ui, groups, series, None, height, true)
|
||||
}
|
||||
|
||||
/// [`bar_chart`] that opens nothing: hovering shows each group's figures,
|
||||
/// and the pointer stays an arrow, since no page shows those figures.
|
||||
pub fn bar_chart_view(ui: &mut Ui, groups: &[BarGroup], series: &[(&str, Color32)], height: f32) {
|
||||
bars(ui, groups, series, height, false);
|
||||
bars(ui, groups, series, None, height, false);
|
||||
}
|
||||
|
||||
/// A line under a [`bar_chart_view`]'s bars: one value per group, marked
|
||||
/// at each group's center (e.g. a running net toward break-even), in a
|
||||
/// strip of its own with its own scale and a $0 rule, so a running total
|
||||
/// many times one group's figures doesn't flatten the bars.
|
||||
pub struct Overlay<'a> {
|
||||
pub name: &'a str,
|
||||
pub color: Color32,
|
||||
pub values: &'a [Decimal],
|
||||
}
|
||||
|
||||
/// [`bar_chart_view`] with an [`Overlay`] line in a strip under the bars
|
||||
/// ([`LINE_STRIP`] taller); its value joins each group's hover figures.
|
||||
pub fn bar_chart_view_with_line(
|
||||
ui: &mut Ui,
|
||||
groups: &[BarGroup],
|
||||
series: &[(&str, Color32)],
|
||||
line: &Overlay<'_>,
|
||||
height: f32,
|
||||
) {
|
||||
bars(ui, groups, series, Some(line), height, false);
|
||||
}
|
||||
|
||||
/// How much taller an [`Overlay`] makes a bar chart: its strip.
|
||||
pub const LINE_STRIP: f32 = 84.0;
|
||||
|
||||
fn bars(
|
||||
ui: &mut Ui,
|
||||
groups: &[BarGroup],
|
||||
series: &[(&str, Color32)],
|
||||
line: Option<&Overlay<'_>>,
|
||||
height: f32,
|
||||
clickable: bool,
|
||||
) -> Option<usize> {
|
||||
@@ -278,17 +304,32 @@ fn bars(
|
||||
);
|
||||
ui.add_space(8.0);
|
||||
}
|
||||
if let Some(l) = line {
|
||||
let (r, _) = ui.allocate_exact_size(vec2(16.0, 10.0), Sense::hover());
|
||||
ui.painter().line_segment(
|
||||
[r.left_center(), r.right_center()],
|
||||
Stroke::new(2.0, l.color),
|
||||
);
|
||||
ui.painter().circle_filled(r.center(), 3.0, l.color);
|
||||
ui.label(
|
||||
egui::RichText::new(l.name)
|
||||
.font(theme::regular(12.0))
|
||||
.color(p.muted),
|
||||
);
|
||||
}
|
||||
});
|
||||
let sense = if clickable {
|
||||
Sense::click()
|
||||
} else {
|
||||
Sense::hover()
|
||||
};
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), height), sense);
|
||||
let strip_h = if line.is_some() { LINE_STRIP } else { 0.0 };
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), height + strip_h), sense);
|
||||
let painter = ui.painter_at(rect);
|
||||
if groups.is_empty() {
|
||||
return None;
|
||||
}
|
||||
// The bars' scale is the bars' own; the line has its strip's.
|
||||
let all: Vec<f64> = groups
|
||||
.iter()
|
||||
.flat_map(|g| g.values.iter().map(|v| f(*v)))
|
||||
@@ -297,7 +338,17 @@ fn bars(
|
||||
let hi = all.iter().cloned().fold(0.0f64, f64::max).max(lo + 1.0);
|
||||
let ts = ticks(lo, hi, 4);
|
||||
let (lo, hi) = (ts[0].min(lo), ts[ts.len() - 1].max(hi));
|
||||
let plot = Rect::from_min_max(rect.min + vec2(52.0, 6.0), rect.max - vec2(4.0, 22.0));
|
||||
let plot = Rect::from_min_max(
|
||||
rect.min + vec2(52.0, 6.0),
|
||||
pos2(rect.max.x - 4.0, rect.min.y + height - 22.0),
|
||||
);
|
||||
let strip = line.map(|_| {
|
||||
Rect::from_min_max(
|
||||
pos2(plot.left(), plot.bottom() + 24.0),
|
||||
pos2(plot.right(), plot.bottom() + strip_h - 4.0),
|
||||
)
|
||||
});
|
||||
let bottom = strip.map_or(plot.bottom(), |s| s.bottom());
|
||||
let y = |v: f64| plot.bottom() - ((v - lo) / (hi - lo)) as f32 * plot.height();
|
||||
for t in &ts {
|
||||
let yy = y(*t);
|
||||
@@ -319,13 +370,57 @@ fn bars(
|
||||
let mut clicked = None;
|
||||
let hover = resp.hover_pos();
|
||||
let mut drawn_year: Option<i32> = None;
|
||||
let mut tip: Option<(egui::Pos2, String, String)> = None;
|
||||
// The line goes over the hover band and under the tooltip, on its
|
||||
// own scale with room above and below (its markers never sit on the
|
||||
// strip's edge), and a dashed $0: break-even.
|
||||
let mut overlay = Vec::new();
|
||||
if let (Some(l), Some(s)) = (line, strip) {
|
||||
let vals: Vec<f64> = l.values.iter().take(groups.len()).map(|v| f(*v)).collect();
|
||||
let lo = vals.iter().cloned().fold(0.0f64, f64::min);
|
||||
let hi = vals.iter().cloned().fold(0.0f64, f64::max).max(lo + 1.0);
|
||||
let pad = (hi - lo) * 0.12;
|
||||
let (lo, hi) = (lo - pad, hi + pad);
|
||||
let sy = |v: f64| s.bottom() - ((v - lo) / (hi - lo)) as f32 * s.height();
|
||||
let zero = sy(0.0);
|
||||
overlay.extend(egui::Shape::dashed_line(
|
||||
&[pos2(s.left(), zero), pos2(s.right(), zero)],
|
||||
Stroke::new(1.0, p.faint),
|
||||
4.0,
|
||||
3.0,
|
||||
));
|
||||
let label = |v: f64, at: f32| {
|
||||
let g = painter.layout_no_wrap(money_label(v), theme::regular(11.0), p.faint);
|
||||
let r = Align2::RIGHT_CENTER.anchor_size(pos2(s.left() - 8.0, at), g.size());
|
||||
egui::Shape::galley(r.min, g, p.faint)
|
||||
};
|
||||
overlay.push(label(0.0, zero));
|
||||
let (vmin, vmax) = (
|
||||
vals.iter().cloned().fold(0.0f64, f64::min),
|
||||
vals.iter().cloned().fold(0.0f64, f64::max),
|
||||
);
|
||||
for v in [vmax, vmin] {
|
||||
if v != 0.0 && (sy(v) - zero).abs() >= 12.0 {
|
||||
overlay.push(label(v, sy(v)));
|
||||
}
|
||||
}
|
||||
let pts: Vec<egui::Pos2> = vals
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(gi, v)| pos2(plot.left() + gw * (gi as f32 + 0.5), sy(*v)))
|
||||
.collect();
|
||||
overlay.push(egui::Shape::line(pts.clone(), Stroke::new(2.0, l.color)));
|
||||
for pt in pts {
|
||||
overlay.push(egui::Shape::circle_filled(pt, 3.0, l.color));
|
||||
}
|
||||
}
|
||||
for (gi, g) in groups.iter().enumerate() {
|
||||
let full = match g.year {
|
||||
Some(y) => format!("{} {:02}", g.label, y.rem_euclid(100)),
|
||||
None => g.label.clone(),
|
||||
};
|
||||
let gx = plot.left() + gw * gi as f32;
|
||||
let grect = Rect::from_min_max(pos2(gx, plot.top()), pos2(gx + gw, plot.bottom()));
|
||||
let grect = Rect::from_min_max(pos2(gx, plot.top()), pos2(gx + gw, bottom));
|
||||
let hovered = hover.is_some_and(|h| grect.contains(h));
|
||||
if hovered {
|
||||
painter.rect_filled(grect, CornerRadius::same(6), p.hover);
|
||||
@@ -358,7 +453,7 @@ fn bars(
|
||||
let new_year = g.year.is_some() && g.year != drawn_year;
|
||||
drawn_year = g.year.or(drawn_year);
|
||||
painter.text(
|
||||
pos2(gx + gw / 2.0, plot.bottom() + 6.0),
|
||||
pos2(gx + gw / 2.0, bottom + 6.0),
|
||||
Align2::CENTER_TOP,
|
||||
if new_year { &full } else { &g.label },
|
||||
theme::regular(11.0),
|
||||
@@ -366,22 +461,17 @@ fn bars(
|
||||
);
|
||||
}
|
||||
if hovered {
|
||||
let lines: Vec<String> = series
|
||||
let mut lines: Vec<String> = series
|
||||
.iter()
|
||||
.zip(&g.values)
|
||||
.map(|((name, _), v)| format!("{name}: {}", fmt::dollars(*v)))
|
||||
.collect();
|
||||
tooltip(
|
||||
&painter,
|
||||
pos2(gx + gw / 2.0, plot.top()),
|
||||
&full,
|
||||
&lines.join("\n"),
|
||||
p.surface,
|
||||
p.border,
|
||||
p.text,
|
||||
p.muted,
|
||||
rect,
|
||||
);
|
||||
if let Some(l) = line
|
||||
&& let Some(v) = l.values.get(gi)
|
||||
{
|
||||
lines.push(format!("{}: {}", l.name, fmt::dollars(*v)));
|
||||
}
|
||||
tip = Some((pos2(gx + gw / 2.0, plot.top()), full, lines.join("\n")));
|
||||
if clickable {
|
||||
if resp.clicked() {
|
||||
clicked = Some(gi);
|
||||
@@ -390,6 +480,12 @@ fn bars(
|
||||
}
|
||||
}
|
||||
}
|
||||
painter.extend(overlay);
|
||||
if let Some((at, title, body)) = tip {
|
||||
tooltip(
|
||||
&painter, at, &title, &body, p.surface, p.border, p.text, p.muted, rect,
|
||||
);
|
||||
}
|
||||
clicked
|
||||
}
|
||||
|
||||
|
||||
@@ -246,7 +246,13 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
|
||||
let client_class = s.add_project(
|
||||
"Client work",
|
||||
&ledger_core::ProjectFacts {
|
||||
description: Some("Design work for the studio's clients".into()),
|
||||
// Its invoices can't be tagged to it: an invoice posts one
|
||||
// untagged revenue line (Ledger::create_invoice takes no
|
||||
// class), so only the contractors it pays show here, said so.
|
||||
description: Some(
|
||||
"Design work for the studio's clients. Only its contractors are tagged to it: an invoice can't carry a project yet, so what the clients pay shows on the profit and loss, not here."
|
||||
.into(),
|
||||
),
|
||||
kind: Some(ledger_core::ProjectKind::Client),
|
||||
..Default::default()
|
||||
},
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! Projects: every class, with its facts (what it is, where it stands, who
|
||||
//! it is for), and one project completely: what it earned and cost, by
|
||||
//! month and by account, its entries, documents, decisions and history
|
||||
//! (rules.md B-37). Every figure is ledger-core's (`project_figures`,
|
||||
//! it is for), and one project completely: its snapshot (revenue, direct
|
||||
//! costs, contribution before overhead and its margin, to date or this
|
||||
//! fiscal year, rules.md B-37 item 7), revenue and costs by month with the
|
||||
//! running net, costs by category and by account, its entries, documents,
|
||||
//! decisions and history (rules.md B-37). Every figure is ledger-core's (`project_figures`,
|
||||
//! `project_to_date`, `project_entries`), the same the server's
|
||||
//! `books.projects` and `books.project` return; this module only arranges
|
||||
//! them, and never writes.
|
||||
@@ -27,6 +29,9 @@ use rust_decimal::Decimal;
|
||||
pub struct State {
|
||||
/// Only projects with this status (None: every one).
|
||||
pub status: Option<ProjectStatus>,
|
||||
/// A project's snapshot shows this fiscal year to date (B-30's
|
||||
/// omitted dates), not to date.
|
||||
pub fiscal: bool,
|
||||
}
|
||||
|
||||
/// One project on the list: its id, name, facts and net to date.
|
||||
@@ -160,17 +165,24 @@ pub fn show_list(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
let amt = widgets::AMOUNT_MIN + 24.0;
|
||||
let avail = ui.available_width();
|
||||
let status_w = 100.0;
|
||||
let kind_w = if avail >= 760.0 { 140.0 } else { 0.0 };
|
||||
// The cue repeats what the net's color says: it goes
|
||||
// first as the list narrows, then the kind.
|
||||
let kind_w = if avail >= 640.0 { 140.0 } else { 0.0 };
|
||||
let cue_w = if avail >= 760.0 { 120.0 } else { 0.0 };
|
||||
let party_w = if avail >= 900.0 {
|
||||
(avail * 0.18).clamp(140.0, 220.0)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let w = avail.max(status_w + kind_w + party_w + amt + 180.0);
|
||||
let cells = |c: [(String, f32, bool, egui::Color32); 5]| -> Vec<_> {
|
||||
let w = avail.max(status_w + kind_w + party_w + cue_w + amt + 180.0);
|
||||
let cells = |c: [(String, f32, bool, egui::Color32); 6]| -> Vec<_> {
|
||||
c.into_iter()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| (*i != 2 || kind_w > 0.0) && (*i != 3 || party_w > 0.0))
|
||||
.filter(|(i, _)| {
|
||||
(*i != 2 || kind_w > 0.0)
|
||||
&& (*i != 3 || party_w > 0.0)
|
||||
&& (*i != 4 || cue_w > 0.0)
|
||||
})
|
||||
.map(|(_, c)| c)
|
||||
.collect()
|
||||
};
|
||||
@@ -184,6 +196,7 @@ pub fn show_list(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
head("Status", status_w, false),
|
||||
head("Kind", kind_w, false),
|
||||
head("Party", party_w, false),
|
||||
head("", cue_w, true),
|
||||
head("Net to date", amt, true),
|
||||
]),
|
||||
None,
|
||||
@@ -198,12 +211,29 @@ pub fn show_list(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
.party
|
||||
.map(|x| b.party_label(x))
|
||||
.unwrap_or_else(|| "—".into());
|
||||
let (net_t, net_c) = match r.to_date.as_ref().map(|t| t.net()) {
|
||||
Some(n) if n.is_zero() => ("—".to_string(), p.faint),
|
||||
Some(n) if n < Decimal::ZERO => (fmt::accounting(n), p.negative),
|
||||
Some(n) => (fmt::accounting(n), p.text),
|
||||
let snap = r.to_date.as_ref().map(Snap::to_date);
|
||||
let (net_t, net_c) = match &snap {
|
||||
Some(s) if s.standing == Standing::Nothing => {
|
||||
("—".to_string(), p.faint)
|
||||
}
|
||||
Some(s) => (fmt::accounting(s.contribution), s.standing.colors(&p).0),
|
||||
None => (fmt::CANT_COMPUTE.to_string(), p.faint),
|
||||
};
|
||||
// A small cue beside the net: where it stands.
|
||||
let (cue_t, cue_c) = match snap.as_ref().map(|s| s.standing) {
|
||||
Some(st @ (Standing::Black | Standing::Red | Standing::Even)) => {
|
||||
let mark = match st {
|
||||
Standing::Black => icon::TREND_UP,
|
||||
Standing::Red => icon::TREND_DOWN,
|
||||
_ => icon::EQUALS,
|
||||
};
|
||||
(
|
||||
format!("{mark} {}", st.words().to_lowercase()),
|
||||
st.colors(&p).0,
|
||||
)
|
||||
}
|
||||
_ => (String::new(), p.faint),
|
||||
};
|
||||
let sub = r
|
||||
.facts
|
||||
.description
|
||||
@@ -218,6 +248,7 @@ pub fn show_list(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
(r.facts.status.words().to_string(), status_w, false, sc),
|
||||
(kind_words(r.facts.kind).to_string(), kind_w, false, p.muted),
|
||||
(party, party_w, false, p.muted),
|
||||
(cue_t, cue_w, true, cue_c),
|
||||
(net_t, amt, true, net_c),
|
||||
]),
|
||||
Some((sub.as_str(), p.faint)),
|
||||
@@ -240,7 +271,7 @@ pub fn show_list(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
ui.add(
|
||||
egui::Label::new(
|
||||
RichText::new(format!(
|
||||
"Net to date: every posted revenue and expense line tagged to the project through {}, closing entries left out; revenue less expenses.",
|
||||
"Net to date: every posted revenue and expense line tagged to the project through {}, closing entries left out; revenue less its direct costs, before overhead (untagged company costs aren't counted).",
|
||||
fmt::date(b.today)
|
||||
))
|
||||
.font(theme::regular(11.5))
|
||||
@@ -285,14 +316,248 @@ pub fn project_events(b: &Books, id: ClassId) -> Vec<usize> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A number tile: (label, value, how it is computed, the line under it).
|
||||
type Tile = (
|
||||
String,
|
||||
Option<Decimal>,
|
||||
/// A snapshot tile: (label, value, the line under it, how it is
|
||||
/// computed, the amount its menu copies).
|
||||
type Tile<'a> = (
|
||||
&'a str,
|
||||
String,
|
||||
Option<(String, egui::Color32)>,
|
||||
String,
|
||||
Option<Decimal>,
|
||||
);
|
||||
|
||||
/// Where a project stands on its contribution before overhead.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Standing {
|
||||
/// Contribution above zero.
|
||||
Black,
|
||||
/// Contribution below zero.
|
||||
Red,
|
||||
/// Lines tagged, netting to zero (a cost and its reversal too).
|
||||
Even,
|
||||
/// No revenue or expense line tagged (in the span shown).
|
||||
Nothing,
|
||||
}
|
||||
|
||||
impl Standing {
|
||||
/// From a contribution, and whether any posted revenue or expense
|
||||
/// line is tagged at all (counted lines, not accounts left with a
|
||||
/// balance: a reversed cost is tagged and breaks even).
|
||||
pub fn of(contribution: Decimal, any: bool) -> Self {
|
||||
if contribution > Decimal::ZERO {
|
||||
Standing::Black
|
||||
} else if contribution < Decimal::ZERO {
|
||||
Standing::Red
|
||||
} else if any {
|
||||
Standing::Even
|
||||
} else {
|
||||
Standing::Nothing
|
||||
}
|
||||
}
|
||||
|
||||
/// Its words on a pill.
|
||||
pub fn words(self) -> &'static str {
|
||||
match self {
|
||||
Standing::Black => "In the black",
|
||||
Standing::Red => "In the red",
|
||||
Standing::Even => "Break-even",
|
||||
Standing::Nothing => "Nothing tagged yet",
|
||||
}
|
||||
}
|
||||
|
||||
/// Its colors: (text, fill), the palette's positive and negative.
|
||||
pub fn colors(self, p: &theme::Palette) -> (egui::Color32, egui::Color32) {
|
||||
match self {
|
||||
Standing::Black => (p.positive, p.positive_soft),
|
||||
Standing::Red => (p.negative, p.negative_soft),
|
||||
Standing::Even | Standing::Nothing => (p.muted, p.hover),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What a snapshot shows, all ledger-core's: to date
|
||||
/// (`project_to_date`) or for a period (`project_figures`).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Snap {
|
||||
/// Tagged revenue.
|
||||
pub revenue: Decimal,
|
||||
/// Tagged expenses: its direct costs.
|
||||
pub expenses: Decimal,
|
||||
/// Revenue less direct costs: its contribution before overhead.
|
||||
pub contribution: Decimal,
|
||||
/// Contribution as a percent of revenue (none without revenue).
|
||||
pub margin_pct: Option<Decimal>,
|
||||
/// Where it stands.
|
||||
pub standing: Standing,
|
||||
}
|
||||
|
||||
impl Snap {
|
||||
/// To date.
|
||||
pub fn to_date(t: &ledger_core::ProjectToDate) -> Self {
|
||||
let any = t.any_tagged();
|
||||
Snap {
|
||||
revenue: t.revenue,
|
||||
expenses: t.expenses,
|
||||
contribution: t.net(),
|
||||
margin_pct: t.margin_pct(),
|
||||
standing: Standing::of(t.net(), any),
|
||||
}
|
||||
}
|
||||
|
||||
/// For the figures' period.
|
||||
pub fn period(f: &ledger_core::ProjectFigures) -> Self {
|
||||
let pl = &f.period;
|
||||
let any = f.period_lines > 0;
|
||||
Snap {
|
||||
revenue: pl.total_revenue,
|
||||
expenses: pl.total_expenses,
|
||||
contribution: pl.net_income(),
|
||||
margin_pct: f.period_margin_pct(),
|
||||
standing: Standing::of(pl.net_income(), any),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The note on contribution: what it counts and what it leaves out.
|
||||
pub const CONTRIBUTION_NOTE: &str = "Revenue less this project's direct costs: every expense line tagged to it, fixed and variable alike (what management accounting calls a segment margin, not a variable-cost contribution margin). Company overhead that isn't tagged to a project (rent, software, insurance, the owner's time) isn't counted, so a project in the black can still leave the company in the red. A management-accounting view (rules.md B-37 item 7), not a GAAP figure.";
|
||||
|
||||
/// The snapshot: a header with where it stands to date and the to date /
|
||||
/// fiscal year switch, then its tiles (`shown`: to date, or for `year`,
|
||||
/// the fiscal year to date, when `fiscal`). Returns the switch's new
|
||||
/// setting (true: this fiscal year) when clicked.
|
||||
fn snapshot(
|
||||
ui: &mut Ui,
|
||||
b: &Books,
|
||||
kind: Option<ProjectKind>,
|
||||
to_date: Option<&ledger_core::ProjectToDate>,
|
||||
year: Option<(NaiveDate, NaiveDate)>,
|
||||
shown: Option<Snap>,
|
||||
fiscal: bool,
|
||||
) -> Option<bool> {
|
||||
let p = pal(ui.ctx());
|
||||
let mut switch = None;
|
||||
let year = year.filter(|_| fiscal);
|
||||
let span = match year {
|
||||
Some((s, e)) => format!(
|
||||
"Every posted line tagged to this project from {} through {} (the fiscal year to date), closing entries left out.",
|
||||
fmt::date(s),
|
||||
fmt::date(e)
|
||||
),
|
||||
None => format!(
|
||||
"Every posted line tagged to this project through {}, closing entries left out.",
|
||||
fmt::date(b.today)
|
||||
),
|
||||
};
|
||||
let when = match year {
|
||||
Some((s, _)) => format!("since {}", fmt::date(s)),
|
||||
None => format!("through {}", fmt::date(b.today)),
|
||||
};
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(RichText::new("Snapshot").font(theme::semibold(14.0)));
|
||||
// Where it stands is always to date, whichever view: the list's
|
||||
// cue too, and the page's to-date cards beside it agree.
|
||||
if let Some(t) = to_date.map(Snap::to_date) {
|
||||
let (fg, bg) = t.standing.colors(&p);
|
||||
widgets::pill(ui, t.standing.words(), fg, bg).on_hover_text(format!(
|
||||
"Contribution before overhead, to date (through {}): {}.",
|
||||
fmt::date(b.today),
|
||||
fmt::dollars(t.contribution)
|
||||
));
|
||||
}
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
ui.spacing_mut().item_spacing.x = 6.0;
|
||||
if chip(ui, "This fiscal year", fiscal) && !fiscal {
|
||||
switch = Some(true);
|
||||
}
|
||||
if chip(ui, "To date", !fiscal) && fiscal {
|
||||
switch = Some(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
ui.add_space(8.0);
|
||||
let money = |v: Option<Decimal>| fmt::dollars_or(v);
|
||||
let contribution_sub = shown.as_ref().map(|s| {
|
||||
let (fg, _) = s.standing.colors(&p);
|
||||
let text = match s.standing {
|
||||
Standing::Black | Standing::Red | Standing::Even => "before overhead",
|
||||
Standing::Nothing if fiscal => "nothing this fiscal year",
|
||||
Standing::Nothing => "nothing tagged yet",
|
||||
};
|
||||
(
|
||||
text.to_string(),
|
||||
if s.standing == Standing::Nothing {
|
||||
p.muted
|
||||
} else {
|
||||
fg
|
||||
},
|
||||
)
|
||||
});
|
||||
let mut tiles: Vec<Tile<'_>> = vec![
|
||||
(
|
||||
"Revenue",
|
||||
money(shown.as_ref().map(|s| s.revenue)),
|
||||
Some((when.clone(), p.muted)),
|
||||
format!("{span} Revenue lines."),
|
||||
shown.as_ref().map(|s| s.revenue),
|
||||
),
|
||||
(
|
||||
"Direct costs",
|
||||
money(shown.as_ref().map(|s| s.expenses)),
|
||||
Some((when.clone(), p.muted)),
|
||||
format!("{span} Expense lines: the costs tagged to it."),
|
||||
shown.as_ref().map(|s| s.expenses),
|
||||
),
|
||||
(
|
||||
"Contribution",
|
||||
money(shown.as_ref().map(|s| s.contribution)),
|
||||
contribution_sub,
|
||||
format!("{span} {CONTRIBUTION_NOTE}"),
|
||||
shown.as_ref().map(|s| s.contribution),
|
||||
),
|
||||
];
|
||||
if let Some(m) = shown.as_ref().and_then(|s| s.margin_pct) {
|
||||
tiles.push((
|
||||
"Contribution margin",
|
||||
format!("{m}%"),
|
||||
Some(("of revenue".to_string(), p.muted)),
|
||||
format!("{span} Contribution as a share of revenue, to one place. Shown only when there is revenue."),
|
||||
None,
|
||||
));
|
||||
}
|
||||
if kind == Some(ProjectKind::OnSpec)
|
||||
&& let Some(u) = to_date.and_then(|t| t.unrecovered_cost)
|
||||
{
|
||||
tiles.push((
|
||||
"Unrecovered cost",
|
||||
fmt::dollars(u),
|
||||
Some(("if repaid: not a receivable".into(), p.muted)),
|
||||
format!(
|
||||
"Every posted line tagged to this project through {}, whichever view: direct costs less revenue to date, never below zero, what it would take to be repaid in full. A hoped-for payment isn't income or a receivable until it arrives (ASC 450-30-25-1), so this is a figure, not something owed to the company.",
|
||||
fmt::date(b.today)
|
||||
),
|
||||
Some(u),
|
||||
));
|
||||
}
|
||||
let n = tiles.len();
|
||||
super::grid(ui, n, 170.0, 6, |i, ui| {
|
||||
let (label, value, sub, how, copy) = &tiles[i];
|
||||
let r = widgets::kpi(
|
||||
ui,
|
||||
widgets::Kpi {
|
||||
label,
|
||||
value: value.clone(),
|
||||
sub: sub.clone(),
|
||||
spark: None,
|
||||
how,
|
||||
},
|
||||
);
|
||||
if let Some(v) = copy {
|
||||
r.context_menu(|ui| widgets::copy_item(ui, "Copy amount", &v.to_string()));
|
||||
}
|
||||
});
|
||||
switch
|
||||
}
|
||||
|
||||
/// One project, completely.
|
||||
pub fn show_project(ui: &mut Ui, b: &Books, id: ClassId, shell: &mut Shell<'_>) {
|
||||
let p = pal(ui.ctx());
|
||||
@@ -308,9 +573,10 @@ pub fn show_project(ui: &mut Ui, b: &Books, id: ClassId, shell: &mut Shell<'_>)
|
||||
return;
|
||||
};
|
||||
let mut go: Option<Route> = None;
|
||||
let today = b.today;
|
||||
let to_date = ledger_core::project_to_date(&b.ledger, id, today).ok();
|
||||
let entries = ledger_core::project_entries(&b.ledger, id);
|
||||
let fiscal = shell.state.projects.fiscal;
|
||||
let figs = figures(ui.ctx(), b, id, f.started_on, fiscal);
|
||||
let to_date = figs.to_date.as_ref();
|
||||
let entries = &figs.entries;
|
||||
page(ui, "project", |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
if widgets::link(
|
||||
@@ -363,110 +629,37 @@ pub fn show_project(ui: &mut Ui, b: &Books, id: ClassId, shell: &mut Shell<'_>)
|
||||
}
|
||||
ui.add_space(14.0);
|
||||
|
||||
// What it earned and cost, to date.
|
||||
let span = format!(
|
||||
"Every posted line tagged to this project through {}, closing entries left out.",
|
||||
fmt::date(today)
|
||||
);
|
||||
let mut tiles: Vec<Tile> = vec![
|
||||
(
|
||||
"Income to date".into(),
|
||||
to_date.as_ref().map(|t| t.revenue),
|
||||
format!("{span} Revenue lines."),
|
||||
None,
|
||||
),
|
||||
(
|
||||
"Spending to date".into(),
|
||||
to_date.as_ref().map(|t| t.expenses),
|
||||
format!("{span} Expense lines."),
|
||||
None,
|
||||
),
|
||||
(
|
||||
"Net to date".into(),
|
||||
to_date.as_ref().map(|t| t.net()),
|
||||
format!("{span} Income less spending."),
|
||||
to_date.as_ref().map(|t| {
|
||||
let n = t.net();
|
||||
if n < Decimal::ZERO {
|
||||
("a loss so far".to_string(), p.negative)
|
||||
} else if n.is_zero() {
|
||||
("even".to_string(), p.muted)
|
||||
} else {
|
||||
("a profit so far".to_string(), p.positive)
|
||||
}
|
||||
}),
|
||||
),
|
||||
];
|
||||
if let Some(u) = to_date.as_ref().and_then(|t| t.unrecovered_cost) {
|
||||
tiles.push((
|
||||
"Unrecovered cost".into(),
|
||||
Some(u),
|
||||
format!(
|
||||
"{span} Spending less income, never below zero: what it would take to be repaid in full. A hoped-for payment isn't income or a receivable until it arrives (ASC 450-30-25-1), so this is a figure, not something owed to the company."
|
||||
),
|
||||
Some(("if repaid: not a receivable".into(), p.muted)),
|
||||
));
|
||||
// The snapshot: what it earned and cost, to date or this fiscal
|
||||
// year, before overhead.
|
||||
let shown = if fiscal {
|
||||
figs.year.as_ref().map(Snap::period)
|
||||
} else {
|
||||
to_date.map(Snap::to_date)
|
||||
};
|
||||
let year = figs.year.as_ref().map(|y| (y.period.start, y.period.end));
|
||||
if let Some(fy) = snapshot(ui, b, f.kind, to_date, year, shown, fiscal) {
|
||||
shell.state.projects.fiscal = fy;
|
||||
}
|
||||
if f.kind == Some(ProjectKind::Client)
|
||||
&& let Some(t) = &to_date
|
||||
&& !t.revenue.is_zero()
|
||||
{
|
||||
tiles.push((
|
||||
"Margin to date".into(),
|
||||
None,
|
||||
format!("{span} Net as a share of income."),
|
||||
None,
|
||||
));
|
||||
}
|
||||
let margin = to_date
|
||||
.as_ref()
|
||||
.and_then(|t| super::profit::pct(t.net(), t.revenue));
|
||||
let n = tiles.len();
|
||||
super::grid(ui, n, 170.0, 6, |i, ui| {
|
||||
let (label, value, how, sub) = &tiles[i];
|
||||
let text = if label == "Margin to date" {
|
||||
margin.map_or_else(|| "—".into(), |m| format!("{m}%"))
|
||||
} else {
|
||||
fmt::dollars_or(*value)
|
||||
};
|
||||
let r = widgets::kpi(
|
||||
ui,
|
||||
widgets::Kpi {
|
||||
label,
|
||||
value: text,
|
||||
sub: sub.clone(),
|
||||
spark: None,
|
||||
how,
|
||||
},
|
||||
);
|
||||
if let Some(v) = value {
|
||||
r.context_menu(|ui| widgets::copy_item(ui, "Copy amount", &v.to_string()));
|
||||
}
|
||||
});
|
||||
ui.add_space(2.0);
|
||||
|
||||
// By month and by account.
|
||||
let first = entries
|
||||
.iter()
|
||||
.filter_map(|(e, _)| b.ledger.get_entry(*e))
|
||||
.filter(|e| e.status() == ledger_core::EntryStatus::Posted)
|
||||
.map(|e| e.date())
|
||||
.min();
|
||||
super::columns(ui, 860.0, &[1.0, 1.0], |col, ui| {
|
||||
// By month and by category.
|
||||
super::columns(ui, 860.0, &[1.3, 1.0], |col, ui| {
|
||||
if col == 0 {
|
||||
months_card(ui, b, id, first);
|
||||
months_card(ui, &figs);
|
||||
} else {
|
||||
accounts_card(ui, b, id, &mut go);
|
||||
costs_card(ui, b, to_date, &mut go);
|
||||
}
|
||||
});
|
||||
super::columns(ui, 860.0, &[1.0, 1.0], |col, ui| {
|
||||
if col == 0 {
|
||||
facts_card(ui, b, id, &class.name, &f, &mut go);
|
||||
} else {
|
||||
accounts_card(ui, b, to_date, &mut go);
|
||||
ui.add_space(16.0);
|
||||
decisions_card(ui, b, id, &mut go);
|
||||
}
|
||||
});
|
||||
entries_card(ui, b, &entries, &mut go);
|
||||
entries_card(ui, b, entries, &mut go);
|
||||
ui.add_space(14.0);
|
||||
let here = [ledger_store::Subject::Project(u64::from(id))];
|
||||
super::documents::documents_card_with(
|
||||
@@ -503,21 +696,108 @@ pub fn show_project(ui: &mut Ui, b: &Books, id: ClassId, shell: &mut Shell<'_>)
|
||||
}
|
||||
}
|
||||
|
||||
/// Income, spending and net by month: from the month of its first posted
|
||||
/// tagged entry through this month, at most the last 24.
|
||||
fn months_card(ui: &mut Ui, b: &Books, id: ClassId, first: Option<NaiveDate>) {
|
||||
/// The most months the by-month chart shows: the latest.
|
||||
pub const CHART_MONTHS: usize = 24;
|
||||
|
||||
/// The months the by-month chart spans: from the month the project
|
||||
/// started, or of its first posted tagged entry if earlier, through
|
||||
/// today, the last [`CHART_MONTHS`] at most. None with neither date.
|
||||
pub fn chart_span(
|
||||
today: NaiveDate,
|
||||
started_on: Option<NaiveDate>,
|
||||
first_entry: Option<NaiveDate>,
|
||||
) -> Option<(NaiveDate, NaiveDate)> {
|
||||
let from = match (started_on, first_entry) {
|
||||
(Some(s), Some(e)) => s.min(e),
|
||||
(s, e) => s.or(e)?,
|
||||
};
|
||||
let start = ledger_core::analysis::interval_start(
|
||||
from.min(today),
|
||||
ledger_core::analysis::Interval::Month,
|
||||
);
|
||||
Some(Books::last_months((start, today), CHART_MONTHS as u32))
|
||||
}
|
||||
|
||||
/// A project page's figures, all ledger-core's, worked out once per load
|
||||
/// of the books (and the fiscal year's once it is asked for), not every
|
||||
/// frame: a hovered chart repaints continuously.
|
||||
#[derive(Clone)]
|
||||
pub struct PageFigures {
|
||||
/// Through today (`project_to_date`).
|
||||
pub to_date: Option<ledger_core::ProjectToDate>,
|
||||
/// Every entry tagged to it (`project_entries`).
|
||||
pub entries: Vec<(u64, Decimal)>,
|
||||
/// The by-month chart's span ([`chart_span`]) and figures, when
|
||||
/// anything tagged is posted.
|
||||
pub months: Option<((NaiveDate, NaiveDate), ledger_core::ProjectFigures)>,
|
||||
/// The fiscal year to date (B-30's omitted dates, as `books.project`),
|
||||
/// only when the snapshot shows it.
|
||||
pub year: Option<ledger_core::ProjectFigures>,
|
||||
}
|
||||
|
||||
/// [`PageFigures`] for project `id`, kept in egui's memory against the
|
||||
/// load of the books they came from (a reload, a new day or another
|
||||
/// project works them out again).
|
||||
pub fn figures(
|
||||
ctx: &egui::Context,
|
||||
b: &Books,
|
||||
id: ClassId,
|
||||
started_on: Option<NaiveDate>,
|
||||
fiscal: bool,
|
||||
) -> std::sync::Arc<PageFigures> {
|
||||
type Stamp = (chrono::DateTime<chrono::Utc>, String, NaiveDate, bool);
|
||||
let key = egui::Id::new(("project_figures", id));
|
||||
let stamp: Stamp = (b.loaded_at, b.head.clone(), b.today, fiscal);
|
||||
if let Some((was, figs)) = ctx.data(|d| d.get_temp::<(Stamp, std::sync::Arc<PageFigures>)>(key))
|
||||
&& was == stamp
|
||||
{
|
||||
return figs;
|
||||
}
|
||||
let figs = std::sync::Arc::new(page_figures(b, id, started_on, fiscal));
|
||||
ctx.data_mut(|d| d.insert_temp(key, (stamp, figs.clone())));
|
||||
figs
|
||||
}
|
||||
|
||||
/// [`PageFigures`], worked out.
|
||||
pub fn page_figures(
|
||||
b: &Books,
|
||||
id: ClassId,
|
||||
started_on: Option<NaiveDate>,
|
||||
fiscal: bool,
|
||||
) -> PageFigures {
|
||||
let to_date = ledger_core::project_to_date(&b.ledger, id, b.today).ok();
|
||||
let entries = ledger_core::project_entries(&b.ledger, id);
|
||||
let first = entries
|
||||
.iter()
|
||||
.filter_map(|(e, _)| b.ledger.get_entry(*e))
|
||||
.filter(|e| e.status() == ledger_core::EntryStatus::Posted)
|
||||
.map(|e| e.date())
|
||||
.min();
|
||||
// Nothing posted yet: no bars to draw, however long ago it started.
|
||||
let months = chart_span(b.today, started_on, first)
|
||||
.filter(|_| first.is_some())
|
||||
.and_then(|(s, e)| {
|
||||
let fig = ledger_core::project_figures(&b.ledger, id, s, e).ok()?;
|
||||
Some(((s, e), fig))
|
||||
});
|
||||
let year = fiscal
|
||||
.then(|| b.periods().statement(None, None).ok())
|
||||
.flatten()
|
||||
.and_then(|(s, e)| ledger_core::project_figures(&b.ledger, id, s, e).ok());
|
||||
PageFigures {
|
||||
to_date,
|
||||
entries,
|
||||
months,
|
||||
year,
|
||||
}
|
||||
}
|
||||
|
||||
/// Revenue and costs by month, with the net to date at each month's end:
|
||||
/// the trend toward or away from break-even.
|
||||
fn months_card(ui: &mut Ui, figs: &PageFigures) {
|
||||
let p = pal(ui.ctx());
|
||||
let span = first.map(|f| {
|
||||
let start = ledger_core::analysis::interval_start(
|
||||
f.min(b.today),
|
||||
ledger_core::analysis::Interval::Month,
|
||||
);
|
||||
crate::books::Books::last_months((start, b.today), 24)
|
||||
});
|
||||
let months = span
|
||||
.and_then(|(s, e)| ledger_core::project_figures(&b.ledger, id, s, e).ok())
|
||||
.map(|f| f.months)
|
||||
.unwrap_or_default();
|
||||
let span = figs.months.as_ref().map(|(span, _)| *span);
|
||||
let fig = figs.months.as_ref().map(|(_, fig)| fig);
|
||||
widgets::titled_card(
|
||||
ui,
|
||||
"By month",
|
||||
@@ -531,55 +811,143 @@ fn months_card(ui: &mut Ui, b: &Books, id: ClassId, first: Option<NaiveDate>) {
|
||||
}
|
||||
},
|
||||
|ui| {
|
||||
if months.is_empty() {
|
||||
let Some(fig) = fig.filter(|f| !f.months.is_empty()) else {
|
||||
widgets::empty(ui, icon::CHART_BAR, "No tagged lines yet", "");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let months = &fig.months;
|
||||
let multi_year =
|
||||
months.first().map(|m| m.start.year()) != months.last().map(|m| m.start.year());
|
||||
let groups: Vec<BarGroup> = months
|
||||
.iter()
|
||||
.map(|m| BarGroup {
|
||||
label: m.start.format("%b").to_string(),
|
||||
values: vec![m.revenue, m.expenses, m.net()],
|
||||
values: vec![m.revenue, m.expenses],
|
||||
year: multi_year.then(|| m.start.year()),
|
||||
})
|
||||
.collect();
|
||||
// Hover shows a month's figures; a click opens nothing, since
|
||||
// the reports are the whole company's, not the project's.
|
||||
charts::bar_chart_view(
|
||||
charts::bar_chart_view_with_line(
|
||||
ui,
|
||||
&groups,
|
||||
&[
|
||||
("Income", p.series[1]),
|
||||
("Spending", p.series[2]),
|
||||
("Net", p.series[0]),
|
||||
],
|
||||
&[("Revenue", p.series[1]), ("Direct costs", p.series[2])],
|
||||
&charts::Overlay {
|
||||
name: "Net to date",
|
||||
color: p.series[0],
|
||||
values: &fig.months_net_to_date,
|
||||
},
|
||||
200.0,
|
||||
);
|
||||
ui.add_space(4.0);
|
||||
ui.add(
|
||||
egui::Label::new(
|
||||
RichText::new(
|
||||
"Net to date: every tagged line through each month's end, before overhead; above $0 it is in the black.",
|
||||
)
|
||||
.font(theme::regular(11.5))
|
||||
.color(p.faint),
|
||||
)
|
||||
.wrap(),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Its direct costs by expense account to date, largest first; a click
|
||||
/// opens the account's register.
|
||||
fn costs_card(
|
||||
ui: &mut Ui,
|
||||
b: &Books,
|
||||
to_date: Option<&ledger_core::ProjectToDate>,
|
||||
go: &mut Option<Route>,
|
||||
) {
|
||||
let p = pal(ui.ctx());
|
||||
let mut lines: Vec<&ledger_core::StatementLine> = to_date
|
||||
.map(|t| t.expenses_by_account.iter().collect())
|
||||
.unwrap_or_default();
|
||||
// Largest first, then by code: the bars rank.
|
||||
lines.sort_by(|x, y| y.amount.cmp(&x.amount).then_with(|| x.code.cmp(&y.code)));
|
||||
widgets::titled_card(
|
||||
ui,
|
||||
"Costs by category, to date",
|
||||
|ui| {
|
||||
if let Some(t) = to_date.filter(|_| !lines.is_empty()) {
|
||||
ui.label(
|
||||
RichText::new(fmt::dollars(t.expenses))
|
||||
.font(theme::regular(12.0))
|
||||
.color(p.muted),
|
||||
);
|
||||
}
|
||||
},
|
||||
|ui| {
|
||||
if lines.is_empty() {
|
||||
// Costs tagged and cancelled out (a reversal, or repaid
|
||||
// exactly) leave no account, but they were tagged.
|
||||
if to_date.is_some_and(|t| t.expenses_lines > 0) {
|
||||
widgets::empty(
|
||||
ui,
|
||||
icon::CHART_BAR_HORIZONTAL,
|
||||
"Tagged costs net to zero",
|
||||
"Every cost tagged to it was reversed or repaid in full.",
|
||||
);
|
||||
} else {
|
||||
widgets::empty(ui, icon::CHART_BAR_HORIZONTAL, "No costs tagged", "");
|
||||
}
|
||||
return;
|
||||
}
|
||||
let rows: Vec<(String, Decimal)> = lines
|
||||
.iter()
|
||||
.map(|l| (format!("{} {}", l.code, l.name), l.amount))
|
||||
.collect();
|
||||
if let Some(i) = charts::hbars_signed(ui, &rows, p.series[2], p.positive)
|
||||
&& let Some(a) = b.ledger.chart().find_by_code(&lines[i].code)
|
||||
{
|
||||
*go = Some(Route::Account(a.id));
|
||||
}
|
||||
if lines.iter().any(|l| l.amount < Decimal::ZERO) {
|
||||
ui.add_space(4.0);
|
||||
ui.add(
|
||||
egui::Label::new(
|
||||
RichText::new(
|
||||
"Below zero: an account credited more than it cost (a reimbursement).",
|
||||
)
|
||||
.font(theme::regular(11.5))
|
||||
.color(p.faint),
|
||||
)
|
||||
.wrap(),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Its revenue then expense accounts to date, with a total.
|
||||
fn accounts_card(ui: &mut Ui, b: &Books, id: ClassId, go: &mut Option<Route>) {
|
||||
fn accounts_card(
|
||||
ui: &mut Ui,
|
||||
b: &Books,
|
||||
to_date: Option<&ledger_core::ProjectToDate>,
|
||||
go: &mut Option<Route>,
|
||||
) {
|
||||
let p = pal(ui.ctx());
|
||||
// The to-date statement alone: project_figures would build every
|
||||
// month's too, each frame.
|
||||
let pl = ledger_core::reports::profit_and_loss_for_period(
|
||||
&b.ledger,
|
||||
ledger_core::calendar::FIRST_DAY.min(b.today),
|
||||
b.today,
|
||||
Some(id),
|
||||
)
|
||||
.ok();
|
||||
widgets::titled_card(
|
||||
ui,
|
||||
"By account, to date",
|
||||
|_| {},
|
||||
|ui| {
|
||||
let Some(pl) = pl.filter(|pl| !pl.revenue.is_empty() || !pl.expenses.is_empty()) else {
|
||||
widgets::empty(ui, icon::TREE_STRUCTURE, "No income or spending tagged", "");
|
||||
let Some(t) = to_date
|
||||
.filter(|t| !t.revenue_by_account.is_empty() || !t.expenses_by_account.is_empty())
|
||||
else {
|
||||
if to_date.is_some_and(|t| t.any_tagged()) {
|
||||
widgets::empty(
|
||||
ui,
|
||||
icon::TREE_STRUCTURE,
|
||||
"Tagged lines net to zero",
|
||||
"Each account's tagged lines cancel out (a reversal, or a cost repaid in full): a contribution of 0.00.",
|
||||
);
|
||||
} else {
|
||||
widgets::empty(ui, icon::TREE_STRUCTURE, "No income or spending tagged", "");
|
||||
}
|
||||
return;
|
||||
};
|
||||
let amt = widgets::amount_col_w(ui.available_width(), 1, 160.0, 140.0);
|
||||
@@ -612,19 +980,19 @@ fn accounts_card(ui: &mut Ui, b: &Books, id: ClassId, go: &mut Option<Route>) {
|
||||
*go = Some(Route::Account(a.id));
|
||||
}
|
||||
};
|
||||
for l in &pl.revenue {
|
||||
for l in &t.revenue_by_account {
|
||||
line(ui, l, false);
|
||||
}
|
||||
for l in &pl.expenses {
|
||||
for l in &t.expenses_by_account {
|
||||
line(ui, l, true);
|
||||
}
|
||||
widgets::rule(ui);
|
||||
let net = pl.net_income();
|
||||
let net = t.net();
|
||||
row(
|
||||
ui,
|
||||
w,
|
||||
&[
|
||||
("Net".to_string(), 0.0, false, p.text),
|
||||
("Contribution".to_string(), 0.0, false, p.text),
|
||||
(
|
||||
fmt::accounting(net),
|
||||
amt,
|
||||
@@ -639,7 +1007,8 @@ fn accounts_card(ui: &mut Ui, b: &Books, id: ClassId, go: &mut Option<Route>) {
|
||||
None,
|
||||
true,
|
||||
false,
|
||||
);
|
||||
)
|
||||
.on_hover_text(CONTRIBUTION_NOTE);
|
||||
ui.add_space(4.0);
|
||||
ui.label(
|
||||
RichText::new(
|
||||
|
||||
@@ -22,18 +22,36 @@ use std::path::{Path, PathBuf};
|
||||
const SSN: &str = "620-37-1111";
|
||||
|
||||
/// This test binary's downloads folder: its own, never the human's.
|
||||
static DOWNLOADS: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
|
||||
|
||||
unsafe extern "C" {
|
||||
/// The C library's: runs `f` as the process exits, which libtest's
|
||||
/// main does pass or fail (it returns, or calls `exit`).
|
||||
safe fn atexit(f: extern "C" fn()) -> std::ffi::c_int;
|
||||
}
|
||||
|
||||
/// Removes the downloads folder as the test binary exits: its files hold
|
||||
/// (made-up) TINs, and every test's dir is removed when it ends (the
|
||||
/// justfile's sweep-test-tmp catches a run killed first: it is `wb-*`).
|
||||
extern "C" fn remove_downloads() {
|
||||
if let Some(d) = DOWNLOADS.get() {
|
||||
let _ = std::fs::remove_dir_all(d);
|
||||
}
|
||||
}
|
||||
|
||||
fn downloads() -> PathBuf {
|
||||
static DIR: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
|
||||
DIR.get_or_init(|| {
|
||||
let d = tempfile::Builder::new()
|
||||
.prefix("wb-gui-downloads-")
|
||||
.tempdir()
|
||||
.unwrap()
|
||||
.keep();
|
||||
ledger_gui::export::set_downloads_dir(d.clone());
|
||||
d
|
||||
})
|
||||
.clone()
|
||||
DOWNLOADS
|
||||
.get_or_init(|| {
|
||||
let d = tempfile::Builder::new()
|
||||
.prefix("wb-gui-downloads-")
|
||||
.tempdir()
|
||||
.unwrap()
|
||||
.keep();
|
||||
assert_eq!(atexit(remove_downloads), 0, "atexit refused");
|
||||
ledger_gui::export::set_downloads_dir(d.clone());
|
||||
d
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// A fresh folder in the downloads folder for one test (tests run side by
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# Seeds for failure cases proptest has generated in the past. It is
|
||||
# automatically read and these particular cases re-run before any
|
||||
# novel cases are generated.
|
||||
#
|
||||
# It is recommended to check this file in to source control so that
|
||||
# everyone who runs the test benefits from these saved cases.
|
||||
cc 9afb8bbc956b20e53a67ecdd13e018dc139152a98f0d94057929cfeea935e4e2 # shrinks to lines = [(0, 1, false, true, true)]
|
||||
@@ -11,7 +11,7 @@ use ledger_core::{AccountType, ClassId, Currency, Money, ProjectFacts, ProjectKi
|
||||
use ledger_gui::books::Books;
|
||||
use ledger_gui::nav::{Route, Section};
|
||||
use ledger_gui::pages::journal::describe;
|
||||
use ledger_gui::pages::projects::listed;
|
||||
use ledger_gui::pages::projects::{CONTRIBUTION_NOTE, Snap, Standing, chart_span, listed};
|
||||
use ledger_gui::{App, Source, data};
|
||||
use ledger_store::ops::ViewerTarget;
|
||||
use ledger_store::{Store, Subject};
|
||||
@@ -224,7 +224,8 @@ fn project_figures_are_ledger_cores() {
|
||||
assert_eq!(row.to_date, Some(t.clone()));
|
||||
let h = harness(&f, Route::Project(f.zeta));
|
||||
h.get_by_label("$5,000.00");
|
||||
h.get_by_label("$1,800.00");
|
||||
// Direct costs, and the costs card's total.
|
||||
assert_eq!(h.get_all_by_label("$1,800.00").count(), 2);
|
||||
h.get_by_label("$3,200.00");
|
||||
// By account to date: the same statement lines.
|
||||
let fig = ledger_core::project_figures(
|
||||
@@ -234,10 +235,21 @@ fn project_figures_are_ledger_cores() {
|
||||
today(),
|
||||
)
|
||||
.unwrap();
|
||||
for l in fig.period.revenue.iter().chain(&fig.period.expenses) {
|
||||
assert_eq!(fig.to_date.revenue_by_account, fig.period.revenue);
|
||||
assert_eq!(fig.to_date.expenses_by_account, fig.period.expenses);
|
||||
for l in &fig.period.revenue {
|
||||
h.get_by_role_and_label(Role::Button, &format!("{} {}", l.code, l.name));
|
||||
}
|
||||
h.get_by_label("Margin to date");
|
||||
// An expense account is a row and a bar on Costs by category.
|
||||
for l in &fig.period.expenses {
|
||||
let label = format!("{} {}", l.code, l.name);
|
||||
assert_eq!(
|
||||
h.get_all_by_role_and_label(Role::Button, &label).count(),
|
||||
2,
|
||||
"{label}"
|
||||
);
|
||||
}
|
||||
h.get_by_label("Contribution margin");
|
||||
h.get_by_label("64.0%");
|
||||
}
|
||||
|
||||
@@ -247,9 +259,10 @@ fn unrecovered_cost_shows_only_on_spec() {
|
||||
let h = harness(&f, Route::Project(f.alpha));
|
||||
h.get_by_label("Unrecovered cost");
|
||||
h.get_by_label("if repaid: not a receivable");
|
||||
// The devices (an asset) count nothing: spending and the unrecovered
|
||||
// cost are both the prototype's $300.00.
|
||||
assert_eq!(h.get_all_by_label("$300.00").count(), 2);
|
||||
// The devices (an asset) count nothing: direct costs, the costs
|
||||
// card's total and the unrecovered cost are all the prototype's
|
||||
// $300.00.
|
||||
assert_eq!(h.get_all_by_label("$300.00").count(), 3);
|
||||
let h = harness(&f, Route::Project(f.zeta));
|
||||
assert!(h.query_by_label("Unrecovered cost").is_none());
|
||||
}
|
||||
@@ -464,8 +477,8 @@ fn a_long_link_stays_in_its_card() {
|
||||
decisions.left()
|
||||
);
|
||||
}
|
||||
let net = h.get_by_label("Net to date").rect();
|
||||
assert!(net.right() <= w, "{w}: Net to date at {net:?}");
|
||||
let net = h.get_by_label("Contribution").rect();
|
||||
assert!(net.right() <= w, "{w}: Contribution at {net:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -478,9 +491,10 @@ fn a_long_name_keeps_the_page_in_the_window() {
|
||||
("H".repeat(200), "name-nospace"),
|
||||
] {
|
||||
let (_dir, h) = edge(tag, &name, "https://harbor.example", 1440.0, 2000.0);
|
||||
for tile in ["Income to date", "Net to date", "Margin to date"] {
|
||||
let r = h.get_by_label(tile).rect();
|
||||
assert!(r.right() <= 1440.0, "{tag}: {tile} at {r:?}");
|
||||
for tile in ["Revenue", "Contribution", "Contribution margin"] {
|
||||
for r in h.get_all_by_label(tile).map(|n| n.rect()) {
|
||||
assert!(r.right() <= 1440.0, "{tag}: {tile} at {r:?}");
|
||||
}
|
||||
}
|
||||
let decisions = h.get_by_label("Decisions").rect();
|
||||
assert!(
|
||||
@@ -588,3 +602,653 @@ fn a_long_party_name_keeps_to_its_card() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------- the snapshot (B-37 item 7)
|
||||
|
||||
/// Books holding one project of `kind`, started `started_on`, with
|
||||
/// `posts`: (date, debit code, credit code, cents), the line that isn't
|
||||
/// cash tagged to it. Codes: 1000 Cash, 4000 Design Services, 6000
|
||||
/// Contractors, 6100 Software. Rent (6200) is no project's.
|
||||
struct Mini {
|
||||
dir: TempDir,
|
||||
id: ClassId,
|
||||
tag: String,
|
||||
}
|
||||
|
||||
fn mini(
|
||||
tag: &str,
|
||||
kind: ProjectKind,
|
||||
started_on: Option<NaiveDate>,
|
||||
posts: &[(NaiveDate, &str, &str, i64)],
|
||||
) -> Mini {
|
||||
let dir = TempDir(std::env::temp_dir().join(format!(
|
||||
"ledger-gui-projects-snap-{tag}-{}",
|
||||
std::process::id()
|
||||
)));
|
||||
let _ = std::fs::remove_dir_all(&dir.0);
|
||||
let mut s = Store::create(&dir.0.join("books-data"), Currency::USD).unwrap();
|
||||
for (code, name, ty) in [
|
||||
("1000", "Cash", AccountType::Asset),
|
||||
("4000", "Design Services", AccountType::Revenue),
|
||||
("6000", "Contractors", AccountType::Expense),
|
||||
("6100", "Software", AccountType::Expense),
|
||||
("6200", "Rent", AccountType::Expense),
|
||||
] {
|
||||
s.add_account(code, name, ty, None, false).unwrap();
|
||||
}
|
||||
let id = s
|
||||
.add_project(
|
||||
"Kestrel",
|
||||
&ProjectFacts {
|
||||
kind: Some(kind),
|
||||
started_on,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let code = |s: &Store, c: &str| s.ledger().chart().find_by_code(c).unwrap().id;
|
||||
for &(date, dr, cr, c) in posts {
|
||||
let e = s.create_entry(date, "Line").unwrap();
|
||||
let (dr_id, cr_id) = (code(&s, dr), code(&s, cr));
|
||||
s.add_line(e, dr_id, Side::Debit, usd(c)).unwrap();
|
||||
s.add_line(e, cr_id, Side::Credit, usd(c)).unwrap();
|
||||
s.set_line_class(e, if dr == "1000" { 1 } else { 0 }, Some(id))
|
||||
.unwrap();
|
||||
s.post_entry(e).unwrap();
|
||||
}
|
||||
// Overhead, no project's: never in the snapshot.
|
||||
let e = s.create_entry(d(2026, 4, 30), "Rent").unwrap();
|
||||
let (rent, cash) = (code(&s, "6200"), code(&s, "1000"));
|
||||
s.add_line(e, rent, Side::Debit, usd(150_000)).unwrap();
|
||||
s.add_line(e, cash, Side::Credit, usd(150_000)).unwrap();
|
||||
s.post_entry(e).unwrap();
|
||||
drop(s);
|
||||
Mini {
|
||||
dir,
|
||||
id,
|
||||
tag: format!("snap-{tag}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn mini_harness(m: &Mini) -> Harness<'static, App> {
|
||||
let books = load(&m.dir);
|
||||
let id = m.id;
|
||||
let mut h = Harness::builder()
|
||||
.with_size(egui::vec2(1440.0, 2000.0))
|
||||
.build_eframe(move |cc| App::with_books(&cc.egui_ctx, books, false, Route::Project(id)));
|
||||
h.run_steps(3);
|
||||
shot(&mut h, &m.tag);
|
||||
h
|
||||
}
|
||||
|
||||
/// For eyes: SHOT_DIR keeps a picture of the page.
|
||||
fn shot(h: &mut Harness<'static, App>, tag: &str) {
|
||||
if let Ok(out) = std::env::var("SHOT_DIR") {
|
||||
let _ = std::fs::create_dir_all(&out);
|
||||
if let Ok(img) = h.render() {
|
||||
img.save(std::path::Path::new(&out).join(format!("{tag}.png")))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every text painted this frame.
|
||||
fn painted(h: &Harness<'static, App>) -> Vec<String> {
|
||||
painted_colored(h).into_iter().map(|(t, _)| t).collect()
|
||||
}
|
||||
|
||||
/// Every text painted this frame, with its color.
|
||||
fn painted_colored(h: &Harness<'static, App>) -> Vec<(String, egui::Color32)> {
|
||||
fn walk(s: &egui::Shape, out: &mut Vec<(String, egui::Color32)>) {
|
||||
match s {
|
||||
egui::Shape::Text(t) => {
|
||||
let color = t
|
||||
.galley
|
||||
.job
|
||||
.sections
|
||||
.first()
|
||||
.map_or(t.fallback_color, |s| s.format.color);
|
||||
let color = t.override_text_color.unwrap_or(color);
|
||||
out.push((t.galley.text().to_string(), color));
|
||||
}
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, out)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
for c in &h.output().shapes {
|
||||
walk(&c.shape, &mut out);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The value a snapshot tile labelled `label` shows: the label directly
|
||||
/// under that tile's own (the same left edge, just below), so a figure
|
||||
/// elsewhere on the page can't stand in for it. Exactly one such tile.
|
||||
fn tile(h: &Harness<'static, App>, label: &str) -> String {
|
||||
let nodes: Vec<(egui::Rect, String)> = h
|
||||
.query_all(egui_kittest::kittest::By::new().role(Role::Label))
|
||||
.filter_map(|n| Some((n.rect(), n.value()?)))
|
||||
.collect();
|
||||
let found: Vec<String> = nodes
|
||||
.iter()
|
||||
.filter(|(_, v)| v == label)
|
||||
.filter_map(|(r, _)| {
|
||||
nodes
|
||||
.iter()
|
||||
.filter(|(x, _)| {
|
||||
(x.left() - r.left()).abs() < 1.0
|
||||
&& x.top() >= r.bottom() - 1.0
|
||||
&& x.top() <= r.bottom() + 12.0
|
||||
})
|
||||
.min_by(|a, b| a.0.top().total_cmp(&b.0.top()))
|
||||
.map(|(_, v)| v.clone())
|
||||
})
|
||||
.filter(|v| v.contains('$') || v.ends_with('%'))
|
||||
.collect();
|
||||
assert_eq!(found.len(), 1, "one tile {label}: {found:?}");
|
||||
found[0].clone()
|
||||
}
|
||||
|
||||
/// The tiles' figures are ledger-core's, the same books.project returns
|
||||
/// (to_date's revenue, expenses, net and margin_pct; its tests check the
|
||||
/// server's side, and ledger-cli's projects_parity both on one book).
|
||||
fn assert_tiles(h: &Harness<'static, App>, s: &Snap) {
|
||||
use ledger_gui::fmt::dollars;
|
||||
assert_eq!(tile(h, "Revenue"), dollars(s.revenue), "revenue");
|
||||
assert_eq!(tile(h, "Direct costs"), dollars(s.expenses), "direct costs");
|
||||
assert_eq!(
|
||||
tile(h, "Contribution"),
|
||||
dollars(s.contribution),
|
||||
"contribution"
|
||||
);
|
||||
match s.margin_pct {
|
||||
Some(m) => assert_eq!(tile(h, "Contribution margin"), format!("{m}%")),
|
||||
None => assert!(h.query_by_label("Contribution margin").is_none()),
|
||||
}
|
||||
}
|
||||
|
||||
/// [`assert_tiles`], and the pill says where it stands (to date, whichever
|
||||
/// view: `standing`).
|
||||
fn assert_snapshot(h: &Harness<'static, App>, s: &Snap, standing: Standing) {
|
||||
assert_tiles(h, s);
|
||||
h.get_by_label(standing.words());
|
||||
}
|
||||
|
||||
/// The tooltip shown after hovering `at` a while: every label containing
|
||||
/// `text` once it shows.
|
||||
fn hover_shows(h: &mut Harness<'static, App>, at: egui::Pos2, text: &str) -> bool {
|
||||
h.hover_at(at);
|
||||
for _ in 0..60 {
|
||||
h.run_steps(1);
|
||||
if h.query_all_by_label_contains(text).next().is_some()
|
||||
|| painted(h).iter().any(|t| t.contains(text))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_profitable_projects_snapshot_is_ledger_cores() {
|
||||
let f = fixture("snap-profit");
|
||||
let t = ledger_core::project_to_date(&f.books.ledger, f.zeta, today()).unwrap();
|
||||
let s = Snap::to_date(&t);
|
||||
assert_eq!(
|
||||
s,
|
||||
Snap {
|
||||
revenue: cents(500_000),
|
||||
expenses: cents(180_000),
|
||||
contribution: cents(320_000),
|
||||
margin_pct: Some(Decimal::new(640, 1)),
|
||||
standing: Standing::Black,
|
||||
}
|
||||
);
|
||||
assert_eq!(s.contribution, t.net());
|
||||
let mut h = harness(&f, Route::Project(f.zeta));
|
||||
shot(&mut h, "snap-profit");
|
||||
assert_snapshot(&h, &s, s.standing);
|
||||
h.get_by_label("before overhead");
|
||||
assert!(h.query_all_by_label("Snapshot").next().is_some());
|
||||
// The Contribution tile says what it leaves out, on hover.
|
||||
let at = h
|
||||
.query_all_by_label("Contribution")
|
||||
.map(|n| n.rect())
|
||||
.min_by(|a, b| a.top().total_cmp(&b.top()))
|
||||
.unwrap()
|
||||
.center();
|
||||
assert!(
|
||||
hover_shows(&mut h, at, CONTRIBUTION_NOTE),
|
||||
"the Contribution tile's hover note"
|
||||
);
|
||||
// Costs by category: its one expense account, a bar that opens it.
|
||||
h.get_by_label("Costs by category, to date");
|
||||
assert_eq!(
|
||||
h.get_all_by_role_and_label(Role::Button, "6000 Contractors")
|
||||
.count(),
|
||||
2,
|
||||
"a bar and By account's row"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_loss_making_project_is_in_the_red() {
|
||||
let m = mini(
|
||||
"loss",
|
||||
ProjectKind::Client,
|
||||
None,
|
||||
&[
|
||||
(d(2026, 3, 2), "1000", "4000", 20_000),
|
||||
(d(2026, 3, 9), "6000", "1000", 10_000),
|
||||
(d(2026, 4, 9), "6100", "1000", 50_000),
|
||||
],
|
||||
);
|
||||
let b = load(&m.dir);
|
||||
let t = ledger_core::project_to_date(&b.ledger, m.id, today()).unwrap();
|
||||
let s = Snap::to_date(&t);
|
||||
assert_eq!(s.contribution, cents(-40_000));
|
||||
assert_eq!(s.standing, Standing::Red);
|
||||
assert_eq!(s.margin_pct, Some(Decimal::new(-2000, 1)));
|
||||
let h = mini_harness(&m);
|
||||
assert_snapshot(&h, &s, Standing::Red);
|
||||
// Rent, overhead, is no part of it.
|
||||
assert!(h.query_by_label("$1,500.00").is_none());
|
||||
// Both cost accounts, largest first: the costs card's bars (the
|
||||
// first buttons so named; By account has the others, by code), so
|
||||
// Software's $500 is above Contractors' $100 though its code is after.
|
||||
let first = |label: &str| {
|
||||
h.get_all_by_role_and_label(Role::Button, label)
|
||||
.next()
|
||||
.unwrap()
|
||||
.rect()
|
||||
};
|
||||
assert!(first("6100 Software").top() < first("6000 Contractors").top());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_on_spec_projects_snapshot_has_its_unrecovered_cost() {
|
||||
let f = fixture("snap-spec");
|
||||
let t = ledger_core::project_to_date(&f.books.ledger, f.alpha, today()).unwrap();
|
||||
let s = Snap::to_date(&t);
|
||||
assert_eq!(s.standing, Standing::Red);
|
||||
assert_eq!(t.unrecovered_cost, Some(cents(30_000)));
|
||||
let mut h = harness(&f, Route::Project(f.alpha));
|
||||
shot(&mut h, "snap-spec");
|
||||
assert_snapshot(&h, &s, Standing::Red);
|
||||
h.get_by_label("Unrecovered cost");
|
||||
h.get_by_label("if repaid: not a receivable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_revenue_means_no_margin() {
|
||||
let m = mini(
|
||||
"no-revenue",
|
||||
ProjectKind::Internal,
|
||||
None,
|
||||
&[(d(2026, 5, 1), "6100", "1000", 9_900)],
|
||||
);
|
||||
let h = mini_harness(&m);
|
||||
assert!(h.query_by_label("Contribution margin").is_none());
|
||||
h.get_by_label("In the red");
|
||||
h.get_by_label("−$99.00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_project_with_no_entries_has_nothing_tagged_yet() {
|
||||
let m = mini("empty", ProjectKind::Client, None, &[]);
|
||||
let b = load(&m.dir);
|
||||
let t = ledger_core::project_to_date(&b.ledger, m.id, today()).unwrap();
|
||||
assert_eq!(Snap::to_date(&t).standing, Standing::Nothing);
|
||||
assert_eq!(chart_span(today(), None, None), None);
|
||||
let h = mini_harness(&m);
|
||||
h.get_by_label("Nothing tagged yet");
|
||||
h.get_by_label("nothing tagged yet");
|
||||
h.get_by_label("No tagged lines yet");
|
||||
h.get_by_label("No costs tagged");
|
||||
assert!(h.query_by_label("Contribution margin").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_reimbursement_above_its_cost_leaves_costs_below_zero() {
|
||||
let m = mini(
|
||||
"reimbursed",
|
||||
ProjectKind::Client,
|
||||
None,
|
||||
&[
|
||||
(d(2026, 2, 9), "6000", "1000", 20_000),
|
||||
// The client repays the contractor and more, credited to it.
|
||||
(d(2026, 3, 1), "1000", "6000", 30_000),
|
||||
],
|
||||
);
|
||||
let b = load(&m.dir);
|
||||
let t = ledger_core::project_to_date(&b.ledger, m.id, today()).unwrap();
|
||||
assert_eq!(t.expenses, cents(-10_000));
|
||||
assert_eq!(t.expenses_by_account[0].amount, cents(-10_000));
|
||||
let s = Snap::to_date(&t);
|
||||
assert_eq!(
|
||||
(s.contribution, s.standing),
|
||||
(cents(10_000), Standing::Black)
|
||||
);
|
||||
assert_eq!(s.margin_pct, None, "no revenue");
|
||||
let h = mini_harness(&m);
|
||||
// Direct costs, and the costs card's total.
|
||||
assert_eq!(h.get_all_by_label("−$100.00").count(), 2);
|
||||
h.get_by_label("Below zero: an account credited more than it cost (a reimbursement).");
|
||||
assert_eq!(
|
||||
h.get_all_by_role_and_label(Role::Button, "6000 Contractors")
|
||||
.count(),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_project_started_this_month_charts_one_month() {
|
||||
let m = mini(
|
||||
"this-month",
|
||||
ProjectKind::Client,
|
||||
Some(d(2026, 10, 1)),
|
||||
&[(d(2026, 10, 5), "1000", "4000", 50_000)],
|
||||
);
|
||||
let b = load(&m.dir);
|
||||
assert_eq!(
|
||||
chart_span(today(), Some(d(2026, 10, 1)), Some(d(2026, 10, 5))),
|
||||
Some((d(2026, 10, 1), today()))
|
||||
);
|
||||
let f = ledger_core::project_figures(&b.ledger, m.id, d(2026, 10, 1), today()).unwrap();
|
||||
assert_eq!(f.months.len(), 1);
|
||||
assert_eq!(f.months_net_to_date, vec![cents(50_000)]);
|
||||
let h = mini_harness(&m);
|
||||
h.get_by_label("since Oct 2026");
|
||||
h.get_by_label("In the black");
|
||||
h.get_by_label("100.0%");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_chart_runs_from_the_start_or_first_entry_and_caps() {
|
||||
// The earlier of the two, from its month's first day.
|
||||
assert_eq!(
|
||||
chart_span(today(), Some(d(2026, 6, 20)), Some(d(2026, 3, 9))),
|
||||
Some((d(2026, 3, 1), today()))
|
||||
);
|
||||
assert_eq!(
|
||||
chart_span(today(), Some(d(2026, 2, 2)), Some(d(2026, 3, 9))),
|
||||
Some((d(2026, 2, 1), today()))
|
||||
);
|
||||
assert_eq!(
|
||||
chart_span(today(), Some(d(2026, 2, 2)), None),
|
||||
Some((d(2026, 2, 1), today()))
|
||||
);
|
||||
// A start after today charts only this month.
|
||||
assert_eq!(
|
||||
chart_span(today(), Some(d(2027, 1, 1)), None),
|
||||
Some((d(2026, 10, 1), today()))
|
||||
);
|
||||
// Ten years back: the last 24 months.
|
||||
assert_eq!(
|
||||
chart_span(today(), Some(d(2016, 1, 1)), None),
|
||||
Some((d(2024, 11, 1), today()))
|
||||
);
|
||||
}
|
||||
|
||||
/// The fiscal year switch shows books.project's default period (B-30's
|
||||
/// omitted dates: the fiscal year to date), and switching writes nothing.
|
||||
#[test]
|
||||
fn the_fiscal_year_switch_shows_the_default_period() {
|
||||
let m = mini(
|
||||
"fiscal",
|
||||
ProjectKind::Client,
|
||||
None,
|
||||
&[
|
||||
(d(2025, 11, 3), "1000", "4000", 100_000),
|
||||
(d(2025, 12, 1), "6100", "1000", 25_000),
|
||||
(d(2026, 2, 9), "6000", "1000", 40_000),
|
||||
],
|
||||
);
|
||||
let b = load(&m.dir);
|
||||
let (start, end) = b.periods().statement(None, None).unwrap();
|
||||
assert!(
|
||||
start > d(2025, 12, 1),
|
||||
"the sale and the software are before this fiscal year"
|
||||
);
|
||||
let fy = Snap::period(&ledger_core::project_figures(&b.ledger, m.id, start, end).unwrap());
|
||||
let all = Snap::to_date(&ledger_core::project_to_date(&b.ledger, m.id, today()).unwrap());
|
||||
assert_eq!(
|
||||
(all.expenses, all.contribution, all.standing),
|
||||
(cents(65_000), cents(35_000), Standing::Black)
|
||||
);
|
||||
assert_eq!(
|
||||
(fy.revenue, fy.expenses, fy.contribution, fy.standing),
|
||||
(Decimal::ZERO, cents(40_000), cents(-40_000), Standing::Red)
|
||||
);
|
||||
let journal = std::fs::read(m.dir.0.join("books-data").join("journal.jsonl")).unwrap();
|
||||
let mut h = mini_harness(&m);
|
||||
assert_snapshot(&h, &all, Standing::Black);
|
||||
h.get_by_label("This fiscal year").click();
|
||||
h.run_steps(3);
|
||||
assert!(h.state_mut().state_mut().projects.fiscal);
|
||||
shot(&mut h, "snap-fiscal");
|
||||
// The tiles are the fiscal year's; where it stands is still to date,
|
||||
// as the to-date cards beside it and the list say.
|
||||
assert_snapshot(&h, &fy, Standing::Black);
|
||||
assert!(h.query_by_label("In the red").is_none());
|
||||
assert_eq!(
|
||||
h.query_all_by_label(&format!("since {}", ledger_gui::fmt::date(start)))
|
||||
.count(),
|
||||
2,
|
||||
"Revenue's and Direct costs' span"
|
||||
);
|
||||
h.get_by_label("To date").click();
|
||||
h.run_steps(3);
|
||||
assert!(!h.state_mut().state_mut().projects.fiscal);
|
||||
assert_snapshot(&h, &all, Standing::Black);
|
||||
assert_eq!(
|
||||
std::fs::read(m.dir.0.join("books-data").join("journal.jsonl")).unwrap(),
|
||||
journal,
|
||||
"the viewer wrote nothing"
|
||||
);
|
||||
}
|
||||
|
||||
/// The list colors each net by its sign and says where it stands.
|
||||
#[test]
|
||||
fn the_list_cues_in_the_black_and_in_the_red() {
|
||||
let f = fixture("list-cue");
|
||||
let rows = listed(&f.books);
|
||||
let standing = |id| {
|
||||
rows.iter()
|
||||
.find(|r| r.id == id)
|
||||
.and_then(|r| r.to_date.as_ref())
|
||||
.map(|t| Snap::to_date(t).standing)
|
||||
};
|
||||
assert_eq!(standing(f.zeta), Some(Standing::Black));
|
||||
assert_eq!(standing(f.alpha), Some(Standing::Red));
|
||||
assert_eq!(standing(f.beta), Some(Standing::Nothing));
|
||||
let mut h = harness(&f, Route::Projects);
|
||||
shot(&mut h, "snap-list");
|
||||
let texts = painted_colored(&h);
|
||||
let color = |t: &str| {
|
||||
texts
|
||||
.iter()
|
||||
.find(|(x, _)| x.ends_with(t))
|
||||
.map(|(_, c)| *c)
|
||||
.unwrap_or_else(|| panic!("{t} painted: {texts:?}"))
|
||||
};
|
||||
// Each net in its standing's color, as its cue: never the names'.
|
||||
let (black, red, name) = (color("3,200.00"), color("(300.00)"), color("Zeta site"));
|
||||
assert_eq!(black, color("in the black"));
|
||||
assert_eq!(red, color("in the red"));
|
||||
assert!(
|
||||
black != name && red != name && black != red,
|
||||
"{black:?} {red:?} {name:?}"
|
||||
);
|
||||
// Alphabetical still.
|
||||
let names: Vec<&str> = rows.iter().map(|r| r.name.as_str()).collect();
|
||||
assert_eq!(names, ["alpha app", "Beta", "Zeta site"]);
|
||||
}
|
||||
|
||||
/// A cost tagged and then repaid exactly (or reversed) leaves no account
|
||||
/// with a balance, but it was tagged: break-even, never "nothing tagged".
|
||||
#[test]
|
||||
fn a_cost_repaid_exactly_breaks_even() {
|
||||
let m = mini(
|
||||
"even",
|
||||
ProjectKind::Client,
|
||||
None,
|
||||
&[
|
||||
(d(2026, 3, 9), "6000", "1000", 40_000),
|
||||
(d(2026, 4, 1), "1000", "6000", 40_000),
|
||||
],
|
||||
);
|
||||
let b = load(&m.dir);
|
||||
let t = ledger_core::project_to_date(&b.ledger, m.id, today()).unwrap();
|
||||
assert!(t.expenses_by_account.is_empty(), "the account nets to zero");
|
||||
assert_eq!((t.revenue_lines, t.expenses_lines), (0, 2));
|
||||
let s = Snap::to_date(&t);
|
||||
assert_eq!(
|
||||
(s.contribution, s.standing),
|
||||
(Decimal::ZERO, Standing::Even)
|
||||
);
|
||||
let mut h = mini_harness(&m);
|
||||
assert_snapshot(&h, &s, Standing::Even);
|
||||
h.get_by_label("before overhead");
|
||||
h.get_by_label("Tagged costs net to zero");
|
||||
h.get_by_label("Tagged lines net to zero");
|
||||
assert!(h.query_by_label("Nothing tagged yet").is_none());
|
||||
assert!(h.query_by_label("No costs tagged").is_none());
|
||||
// The list: 0.00 and its cue, not a dash.
|
||||
h.state_mut().goto(Route::Projects);
|
||||
h.run_steps(3);
|
||||
let texts = painted(&h);
|
||||
assert!(texts.iter().any(|t| t.ends_with("break-even")), "{texts:?}");
|
||||
assert!(texts.iter().any(|t| t == "0.00"), "{texts:?}");
|
||||
}
|
||||
|
||||
/// Lines only in earlier years: this fiscal year has nothing, said so,
|
||||
/// while the pill keeps the standing to date.
|
||||
#[test]
|
||||
fn a_fiscal_year_with_nothing_says_so() {
|
||||
let m = mini(
|
||||
"fiscal-empty",
|
||||
ProjectKind::Client,
|
||||
None,
|
||||
&[(d(2025, 6, 3), "1000", "4000", 70_000)],
|
||||
);
|
||||
let mut h = mini_harness(&m);
|
||||
h.get_by_label("This fiscal year").click();
|
||||
h.run_steps(3);
|
||||
shot(&mut h, "snap-fiscal-empty");
|
||||
h.get_by_label("nothing this fiscal year");
|
||||
h.get_by_label("In the black");
|
||||
assert!(h.query_by_label("Nothing tagged yet").is_none());
|
||||
}
|
||||
|
||||
/// The by-month chart's running net is ledger-core's months_net_to_date:
|
||||
/// hovering each month shows its "Net to date", in the chart's own strip.
|
||||
#[test]
|
||||
fn the_running_net_is_each_months_net_to_date() {
|
||||
let m = mini(
|
||||
"running",
|
||||
ProjectKind::Client,
|
||||
None,
|
||||
&[
|
||||
(d(2026, 3, 2), "1000", "4000", 120_000),
|
||||
(d(2026, 3, 9), "6000", "1000", 10_000),
|
||||
(d(2026, 5, 9), "6100", "1000", 30_000),
|
||||
(d(2026, 8, 1), "1000", "4000", 45_000),
|
||||
],
|
||||
);
|
||||
let b = load(&m.dir);
|
||||
let (s, e) = chart_span(today(), None, Some(d(2026, 3, 2))).unwrap();
|
||||
let f = ledger_core::project_figures(&b.ledger, m.id, s, e).unwrap();
|
||||
assert_eq!(f.months.len(), 8);
|
||||
// In month order, a month repeating the one before it (nothing new
|
||||
// that month) told once: what sweeping the pointer across shows.
|
||||
let mut want: Vec<String> = f
|
||||
.months_net_to_date
|
||||
.iter()
|
||||
.map(|v| format!("Net to date: {}", ledger_gui::fmt::dollars(*v)))
|
||||
.collect();
|
||||
want.dedup();
|
||||
assert_eq!(want.len(), 3, "{want:?}");
|
||||
let mut h = mini_harness(&m);
|
||||
let title = h.get_by_label("By month").rect();
|
||||
let right = h.get_by_label("Costs by category, to date").rect().left();
|
||||
// Over the bars, and over the strip under them.
|
||||
for dy in [100.0, 230.0] {
|
||||
let mut seen = Vec::new();
|
||||
let mut x = title.left();
|
||||
while x < right {
|
||||
h.hover_at(egui::pos2(x, title.bottom() + dy));
|
||||
h.run_steps(2);
|
||||
for t in painted(&h) {
|
||||
seen.extend(
|
||||
t.lines()
|
||||
.filter(|l| {
|
||||
l.starts_with("Net to date: $") || l.starts_with("Net to date: −$")
|
||||
})
|
||||
.map(str::to_string),
|
||||
);
|
||||
}
|
||||
x += 12.0;
|
||||
}
|
||||
seen.dedup();
|
||||
assert_eq!(seen, want, "at {dy}");
|
||||
}
|
||||
shot(&mut h, "snap-running");
|
||||
}
|
||||
|
||||
proptest::proptest! {
|
||||
#![proptest_config(proptest::prelude::ProptestConfig::with_cases(64))]
|
||||
|
||||
/// The snapshot's contribution is project_to_date's net, whatever the
|
||||
/// lines (refunds, reimbursements), and its standing is the net's sign.
|
||||
#[test]
|
||||
fn prop_snapshot_contribution_is_the_net_to_date(
|
||||
lines in proptest::collection::vec((0usize..3, 1i64..500_000, proptest::bool::ANY, proptest::bool::ANY, proptest::bool::ANY), 0..12),
|
||||
) {
|
||||
let mut l = ledger_core::Ledger::new(Currency::USD);
|
||||
let cash = l.add_account("1000", "Cash", AccountType::Asset, None, false).unwrap();
|
||||
let accts = [
|
||||
l.add_account("4000", "Sales", AccountType::Revenue, None, false).unwrap(),
|
||||
l.add_account("6000", "Costs", AccountType::Expense, None, false).unwrap(),
|
||||
l.add_account("6100", "Tools", AccountType::Expense, None, false).unwrap(),
|
||||
];
|
||||
let k = l.add_class("K").unwrap();
|
||||
let mut tagged = 0usize;
|
||||
for (a, c, debit, tag, reverse) in lines {
|
||||
let e = l.create_entry(d(2026, 3, 1), "e");
|
||||
let (dr, cr) = if debit { (accts[a], cash) } else { (cash, accts[a]) };
|
||||
l.add_line(e, dr, Side::Debit, usd(c)).unwrap();
|
||||
l.add_line(e, cr, Side::Credit, usd(c)).unwrap();
|
||||
if tag {
|
||||
l.set_line_class(e, if debit { 0 } else { 1 }, Some(k)).unwrap();
|
||||
tagged += 1;
|
||||
}
|
||||
l.post_entry(e).unwrap();
|
||||
// A reversal: its tagged line too, netting the account to
|
||||
// zero (it was tagged all the same).
|
||||
if reverse {
|
||||
l.reverse_entry(e, d(2026, 3, 2), "undo").unwrap();
|
||||
if tag {
|
||||
tagged += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
let t = ledger_core::project_to_date(&l, k, today()).unwrap();
|
||||
proptest::prop_assert_eq!(t.revenue_lines + t.expenses_lines, tagged);
|
||||
let s = Snap::to_date(&t);
|
||||
proptest::prop_assert_eq!(s.contribution, t.net());
|
||||
proptest::prop_assert_eq!(s.revenue - s.expenses, s.contribution);
|
||||
proptest::prop_assert_eq!(s.margin_pct, t.margin_pct());
|
||||
let want = if t.net() > Decimal::ZERO {
|
||||
Standing::Black
|
||||
} else if t.net() < Decimal::ZERO {
|
||||
Standing::Red
|
||||
} else if tagged == 0 {
|
||||
Standing::Nothing
|
||||
} else {
|
||||
Standing::Even
|
||||
};
|
||||
proptest::prop_assert_eq!(s.standing, want);
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,7 @@ fn screenshots() {
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or_else(|| project_named("Tidepool app"));
|
||||
let project_client = project_named("Client work");
|
||||
let project_templates = project_named("Templates");
|
||||
drop(loaded);
|
||||
let routes: Vec<(&str, Route)> = vec![
|
||||
("overview", Route::Overview),
|
||||
@@ -141,6 +142,7 @@ fn screenshots() {
|
||||
("projects", Route::Projects),
|
||||
("project", Route::Project(project)),
|
||||
("project-client", Route::Project(project_client)),
|
||||
("project-templates", Route::Project(project_templates)),
|
||||
("document", Route::Document(last_doc)),
|
||||
("document-linked", Route::Document(linked_doc)),
|
||||
("reconciliation", Route::Reconciliation(reconciliation)),
|
||||
|
||||
Reference in new issue
Block a user