215 lines
11 KiB
Rust
215 lines
11 KiB
Rust
//! A project's numbers: spent by line and unplanned, payments by hand and by rule within the project's dates,
|
|
//! income without VAT and never twice, profit and margin, progress by estimates or by count, the forecast, late,
|
|
//! time by task and week, billed and unbilled, and the two project medals.
|
|
|
|
use fiscus_insight::projects::{project, projects};
|
|
use fiscus_insight::rewards::{rewards, Medal};
|
|
use fiscus_insight::subscriptions::analyse;
|
|
use fiscus_ledger::goal::Colour;
|
|
use fiscus_ledger::invoice::{Invoice, InvoiceLine, Language, Party, Supplier};
|
|
use fiscus_ledger::project::{Kind, Status, TaskFields, TaskStatus};
|
|
use fiscus_ledger::{Date, Event, EventBody, Haler, State, Timestamp};
|
|
use std::collections::BTreeSet;
|
|
|
|
fn d(s: &str) -> Date {
|
|
s.parse().unwrap()
|
|
}
|
|
|
|
fn ledger(bodies: Vec<EventBody>) -> State {
|
|
let log: Vec<Event> = bodies.into_iter().enumerate().map(|(i, b)| Event::new(format!("e{i}"), Timestamp(0), b)).collect();
|
|
let s = State::replay(&log);
|
|
assert!(s.rejected.is_empty(), "{:?}", s.rejected);
|
|
s
|
|
}
|
|
|
|
fn made(id: &str, kind: Kind, starts: &str, ends: &str) -> EventBody {
|
|
EventBody::ProjectCreated { project: id.into(), name: format!("Project {id}"), kind, template: None, starts: Some(d(starts)), ends: Some(d(ends)), colour: Colour::Blue, rate: Some(Haler(80_000)) }
|
|
}
|
|
|
|
fn line(project: &str, id: &str, planned: i64) -> EventBody {
|
|
EventBody::ProjectLineSet { project: project.into(), line: id.into(), name: id.to_uppercase(), planned: Haler(planned) }
|
|
}
|
|
|
|
fn paid(id: &str, amount: i64, date: &str, merchant: &str, category: Option<&str>) -> EventBody {
|
|
EventBody::PaymentAdded { payment: id.into(), amount: Haler(amount), date: d(date), merchant: merchant.into(), category: category.map(str::to_string), note: None }
|
|
}
|
|
|
|
fn into(payment: &str, project: &str, line: Option<&str>) -> EventBody {
|
|
EventBody::PaymentProjected { payment: payment.into(), project: project.into(), line: line.map(str::to_string) }
|
|
}
|
|
|
|
fn task(id: &str, project: &str, due: Option<&str>, estimate: Option<i64>) -> EventBody {
|
|
EventBody::TaskCreated {
|
|
task: id.into(),
|
|
project: project.into(),
|
|
fields: TaskFields { title: format!("Task {id}"), phase: None, due: due.map(d), estimate: estimate.map(Haler), line: None, assignee: None, milestone: false },
|
|
}
|
|
}
|
|
|
|
fn done(task: &str, on: &str) -> EventBody {
|
|
EventBody::TaskStatusSet { task: task.into(), status: TaskStatus::Done, on: d(on) }
|
|
}
|
|
|
|
#[test]
|
|
fn a_renovation_spent_by_line_by_hand_and_by_rule_within_its_dates() {
|
|
let today = d("2026-11-20");
|
|
let s = ledger(vec![
|
|
made("flat", Kind::Simple, "2026-11-01", "2027-02-28"),
|
|
line("flat", "kitchen", 12_000_000),
|
|
line("flat", "tiles", 4_500_000),
|
|
paid("p1", -9_000_000, "2026-11-04", "IKEA Zlicin", None),
|
|
into("p1", "flat", Some("kitchen")),
|
|
paid("p2", -5_000_000, "2026-11-06", "Siko koupelny", None),
|
|
into("p2", "flat", Some("tiles")),
|
|
paid("p3", -120_000, "2026-11-08", "Hornbach Cerny Most", None),
|
|
paid("p4", -80_000, "2026-10-20", "Hornbach Cerny Most", None),
|
|
paid("p5", -50_000, "2026-11-09", "Albert", Some("groceries")),
|
|
EventBody::ProjectRuleSet { merchant_key: fiscus_ledger::merchant::merchant_key("Hornbach Cerny Most"), project: Some("flat".into()) },
|
|
task("t1", "flat", Some("2026-11-10"), None),
|
|
task("t2", "flat", Some("2026-12-10"), None),
|
|
task("t3", "flat", None, None),
|
|
done("t3", "2026-11-07"),
|
|
]);
|
|
let p = project(&s, "flat", today).unwrap();
|
|
let lines: Vec<(&str, Haler, Haler, bool)> = p.lines.iter().map(|l| (l.id.as_str(), l.spent, l.left, l.over)).collect();
|
|
assert_eq!(lines, [("kitchen", Haler(9_000_000), Haler(3_000_000), false), ("tiles", Haler(5_000_000), Haler(-500_000), true)]);
|
|
// Hornbach in November by the rule (before the project started: not), Albert never.
|
|
assert_eq!(p.unplanned, Haler(120_000));
|
|
assert_eq!(p.summary.spent, Haler(14_120_000));
|
|
assert_eq!(p.payments.iter().map(|x| (x.id.as_str(), x.by_rule)).collect::<Vec<_>>(), [("p3", true), ("p2", false), ("p1", false)]);
|
|
assert_eq!((p.summary.planned, p.summary.over_budget), (Haler(16_500_000), false), "a line over is not the project over");
|
|
// One task of three done (no estimates: by count); t1 is past its due date.
|
|
assert_eq!((p.summary.progress_permille, p.summary.late_tasks, p.summary.late), (333, 1, true));
|
|
assert_eq!((p.summary.tasks.todo, p.summary.tasks.done), (2, 1));
|
|
assert_eq!(p.tasks.iter().map(|t| (t.task.id.as_str(), t.late)).collect::<Vec<_>>(), [("t1", true), ("t2", false), ("t3", false)]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_forecast_and_progress_by_estimates() {
|
|
let today = d("2026-11-20");
|
|
let s = ledger(vec![
|
|
made("flat", Kind::Simple, "2026-11-01", "2027-02-28"),
|
|
line("flat", "all", 10_000_000),
|
|
paid("p1", -6_000_000, "2026-11-04", "Firma Novak", None),
|
|
into("p1", "flat", Some("all")),
|
|
task("t1", "flat", None, Some(6_000_000)),
|
|
task("t2", "flat", None, Some(5_000_000)),
|
|
done("t1", "2026-11-05"),
|
|
]);
|
|
let p = &projects(&s, today)[0];
|
|
// 6 of 11 (by estimates) done; 6 spent + 5 to come = 11 > 10 planned.
|
|
assert_eq!((p.progress_permille, p.open_estimates, p.forecast, p.over_budget), (545, Haler(5_000_000), Haler(11_000_000), true));
|
|
}
|
|
|
|
#[test]
|
|
fn a_business_earns_from_paid_invoices_without_vat_and_never_twice() {
|
|
let today = d("2026-11-20");
|
|
let party = |name: &str, ico: &str| Party { name: name.into(), address: vec!["Dlouhá 1".into(), "110 00 Praha 1".into()], ico: Some(ico.into()), dic: None };
|
|
let invoice = |id: &str, number: &str| Invoice {
|
|
id: id.into(),
|
|
number: number.into(),
|
|
issued: d("2026-11-05"),
|
|
due: d("2026-11-19"),
|
|
taxable: None,
|
|
customer: party("Pekárna Novák", "45244782"),
|
|
lines: vec![InvoiceLine { text: "Web".into(), quantity: 10_000, unit: "h".into(), unit_price: Haler(80_000), vat: None }],
|
|
budget: None,
|
|
language: Language::Cs,
|
|
};
|
|
let s = ledger(vec![
|
|
made("web", Kind::Business, "2026-11-01", "2026-12-31"),
|
|
EventBody::SupplierSet { supplier: Supplier { party: party("Marek Novák", "27074358"), vat_payer: false, reserve_permille: 150, registration: None } },
|
|
EventBody::InvoiceIssued { invoice: invoice("i1", "20260001") },
|
|
EventBody::InvoiceIssued { invoice: invoice("i2", "20260002") },
|
|
EventBody::TimeLogged { entry: "l1".into(), project: "web".into(), task: None, minutes: 600, on: d("2026-11-03"), rate: Haler(80_000), note: String::new() },
|
|
EventBody::TimeLogged { entry: "l2".into(), project: "web".into(), task: None, minutes: 90, on: d("2026-11-10"), rate: Haler(80_000), note: String::new() },
|
|
EventBody::InvoiceProjected { invoice: "i1".into(), project: "web".into(), entries: vec!["l1".into()] },
|
|
EventBody::InvoiceProjected { invoice: "i2".into(), project: "web".into(), entries: vec![] },
|
|
// i1 marked paid by hand (its money in is not put into the project: the invoice is the income).
|
|
EventBody::InvoicePaid { invoice: "i1".into(), payment: None, on: d("2026-11-18") },
|
|
// A tip from the client straight into the project; a tool bought for it.
|
|
paid("in2", 50_000, "2026-11-19", "Pekarna Novak", Some("income")),
|
|
into("in2", "web", None),
|
|
paid("out1", -100_000, "2026-11-12", "Adobe", None),
|
|
into("out1", "web", None),
|
|
]);
|
|
let p = project(&s, "web", today).unwrap();
|
|
// i1 paid (8 000 Kč without VAT) and the tip (500 Kč); i2 is not paid yet.
|
|
assert_eq!(p.summary.income, Haler(800_000 + 50_000));
|
|
let s2 = ledger(vec![
|
|
made("web", Kind::Business, "2026-11-01", "2026-12-31"),
|
|
EventBody::SupplierSet { supplier: Supplier { party: party("Marek Novák", "27074358"), vat_payer: false, reserve_permille: 150, registration: None } },
|
|
EventBody::InvoiceIssued { invoice: invoice("i1", "20260001") },
|
|
EventBody::InvoiceProjected { invoice: "i1".into(), project: "web".into(), entries: vec![] },
|
|
EventBody::PaymentImported {
|
|
payment: "line1".into(),
|
|
line: fiscus_ledger::import::StatementLine {
|
|
bank: "test".into(),
|
|
account: "1/0000".into(),
|
|
booked: d("2026-11-18"),
|
|
amount: Haler(800_000),
|
|
merchant: "Pekarna Novak".into(),
|
|
counterparty: None,
|
|
variable_symbol: Some("20260001".into()),
|
|
message: None,
|
|
bank_ref: None,
|
|
},
|
|
line_key: "k1".into(),
|
|
},
|
|
EventBody::InvoicePaid { invoice: "i1".into(), payment: Some("line1".into()), on: d("2026-11-18") },
|
|
into("line1", "web", None),
|
|
]);
|
|
// A statement line that paid the project's invoice, put into the project too, is the invoice: counted once.
|
|
assert_eq!(project(&s2, "web", today).unwrap().summary.income, Haler(800_000));
|
|
|
|
assert_eq!((p.summary.spent, p.summary.profit), (Haler(100_000), Haler(750_000)));
|
|
assert_eq!(p.summary.margin_permille, Some(882));
|
|
// 11 h 30 min logged; 10 h billed by i1, 1 h 30 min (1 200 Kč) not yet.
|
|
assert_eq!((p.summary.minutes, p.summary.unbilled_minutes, p.summary.unbilled, p.billable), (690, 90, Haler(120_000), Haler(920_000)));
|
|
assert_eq!(p.unbilled_entries, ["l2"]);
|
|
assert_eq!(p.time_by_week, [(d("2026-11-02"), 600), (d("2026-11-09"), 90)]);
|
|
assert_eq!((p.time_by_task[0].task.clone(), p.time_by_task[0].minutes), (None, 690));
|
|
assert_eq!(p.invoices, ["i1", "i2"]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_project_late_past_its_end_and_the_medals_when_done() {
|
|
let s = ledger(vec![
|
|
made("trip", Kind::Simple, "2026-08-01", "2026-09-30"),
|
|
line("trip", "all", 3_000_000),
|
|
paid("p1", -2_500_000, "2026-08-15", "Booking.com", None),
|
|
into("p1", "trip", None),
|
|
task("t1", "trip", None, None),
|
|
]);
|
|
let today = d("2026-10-05");
|
|
assert!(projects(&s, today)[0].late, "past its end with a task open");
|
|
|
|
let mut log = vec![
|
|
made("trip", Kind::Simple, "2026-08-01", "2026-09-30"),
|
|
line("trip", "all", 3_000_000),
|
|
paid("p1", -2_500_000, "2026-08-15", "Booking.com", None),
|
|
into("p1", "trip", None),
|
|
task("t1", "trip", None, None),
|
|
done("t1", "2026-09-20"),
|
|
EventBody::ProjectStatusSet { project: "trip".into(), status: Status::Done, on: d("2026-09-25") },
|
|
made("late", Kind::Simple, "2026-08-01", "2026-09-01"),
|
|
line("late", "all", 100_000),
|
|
paid("p2", -500_000, "2026-08-15", "Bauhaus", None),
|
|
into("p2", "late", None),
|
|
EventBody::ProjectStatusSet { project: "late".into(), status: Status::Done, on: d("2026-09-10") },
|
|
];
|
|
let s = ledger(std::mem::take(&mut log));
|
|
assert!(!projects(&s, today).iter().any(|p| p.late), "a done project is never late");
|
|
let r = rewards(&s, &analyse(&s, today), &BTreeSet::new(), today);
|
|
let got: Vec<(&str, Date)> = r
|
|
.medals
|
|
.iter()
|
|
.filter_map(|e| match &e.medal {
|
|
Medal::ProjectOnBudget { project, .. } => Some(("on budget", if project == "trip" { e.on } else { d("1970-01-01") })),
|
|
Medal::FinishedOnTime { project, .. } => Some(("on time", if project == "trip" { e.on } else { d("1970-01-01") })),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(got, [("on budget", d("2026-09-25")), ("on time", d("2026-09-25"))], "the trip earns both; the late, over one neither");
|
|
}
|