Compute invoice discounts in integer cents

C++ · C++ · intermediate · modification

The order discount went through `double` (`std::round(subtotal * discount_bp / 10000.0)`), which can be off by a cent or more on large orders, once the values involved no longer fit in a double's 53-bit mantissa, and the line totals were summed with plain `*` and `+`, so a large enough order was signed overflow. This PR keeps the signature in `src/billing/invoice.hpp` (only its comment changes) and rewrites `compute_totals` in `src/billing/invoice.cpp`: line totals and the running subtotal go through two small helpers, `checked_mul` and `checked_add`, that return nothing instead of overflowing, and `discount_for` computes the half-up discount in integers by splitting the subtotal into whole multiples of 10000 and a remainder. `tests/invoice_test.cpp` is new: it prices small orders with half-cent and other fractional discounts, 0% and 100% discounts, an order at the largest subtotal, orders that overflow, and orders with invalid inputs.

C++20, standard library only, built with CMake (unchanged: it builds `src/billing/*.cpp` into the billing library and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) by Apple clang 17 locally; CI builds and runs the tests with both Apple clang 17 and GCC 13, with `-Wall -Wextra -Wconversion -Werror`. `compute_totals` is a pure function with no shared state; the checkout service calls it from its request threads. Existing and unchanged: the only caller, `checkout::place_order`, looks up the promo code's discount (stored as basis points), calls `compute_totals` with the cart's lines, and on an empty result refuses the order with a generic "this order can't be priced" message and logs the order id. Amounts here are never negative: refunds, tax and currency conversion are separate modules. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed.

Requirements

Files touched

Review this PR

C++ practice