Prevent and remove zero values for billable rates

This commit is contained in:
Constantin Graf
2024-06-10 19:19:51 +02:00
committed by Constantin Graf
parent 92dde6a701
commit bd9cede081
20 changed files with 343 additions and 9 deletions

View File

@@ -58,12 +58,14 @@ class ImportDatabaseHelper
*/
private array $validate;
private ?Closure $beforeSave;
/**
* @param class-string<TModel> $model
* @param array<string> $identifiers
* @param array<string, array<int, string>> $validate
*/
public function __construct(string $model, array $identifiers, bool $attachToExisting = false, ?Closure $queryModifier = null, ?Closure $afterCreate = null, array $validate = [])
public function __construct(string $model, array $identifiers, bool $attachToExisting = false, ?Closure $queryModifier = null, ?Closure $afterCreate = null, array $validate = [], ?Closure $beforeSave = null)
{
$this->model = $model;
$this->identifiers = $identifiers;
@@ -72,6 +74,7 @@ class ImportDatabaseHelper
$this->afterCreate = $afterCreate;
$this->createdCount = 0;
$this->validate = $validate;
$this->beforeSave = $beforeSave;
}
/**
@@ -99,6 +102,9 @@ class ImportDatabaseHelper
foreach ($data as $key => $value) {
$model->{$key} = $value;
}
if ($this->beforeSave !== null) {
($this->beforeSave)($model);
}
$model->save();
if ($this->afterCreate !== null) {

View File

@@ -107,7 +107,11 @@ abstract class DefaultImporter implements ImporterContract
'nullable',
'integer',
],
]);
], beforeSave: function (Project $project) {
if ($project->billable_rate === 0) {
$project->billable_rate = null;
}
});
$this->projectMemberImportHelper = new ImportDatabaseHelper(ProjectMember::class, ['project_id', 'member_id'], true, function (Builder $builder) {
/** @var Builder<ProjectMember> $builder */
return $builder->whereBelongsToOrganization($this->organization);
@@ -116,7 +120,11 @@ abstract class DefaultImporter implements ImporterContract
'nullable',
'integer',
],
]);
], beforeSave: function (ProjectMember $projectMember) {
if ($projectMember->billable_rate === 0) {
$projectMember->billable_rate = null;
}
});
$this->tagImportHelper = new ImportDatabaseHelper(Tag::class, ['name', 'organization_id'], true, function (Builder $builder) {
return $builder->where('organization_id', $this->organization->id);
}, validate: [