| Current Path : /var/www/html/LLCF-APP/app/Http/Controllers/Admin/ |
| Current File : /var/www/html/LLCF-APP/app/Http/Controllers/Admin/VoucherExpenseController.php |
<?php
namespace App\Http\Controllers\Admin;
use Exception;
use Carbon\Carbon;
use Illuminate\View\View;
use Illuminate\Http\Request;
use App\Models\VoucherExpense;
use Barryvdh\DomPDF\Facade\Pdf;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Session;
use App\Http\Requests\VoucherExpenseFormRequest;
class VoucherExpenseController extends Controller
{
public function index(Request $request): View
{
$query = VoucherExpense::query();
if ($request->filled('title')) {
$query->where('title', 'like', '%' . $request->title . '%');
}
if ($request->filled('date_from')) {
$query->whereDate('created_at', '>=', $request->date_from);
}
if ($request->filled('date_to')) {
$query->whereDate('created_at', '<=', $request->date_to);
}
$voucherExpenses = $query
->orderBy('created_at', 'desc')
->paginate(10)
->withQueryString();
return view('admin.voucher-expenses.index', compact('voucherExpenses'));
}
public function create(): View
{
return view('admin.voucher-expenses.create');
}
public function store(VoucherExpenseFormRequest $request): RedirectResponse
{
try {
VoucherExpense::create($request->validated());
Session::flash('msg.success', 'Voucher expense created successfully.');
return redirect()->route('admin.voucher-expenses.index');
} catch (Exception $e) {
Session::flash('msg.error', 'Failed to create voucher expense: ' . $e->getMessage());
return redirect()->back()->withInput();
}
}
public function edit(int $id): View
{
$voucherExpense = VoucherExpense::findOrFail($id);
return view('admin.voucher-expenses.edit', compact('voucherExpense'));
}
public function update(VoucherExpenseFormRequest $request, int $id): RedirectResponse
{
try {
$voucherExpense = VoucherExpense::findOrFail($id);
$voucherExpense->update($request->validated());
Session::flash('msg.success', 'Voucher expense updated successfully.');
return redirect()->route('admin.voucher-expenses.index');
} catch (Exception $e) {
Session::flash('msg.error', 'Failed to update voucher expense: ' . $e->getMessage());
return redirect()->back()->withInput();
}
}
public function destroy(int $id): RedirectResponse
{
try {
$voucherExpense = VoucherExpense::findOrFail($id);
$voucherExpense->delete();
Session::flash('msg.success', 'Voucher expense deleted successfully.');
return redirect()->route('admin.voucher-expenses.index');
} catch (Exception $e) {
Session::flash('msg.error', 'Failed to delete voucher expense: ' . $e->getMessage());
return redirect()->back();
}
}
public function exportPdf(Request $request)
{
$voucherExpenses = VoucherExpense::query()
->when($request->title, fn ($q) =>
$q->where('title', 'like', '%' . $request->title . '%')
)
->when($request->date_from, fn ($q) =>
$q->whereDate('created_at', '>=', $request->date_from)
)
->when($request->date_to, fn ($q) =>
$q->whereDate('created_at', '<=', $request->date_to)
)
->get();
$pdf = Pdf::loadView('admin.voucher-expenses.exports.pdf', compact('voucherExpenses'));
$filename = 'voucher-expenses-' . Carbon::now()->format('Y-m-d-H:i:s') . '.pdf';
return $pdf->download($filename);
}
public function exportCsv(Request $request)
{
$filename = 'voucher-expenses-' . Carbon::now()->format('Y-m-d-H:i:s') . '.csv';
$voucherExpenses = VoucherExpense::query()
->when($request->title, fn ($q) =>
$q->where('title', 'like', '%' . $request->title . '%')
)
->when($request->date_from, fn ($q) =>
$q->whereDate('created_at', '>=', $request->date_from)
)
->when($request->date_to, fn ($q) =>
$q->whereDate('created_at', '<=', $request->date_to)
)
->get();
$headers = [
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=\"$filename\"",
];
$callback = function () use ($voucherExpenses) {
$handle = fopen('php://output', 'w');
fputcsv($handle, ['Title', 'Created At']);
foreach ($voucherExpenses as $expense) {
fputcsv($handle, [
$expense->title,
$expense->created_at?->format('Y-m-d'),
]);
}
fclose($handle);
};
return response()->stream($callback, 200, $headers);
}
}