diff --git a/app/Http/Requests/Task/StoreTaskRequest.php b/app/Http/Requests/Task/StoreTaskRequest.php index 826954b0cd80..52d331d3ab92 100644 --- a/app/Http/Requests/Task/StoreTaskRequest.php +++ b/app/Http/Requests/Task/StoreTaskRequest.php @@ -84,9 +84,9 @@ class StoreTaskRequest extends Request public function prepareForValidation() { - $input = $this->all(); - $input = $this->decodePrimaryKeys($this->all()); + $input = $this->decodePrimaryKeys($this->all()); + if (array_key_exists('status_id', $input) && is_string($input['status_id'])) { $input['status_id'] = $this->decodePrimaryKey($input['status_id']); } diff --git a/app/Models/Task.php b/app/Models/Task.php index caee4ca8a374..be0c86135e8a 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -226,4 +226,15 @@ class Task extends BaseModel { return ctrans('texts.task'); } + + public function getRate(): float + { + if($this->project && $this->project->task_rate > 0) + return $this->project->task_rate; + + if($this->client) + return $this->client->getSetting('default_task_rate'); + + return $this->company->settings->default_task_rate ?? 0; + } } diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index ab97fe7f7aa3..e77c9a5addd8 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -40,7 +40,7 @@ class TaskRepository extends BaseRepository if ($task->id) { $this->new_task = false; } - +nlog($data); $task->fill($data); $task->saveQuietly(); @@ -48,12 +48,17 @@ class TaskRepository extends BaseRepository $task->status_id = $this->setDefaultStatus($task); } + if($this->new_task && (!$task->rate || $task->rate <= 0)) { + nlog($task->getRate()); + $task->rate = $task->getRate(); + } + $task->number = empty($task->number) || ! array_key_exists('number', $data) ? $this->trySaving($task) : $data['number']; if (isset($data['description'])) { $task->description = trim($data['description']); } - +nlog($task->toArray()); //todo i can't set it - i need to calculate it. if (isset($data['status_order'])) { $task->status_order = $data['status_order']; diff --git a/tests/Feature/TaskApiTest.php b/tests/Feature/TaskApiTest.php index 931f0062cf26..66d41ed50edf 100644 --- a/tests/Feature/TaskApiTest.php +++ b/tests/Feature/TaskApiTest.php @@ -11,8 +11,10 @@ namespace Tests\Feature; +use App\DataMapper\ClientSettings; use Tests\TestCase; use App\Models\Task; +use App\Models\Client; use App\Models\Project; use Tests\MockAccountData; use App\Utils\Traits\MakesHash; @@ -101,8 +103,93 @@ class TaskApiTest extends TestCase return true; } } - - public function testStatusSets() + + public function testTaskCompanyRateSet() + { + $settings = $this->company->settings; + $settings->default_task_rate = 31; + + $this->company->saveSettings((array)$settings, $this->company); + $this->company->push(); + $this->company->save(); + + $data = [ + 'client_id' => $this->client->hashed_id, + 'description' => 'Test Task', + 'time_log' => '[[1681165417,1681165432,"sumtin",true],[1681165446,0]]', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson("/api/v1/tasks", $data); + + $response->assertStatus(200); + $arr = $response->json(); + + $this->assertEquals(31, $arr['data']['rate']); + } + + public function testTaskClientRateSet() + { + $settings = ClientSettings::defaults(); + $settings->default_task_rate = 41; + + $c = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'settings' => $settings, + ]); + + $data = [ + 'client_id' => $c->hashed_id, + 'description' => 'Test Task', + 'time_log' => '[[1681165417,1681165432,"sumtin",true],[1681165446,0]]', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson("/api/v1/tasks", $data); + + $response->assertStatus(200); + $arr = $response->json(); + + $this->assertEquals(41, $arr['data']['rate']); + } + + public function testTaskProjectRateSet() + { + + $p = Project::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->client->id, + 'name' => 'proggy', + 'task_rate' => 101, + ]); + + $data = [ + 'project_id' => $p->hashed_id, + 'client_id' => $this->client->id, + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'description' => 'Test Task', + 'time_log' => '[[1681165417,1681165432,"sumtin",true],[1681165446,0]]', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson("/api/v1/tasks", $data); + + $response->assertStatus(200); + $arr = $response->json(); + + $this->assertEquals(101, $arr['data']['rate']); + } + + public function testStatusSet() { $data = [