Skip to content

Commit

Permalink
Time logged activities
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaisolomon committed Jan 9, 2023
1 parent c43f5d1 commit 72fac8d
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/Exports/ProjectHoursExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function headings(): array
'User',
'Time',
'Hours',
'Activity',
'Date',
];
}
Expand All @@ -45,6 +46,7 @@ public function collection()
'user' => $item->user->name,
'time' => $item->forHumans,
'hours' => number_format($item->value, 2, ',', ' '),
'activity' => $item->activity ? $item->activity->name : '-',
'date' => $item->created_at->format(__('Y-m-d g:i A')),
]))
);
Expand Down
2 changes: 2 additions & 0 deletions app/Exports/TicketHoursExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function headings(): array
'User',
'Time',
'Hours',
'Activity',
'Date',
'Comment',
];
Expand All @@ -41,6 +42,7 @@ public function collection()
'user' => $item->user->name,
'time' => $item->forHumans,
'hours' => number_format($item->value, 2, ',', ' '),
'activity' => $item->activity ? $item->activity->name : '-',
'date' => $item->created_at->format(__('Y-m-d g:i A')),
'comment' => $item->comment
]);
Expand Down
104 changes: 104 additions & 0 deletions app/Filament/Resources/ActivityResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ActivityResource\Pages;
use App\Filament\Resources\ActivityResource\RelationManagers;
use App\Models\Activity;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;

class ActivityResource extends Resource
{
protected static ?string $model = Activity::class;

protected static ?string $navigationIcon = 'heroicon-o-clipboard';

protected static ?int $navigationSort = 1;

protected static function getNavigationLabel(): string
{
return __('Activities');
}

public static function getPluralLabel(): ?string
{
return static::getNavigationLabel();
}

protected static function getNavigationGroup(): ?string
{
return __('Referential');
}

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Card::make()
->schema([
Forms\Components\Grid::make()
->schema([
Forms\Components\TextInput::make('name')
->label(__('Activity name'))
->required()
->maxLength(255),

Forms\Components\RichEditor::make('description')
->label(__('Description'))
->required()
->columnSpan(2),

])
])
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label(__('Activity name'))
->sortable()
->searchable(),

Tables\Columns\TextColumn::make('created_at')
->label(__('Created at'))
->dateTime()
->sortable()
->searchable(),
])
->filters([
//
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
])
->defaultSort('id');
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListActivities::route('/'),
'create' => Pages\CreateActivity::route('/create'),
'view' => Pages\ViewActivity::route('/{record}'),
'edit' => Pages\EditActivity::route('/{record}/edit'),
];
}
}
21 changes: 21 additions & 0 deletions app/Filament/Resources/ActivityResource/Pages/CreateActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Filament\Resources\ActivityResource\Pages;

use App\Filament\Resources\ActivityResource;
use App\Models\TicketType;
use Filament\Resources\Pages\CreateRecord;

class CreateActivity extends CreateRecord
{
protected static string $resource = ActivityResource::class;

protected function afterCreate(): void
{
if ($this->record->is_default) {
TicketType::where('id', '<>', $this->record->id)
->where('is_default', true)
->update(['is_default' => false]);
}
}
}
30 changes: 30 additions & 0 deletions app/Filament/Resources/ActivityResource/Pages/EditActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Filament\Resources\ActivityResource\Pages;

use App\Filament\Resources\ActivityResource;
use App\Models\TicketStatus;
use Filament\Pages\Actions;
use Filament\Resources\Pages\EditRecord;

class EditActivity extends EditRecord
{
protected static string $resource = ActivityResource::class;

protected function getActions(): array
{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
];
}

protected function afterSave(): void
{
if ($this->record->is_default) {
TicketStatus::where('id', '<>', $this->record->id)
->where('is_default', true)
->update(['is_default' => false]);
}
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/ActivityResource/Pages/ListActivities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\ActivityResource\Pages;

use App\Filament\Resources\ActivityResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;

class ListActivities extends ListRecords
{
protected static string $resource = ActivityResource::class;

protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/ActivityResource/Pages/ViewActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\ActivityResource\Pages;

use App\Filament\Resources\ActivityResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewActivity extends ViewRecord
{
protected static string $resource = ActivityResource::class;

protected function getActions(): array
{
return [
Actions\EditAction::make(),
];
}
}
11 changes: 10 additions & 1 deletion app/Filament/Resources/TicketResource/Pages/ViewTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use App\Exports\TicketHoursExport;
use App\Filament\Resources\TicketResource;
use App\Models\Activity;
use App\Models\TicketComment;
use App\Models\TicketHour;
use App\Models\TicketSubscriber;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\TimePicker;
Expand Down Expand Up @@ -99,7 +101,13 @@ protected function getActions(): array
->label(__('Time to log'))
->numeric()
->required(),

Select::make('activity_id')
->label(__('Activity'))
->searchable()
->reactive()
->options(function ($get, $set) {
return Activity::all()->pluck('name', 'id')->toArray();
}),
Textarea::make('comment')
->label(__('Comment'))
->rows(3),
Expand All @@ -109,6 +117,7 @@ protected function getActions(): array
$comment = $data['comment'];
TicketHour::create([
'ticket_id' => $this->record->id,
'activity_id' => $data['activity_id'],
'user_id' => auth()->user()->id,
'value' => $value,
'comment' => $comment
Expand Down
16 changes: 16 additions & 0 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Activity extends Model
{
use HasFactory;

protected $fillable = [
'name',
'description'
];
}
7 changes: 6 additions & 1 deletion app/Models/TicketHour.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TicketHour extends Model
use HasFactory;

protected $fillable = [
'user_id', 'ticket_id', 'value', 'comment'
'user_id', 'ticket_id', 'value', 'comment', 'activity_id'
];

public function user(): BelongsTo
Expand All @@ -26,6 +26,11 @@ public function ticket(): BelongsTo
return $this->belongsTo(Ticket::class, 'ticket_id', 'id');
}

public function activity(): BelongsTo
{
return $this->belongsTo(Activity::class, 'activity_id', 'id');
}

public function forHumans(): Attribute
{
return new Attribute(
Expand Down
35 changes: 35 additions & 0 deletions database/migrations/2023_01_09_113159_create_activities_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('activities', function (Blueprint $table) {
$table->id();

$table->string('name');
$table->mediumText('description');

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activities');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('ticket_hours', function (Blueprint $table) {
$table->foreignId('activity_id')->nullable()->constrained('activities');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('ticket_hours', function (Blueprint $table) {
$table->dropForeign(['activity_id']);
$table->dropColumn('activity_id');
});
}
};
6 changes: 6 additions & 0 deletions resources/views/filament/resources/tickets/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ class="text-danger-500 text-xs hover:text-danger-600 hover:underline">
{{ $item->forHumans }}
</span>
</div>
<div class="w-full flex items-center gap-1">
<span class="text-gray-400">{{ __('Activity:') }}</span>
<span class="text-primary-500 font-medium">
{{ $item->activity ? $item->activity->name : '-' }}
</span>
</div>
@if($item->comment)
<div class="w-full">
{!! nl2br(e($item->comment)) !!}
Expand Down

0 comments on commit 72fac8d

Please sign in to comment.