Cost tracking + Profit/Loss reporting (#82)#163
Merged
Merged
Conversation
Add optional, admin-only cost tracking via a generic resource_cost table weakly linked to any resource by (resource_type, resource_id), plus a profit/loss report. - DB: resource_cost table (recurring/one-time, interval + billing_start/ billing_end), AdminResource::ResourceCost (24) RBAC + migration - Model: ResourceCost, CostResourceType, CostType enums - AdminDb CRUD + active-between query; mysql + mock impls - Admin API: dedicated /api/admin/v1/resource_costs endpoints - Report: GET /api/admin/v1/reports/profit-loss (month/year), converted to a single target currency. Revenue uses each payment's stored historical rate; costs use current exchange rates. company_id + region_id filters. - Docs: ADMIN_API_ENDPOINTS.md, API_CHANGELOG.md
Add CostResourceType::Generic (2) for costs not tied to an internal DB entity, identified by a user-supplied free-form label (e.g. colo/transit subscriptions). Adds a nullable label column; resource_id is ignored for generic costs and they are excluded from region-filtered reports.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #82.
Summary
Adds optional, admin-only cost tracking used to compute profit/loss, plus a P/L report endpoint.
Rather than adding cost columns to
vm_host/ip_range, costs live in one genericresource_costtable weakly linked to any resource via(resource_type, resource_id). This is extensible (new cost-bearing resource kinds need no schema change) and lets a single resource carry multiple cost rows (e.g. a host's recurring rent and a one-time hardware investment).DB changes
resource_costtable:resource_type(TINYINT enum:vm_host/ip_range),resource_id,cost_type(recurring/one_time),amount(smallest units; per-IP for ip_range recurring),currency,interval_amount/interval_type(reuses existingIntervalType),billing_start,billing_end(null = ongoing), timestamps.AdminResource::ResourceCost(24) + migration grantingsuper_adminthe full permission set.ResourceCost,CostResourceType,CostTypemodels;AdminDbCRUD +admin_list_resource_costs_active_between; mysql + mock impls.Admin API
Dedicated cost endpoints (
resource_costpermission):GET/POST /api/admin/v1/resource_costsGET/PATCH/DELETE /api/admin/v1/resource_costs/{id}(PATCH-clear semantics for interval/billing fields)Profit/Loss report
GET /api/admin/v1/reports/profit-loss(modelled on the sales time-series report,analytics::view):month/year) rows, reported in a single target currency (currencyparam or the company's base currency).rateto value it in its company base currency (never live rates); a base→report-currency step only applies when aggregating companies with differing base currencies.ip_rangeper-IP costs scaled by the range's current assigned-IP count, one-time costs booked in theirbilling_startperiod.company_idandregion_idfilters.Deferred (follow-up)
company_idfilters revenue only).Testing
cargo build --workspaceclean;cargo test -p lnvps_dbmodel roundtrip tests pass (incl. newResourceCost/CostTypeenum roundtrips).Docs
ADMIN_API_ENDPOINTS.mdandAPI_CHANGELOG.mdupdated.