Skip to content

Project Task Costs Developer Guide

Ed Mozley edited this page Nov 29, 2026 · 1 revision

💷 Costs on tasks - Developer Guide

Asked for by a supporter of the project · Projects · built after 5.13.0 (#3258-#3259)

How a project's budget line can belong to one task, so a supplier's price for one piece of work goes on that task and the Plan tab, the stage and the project add it up. For what a project manager sees, start at Projects → Costs on tasks. It builds on the budget (Projects → Budget) and the chart of accounts (Chart of Accounts - Developer Guide, design #282).

The request, and what it was not

The request: "I need to enter the specific amount directly into each task, assigning the cost to the appropriate account, and have the project automatically calculate the total." With two renewals of 5,000.01 and 3,750.65 in one stage, the only way was a project-level line for the total, split by percentage.

That was not the #282 decision coming back. Decision 2 there, "Splits on a task: percentage only", is about the hours logged on a task (hours are logged against the task as a whole, so a share is the only honest split), and it still stands. The gap was that a task had no money of its own: money lived only on budget lines, which belonged to the project. Budget lines could already go to an account by amount. What was missing was the link from a line to a task.

Decisions

Date Question Decided Why
29 Nov 2026 Build it? Yes (Ed) A quote arrives as a price for one piece of work; making people do sums to fit the tool is the tool's fault
29 Nov 2026 A new "task cost" table, or a budget line that names a task? A budget line with a task_id Every total (tiles, forecast, spend over time, earned value, by account, portfolio, report packs, REST, AI) is already one sum over lines, so a task's cost is counted once with no new code in any of them. A separate table would have needed adding into every one, and missing one would count it twice or not at all
29 Nov 2026 Replace the percentage method? No, both together Costs that belong to no one task stay as costs of the whole project; time keeps its percentage split
29 Nov 2026 A task deleted, or moved to another project Its cost stays, as a cost of the whole project The budget must never lose money silently, and a cost must never travel to another project's budget

1. Files

Key: 🗄️ schema · 📖 read · ✏️ write · 🔌 REST/MCP · 🖥️ UI · 🧪 test

🎨 File What it does
🗄️ database/freeitsm.sql, includes/db_verify_schema.php, api/system/db_verify.php project_budget_lines.task_id INT NULL, index ix_pbl_task, foreign key fk_pbl_task → tasks(id) ON DELETE SET NULL
🗄️ includes/db_verify_indexes.php Generated (php scripts/gen_db_verify_indexes.php), never edited by hand
📖 includes/projects/budget.php projectBudgetTaskReady(); projectBudgetDetail() adds task (id, title, stage_id) to each line and task_costs to the detail
✏️ includes/services/project_tools.php saveBudgetLine() checks and saves task_id
✏️ includes/services/projects.php assignTask() lets go of a task's costs when it leaves its project
🔌 api/v1/resources/projects.php, api/v1/lib/openapi_schemas.php task_id and task on a budget line, read and write; PATCH keeps it when left out
🔌 includes/mcp/tools.php The MCP budget answer says which task a line is a cost of
🖥️ assets/js/projects-budget.js A cost of in the line dialog; the lines grouped by stage with subtotals; PrjBudget.addCost() / showTask() for the Plan tab
🖥️ assets/js/projects-view.js The Plan tab's cost pill and + cost, and the lane total
🖥️ projects/view.php, assets/css/projects.css, assets/css/mobile.css The dialog field, styles, the phone layout of the pill
🖥️ lang/en/projects.php, projects/help.php Strings (budget.cost_of*, budget.task_of, budget.group_project, costs.*) and help paragraph help.budget.p11
🖥️ includes/feature_bingo/cards/projects.php Card projects.task_costs
🧪 tests/project-task-costs.php 22 checks (section 6)

What is NOT touched: every total. projectBudgetTotals(), projectBudgetTimeline(), projectEarnedValue(), projectAccountBreakdown(), the portfolio, report packs and the AI briefing read the same lines they always did. That's the point of the design.

2. The column, and the rule that keeps it honest

`task_id` INT NULL,   -- a cost of one task of the project (top-level); NULL = the project as a whole
KEY `ix_pbl_task` (`task_id`),
CONSTRAINT `fk_pbl_task` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE SET NULL,

A line's task_id only counts while that task is still this project's. Two things make sure of that:

  1. When a task leaves (ProjectsService::assignTask(), the one place a task's project_id changes), its lines in the old project are let go:
// includes/services/projects.php - assignTask()
if (!empty($task['project_id']) && (int)$task['project_id'] !== (int)$projectId) {
    require_once __DIR__ . '/../projects/budget.php';
    if (projectBudgetReady($conn) && projectBudgetTaskReady($conn)) {
        $conn->prepare("UPDATE project_budget_lines SET task_id = NULL WHERE task_id = ? AND project_id = ?")
             ->execute([$taskId, (int)$task['project_id']]);
    }
}
  1. When reading, the join checks the project too, so a stale task_id (from a database edited by hand, say) can never show another project's task:
// includes/projects/budget.php - projectBudgetDetail()
$taskJoin = $taskReady ? " LEFT JOIN tasks t ON t.id = l.task_id AND t.project_id = l.project_id" : '';
...
'task' => !empty($r['line_task_id']) ? ['id' => (int)$r['line_task_id'], 'title' => $r['line_task_title'],
    'stage_id' => $r['line_task_stage_id'] !== null ? (int)$r['line_task_stage_id'] : null] : null,

Deleting a task needs no code: the foreign key sets task_id NULL. Deleting the project deletes its lines, as before (fk_pbl_project cascades).

⚠️ TRAP: putting a task back doesn't re-attach its old cost. Once let go, the line is a cost of the whole project, and nothing remembers which task it came from. That's deliberate (a task can come back changed), and the test pins it. Pick the task again under A cost of.

3. Saving

// includes/services/project_tools.php - saveBudgetLine(), before anything is written
$setTask = array_key_exists('task_id', $in);
$taskId = $setTask && !empty($in['task_id']) ? (int)$in['task_id'] : null;
...
if ($taskId !== null) {
    $tk = $conn->prepare("SELECT 1 FROM tasks WHERE id = ? AND project_id = ? AND parent_task_id IS NULL");
    $tk->execute([$taskId, $projectId]);
    if (!$tk->fetchColumn()) throw new ServiceError('validation', 'invalid_field', 'That task is not part of this project.');
}
...
if ($setTask) $conn->prepare("UPDATE project_budget_lines SET task_id = ? WHERE id = ?")->execute([$taskId, $id]);
Sent Result
task_id left out The line keeps its task (an older caller, a REST PATCH without it)
null or 0 A cost of the whole project
A top-level task of this project A cost of that task
Another project's task, a subtask, a task in no project Refused, nothing written: That task is not part of this project.
Anything, before Database Verification Saved without it; a task is refused with Run System - Database Verification to put costs on tasks.

Only top-level tasks: that's what a project's Plan tab shows, and a subtask belongs to the project through its parent.

Who may do it: the same as any budget line, ProjectToolsService::changeable(), so changing the project. Being able to edit the task isn't enough, because the money is the project's.

4. The screens

The dialog (assets/js/projects-budget.js): fillTaskSelect() builds A cost of: The whole project, then an <optgroup> per stage in plan order, then tasks in no stage, labelled with the Plan tab's own words for them (plan.unassigned with the project's word for a stage: phase, sprint...). It's hidden when the project has no tasks or the column isn't there yet (budget.task_costs). openLine(line, presetTaskId) takes a task for a new line and fills in the line's name from it, because a quote is usually for exactly that piece of work.

The Budget tab: once any line names a task, groupedRows() lays out:

Licence renewals              8,750.66   ← stage header: the sum of its lines, as on the tiles
  Cisco licence renewal       5,000.01
  Fortinet licence renewal    3,750.65
New equipment                18,780.86
  ...
Not in a phase yet               ...     ← only when there are some
The whole project               420.00
  Delivery and insurance        420.00

A budget with no task costs looks exactly as it did. The subtotals use the same fields as the tiles (planned, actual, forecast, leaving out a line whose contract is in another currency), so they add up to them.

The Plan tab (assets/js/projects-view.js):

  • costCell() adds a pill with the task's total, which calls PrjBudget.showTask(): switch to Budget, scroll to the lines and flash them.
  • For an editor, a dashed + cost calls PrjBudget.addCost(). It shows only on the row you point at, like the estimate box beside it, and always on a touch screen (@media (hover: none)).
  • The lane header adds up its tasks.

A pill counts planned, or the forecast where nothing is planned, so a line entered only as a forecast still shows.

Both only appear when the project has the Budget tool and the column exists (taskCostsOn()).

⚠️ TRAP: a read-only viewer's rows have no data-budget-line (that attribute makes a row clickable to edit). showTask() looks rows up by data-line-id, which every row has, so it works for everyone.

5. REST API and MCP

GET /projects/{id}/budget and the budget-line endpoints carry task_id (null = the whole project) and task ({id, title, stage_id}, read only). POST and PATCH /projects/{id}/budget-lines accept task_id. A PATCH that leaves it out keeps it: the merge in apiProjectBudgetLinesUpdate() deliberately doesn't fill it in, as with accounts. The OpenAPI schema ProjectBudgetLine says the same, and php api/v1/lib/openapi_check.php passes.

The MCP project_budget answer adds - a cost of the task "Cisco licence renewal" to such a line.

6. Testing

FREEITSM_TEST_DATABASE=freeitsm_prtest php tests/project-task-costs.php      (from the prtest folder)

The 22 checks use the example from the request: two renewals in one stage, a server and a VMware licence in another, and a cost of the whole project.

Part Pinned
Saving A line keeps its task; the detail names the task and its stage
Counting once The project total is the plain sum of every line; positive control: the same lines in a project with no tasks give the same planned and forecast
Refused Another project's task, a subtask; nothing half-saved
Keeping Left out keeps the task; null clears it; a line can move to another task
Leaving A task moved out leaves its cost behind (row cleared, not hidden), the total is unchanged, the other project gets nothing, putting it back doesn't re-attach
Deleting The foreign key keeps the cost as one of the whole project
REST task_id and task on a line

The existing budget tests (project-budget-accounts, project-account-breakdown, project-income-lines, project-task-accounts, project-currency, project-plan-order, project-move-company, project-member-companies, projects-assistant) all still pass. The screens were checked in Chrome on a project built from the same example.

7. Extending it

  • A cost on a stage (not one task): add stage_id the same way, with assignTask()'s rule mirrored when a stage is deleted. The grouping already has the stage to put it under.
  • Showing a task's costs in the Tasks module's task window: read projectBudgetDetail()['lines'] filtered by task; keep editing in Projects, where the permission is.

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally