Enforce billable rate restriction in controllers and importers instead of model hooks

This commit is contained in:
Constantin Graf
2026-10-14 13:29:32 +02:00
committed by Gregor Vostrak
parent 3197c8c7a0
commit 1c41432389
18 changed files with 66 additions and 257 deletions

View File

@@ -49,4 +49,9 @@ class Controller extends \App\Http\Controllers\Controller
{
return app(BillingContract::class)->hasSubscription($organization) || app(BillingContract::class)->hasTrial($organization);
}
protected function canUseBillableRates(Organization $organization): bool
{
return app(BillingContract::class)->canUseBillableRates($organization);
}
}

View File

@@ -82,7 +82,7 @@ class MemberController extends Controller
$this->checkPermission($organization, 'members:update', $member);
$oldBillableRate = $member->billable_rate;
if ($request->has('billable_rate')) {
if ($request->has('billable_rate') && $this->canUseBillableRates($organization)) {
$member->billable_rate = $request->getBillableRate();
}
if ($request->has('role') && $member->role !== $request->getRole()->value) {

View File

@@ -46,6 +46,7 @@ class OrganizationController extends Controller
public function update(Organization $organization, OrganizationUpdateRequest $request, BillableRateService $billableRateService): OrganizationResource
{
$this->checkPermission($organization, 'organizations:update');
$canUseBillableRates = $this->canUseBillableRates($organization);
if ($request->getName() !== null) {
$organization->name = $request->getName();
@@ -53,7 +54,8 @@ class OrganizationController extends Controller
if ($request->getCurrency() !== null) {
$organization->currency = $request->getCurrency();
}
if ($request->getEmployeesCanSeeBillableRates() !== null) {
// Organizations that can not use billable rates can keep or remove the visibility for employees, but not enable it
if ($request->getEmployeesCanSeeBillableRates() !== null && ($canUseBillableRates || ! $request->getEmployeesCanSeeBillableRates())) {
$organization->employees_can_see_billable_rates = $request->getEmployeesCanSeeBillableRates();
}
if ($request->getEmployeesCanManageTasks() !== null) {
@@ -81,7 +83,7 @@ class OrganizationController extends Controller
$organization->breaks_enabled = $request->getBreaksEnabled();
}
$oldBillableRate = $organization->billable_rate;
if ($request->has('billable_rate')) {
if ($request->has('billable_rate') && $canUseBillableRates) {
$organization->billable_rate = $request->getBillableRate();
}
$organization->save();

View File

@@ -96,11 +96,12 @@ class ProjectController extends Controller
public function store(Organization $organization, ProjectStoreRequest $request): JsonResource
{
$this->checkPermission($organization, 'projects:create');
$canUseBillableRates = $this->canUseBillableRates($organization);
$project = new Project;
$project->name = $request->input('name');
$project->color = $request->input('color');
$project->is_billable = (bool) $request->input('is_billable');
$project->billable_rate = $request->getBillableRate();
$project->is_billable = $canUseBillableRates && $request->boolean('is_billable');
$project->billable_rate = $canUseBillableRates ? $request->getBillableRate() : null;
$project->client_id = $request->input('client_id');
$project->is_public = $request->getIsPublic();
if ($this->canAccessPremiumFeatures($organization) && $request->has('estimated_time')) {
@@ -122,9 +123,13 @@ class ProjectController extends Controller
public function update(Organization $organization, Project $project, ProjectUpdateRequest $request, BillableRateService $billableRateService): JsonResource
{
$this->checkPermission($organization, 'projects:update', $project);
$canUseBillableRates = $this->canUseBillableRates($organization);
$project->name = $request->input('name');
$project->color = $request->input('color');
$project->is_billable = (bool) $request->input('is_billable');
// Organizations that can not use billable rates can keep or remove the billable flag, but not set it
if ($canUseBillableRates || ! $request->boolean('is_billable')) {
$project->is_billable = $request->boolean('is_billable');
}
if ($request->has('is_archived')) {
$project->archived_at = $request->getIsArchived() ? Carbon::now() : null;
}
@@ -136,7 +141,7 @@ class ProjectController extends Controller
}
$oldBillableRate = $project->billable_rate;
$clientIdChanged = false;
if ($request->has('billable_rate')) {
if ($request->has('billable_rate') && $canUseBillableRates) {
$project->billable_rate = $request->getBillableRate();
}
if ($project->client_id !== $request->input('client_id')) {

View File

@@ -73,7 +73,7 @@ class ProjectMemberController extends Controller
}
$projectMember = new ProjectMember;
$projectMember->billable_rate = $request->getBillableRate();
$projectMember->billable_rate = $this->canUseBillableRates($organization) ? $request->getBillableRate() : null;
$projectMember->member()->associate($member);
$projectMember->user()->associate($member->user);
$projectMember->project()->associate($project);
@@ -97,7 +97,9 @@ class ProjectMemberController extends Controller
{
$this->checkPermission($organization, 'project-members:update', projectMember: $projectMember);
$oldBillableRate = $projectMember->billable_rate;
$projectMember->billable_rate = $request->getBillableRate();
if ($this->canUseBillableRates($organization)) {
$projectMember->billable_rate = $request->getBillableRate();
}
$projectMember->save();
if ($oldBillableRate !== $projectMember->billable_rate) {

View File

@@ -647,6 +647,10 @@ class TimeEntryController extends Controller
$timeEntry = new TimeEntry;
$timeEntry->fill($request->validated());
// Organizations that can not use billable rates can not create billable time entries
if (! $this->canUseBillableRates($organization)) {
$timeEntry->billable = false;
}
$timeEntry->client()->associate($client);
$timeEntry->user_id = $member->user_id;
$timeEntry->description = $request->input('description') ?? '';
@@ -714,7 +718,12 @@ class TimeEntryController extends Controller
$task = $request->input('task_id') !== null ? Task::findOrFail((string) $request->input('task_id')) : null;
}
$wasBillable = $timeEntry->billable;
$timeEntry->fill($request->validated());
// Organizations that can not use billable rates can keep or remove the billable flag, but not set it
if (! $wasBillable && ! $this->canUseBillableRates($organization)) {
$timeEntry->billable = false;
}
if ($newMember !== null) {
$timeEntry->member()->associate($newMember);
$timeEntry->user()->associate($newMember->user);
@@ -772,6 +781,7 @@ class TimeEntryController extends Controller
->get();
$changes = $request->validated('changes');
$canUseBillableRates = $this->canUseBillableRates($organization);
if ($request->has('changes.description')) {
$changes['description'] = $request->input('changes.description') ?? '';
@@ -837,8 +847,13 @@ class TimeEntryController extends Controller
}
$oldProject = $timeEntry->project;
$oldTask = $timeEntry->task;
$wasBillable = $timeEntry->billable;
$timeEntry->fill($changes);
// Organizations that can not use billable rates can keep or remove the billable flag, but not set it
if (! $wasBillable && ! $canUseBillableRates) {
$timeEntry->billable = false;
}
if ($newMember !== null) {
$timeEntry->member()->associate($newMember);
$timeEntry->user_id = $newMember->user_id;