Working on conversion of quotes to projects

This commit is contained in:
David Bomba 2023-09-07 14:51:42 +10:00
parent b47e31e23a
commit b9457d72e2
4 changed files with 109 additions and 6 deletions

View File

@ -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']);
}

View File

@ -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;
}
}

View File

@ -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'];

View File

@ -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 = [