| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
use App\Traits\TransMsg;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Concerns\FromArray;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
use TransMsg;
public static function sendSuccess($message = "", $data = null, $statusCode = 200, $headers = [])
{
return self::sendResponse($message, $data, true, $statusCode, $headers);
}
public static function sendError($message = "", $data = null, $statusCode = 404, $headers = [])
{
return self::sendResponse($message, $data, false, $statusCode, $headers);
}
private static function sendResponse($message = "", $data = null, $status = true, $statusCode = 200, $headers = [])
{
$response = array(
"status" => $status,
"message" => $message,
);
if ($data && $data != null) {
if (is_array($data)) {
$response = array_merge($response, $data);
} else if ($data instanceof \Illuminate\Database\Eloquent\Model) {
$response['info'] = $data;
} else if ($data instanceof \Illuminate\Database\Eloquent\Collection) {
$response['list'] = $data;
} else if (is_string($data)) {
$response['data'] = $data;
}
}
return response()->json($response, $statusCode, $headers);
}
protected function exportDetailedPDF($records, $filename)
{
$formattedRecords = $records->map(function ($record) {
return collect($record->getAttributes())
->map(function ($val, $key) {
if (Str::contains($key, ['created_at', 'date', 'time'])) {
return \Carbon\Carbon::parse($val)->format('d M Y, h:i A');
}
if (is_array($val) || is_object($val)) {
$val = (array) $val;
if (array_keys($val) !== range(0, count($val) - 1)) {
return collect($val)
->map(fn($v, $k) => ucfirst($k) . ': ' . $v)
->join(', ');
}
return implode(', ', $val);
}
if (Str::contains($key, ['amount', 'total', 'price'])) {
return '₹ ' . number_format((float)$val, 2);
}
if (is_bool($val)) {
return $val ? 'Yes' : 'No';
}
if (in_array($val, [0, 1]) && Str::contains($key, ['is_', 'open_'])) {
return $val ? 'Yes' : 'No';
}
return $val;
})
->toArray();
});
$pdf = \Barryvdh\DomPDF\Facade\Pdf::loadView('reports.pdf', [
'title' => $filename,
'records' => $formattedRecords
]);
return $pdf->download($filename . '_' . now()->format('Ymd_His') . '.pdf');
}
protected function exportDetailedCSV($records, $filename)
{
$filename = $filename . '_' . now()->format('Ymd_His') . '.xlsx';
// If no records exist
if ($records->isEmpty()) {
$data = [
['No Data Found']
];
return Excel::download(new class($data) implements FromArray {
public function __construct(private $data) {}
public function array(): array
{
return $this->data;
}
}, $filename);
}
// Build dynamic header from model attributes
$headers = collect(array_keys($records->first()->getAttributes()))
->map(fn($key) => ucfirst(str_replace('_', ' ', $key)))
->toArray();
// Build formatted rows
$rows = [];
foreach ($records as $record) {
$formatted = collect($record->toArray())
->map(function ($val, $key) {
if (Str::contains($key, ['created_at', 'date', 'time']) && $val) {
return \Carbon\Carbon::parse($val)->format('Y-m-d h:i A');
}
if (is_array($val) || is_object($val)) {
$val = (array)$val;
if (array_keys($val) !== range(0, count($val) - 1)) {
return collect($val)
->map(fn($v, $k) => ucfirst($k) . ': ' . $v)
->join(', ');
}
return implode(', ', $val);
}
if (Str::contains($key, ['amount', 'total', 'price'])) {
return '₹ ' . number_format((float)$val, 2);
}
if (is_bool($val)) {
return $val ? 'Yes' : 'No';
}
if (in_array($val, [0, 1]) && Str::contains($key, ['is_', 'open_'])) {
return $val ? 'Yes' : 'No';
}
return $val;
})
->toArray();
$rows[] = $formatted;
}
// Merge header + rows
$data = array_merge([$headers], $rows);
return Excel::download(new class($data) implements FromArray {
public function __construct(private $data) {}
public function array(): array
{
return $this->data;
}
}, $filename);
}
// protected function exportDetailedCSV($records, $filename)
// {
// $filename = $filename . '_' . now()->format('Ymd_His') . '.csv';
// $headers = [
// 'Content-Type' => 'text/csv',
// 'Content-Disposition' => "attachment; filename=\"$filename\"",
// ];
// $callback = function () use ($records) {
// $file = fopen('php://output', 'w');
// fprintf($file, chr(0xEF) . chr(0xBB) . chr(0xBF));
// if ($records->isEmpty()) {
// fputcsv($file, ['No Data Found']);
// fclose($file);
// return;
// }
// // --- Dynamic header ---
// $headers = collect(array_keys($records->first()->getAttributes()))
// ->map(fn($key) => ucfirst(str_replace('_', ' ', $key)))
// ->toArray();
// fputcsv($file, $headers);
// // --- Format and export each row ---
// foreach ($records as $record) {
// $data = collect($record->toArray())
// ->map(function ($val, $key) {
// // Format timestamps
// if (Str::contains($key, ['created_at', 'date', 'time'])) {
// return \Carbon\Carbon::parse($val)->format('Y-m-d h:i A');
// }
// if (is_array($val) || is_object($val)) {
// $val = (array) $val;
// if (array_keys($val) !== range(0, count($val) - 1)) {
// return collect($val)
// ->map(fn($v, $k) => ucfirst($k) . ': ' . $v)
// ->join(', ');
// }
// return implode(', ', $val);
// }
// // Format money columns
// if (Str::contains($key, ['amount', 'total', 'price'])) {
// return '₹ ' . number_format((float)$val, 2);
// }
// // Format boolean
// if (is_bool($val)) {
// return $val ? 'Yes' : 'No';
// }
// // Convert integer flags (1/0) that look boolean
// if (in_array($val, [0, 1]) && Str::contains($key, ['is_', 'open_'])) {
// return $val ? 'Yes' : 'No';
// }
// return $val;
// })
// ->toArray();
// fputcsv($file, $data);
// }
// fclose($file);
// };
// return response()->stream($callback, 200, $headers);
// }
}