designed invoice chart
This commit is contained in:
73
app/Filament/Pages/InvoiceDashboard.php
Normal file
73
app/Filament/Pages/InvoiceDashboard.php
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Pages;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Models\Plant;
|
||||||
|
use Filament\Forms\Components\Select;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Forms\Form;
|
||||||
|
use Filament\Pages\Page;
|
||||||
|
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class InvoiceDashboard extends Page
|
||||||
|
{
|
||||||
|
|
||||||
|
use HasFiltersForm;
|
||||||
|
|
||||||
|
protected static ?string $navigationIcon = 'heroicon-o-document-text';
|
||||||
|
|
||||||
|
protected static ?string $navigationGroup = 'Invoice DashBoard';
|
||||||
|
|
||||||
|
protected static string $view = 'filament.pages.invoice-dashboard';
|
||||||
|
|
||||||
|
|
||||||
|
public function mount(): void
|
||||||
|
{
|
||||||
|
$this->filtersForm->fill([
|
||||||
|
'plant' => Plant::first()?->id // Default to first plant
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function filtersForm(Form $form): Form
|
||||||
|
{
|
||||||
|
return $form
|
||||||
|
->statePath('filters') // Explicitly set where to store form data
|
||||||
|
->schema([
|
||||||
|
Select::make('plant')
|
||||||
|
->options(Plant::pluck('name', 'id'))
|
||||||
|
->label('Select Plant')
|
||||||
|
->reactive()
|
||||||
|
->afterStateUpdated(function ($state) {
|
||||||
|
session(['selec_plant' => $state]);
|
||||||
|
$this->dispatch('invoiceChart');
|
||||||
|
}),
|
||||||
|
Select::make('invoice')
|
||||||
|
->options([
|
||||||
|
'serial_invoice' => 'Serial Invoice',
|
||||||
|
'individual_material' => 'Individual Material Invoice',
|
||||||
|
'bundle_material' => 'Bundle Material Invoice',
|
||||||
|
])
|
||||||
|
->label('Select Invoice')
|
||||||
|
->reactive()
|
||||||
|
->default(0)
|
||||||
|
->afterStateUpdated(function ($state) {
|
||||||
|
session(['select_invoice' => $state]);
|
||||||
|
$this->dispatch('invoiceChart');
|
||||||
|
})
|
||||||
|
])
|
||||||
|
->columns(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getNavigationLabel(): string
|
||||||
|
{
|
||||||
|
return 'Invoice';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function canAccess(): bool
|
||||||
|
{
|
||||||
|
return Auth::check() && Auth::user()->can('view invoice dashboard');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
197
app/Filament/Widgets/InvoiceChart.php
Normal file
197
app/Filament/Widgets/InvoiceChart.php
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Widgets;
|
||||||
|
|
||||||
|
use App\Models\InvoiceValidation;
|
||||||
|
use App\Models\Line;
|
||||||
|
use App\Models\Plant;
|
||||||
|
use Filament\Widgets\ChartWidget;
|
||||||
|
|
||||||
|
class InvoiceChart extends ChartWidget
|
||||||
|
{
|
||||||
|
protected static ?string $heading = 'Chart';
|
||||||
|
|
||||||
|
protected int|string|array $columnSpan = 12;
|
||||||
|
|
||||||
|
protected $listeners = ['invoiceChart'];
|
||||||
|
|
||||||
|
|
||||||
|
protected function getData(): array
|
||||||
|
{
|
||||||
|
$selectedPlant =session('selec_plant');
|
||||||
|
|
||||||
|
$selectedInvoice = session('select_invoice');
|
||||||
|
|
||||||
|
$activeFilter = $this->filter;
|
||||||
|
|
||||||
|
if (!$selectedPlant || !$selectedInvoice) {
|
||||||
|
return
|
||||||
|
[
|
||||||
|
'datasets' => [],
|
||||||
|
'labels' => [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($activeFilter === 'yesterday') {
|
||||||
|
$startDate = now()->subDay()->startOfDay();
|
||||||
|
$endDate = now()->subDay()->endOfDay();
|
||||||
|
$groupBy = 'EXTRACT(HOUR FROM invoice_validations.created_at)';
|
||||||
|
}
|
||||||
|
else if ($activeFilter === 'this_week') {
|
||||||
|
$startDate = now()->startOfWeek(); // Monday 12:00 AM
|
||||||
|
$endDate = now()->endOfWeek(); // Sunday 11:59 PM
|
||||||
|
$groupBy = 'EXTRACT(DOW FROM invoice_validations.created_at)'; // Group by day of week
|
||||||
|
}
|
||||||
|
else if ($activeFilter === 'this_month') {
|
||||||
|
$startDate = now()->startOfMonth();
|
||||||
|
$endDate = now()->endOfMonth();
|
||||||
|
$groupBy = "FLOOR((EXTRACT(DAY FROM invoice_validations.created_at) - 1) / 7) + 1";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$startDate = now()->startOfDay();
|
||||||
|
$endDate = now()->endOfDay();
|
||||||
|
$groupBy = 'EXTRACT(HOUR FROM invoice_validations.created_at)';
|
||||||
|
}
|
||||||
|
|
||||||
|
$fullyScannedInvoiceNumbers = \DB::table('invoice_validations')
|
||||||
|
->select('invoice_number')
|
||||||
|
->where('plant_id', $selectedPlant)
|
||||||
|
->groupBy('invoice_number')
|
||||||
|
->havingRaw("SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END) = COUNT(*)")
|
||||||
|
->pluck('invoice_number');
|
||||||
|
|
||||||
|
//query data
|
||||||
|
$query = \DB::table('invoice_validations')
|
||||||
|
->join('plants', 'invoice_validations.plant_id', '=', 'plants.id')
|
||||||
|
->selectRaw("$groupBy AS time_unit, count(distinct invoice_validations.invoice_number) AS total_quantity")
|
||||||
|
->whereBetween('invoice_validations.created_at', [$startDate, $endDate])
|
||||||
|
->where('invoice_validations.plant_id', $selectedPlant)
|
||||||
|
->whereIn('invoice_validations.invoice_number', $fullyScannedInvoiceNumbers)
|
||||||
|
->when($selectedInvoice === 'serial_invoice', fn($q) =>
|
||||||
|
$q->whereNotNull('invoice_validations.serial_number')
|
||||||
|
->where('invoice_validations.serial_number', '!=', '')
|
||||||
|
)
|
||||||
|
->when($selectedInvoice === 'individual_material', fn($q) =>
|
||||||
|
$q->where('invoice_validations.quantity', 1) // Only individual items
|
||||||
|
)
|
||||||
|
->when($selectedInvoice === 'bundle_material', fn($q) =>
|
||||||
|
$q->where('invoice_validations.quantity', '>', 1) // Assume bundles have quantity > 1
|
||||||
|
)
|
||||||
|
->groupByRaw($groupBy)
|
||||||
|
->orderByRaw($groupBy)
|
||||||
|
->pluck('total_quantity', 'time_unit')
|
||||||
|
->toArray();
|
||||||
|
if ($activeFilter === 'this_month') {
|
||||||
|
$weeksCount = ceil($endDate->day / 7); // Calculate total weeks dynamically
|
||||||
|
$allWeeks = array_fill(1, $weeksCount, 0); // Initialize all weeks with 0
|
||||||
|
$data = array_replace($allWeeks, $query); // Fill missing weeks with 0
|
||||||
|
|
||||||
|
// Generate dynamic week labels
|
||||||
|
$labels = [];
|
||||||
|
for ($i = 1; $i <= $weeksCount; $i++) {
|
||||||
|
$weekStart = $startDate->copy()->addDays(($i - 1) * 7)->format('d M');
|
||||||
|
$weekEnd = $startDate->copy()->addDays($i * 7 - 1)->min($endDate)->format('d M');
|
||||||
|
$labels[] = "Week $i ($weekStart - $weekEnd)";
|
||||||
|
}
|
||||||
|
|
||||||
|
$orderedData = array_values($data);
|
||||||
|
}
|
||||||
|
else if ($activeFilter === 'this_week') {
|
||||||
|
// Correct week labels: ['Mon', 'Tue', ..., 'Sun']
|
||||||
|
$labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||||
|
|
||||||
|
// Initialize default data for all 7 days
|
||||||
|
$data = array_fill(0, 7, 0);
|
||||||
|
|
||||||
|
// Fill in data from query results
|
||||||
|
foreach ($query as $dow => $count) {
|
||||||
|
$data[$dow] = $count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure days are ordered from Monday to Sunday
|
||||||
|
$orderedData = [
|
||||||
|
$data[1] ?? 0, // Monday
|
||||||
|
$data[2] ?? 0, // Tuesday
|
||||||
|
$data[3] ?? 0, // Wednesday
|
||||||
|
$data[4] ?? 0, // Thursday
|
||||||
|
$data[5] ?? 0, // Friday
|
||||||
|
$data[6] ?? 0, // Saturday
|
||||||
|
$data[0] ?? 0, // Sunday (move to last)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Hourly data (same as before)
|
||||||
|
$allHours = array_fill(0, 24, 0);
|
||||||
|
$data = array_replace($allHours, $query);
|
||||||
|
|
||||||
|
// Shift hours for proper display (8 AM to 7 AM)
|
||||||
|
$shiftedKeys = array_merge(range(8, 23), range(0, 8));
|
||||||
|
$orderedData = array_map(fn($hour) => $data[$hour], $shiftedKeys);
|
||||||
|
|
||||||
|
// Labels: ["8 AM", "9 AM", ..., "7 AM"]
|
||||||
|
$labels = array_map(fn ($hour) => date("g A", strtotime("$hour:00")), $shiftedKeys);
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
'datasets' => [
|
||||||
|
[
|
||||||
|
'label' => match ($activeFilter) {
|
||||||
|
'this_week' => "Daily imported excel file count",
|
||||||
|
'this_month' => "Weekly imported excel file count", // Updated Label
|
||||||
|
'yesterday' => "Yesterday's imported excel file count",
|
||||||
|
default => "Today's imported excel file count",
|
||||||
|
},
|
||||||
|
'data' => $orderedData,
|
||||||
|
'borderColor' => 'rgba(75, 192, 192, 1)',
|
||||||
|
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
|
||||||
|
'fill' => false,
|
||||||
|
'tension' => 0.3,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'labels' => $labels,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getType(): string
|
||||||
|
{
|
||||||
|
return 'bar';
|
||||||
|
}
|
||||||
|
public static function getDefaultName(): string
|
||||||
|
{
|
||||||
|
return 'invoice-chart';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getOptions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'scales' => [
|
||||||
|
'y' => [
|
||||||
|
'beginAtZero' => true, //Start Y-axis from 0
|
||||||
|
'ticks' => [
|
||||||
|
'stepSize' => 1,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getFilters(): ?array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'today' => 'Today',
|
||||||
|
'yesterday' => 'Yesterday',
|
||||||
|
'this_week'=> 'This Week',
|
||||||
|
'this_month'=> 'This Month',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function canView(): bool
|
||||||
|
{
|
||||||
|
// dd('Checking route:', request()->route()->getName());
|
||||||
|
// to avoid showing the widget in other pages
|
||||||
|
return request()->routeIs('filament.admin.pages.invoice-dashboard');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
19
resources/views/filament/pages/invoice-dashboard.blade.php
Normal file
19
resources/views/filament/pages/invoice-dashboard.blade.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
<x-filament-panels::page>
|
||||||
|
<div class="space-y-4">
|
||||||
|
{{-- Render the Select form fields --}}
|
||||||
|
<div class="space-y-4">
|
||||||
|
{{ $this->filtersForm($this->form) }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{{-- Render the chart widget below the form --}}
|
||||||
|
<div class="mt-6">
|
||||||
|
@livewire(\App\Filament\Widgets\InvoiceChart::class)
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-filament-panels::page>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user