bug fix in clients controller
This commit is contained in:
@@ -13,6 +13,9 @@ use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Barryvdh\DomPDF\Facade\Pdf; // Ensure you have dompdf installed if exporting PDFs
|
||||
|
||||
|
||||
|
||||
class ClientsController extends Controller
|
||||
{
|
||||
@@ -199,14 +202,18 @@ class ClientsController extends Controller
|
||||
|
||||
private function buildFilteredQuery(Request $request)
|
||||
{
|
||||
$query = Models\Client::query();
|
||||
// $query = Models\Client::query();
|
||||
$query = Models\Client::with('account_manager');
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$searchTerm = $request->search;
|
||||
$query->where(function ($q) use ($searchTerm) {
|
||||
$q->where('name', 'like', "%{$searchTerm}%")
|
||||
->orWhere('email', 'like', "%{$searchTerm}%")
|
||||
->orWhere('contact_person', 'like', "%{$searchTerm}%");
|
||||
->orWhere('email', 'like', "%{$searchTerm}%")
|
||||
->orWhere('contact_person', 'like', "%{$searchTerm}%")
|
||||
->orWhereHas('account_manager', function($amQuery) use ($searchTerm) {
|
||||
$amQuery->where('name', 'like', "%{$searchTerm}%");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -240,17 +247,85 @@ class ClientsController extends Controller
|
||||
/**
|
||||
* Handle the Export Request
|
||||
*/
|
||||
|
||||
|
||||
public function export(Request $request)
|
||||
{
|
||||
// Get the filtered data but execute get() instead of paginate()
|
||||
$clients = $this->buildFilteredQuery($request)->get();
|
||||
$format = $request->input('format', 'csv');
|
||||
// Build query using the exact same filters as your data table
|
||||
// $query = Models\Client::query();
|
||||
$query = Models\Client::with('account_manager');
|
||||
|
||||
if ($format === 'pdf') {
|
||||
return $this->exportToPdf($clients);
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->search;
|
||||
$query->where(function($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%")
|
||||
->orWhere('contact_person', 'like', "%{$search}%")
|
||||
->orWhereHas('account_manager', function($amQuery) use ($search) {
|
||||
$amQuery->where('name', 'like', "%{$search}%");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return $this->exportToCsv($clients);
|
||||
if ($request->filled('service')) {
|
||||
$query->whereJsonContains('services', $request->service);
|
||||
}
|
||||
|
||||
if ($request->filled('billing')) {
|
||||
$query->where('pay_mode', $request->billing);
|
||||
}
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->status);
|
||||
}
|
||||
|
||||
$clients = $query->get();
|
||||
$format = $request->get('format', 'csv');
|
||||
|
||||
// Handle CSV Export
|
||||
if ($format === 'csv') {
|
||||
$filename = 'clients_export_' . date('Y-m-d') . '.csv';
|
||||
|
||||
$headers = [
|
||||
"Content-Type" => "text/csv",
|
||||
"Content-Disposition" => "attachment; filename=\"$filename\"",
|
||||
"Pragma" => "no-cache",
|
||||
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
|
||||
"Expires" => "0"
|
||||
];
|
||||
|
||||
$callback = function() use ($clients) {
|
||||
$file = fopen('php://output', 'w');
|
||||
// CSV Header Row
|
||||
fputcsv($file, ['ID', 'Client Name', 'Primary Email', 'Phone', 'Contact Person', 'Account Manager', 'Pay Mode', 'Status', 'Country', 'Date Added']);
|
||||
|
||||
foreach ($clients as $client) {
|
||||
fputcsv($file, [
|
||||
$client->id,
|
||||
$client->name,
|
||||
$client->email,
|
||||
$client->phone,
|
||||
$client->contact_person,
|
||||
$client->account_manager->name,
|
||||
$client->pay_mode,
|
||||
$client->status,
|
||||
$client->country,
|
||||
$client->created_at,
|
||||
]);
|
||||
}
|
||||
fclose($file);
|
||||
};
|
||||
|
||||
return response()->stream($callback, 200, $headers);
|
||||
}
|
||||
|
||||
// Handle PDF Export (Optional: requires barryvdh/laravel-dompdf)
|
||||
if ($format === 'pdf') {
|
||||
$pdf = Pdf::loadView('clients.export-pdf', compact('clients'));
|
||||
return $pdf->download('clients_export_' . date('Y-m-d') . '.pdf');
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -370,7 +445,7 @@ class ClientsController extends Controller
|
||||
'last_modified_by_id' => Auth::user()->id
|
||||
]);
|
||||
}
|
||||
$user_id = $auth_user->id;
|
||||
$user_id = Auth::user()->id;
|
||||
|
||||
Models\UserActivity::create([
|
||||
'type' => 'staff',
|
||||
|
||||
Reference in New Issue
Block a user