Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/Filament/Resources/TicketResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Carbon\Carbon;
use App\Models\IssueSource;
use Filament\Tables\Columns\ViewColumn;
use App\Models\ProblemCategory;

class TicketResource extends Resource
{
Expand Down Expand Up @@ -133,12 +134,19 @@ public static function form(Form $form): Form
->columnSpan(2)
->disabled(),

// input kategori masalah tiket (problem category)
Forms\Components\Select::make('problem_category_id')
->label(__('Problem Category'))
->searchable()
->columnSpan(3)
->options(fn() => ProblemCategory::all()->pluck('name', 'id')->toArray()),

// Input nama tiket
Forms\Components\TextInput::make('name')
->label(__('Ticket name'))
->required()
->columnSpan(
fn($livewire) => !($livewire instanceof CreateRecord) ? 10 : 12
fn($livewire) => !($livewire instanceof CreateRecord) ? 7 : 9
)
->maxLength(255),
]),
Expand Down
197 changes: 175 additions & 22 deletions app/Filament/Widgets/TicketDuplicateChart.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,124 @@
<?php

// namespace App\Filament\Widgets;

// use Filament\Widgets\Widget;
// use Filament\Forms\Contracts\HasForms;
// use Filament\Forms\Concerns\InteractsWithForms;
// use Filament\Forms\Components\Select;
// use Illuminate\Support\Facades\DB;
// use Carbon\Carbon;
// use Filament\Forms\Components\Grid;

// class TicketDuplicateChart extends Widget implements HasForms
// {
// use InteractsWithForms;

// protected static string $view = 'filament.widgets.ticket-duplicate-chart';

// public function getHeading(): string
// {
// return __('Trend duplicates');
// }

// // ✅ deklarasi semua properti Livewire
// public int $month;
// public int $year;

// public function mount(): void
// {
// $this->month = now()->month;
// $this->year = now()->year;

// $this->form->fill([
// 'month' => $this->month,
// 'year' => $this->year,
// ]);
// }

// protected function getFormSchema(): array
// {
// return [
// Grid::make(4)->schema([
// Select::make('month')
// ->label(__('Month'))
// ->options($this->getMonths())
// ->reactive(),

// Select::make('year')
// ->label(__('Year'))
// ->options($this->getYears())
// ->reactive(),
// ])
// ];
// }

// protected function getMonths(): array
// {
// return collect(range(1, 12))
// ->mapWithKeys(fn ($m) => [$m => Carbon::create()->month($m)->translatedFormat('F')])
// ->toArray();
// }

// protected function getYears(): array
// {
// $now = now()->year;
// return collect(range($now - 2, $now))
// ->mapWithKeys(fn ($y) => [$y => $y])
// ->toArray();
// }

// public function getChartData(): array
// {

// $state = $this->form->getState();

// $tickets = DB::table('ticket_relations')
// ->select('relation_id', DB::raw('COUNT(*) as total'))
// ->whereYear('created_at', $state['year'])
// ->whereMonth('created_at', $state['month'])
// ->groupBy('relation_id')
// ->orderByDesc('total')
// ->get();


// return [
// 'labels' => $tickets->pluck('relation_id')->map(fn ($id) => 'ID Tiket : ' . $id)->values(),
// 'data' => $tickets->pluck('total'),
// 'urls' => $tickets->pluck('relation_id')->map(fn ($id) => route('filament.admin.resources.tickets.view', ['record' => $id])),
// ];
// }

// public function getColumnSpan(): int|string|array
// {
// return 'full'; // biar full 1 row
// }


// public function updated($name, $value): void
// {
// $chartData = $this->getChartData();

// $this->dispatch('updateDuplicateChart', [
// 'labels' => $chartData['labels']->toArray(),
// 'data' => $chartData['data']->toArray(),
// 'urls' => $chartData['urls']->toArray(),
// ]);
// }

// public function initChart()
// {
// $chartData = $this->getChartData();

// $this->dispatch('renderDuplicateChart', [
// 'labels' => $chartData['labels']->toArray(),
// 'data' => $chartData['data']->toArray(),
// 'urls' => $chartData['urls']->toArray(),
// ]);
// }
// }


namespace App\Filament\Widgets;

use Filament\Widgets\Widget;
Expand All @@ -9,6 +128,7 @@
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Filament\Forms\Components\Grid;
use App\Models\ProblemCategory;

class TicketDuplicateChart extends Widget implements HasForms
{
Expand All @@ -22,34 +142,61 @@ public function getHeading(): string
}

// ✅ deklarasi semua properti Livewire
public int $month;
public int $year;
public int $start_month;
public int $start_year;
public int $end_month;
public int $end_year;
public int $problem_category_id;

public function mount(): void
{
$this->month = now()->month;
$this->year = now()->year;
$this->start_month = now()->subMonths(2)->month;
$this->start_year = now()->subMonths(2)->year;
$this->end_month = now()->month;
$this->end_year = now()->year;
$this->problem_category_id = ProblemCategory::first()->id; // Default to 'Semua'

$this->form->fill([
'month' => $this->month,
'year' => $this->year,
'start_month' => $this->start_month,
'start_year' => $this->start_year,
'end_month' => $this->end_month,
'end_year' => $this->end_year,
'problem_category_id' => $this->problem_category_id // Default to 'Semua'
]);
}

protected function getFormSchema(): array
{
return [
Grid::make(4)->schema([
Select::make('month')
->label(__('Month'))
Grid::make(5)->schema([
Select::make('start_month')
->label(__('Start month'))
->options($this->getMonths())
->reactive(),

Select::make('year')
->label(__('Year'))
Select::make('start_year')
->label(__('Start year'))
->options($this->getYears())
->reactive(),
])

Select::make('end_month')
->label(__('End month'))
->options($this->getMonths())
->reactive(),

Select::make('end_year')
->label(__('End year'))
->options($this->getYears())
->reactive(),

Select::make('problem_category_id')
->label(__('Problem Category'))
->options(function () {
return ProblemCategory::all()->pluck('name', 'id')->toArray();
})
->reactive(),
]),

];
}

Expand All @@ -72,20 +219,28 @@ public function getChartData(): array
{

$state = $this->form->getState();
// var_dump($state);

if (!empty($state['start_month']) && !empty($state['start_year']) && !empty($state['end_month']) && !empty($state['end_year'])) {
$start = Carbon::create($state['start_year'], $state['start_month'], 1)->startOfMonth();
$end = Carbon::create($state['end_year'], $state['end_month'], 1)->endOfMonth();
} else {
$start = now()->subMonths(2)->startOfMonth();
$end = now()->endOfMonth();
}

$tickets = DB::table('ticket_relations')
->select('relation_id', DB::raw('COUNT(*) as total'))
->whereYear('created_at', $state['year'])
->whereMonth('created_at', $state['month'])
->groupBy('relation_id')
->orderByDesc('total')
$tickets = DB::table('tickets')
->selectRaw("DATE_FORMAT(created_at, '%Y-%m') as bulan, COUNT(*) as total")
->whereBetween('created_at', [$start, $end])
->where('problem_category_id', (int)$state['problem_category_id'])
->groupBy('bulan')
->orderBy('bulan')
->get();


return [
'labels' => $tickets->pluck('relation_id')->map(fn ($id) => 'ID Tiket : ' . $id)->values(),
'labels' => $tickets->pluck('bulan'),
'data' => $tickets->pluck('total'),
'urls' => $tickets->pluck('relation_id')->map(fn ($id) => route('filament.admin.resources.tickets.view', ['record' => $id])),
];
}

Expand All @@ -102,7 +257,6 @@ public function updated($name, $value): void
$this->dispatch('updateDuplicateChart', [
'labels' => $chartData['labels']->toArray(),
'data' => $chartData['data']->toArray(),
'urls' => $chartData['urls']->toArray(),
]);
}

Expand All @@ -113,7 +267,6 @@ public function initChart()
$this->dispatch('renderDuplicateChart', [
'labels' => $chartData['labels']->toArray(),
'data' => $chartData['data']->toArray(),
'urls' => $chartData['urls']->toArray(),
]);
}
}
23 changes: 23 additions & 0 deletions app/Models/ProblemCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

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

class ProblemCategory extends Model
{
use HasFactory;

protected $fillable = [
'name',
'code',
'description',
'is_active',
];

public function tickets()
{
return $this->hasMany(Ticket::class, 'problem_category_id');
}
}
7 changes: 6 additions & 1 deletion app/Models/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Ticket extends Model implements HasMedia
'name', 'content', 'owner_id', 'responsible_id',
'status_id', 'project_id', 'code', 'order', 'type_id',
'priority_id', 'estimation', 'epic_id', 'sprint_id', 'master_application_id', 'milestone_id',
'issue_source_id', 'github_issue_url', 'github_issue_number', 'github_project_item_id',
'issue_source_id', 'github_issue_url', 'github_issue_number', 'github_project_item_id', 'problem_category_id'
];

public static function boot()
Expand Down Expand Up @@ -257,4 +257,9 @@ public function issueSource()
{
return $this->belongsTo(issueSource::class);
}

public function problemCategory()
{
return $this->belongsTo(ProblemCategory::class, 'problem_category_id');
}
}
Loading