mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-14 16:53:17 +01:00
Enforce billable rate restriction in controllers and importers instead of model hooks
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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')) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\CustomAuditable;
|
||||
use App\Models\Concerns\HasUuids;
|
||||
use App\Service\BillingContract;
|
||||
use Database\Factories\MemberFactory;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -49,16 +48,6 @@ class Member extends Pivot implements AuditableContract
|
||||
*/
|
||||
protected $table = 'members';
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (Member $member): void {
|
||||
// Organizations that can not use billable rates can not change the billable rate
|
||||
if ($member->billable_rate !== $member->getOriginal('billable_rate') && ! app(BillingContract::class)->canUseBillableRates($member->loadMissing('organization')->organization)) {
|
||||
$member->billable_rate = $member->getOriginal('billable_rate');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
|
||||
@@ -11,7 +11,6 @@ use App\Enums\NumberFormat;
|
||||
use App\Enums\TimeFormat;
|
||||
use App\Models\Concerns\CustomAuditable;
|
||||
use App\Models\Concerns\HasUuids;
|
||||
use App\Service\BillingContract;
|
||||
use Database\Factories\OrganizationFactory;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -97,25 +96,6 @@ class Organization extends Model implements AuditableContract
|
||||
protected $attributes = [
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (Organization $organization): void {
|
||||
// Organizations that can not use billable rates can keep or remove the visibility for employees, but not
|
||||
// enable it, and can not change the billable rate
|
||||
$enablesVisibility = $organization->employees_can_see_billable_rates && ! $organization->getOriginal('employees_can_see_billable_rates');
|
||||
if (! $enablesVisibility && $organization->billable_rate === $organization->getOriginal('billable_rate')) {
|
||||
return;
|
||||
}
|
||||
if (app(BillingContract::class)->canUseBillableRates($organization)) {
|
||||
return;
|
||||
}
|
||||
if ($enablesVisibility) {
|
||||
$organization->employees_can_see_billable_rates = false;
|
||||
}
|
||||
$organization->billable_rate = $organization->getOriginal('billable_rate');
|
||||
});
|
||||
}
|
||||
|
||||
public function getAuditOwnerOrganizationId(): ?string
|
||||
{
|
||||
return $this->getKey();
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\CustomAuditable;
|
||||
use App\Models\Concerns\HasUuids;
|
||||
use App\Service\BillingContract;
|
||||
use Database\Factories\ProjectFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
@@ -137,25 +136,6 @@ class Project extends Model implements AuditableContract
|
||||
return $this->scopeComputedAttributesGenerate($builder, $attributes);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (Project $project): void {
|
||||
// Organizations that can not use billable rates can keep or remove the billable flag, but not set it,
|
||||
// and can not change the billable rate
|
||||
$enablesBillable = $project->is_billable && ! $project->getOriginal('is_billable');
|
||||
if (! $enablesBillable && $project->billable_rate === $project->getOriginal('billable_rate')) {
|
||||
return;
|
||||
}
|
||||
if (app(BillingContract::class)->canUseBillableRates($project->loadMissing('organization')->organization)) {
|
||||
return;
|
||||
}
|
||||
if ($enablesBillable) {
|
||||
$project->is_billable = false;
|
||||
}
|
||||
$project->billable_rate = $project->getOriginal('billable_rate');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Organization, $this>
|
||||
*/
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace App\Models;
|
||||
use App\Models\Concerns\AuditableThroughParent;
|
||||
use App\Models\Concerns\CustomAuditable;
|
||||
use App\Models\Concerns\HasUuids;
|
||||
use App\Service\BillingContract;
|
||||
use Database\Factories\ProjectMemberFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -49,16 +48,6 @@ class ProjectMember extends Model implements AuditableContract, AuditableThrough
|
||||
'billable_rate' => 'int',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (ProjectMember $projectMember): void {
|
||||
// Organizations that can not use billable rates can not change the billable rate
|
||||
if ($projectMember->billable_rate !== $projectMember->getOriginal('billable_rate') && ! app(BillingContract::class)->canUseBillableRates($projectMember->loadMissing('project.organization')->project->organization)) {
|
||||
$projectMember->billable_rate = $projectMember->getOriginal('billable_rate');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Project, $this>
|
||||
*/
|
||||
|
||||
@@ -8,7 +8,6 @@ use App\Enums\TimeEntryType;
|
||||
use App\Models\Concerns\CustomAuditable;
|
||||
use App\Models\Concerns\HasUuids;
|
||||
use App\Service\BillableRateService;
|
||||
use App\Service\BillingContract;
|
||||
use Carbon\CarbonInterval;
|
||||
use Database\Factories\TimeEntryFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -141,12 +140,6 @@ class TimeEntry extends Model implements AuditableContract
|
||||
$timeEntry->client_id = null;
|
||||
$timeEntry->tags = [];
|
||||
}
|
||||
// Organizations that can not use billable rates can keep or remove the billable flag, but not set it
|
||||
if ($timeEntry->billable && ! $timeEntry->getOriginal('billable') && ! app(BillingContract::class)->canUseBillableRates($timeEntry->loadMissing('organization')->organization)) {
|
||||
$timeEntry->billable = false;
|
||||
// Note: The billable rate is computed before saving, non-billable time entries have no billable rate
|
||||
$timeEntry->billable_rate = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->organization()->associate($this->organization);
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
if (strlen($record['Description']) > 5000) {
|
||||
throw new ImportException('Time entry description is too long');
|
||||
}
|
||||
@@ -128,7 +128,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
if (! in_array($record['Billable'], ['Yes', 'No'], true)) {
|
||||
throw new ImportException('Invalid billable value');
|
||||
}
|
||||
$timeEntry->billable = $record['Billable'] === 'Yes';
|
||||
$timeEntry->billable = $this->canUseBillableRates && $record['Billable'] === 'Yes';
|
||||
}
|
||||
if ($isBreak) {
|
||||
// Breaks can not be billable or belong to a project/task (already skipped above)
|
||||
|
||||
@@ -74,9 +74,16 @@ abstract class DefaultImporter implements ImporterContract
|
||||
|
||||
protected BillableRateService $billableRateService;
|
||||
|
||||
/**
|
||||
* Organizations that can not use billable rates import everything without billable flags and rates
|
||||
*/
|
||||
protected bool $canUseBillableRates;
|
||||
|
||||
public function init(Organization $organization): void
|
||||
{
|
||||
$this->organization = $organization;
|
||||
$this->billableRateService = app(BillableRateService::class);
|
||||
$this->canUseBillableRates = $this->billableRateService->canUseBillableRates($organization);
|
||||
$this->userImportHelper = new ImportDatabaseHelper(User::class, ['email'], true, function (Builder $builder) {
|
||||
/** @var Builder<User> $builder */
|
||||
return $builder->belongsToOrganization($this->organization);
|
||||
@@ -99,7 +106,11 @@ abstract class DefaultImporter implements ImporterContract
|
||||
'string',
|
||||
'in:placeholder',
|
||||
],
|
||||
]);
|
||||
], beforeSave: function (Member $member): void {
|
||||
if (! $this->canUseBillableRates) {
|
||||
$member->billable_rate = null;
|
||||
}
|
||||
});
|
||||
$this->projectImportHelper = new ImportDatabaseHelper(Project::class, ['name', 'client_id', 'organization_id'], true, function (Builder $builder) {
|
||||
/** @var Builder<Project> $builder */
|
||||
return $builder->where('organization_id', $this->organization->id);
|
||||
@@ -126,8 +137,10 @@ abstract class DefaultImporter implements ImporterContract
|
||||
if ($project->billable_rate === 0) {
|
||||
$project->billable_rate = null;
|
||||
}
|
||||
// Note: Prevents a query per project for the billable rate restriction (see Project::booted)
|
||||
$project->setRelation('organization', $this->organization);
|
||||
if (! $this->canUseBillableRates) {
|
||||
$project->is_billable = false;
|
||||
$project->billable_rate = null;
|
||||
}
|
||||
});
|
||||
$this->projectMemberImportHelper = new ImportDatabaseHelper(ProjectMember::class, ['project_id', 'member_id'], true, function (Builder $builder): Builder {
|
||||
/** @var Builder<ProjectMember> $builder */
|
||||
@@ -142,10 +155,8 @@ abstract class DefaultImporter implements ImporterContract
|
||||
if ($projectMember->billable_rate === 0) {
|
||||
$projectMember->billable_rate = null;
|
||||
}
|
||||
// Note: Prevents queries per project member for the billable rate restriction (see ProjectMember::booted)
|
||||
$project = $this->projectImportHelper->getModelById($projectMember->project_id);
|
||||
if ($project !== null) {
|
||||
$projectMember->setRelation('project', $project->setRelation('organization', $this->organization));
|
||||
if (! $this->canUseBillableRates) {
|
||||
$projectMember->billable_rate = null;
|
||||
}
|
||||
});
|
||||
$this->tagImportHelper = new ImportDatabaseHelper(Tag::class, ['name', 'organization_id'], true, function (Builder $builder): Builder {
|
||||
@@ -188,7 +199,6 @@ abstract class DefaultImporter implements ImporterContract
|
||||
$this->timeEntriesCreated = 0;
|
||||
$this->colorService = app(ColorService::class);
|
||||
$this->timezoneService = app(TimezoneService::class);
|
||||
$this->billableRateService = app(BillableRateService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -129,12 +129,12 @@ class GenericTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->organization()->associate($this->organization);
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
$timeEntry->description = $record['description'];
|
||||
if (! in_array($record['billable'], ['true', 'false'], true)) {
|
||||
throw new ImportException('Invalid billable value');
|
||||
}
|
||||
$timeEntry->billable = $record['billable'] === 'true';
|
||||
$timeEntry->billable = $this->canUseBillableRates && $record['billable'] === 'true';
|
||||
$timeEntry->tags = $this->getTags($record['tags']);
|
||||
$timeEntry->is_imported = true;
|
||||
try {
|
||||
|
||||
@@ -107,7 +107,7 @@ class HarvestTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->organization()->associate($this->organization);
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
if (strlen($record['Notes']) > 5000) {
|
||||
throw new ImportException('Time entry note is too long');
|
||||
}
|
||||
@@ -115,7 +115,7 @@ class HarvestTimeEntriesImporter extends DefaultImporter
|
||||
if (! in_array($record['Billable?'], ['Yes', 'No'], true)) {
|
||||
throw new ImportException('Invalid billable value');
|
||||
}
|
||||
$timeEntry->billable = $record['Billable?'] === 'Yes';
|
||||
$timeEntry->billable = $this->canUseBillableRates && $record['Billable?'] === 'Yes';
|
||||
$timeEntry->tags = [];
|
||||
$timeEntry->is_imported = true;
|
||||
|
||||
|
||||
@@ -260,7 +260,7 @@ class SolidtimeImporter extends DefaultImporter
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->organization()->associate($this->organization);
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
if (strlen($timeEntryRow['description']) > 5000) {
|
||||
throw new ImportException('Time entry description is too long');
|
||||
}
|
||||
@@ -268,7 +268,7 @@ class SolidtimeImporter extends DefaultImporter
|
||||
if (! in_array($timeEntryRow['billable'], ['true', 'false'], true)) {
|
||||
throw new ImportException('Invalid billable value');
|
||||
}
|
||||
$timeEntry->billable = $timeEntryRow['billable'] === 'true';
|
||||
$timeEntry->billable = $this->canUseBillableRates && $timeEntryRow['billable'] === 'true';
|
||||
// The type column does not exist in old exports
|
||||
if (($timeEntryRow['type'] ?? '') !== '') {
|
||||
$type = TimeEntryType::tryFrom($timeEntryRow['type']);
|
||||
|
||||
@@ -113,12 +113,12 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->organization()->associate($this->organization);
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
$timeEntry->description = $record['Description'];
|
||||
if (! in_array($record['Billable'], ['Yes', 'No'], true)) {
|
||||
throw new ImportException('Invalid billable value');
|
||||
}
|
||||
$timeEntry->billable = $record['Billable'] === 'Yes';
|
||||
$timeEntry->billable = $this->canUseBillableRates && $record['Billable'] === 'Yes';
|
||||
$timeEntry->tags = $this->getTags($record['Tags']);
|
||||
$timeEntry->is_imported = true;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user