Fixes for rounding

This commit is contained in:
David Bomba 2024-05-10 07:19:52 +10:00
parent a18806f986
commit 01dde73e95
3 changed files with 93 additions and 49 deletions

View File

@ -537,16 +537,6 @@ class TaskController extends BaseController
return $this->listResponse(Task::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
/**
* Returns a client statement.
*
* @return void [type] [description]
*/
public function statement()
{
//todo
}
/**
* Update the specified resource in storage.
*

View File

@ -254,11 +254,11 @@ class TaskRepository extends BaseRepository
public function roundTimeLog(int $start_time, int $end_time): int
{
if($this->task_round_to_nearest == 1)
if($this->task_round_to_nearest == 1 || $end_time == 0)
return $end_time;
$interval = $end_time - $start_time;
if($this->task_round_up)
return $start_time + (int)ceil($interval/$this->task_round_to_nearest)*$this->task_round_to_nearest;
@ -266,17 +266,6 @@ class TaskRepository extends BaseRepository
}
// public function roundTimeLog(int $end_time): int
// {
// if($this->task_round_to_nearest == 1)
// return $end_time;
// if($this->task_round_up)
// return (int)ceil($end_time/$this->task_round_to_nearest)*$this->task_round_to_nearest;
// return (int)floor($end_time/$this->task_round_to_nearest) * $this->task_round_to_nearest;
// }
public function stop(Task $task)
{
$this->init($task);

View File

@ -12,6 +12,14 @@
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\Task;
use App\Models\Client;
use Tests\MockAccountData;
use App\Utils\Traits\MakesHash;
use App\DataMapper\ClientSettings;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -23,10 +31,23 @@ class TaskRoundingTest extends TestCase
public bool $task_round_up = true;
protected function setUp(): void
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
private $faker;
protected function setUp() :void
{
parent::setUp();
$this->makeTestData();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
}
public function testRoundUp()
@ -70,50 +91,94 @@ class TaskRoundingTest extends TestCase
$this->assertEquals($rounded, $this->roundTimeLog($start_time, $end_time));
// $s = \Carbon\Carbon::createFromTimestamp($start_time);
// $e = \Carbon\Carbon::createFromTimestamp($end_time);
// $x = \Carbon\Carbon::createFromTimestamp($rounded);
// echo $s->format('Y-m-d H:i:s').PHP_EOL;
// echo $e->format('Y-m-d H:i:s').PHP_EOL;
// echo $x->format('Y-m-d H:i:s').PHP_EOL;
$s = \Carbon\Carbon::createFromTimestamp($start_time);
}
$e = \Carbon\Carbon::createFromTimestamp($end_time);
public function testRoundUp4()
{
$start_time = 1715238000;
$end_time = 1715238900;
$x = \Carbon\Carbon::createFromTimestamp($rounded);
$this->task_round_to_nearest = 900;
$rounded = $start_time + 60*15;
// $s = \Carbon\Carbon::createFromTimestamp($start_time);
// $e = \Carbon\Carbon::createFromTimestamp($end_time);
// $x = \Carbon\Carbon::createFromTimestamp($rounded);
// echo $s->format('Y-m-d H:i:s').PHP_EOL;
// echo $e->format('Y-m-d H:i:s').PHP_EOL;
// echo $x->format('Y-m-d H:i:s').PHP_EOL;
// echo $s->format('Y-m-d H:i:s').PHP_EOL;
// echo $e->format('Y-m-d H:i:s').PHP_EOL;
// echo $x->format('Y-m-d H:i:s').PHP_EOL;
$this->assertEquals($rounded, $this->roundTimeLog($start_time, $end_time));
}
// public function testRoundUp4()
// {
public function testRoundingViaBulkAction()
{
$this->company->settings->default_task_rate = 41;
$this->company->settings->task_round_to_nearest = 900;
$this->company->settings->task_round_up = true;
$this->company->saveSettings($this->company->settings, $this->company);
$settings = ClientSettings::defaults();
$settings->default_task_rate = 41;
$settings->task_round_to_nearest = 900;
$settings->task_round_up = true;
$c = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
// 'settings' => $settings,
]);
// $start_time = 1715238900;
// $end_time = 1715238000;
// // $end_time = $start_time + 60*15;
// $this->task_round_to_nearest = 900;
$var = time()-800;
// $rounded = $start_time + 60*15;
$data = [
'client_id' => $c->hashed_id,
'description' => 'Test Task',
'time_log' => '[[1681165417,1681165432,"sumtin",true],['.$var.',0]]',
'assigned_user' => [],
'project' => [],
'user' => [],
];
// $this->assertEquals($rounded, $this->roundTimeLog($start_time, $end_time));
$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();
// $s = \Carbon\Carbon::createFromTimestamp($start_time);
$i = $arr['data']['id'];
// $e = \Carbon\Carbon::createFromTimestamp($end_time);
$data = [
'ids' => [$i],
'action' => 'stop'
];
// $x = \Carbon\Carbon::createFromTimestamp($rounded);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson("/api/v1/tasks/bulk", $data);
$response->assertStatus(200);
$arr = $response->json();
// echo $s->format('Y-m-d H:i:s').PHP_EOL;
// echo $e->format('Y-m-d H:i:s').PHP_EOL;
// echo $x->format('Y-m-d H:i:s').PHP_EOL;
// }
$task = Task::find($this->decodePrimaryKey($i));
}
public function testRoundDown()
{