refactor(customer): merubah created at dari table users ke customers

- menambahkan counting total order dan total pembayaran nya di table
- membuat stats
- menambahkan kolom terakhir order di datatable
This commit is contained in:
Yoga Pangestu 2025-11-12 10:27:07 +07:00
parent 30c30c7642
commit d203df9cd8
3 changed files with 56 additions and 5 deletions

View File

@ -6,11 +6,12 @@
use App\Traits\Datatable\WithAppendColumn;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Blade;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\CountColumn;
use Rappasoft\LaravelLivewireTables\Views\Columns\SumColumn;
class CustomersTable extends DataTableComponent
{
@ -44,8 +45,27 @@ public function columns(): array
})
->html(),
Column::make('Terdaftar Sejak', 'user.created_at')
->format(fn ($value) => $value ? Carbon::parse($value)->diffForHumans() : '')
CountColumn::make('Total Order')
->setDataSource('orders')
->label(fn ($value) => currency($value->orders_count))
->sortable(),
SumColumn::make('Total Pembayaran')
->setDataSource('orders', 'total')
->label(fn ($value) => currency($value->orders_sum_total, 'Rp'))
->sortable(),
Column::make('Terakhir Order')
->label(function ($record) {
$latestOrder = $record->orders()->latest()->first();
return $latestOrder
? timeAgo($latestOrder->created_at)
: '';
}),
Column::make('Terdaftar Sejak', 'created_at')
->format(fn ($value) => $value ? timeAgo($value) : '')
->searchable()
->sortable(),
@ -76,6 +96,6 @@ public function columns(): array
public function builder(): Builder
{
return Customer::select('customers.id', 'name', 'phone_number', 'gender')->with('user');
return Customer::select('customers.id', 'name', 'phone_number', 'gender', 'customers.created_at')->with('user', 'orders');
}
}

View File

@ -25,6 +25,26 @@ class Customer extends Component
public string $modalTitle = '';
public array $stats = [];
public function mount()
{
$this->stats = [
[
'title' => 'Total Customer',
'value' => CustomerModel::whereDoesntHave('user')->count(),
],
[
'title' => 'Total Member',
'value' => CustomerModel::whereHas('user')->count(),
],
[
'title' => 'Top Member',
'value' => CustomerModel::whereHas('user')->withCount('orders')->orderByDesc('orders_count')->first()?->name,
],
];
}
#[On('modal:open')]
public function openModal(string $method, string $modalTitle, ?string $id = null)
{

View File

@ -15,6 +15,17 @@
</div>
<div class="mt-6">
@if (!empty($stats))
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
@foreach ($stats as $stat)
<div class="rounded-lg px-6 py-4 bg-zinc-200 dark:bg-zinc-700">
<flux:subheading>{{ $stat['title'] }}</flux:subheading>
<flux:heading size="xl" class="mb-2">{{ $stat['value'] }}</flux:heading>
</div>
@endforeach
</div>
@endif
<livewire:datatable.studio.loyalty.customers-table />
</div>
@ -30,7 +41,7 @@
<flux:error name="form.name" />
</flux:field>
<flux:input label="Nomor Telepon" placeholder="0821 xxxx xxxx" mask="9999 9999 99999"
<flux:input label="Nomor Telepon" placeholder="0821 xxxx xxxx" mask="9999 9999 99999"
wire:model.live.debounce.500ms="form.phone_number" autocomplete="off" />
<flux:field>