add billable rate restriction for free organizations

This commit is contained in:
Gregor Vostrak
2026-10-13 14:59:12 +02:00
parent 2564f74d9a
commit d9941e338f
55 changed files with 1207 additions and 103 deletions

View File

@@ -6,6 +6,7 @@ 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;
@@ -48,6 +49,16 @@ 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>
*/

View File

@@ -11,6 +11,7 @@ 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;
@@ -96,6 +97,25 @@ 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();

View File

@@ -6,6 +6,7 @@ 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;
@@ -136,6 +137,25 @@ 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>
*/

View File

@@ -7,6 +7,7 @@ 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;
@@ -48,6 +49,16 @@ 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>
*/

View File

@@ -8,6 +8,7 @@ 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;
@@ -140,6 +141,12 @@ 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;
}
});
}