Added billable flag to projects

This commit is contained in:
Constantin Graf
2024-06-04 14:35:02 +02:00
committed by Constantin Graf
parent 20f9b344f6
commit 86555664c5
10 changed files with 106 additions and 19 deletions

View File

@@ -85,6 +85,7 @@ class ProjectController extends Controller
$project = new Project(); $project = new Project();
$project->name = $request->input('name'); $project->name = $request->input('name');
$project->color = $request->input('color'); $project->color = $request->input('color');
$project->is_billable = (bool) $request->input('is_billable');
$project->billable_rate = $request->input('billable_rate'); $project->billable_rate = $request->input('billable_rate');
$project->client_id = $request->input('client_id'); $project->client_id = $request->input('client_id');
$project->organization()->associate($organization); $project->organization()->associate($organization);
@@ -105,6 +106,7 @@ class ProjectController extends Controller
$this->checkPermission($organization, 'projects:update', $project); $this->checkPermission($organization, 'projects:update', $project);
$project->name = $request->input('name'); $project->name = $request->input('name');
$project->color = $request->input('color'); $project->color = $request->input('color');
$project->is_billable = (bool) $request->input('is_billable');
$project->billable_rate = $request->input('billable_rate'); $project->billable_rate = $request->input('billable_rate');
$project->client_id = $request->input('client_id'); $project->client_id = $request->input('client_id');
$project->save(); $project->save();

View File

@@ -38,6 +38,10 @@ class ProjectStoreRequest extends FormRequest
'max:255', 'max:255',
new ColorRule(), new ColorRule(),
], ],
'is_billable' => [
'required',
'boolean',
],
'billable_rate' => [ 'billable_rate' => [
'nullable', 'nullable',
'integer', 'integer',

View File

@@ -37,6 +37,10 @@ class ProjectUpdateRequest extends FormRequest
'max:255', 'max:255',
new ColorRule(), new ColorRule(),
], ],
'is_billable' => [
'required',
'boolean',
],
'billable_rate' => [ 'billable_rate' => [
'nullable', 'nullable',
'integer', 'integer',

View File

@@ -20,6 +20,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
* @property string $organization_id * @property string $organization_id
* @property string $client_id * @property string $client_id
* @property int|null $billable_rate * @property int|null $billable_rate
* @property bool $is_billable
* @property-read Organization $organization * @property-read Organization $organization
* @property-read Client|null $client * @property-read Client|null $client
* @property-read Collection<int, Task> $tasks * @property-read Collection<int, Task> $tasks
@@ -43,6 +44,15 @@ class Project extends Model
'color' => 'string', 'color' => 'string',
]; ];
/**
* Set default values for attributes.
*
* @var array<string, mixed>
*/
protected $attributes = [
'is_billable' => false,
];
/** /**
* @return BelongsTo<Organization, Project> * @return BelongsTo<Organization, Project>
*/ */

View File

@@ -41,6 +41,7 @@ class ClockifyProjectsImporter extends DefaultImporter
], [ ], [
'client_id' => $clientId, 'client_id' => $clientId,
'color' => $this->colorService->getRandomColor(), 'color' => $this->colorService->getRandomColor(),
'is_billable' => $record['Billability'] === 'Yes',
'billable_rate' => $billableRateKey !== null && $record[$billableRateKey] !== '' ? (int) (((float) $record[$billableRateKey]) * 100) : null, 'billable_rate' => $billableRateKey !== null && $record[$billableRateKey] !== '' ? (int) (((float) $record[$billableRateKey]) * 100) : null,
]); ]);
} }

View File

@@ -96,6 +96,10 @@ abstract class DefaultImporter implements ImporterContract
'required', 'required',
'max:255', 'max:255',
], ],
'is_billable' => [
'required',
'boolean',
],
'billable_rate' => [ 'billable_rate' => [
'nullable', 'nullable',
'integer', 'integer',

View File

@@ -121,6 +121,7 @@ class TogglDataImporter extends DefaultImporter
], [ ], [
'client_id' => $clientId, 'client_id' => $clientId,
'color' => $project->color, 'color' => $project->color,
'is_billable' => $project->rate !== null,
'billable_rate' => $project->rate !== null ? (int) ($project->rate * 100) : null, 'billable_rate' => $project->rate !== null ? (int) ($project->rate * 100) : null,
], (string) $project->id); ], (string) $project->id);

View File

@@ -27,13 +27,24 @@ class ProjectFactory extends Factory
return [ return [
'name' => $this->faker->company(), 'name' => $this->faker->company(),
'color' => app(ColorService::class)->getRandomColor(), 'color' => app(ColorService::class)->getRandomColor(),
'billable_rate' => $this->faker->numberBetween(50, 1000) * 100, 'is_billable' => false,
'billable_rate' => null,
'is_public' => false, 'is_public' => false,
'client_id' => null, 'client_id' => null,
'organization_id' => Organization::factory(), 'organization_id' => Organization::factory(),
]; ];
} }
public function billable(): self
{
return $this->state(function (array $attributes): array {
return [
'is_billable' => true,
'billable_rate' => $this->faker->numberBetween(50, 1000) * 100,
];
});
}
public function forOrganization(Organization $organization): self public function forOrganization(Organization $organization): self
{ {
return $this->state(function (array $attributes) use ($organization): array { return $this->state(function (array $attributes) use ($organization): array {

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('projects', function (Blueprint $table) {
$table->boolean('is_billable')->default(false);
});
DB::statement('
update projects
set is_billable = true
where projects.billable_rate is not null and projects.billable_rate > 0
');
Schema::table('projects', function (Blueprint $table) {
$table->boolean('is_billable')->default(null)->change();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('projects', function (Blueprint $table) {
$table->dropColumn('is_billable');
});
}
};

View File

@@ -128,6 +128,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [ $response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
'name' => $projectFake->name, 'name' => $projectFake->name,
'color' => $projectFake->color, 'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
]); ]);
// Assert // Assert
@@ -140,21 +141,23 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([ $data = $this->createUserWithPermission([
'projects:create', 'projects:create',
]); ]);
$project = Project::factory()->forOrganization($data->organization)->make(); $projectFake = Project::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user); Passport::actingAs($data->user);
// Act // Act
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [ $response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
'name' => $project->name, 'name' => $projectFake->name,
'color' => $project->color, 'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
]); ]);
// Assert // Assert
$response->assertStatus(201); $response->assertStatus(201);
$this->assertDatabaseHas(Project::class, [ $this->assertDatabaseHas(Project::class, [
'name' => $project->name, 'name' => $projectFake->name,
'color' => $project->color, 'color' => $projectFake->color,
'organization_id' => $project->organization_id, 'organization_id' => $projectFake->organization_id,
'is_billable' => $projectFake->is_billable,
]); ]);
} }
@@ -165,22 +168,24 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
'projects:create', 'projects:create',
]); ]);
$client = Client::factory()->forOrganization($data->organization)->create(); $client = Client::factory()->forOrganization($data->organization)->create();
$project = Project::factory()->forOrganization($data->organization)->make(); $projectFake = Project::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user); Passport::actingAs($data->user);
// Act // Act
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [ $response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
'name' => $project->name, 'name' => $projectFake->name,
'color' => $project->color, 'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
'client_id' => $client->getKey(), 'client_id' => $client->getKey(),
]); ]);
// Assert // Assert
$response->assertStatus(201); $response->assertStatus(201);
$this->assertDatabaseHas(Project::class, [ $this->assertDatabaseHas(Project::class, [
'name' => $project->name, 'name' => $projectFake->name,
'color' => $project->color, 'color' => $projectFake->color,
'organization_id' => $project->organization_id, 'is_billable' => $projectFake->is_billable,
'organization_id' => $projectFake->organization_id,
'client_id' => $client->getKey(), 'client_id' => $client->getKey(),
]); ]);
} }
@@ -191,23 +196,25 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$data = $this->createUserWithPermission([ $data = $this->createUserWithPermission([
'projects:create', 'projects:create',
]); ]);
$project = Project::factory()->forOrganization($data->organization)->make(); $projectFake = Project::factory()->forOrganization($data->organization)->make();
Passport::actingAs($data->user); Passport::actingAs($data->user);
// Act // Act
$response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [ $response = $this->postJson(route('api.v1.projects.store', [$data->organization->getKey()]), [
'name' => $project->name, 'name' => $projectFake->name,
'color' => $project->color, 'color' => $projectFake->color,
'is_billable' => true,
'billable_rate' => 10001, 'billable_rate' => 10001,
]); ]);
// Assert // Assert
$response->assertStatus(201); $response->assertStatus(201);
$this->assertDatabaseHas(Project::class, [ $this->assertDatabaseHas(Project::class, [
'name' => $project->name, 'name' => $projectFake->name,
'color' => $project->color, 'color' => $projectFake->color,
'organization_id' => $project->organization_id, 'is_billable' => true,
'billable_rate' => 10001, 'billable_rate' => 10001,
'organization_id' => $projectFake->organization_id,
]); ]);
} }
@@ -226,6 +233,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [ $response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
'name' => $projectFake->name, 'name' => $projectFake->name,
'color' => $projectFake->color, 'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
]); ]);
// Assert // Assert
@@ -245,6 +253,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [ $response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
'name' => $projectFake->name, 'name' => $projectFake->name,
'color' => $projectFake->color, 'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
]); ]);
// Assert // Assert
@@ -266,6 +275,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [ $response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
'name' => $projectFake->name, 'name' => $projectFake->name,
'color' => $projectFake->color, 'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
'client_id' => $client->getKey(), 'client_id' => $client->getKey(),
]); ]);
@@ -292,6 +302,7 @@ class ProjectEndpointTest extends ApiEndpointTestAbstract
$response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [ $response = $this->putJson(route('api.v1.projects.update', [$data->organization->getKey(), $project->getKey()]), [
'name' => $projectFake->name, 'name' => $projectFake->name,
'color' => $projectFake->color, 'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
'billable_rate' => 10002, 'billable_rate' => 10002,
]); ]);