complete story 6.5 with qa tests
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\ConsultationStatus;
|
||||
use App\Enums\ConsultationType;
|
||||
use App\Enums\PaymentStatus;
|
||||
use App\Models\Consultation;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use League\Csv\Writer;
|
||||
use Livewire\Volt\Component;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
new class extends Component {
|
||||
public string $consultationType = 'all';
|
||||
public string $status = 'all';
|
||||
public string $paymentStatus = 'all';
|
||||
public string $dateFrom = '';
|
||||
public string $dateTo = '';
|
||||
|
||||
public function exportCsv(): ?StreamedResponse
|
||||
{
|
||||
$count = $this->getFilteredConsultations()->count();
|
||||
|
||||
if ($count === 0) {
|
||||
$this->dispatch('notify', type: 'info', message: __('export.no_consultations_match'));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$locale = auth()->user()->preferred_language ?? 'ar';
|
||||
|
||||
return response()->streamDownload(function () use ($locale) {
|
||||
// UTF-8 BOM for Excel Arabic support
|
||||
echo "\xEF\xBB\xBF";
|
||||
|
||||
$csv = Writer::createFromString();
|
||||
|
||||
// Headers based on admin language
|
||||
$csv->insertOne([
|
||||
__('export.client_name', [], $locale),
|
||||
__('export.date', [], $locale),
|
||||
__('export.time', [], $locale),
|
||||
__('export.consultation_type', [], $locale),
|
||||
__('export.status', [], $locale),
|
||||
__('export.payment_status', [], $locale),
|
||||
__('export.problem_summary', [], $locale),
|
||||
]);
|
||||
|
||||
$this->getFilteredConsultations()->cursor()->each(function ($consultation) use ($csv, $locale) {
|
||||
$csv->insertOne([
|
||||
$consultation->user->full_name,
|
||||
$consultation->booking_date->format('Y-m-d'),
|
||||
$consultation->booking_time,
|
||||
__('export.type_'.$consultation->consultation_type->value, [], $locale),
|
||||
__('export.status_'.$consultation->status->value, [], $locale),
|
||||
$this->getPaymentStatusLabel($consultation->payment_status, $locale),
|
||||
$consultation->problem_summary,
|
||||
]);
|
||||
});
|
||||
|
||||
echo $csv->toString();
|
||||
}, 'consultations-export-'.now()->format('Y-m-d').'.csv', [
|
||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||
]);
|
||||
}
|
||||
|
||||
public function exportPdf(): ?StreamedResponse
|
||||
{
|
||||
$consultations = $this->getFilteredConsultations()->get();
|
||||
|
||||
if ($consultations->isEmpty()) {
|
||||
$this->dispatch('notify', type: 'info', message: __('export.no_consultations_match'));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($consultations->count() > 500) {
|
||||
$this->dispatch('notify', type: 'warning', message: __('export.large_export_warning'));
|
||||
}
|
||||
|
||||
$locale = auth()->user()->preferred_language ?? 'ar';
|
||||
|
||||
$pdf = Pdf::loadView('pdf.consultations-export', [
|
||||
'consultations' => $consultations,
|
||||
'locale' => $locale,
|
||||
'generatedAt' => now(),
|
||||
'filters' => $this->getActiveFilters(),
|
||||
'totalCount' => $consultations->count(),
|
||||
]);
|
||||
|
||||
$pdf->setOption('isHtml5ParserEnabled', true);
|
||||
$pdf->setOption('defaultFont', 'DejaVu Sans');
|
||||
|
||||
return response()->streamDownload(
|
||||
fn () => print($pdf->output()),
|
||||
'consultations-export-'.now()->format('Y-m-d').'.pdf'
|
||||
);
|
||||
}
|
||||
|
||||
public function clearFilters(): void
|
||||
{
|
||||
$this->consultationType = 'all';
|
||||
$this->status = 'all';
|
||||
$this->paymentStatus = 'all';
|
||||
$this->dateFrom = '';
|
||||
$this->dateTo = '';
|
||||
}
|
||||
|
||||
public function with(): array
|
||||
{
|
||||
return [
|
||||
'consultationTypes' => [
|
||||
'all' => __('export.all_consultation_types'),
|
||||
'free' => __('export.type_free'),
|
||||
'paid' => __('export.type_paid'),
|
||||
],
|
||||
'statuses' => [
|
||||
'all' => __('export.all_statuses'),
|
||||
'pending' => __('export.status_pending'),
|
||||
'approved' => __('export.status_approved'),
|
||||
'completed' => __('export.status_completed'),
|
||||
'cancelled' => __('export.status_cancelled'),
|
||||
'no_show' => __('export.status_no_show'),
|
||||
'rejected' => __('export.status_rejected'),
|
||||
],
|
||||
'paymentStatuses' => [
|
||||
'all' => __('export.all_payment_statuses'),
|
||||
'pending' => __('export.payment_pending'),
|
||||
'received' => __('export.payment_received'),
|
||||
'na' => __('export.payment_not_applicable'),
|
||||
],
|
||||
'previewCount' => $this->getFilteredConsultations()->count(),
|
||||
];
|
||||
}
|
||||
|
||||
private function getFilteredConsultations()
|
||||
{
|
||||
return Consultation::query()
|
||||
->with('user:id,full_name')
|
||||
->when($this->consultationType !== 'all', fn ($q) => $q->where('consultation_type', $this->consultationType))
|
||||
->when($this->status !== 'all', fn ($q) => $q->where('status', $this->status))
|
||||
->when($this->paymentStatus !== 'all', fn ($q) => $q->where('payment_status', $this->paymentStatus))
|
||||
->when($this->dateFrom, fn ($q) => $q->whereDate('booking_date', '>=', $this->dateFrom))
|
||||
->when($this->dateTo, fn ($q) => $q->whereDate('booking_date', '<=', $this->dateTo))
|
||||
->orderBy('booking_date', 'desc');
|
||||
}
|
||||
|
||||
private function getActiveFilters(): array
|
||||
{
|
||||
$filters = [];
|
||||
|
||||
if ($this->consultationType !== 'all') {
|
||||
$filters['consultation_type'] = __('export.type_'.$this->consultationType);
|
||||
}
|
||||
|
||||
if ($this->status !== 'all') {
|
||||
$filters['status'] = __('export.status_'.$this->status);
|
||||
}
|
||||
|
||||
if ($this->paymentStatus !== 'all') {
|
||||
$filters['payment_status'] = $this->getPaymentStatusLabel(PaymentStatus::from($this->paymentStatus));
|
||||
}
|
||||
|
||||
if ($this->dateFrom) {
|
||||
$filters['date_from'] = $this->dateFrom;
|
||||
}
|
||||
|
||||
if ($this->dateTo) {
|
||||
$filters['date_to'] = $this->dateTo;
|
||||
}
|
||||
|
||||
return $filters;
|
||||
}
|
||||
|
||||
private function getPaymentStatusLabel(PaymentStatus $status, ?string $locale = null): string
|
||||
{
|
||||
return match ($status) {
|
||||
PaymentStatus::Pending => __('export.payment_pending', [], $locale),
|
||||
PaymentStatus::Received => __('export.payment_received', [], $locale),
|
||||
PaymentStatus::NotApplicable => __('export.payment_not_applicable', [], $locale),
|
||||
};
|
||||
}
|
||||
}; ?>
|
||||
|
||||
<div>
|
||||
<div class="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<flux:heading size="xl">{{ __('export.export_consultations') }}</flux:heading>
|
||||
<flux:text class="mt-1 text-zinc-500 dark:text-zinc-400">{{ __('export.export_consultations_description') }}</flux:text>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-6 rounded-lg border border-zinc-200 bg-white p-6 dark:border-zinc-700 dark:bg-zinc-800">
|
||||
<flux:heading size="lg" class="mb-4">{{ __('export.filters_applied') }}</flux:heading>
|
||||
|
||||
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-5">
|
||||
<div>
|
||||
<flux:select wire:model.live="consultationType" :label="__('export.consultation_type')">
|
||||
@foreach ($consultationTypes as $value => $label)
|
||||
<flux:select.option value="{{ $value }}">{{ $label }}</flux:select.option>
|
||||
@endforeach
|
||||
</flux:select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<flux:select wire:model.live="status" :label="__('export.status')">
|
||||
@foreach ($statuses as $value => $label)
|
||||
<flux:select.option value="{{ $value }}">{{ $label }}</flux:select.option>
|
||||
@endforeach
|
||||
</flux:select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<flux:select wire:model.live="paymentStatus" :label="__('export.payment_status')">
|
||||
@foreach ($paymentStatuses as $value => $label)
|
||||
<flux:select.option value="{{ $value }}">{{ $label }}</flux:select.option>
|
||||
@endforeach
|
||||
</flux:select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<flux:input
|
||||
wire:model.live="dateFrom"
|
||||
type="date"
|
||||
:label="__('export.date_from')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<flux:input
|
||||
wire:model.live="dateTo"
|
||||
type="date"
|
||||
:label="__('export.date_to')"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if ($consultationType !== 'all' || $status !== 'all' || $paymentStatus !== 'all' || $dateFrom || $dateTo)
|
||||
<div class="mt-4">
|
||||
<flux:button wire:click="clearFilters" variant="ghost" icon="x-mark" size="sm">
|
||||
{{ __('export.clear_filters') }}
|
||||
</flux:button>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-zinc-200 bg-white p-6 dark:border-zinc-700 dark:bg-zinc-800">
|
||||
<div class="flex flex-col items-center justify-between gap-4 sm:flex-row">
|
||||
<div>
|
||||
<flux:text class="text-zinc-600 dark:text-zinc-400">
|
||||
{{ __('export.total_records') }}: <span class="font-semibold text-zinc-900 dark:text-zinc-100">{{ $previewCount }}</span>
|
||||
</flux:text>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-3">
|
||||
<flux:button
|
||||
wire:click="exportCsv"
|
||||
wire:loading.attr="disabled"
|
||||
wire:target="exportCsv,exportPdf"
|
||||
variant="primary"
|
||||
icon="document-arrow-down"
|
||||
:disabled="$previewCount === 0"
|
||||
>
|
||||
<span wire:loading.remove wire:target="exportCsv">{{ __('export.export_csv') }}</span>
|
||||
<span wire:loading wire:target="exportCsv">{{ __('export.exporting') }}</span>
|
||||
</flux:button>
|
||||
|
||||
<flux:button
|
||||
wire:click="exportPdf"
|
||||
wire:loading.attr="disabled"
|
||||
wire:target="exportCsv,exportPdf"
|
||||
variant="filled"
|
||||
icon="document-text"
|
||||
:disabled="$previewCount === 0"
|
||||
>
|
||||
<span wire:loading.remove wire:target="exportPdf">{{ __('export.export_pdf') }}</span>
|
||||
<span wire:loading wire:target="exportPdf">{{ __('export.exporting') }}</span>
|
||||
</flux:button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if ($previewCount === 0)
|
||||
<div class="mt-6 rounded-lg bg-zinc-50 p-8 text-center dark:bg-zinc-900">
|
||||
<flux:icon name="calendar" class="mx-auto mb-4 h-12 w-12 text-zinc-400" />
|
||||
<flux:text class="text-zinc-500 dark:text-zinc-400">{{ __('export.no_consultations_match') }}</flux:text>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,324 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="{{ $locale }}" dir="{{ $locale === 'ar' ? 'rtl' : 'ltr' }}">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||
<title>{{ __('export.consultations_export_title', [], $locale) }}</title>
|
||||
<style>
|
||||
@page {
|
||||
margin: 100px 50px 80px 50px;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'DejaVu Sans', sans-serif;
|
||||
font-size: 10px;
|
||||
color: #333;
|
||||
direction: {{ $locale === 'ar' ? 'rtl' : 'ltr' }};
|
||||
}
|
||||
|
||||
header {
|
||||
position: fixed;
|
||||
top: -80px;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 70px;
|
||||
border-bottom: 3px solid #D4AF37;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
||||
.header-content {
|
||||
display: table;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.header-left, .header-right {
|
||||
display: table-cell;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
text-align: {{ $locale === 'ar' ? 'right' : 'left' }};
|
||||
}
|
||||
|
||||
.header-right {
|
||||
text-align: {{ $locale === 'ar' ? 'left' : 'right' }};
|
||||
}
|
||||
|
||||
.brand-name {
|
||||
font-size: 22px;
|
||||
font-weight: bold;
|
||||
color: #0A1F44;
|
||||
}
|
||||
|
||||
.brand-subtitle {
|
||||
font-size: 11px;
|
||||
color: #666;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.report-title {
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
color: #0A1F44;
|
||||
}
|
||||
|
||||
footer {
|
||||
position: fixed;
|
||||
bottom: -60px;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 50px;
|
||||
border-top: 2px solid #D4AF37;
|
||||
padding-top: 10px;
|
||||
font-size: 9px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.footer-content {
|
||||
display: table;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.footer-left, .footer-right {
|
||||
display: table-cell;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.footer-left {
|
||||
text-align: {{ $locale === 'ar' ? 'right' : 'left' }};
|
||||
}
|
||||
|
||||
.footer-right {
|
||||
text-align: {{ $locale === 'ar' ? 'left' : 'right' }};
|
||||
}
|
||||
|
||||
.page-number:after {
|
||||
content: counter(page);
|
||||
}
|
||||
|
||||
.filters-section {
|
||||
background-color: #f8f9fa;
|
||||
border: 1px solid #e9ecef;
|
||||
border-radius: 4px;
|
||||
padding: 10px 15px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.filters-title {
|
||||
font-weight: bold;
|
||||
color: #0A1F44;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.filter-item {
|
||||
display: inline-block;
|
||||
margin-{{ $locale === 'ar' ? 'left' : 'right' }}: 15px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.summary {
|
||||
margin-bottom: 15px;
|
||||
color: #0A1F44;
|
||||
}
|
||||
|
||||
.summary strong {
|
||||
color: #D4AF37;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #0A1F44;
|
||||
color: #fff;
|
||||
padding: 10px 8px;
|
||||
text-align: {{ $locale === 'ar' ? 'right' : 'left' }};
|
||||
font-weight: bold;
|
||||
font-size: 9px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
td {
|
||||
padding: 8px;
|
||||
border-bottom: 1px solid #e9ecef;
|
||||
text-align: {{ $locale === 'ar' ? 'right' : 'left' }};
|
||||
font-size: 9px;
|
||||
}
|
||||
|
||||
tr:nth-child(even) {
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
|
||||
tr:hover {
|
||||
background-color: #fff3cd;
|
||||
}
|
||||
|
||||
.status-pending {
|
||||
color: #ffc107;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.status-approved {
|
||||
color: #17a2b8;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.status-completed {
|
||||
color: #28a745;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.status-cancelled {
|
||||
color: #dc3545;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.status-no_show {
|
||||
color: #6c757d;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.status-rejected {
|
||||
color: #dc3545;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.consultation-type {
|
||||
background-color: #e9ecef;
|
||||
padding: 2px 6px;
|
||||
border-radius: 3px;
|
||||
font-size: 8px;
|
||||
}
|
||||
|
||||
.type-free {
|
||||
background-color: #d4edda;
|
||||
color: #155724;
|
||||
}
|
||||
|
||||
.type-paid {
|
||||
background-color: #cce5ff;
|
||||
color: #004085;
|
||||
}
|
||||
|
||||
.problem-summary {
|
||||
max-width: 200px;
|
||||
word-wrap: break-word;
|
||||
font-size: 8px;
|
||||
}
|
||||
|
||||
.no-data {
|
||||
text-align: center;
|
||||
padding: 40px;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="header-content">
|
||||
<div class="header-left">
|
||||
<div class="brand-name">Libra</div>
|
||||
<div class="brand-subtitle">{{ __('export.libra_law_firm', [], $locale) }}</div>
|
||||
</div>
|
||||
<div class="header-right">
|
||||
<div class="report-title">{{ __('export.consultations_export_title', [], $locale) }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<footer>
|
||||
<div class="footer-content">
|
||||
<div class="footer-left">
|
||||
{{ __('export.generated_at', [], $locale) }}: {{ $generatedAt->format($locale === 'ar' ? 'd/m/Y H:i' : 'm/d/Y H:i') }}
|
||||
</div>
|
||||
<div class="footer-right">
|
||||
{{ __('export.page', [], $locale) }} <span class="page-number"></span>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<main>
|
||||
@if(count($filters) > 0)
|
||||
<div class="filters-section">
|
||||
<div class="filters-title">{{ __('export.filters_applied', [], $locale) }}:</div>
|
||||
@foreach($filters as $key => $value)
|
||||
<span class="filter-item">
|
||||
@if($key === 'consultation_type')
|
||||
{{ __('export.consultation_type', [], $locale) }}: {{ $value }}
|
||||
@elseif($key === 'status')
|
||||
{{ __('export.status', [], $locale) }}: {{ $value }}
|
||||
@elseif($key === 'payment_status')
|
||||
{{ __('export.payment_status', [], $locale) }}: {{ $value }}
|
||||
@elseif($key === 'date_from')
|
||||
{{ __('export.date_from', [], $locale) }}: {{ $value }}
|
||||
@elseif($key === 'date_to')
|
||||
{{ __('export.date_to', [], $locale) }}: {{ $value }}
|
||||
@endif
|
||||
</span>
|
||||
@endforeach
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="summary">
|
||||
{{ __('export.total_records', [], $locale) }}: <strong>{{ $totalCount }}</strong>
|
||||
</div>
|
||||
|
||||
@if($consultations->count() > 0)
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ __('export.client_name', [], $locale) }}</th>
|
||||
<th>{{ __('export.date', [], $locale) }}</th>
|
||||
<th>{{ __('export.time', [], $locale) }}</th>
|
||||
<th>{{ __('export.consultation_type', [], $locale) }}</th>
|
||||
<th>{{ __('export.status', [], $locale) }}</th>
|
||||
<th>{{ __('export.payment_status', [], $locale) }}</th>
|
||||
<th>{{ __('export.problem_summary', [], $locale) }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($consultations as $consultation)
|
||||
<tr>
|
||||
<td>{{ $consultation->user->full_name }}</td>
|
||||
<td>{{ $consultation->booking_date->format($locale === 'ar' ? 'd/m/Y' : 'm/d/Y') }}</td>
|
||||
<td>{{ $consultation->booking_time }}</td>
|
||||
<td>
|
||||
<span class="consultation-type type-{{ $consultation->consultation_type->value }}">
|
||||
{{ __('export.type_' . $consultation->consultation_type->value, [], $locale) }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="status-{{ $consultation->status->value }}">
|
||||
{{ __('export.status_' . $consultation->status->value, [], $locale) }}
|
||||
</td>
|
||||
<td>
|
||||
@php
|
||||
$paymentLabel = match($consultation->payment_status->value) {
|
||||
'pending' => __('export.payment_pending', [], $locale),
|
||||
'received' => __('export.payment_received', [], $locale),
|
||||
'na' => __('export.payment_not_applicable', [], $locale),
|
||||
};
|
||||
@endphp
|
||||
{{ $paymentLabel }}
|
||||
</td>
|
||||
<td class="problem-summary">
|
||||
@if(strlen($consultation->problem_summary) > 500)
|
||||
{{ Str::limit($consultation->problem_summary, 500, '...') }}
|
||||
@else
|
||||
{{ $consultation->problem_summary }}
|
||||
@endif
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
@else
|
||||
<div class="no-data">
|
||||
{{ __('export.no_consultations_match', [], $locale) }}
|
||||
</div>
|
||||
@endif
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user