Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

clockobot/clockobot

46 stars
4 code files
View clockobot/clockobot on GitHub

app/Helpers.php

Open in GitHub
if (! function_exists('process_hours_total')) {
function process_hours_total($entries)
{
$totalMinutes = 0;
 
foreach ($entries as $time_entry) {
[$hours, $minutes] = explode(':', $time_entry->calculateDurationInHours());
$totalMinutes += $hours * 60 + $minutes;
}
 
$hours = floor($totalMinutes / 60);
$minutes = $totalMinutes % 60;
 
return $hours.':'.str_pad($minutes, 2, '0', STR_PAD_LEFT);
}
}
 
if (! function_exists('export_time_entries')) {
function export_time_entries($entries)
{
return Excel::download(new ReportExport($entries), 'report.xlsx');
}
}

composer.json

Open in GitHub
{
// ...
 
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
 
// ...
}

app/Livewire/Reporting/ReportingIndex.php

Open in GitHub
use Livewire\Attributes\Computed;
 
class ReportingIndex extends Component
{
// ...
 
#[Computed]
public function hours_total()
{
return process_hours_total($this->time_entries());
}
 
// ...
}

tests/Feature/HelpersTest.php

Open in GitHub
use App\Models\Client;
use App\Models\Project;
use App\Models\TimeEntry;
use App\Models\WorkType;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Maatwebsite\Excel\Facades\Excel;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
 
class HelpersTest extends TestCase
{
use RefreshDatabase;
 
#[Group('helpers')]
public function test_process_hours_total_function(): void
{
$this->createLoggedUser();
 
$client = Client::factory()->create();
Project::factory()->create(['client_id' => $client->id]);
WorkType::factory()->create();
$timeEntries = TimeEntry::factory()->count(3)->make([
'start' => now()->subHours(1),
'end' => now(),
]);
 
$total = process_hours_total($timeEntries);
 
$this->assertEquals('3:00', $total); // 3 entries each with 1 hour, so total is 3 hours
}
 
#[Group('helpers')]
public function test_export_time_entries(): void
{
Excel::fake();
 
$timeEntries = TimeEntry::factory()->count(5)->make();
 
export_time_entries($timeEntries);
 
Excel::assertDownloaded('report.xlsx');
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.