<?php
namespace App\Http\Controllers;
use App\Models\Currency; use App\Models\Expense; use App\Models\ExpenseCategory; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth;
class ExpenseController extends Controller {
 public function index(){ $expenses=Expense::with(['category','currency'])->latest()->paginate(20); return view('expenses.index',compact('expenses')); }
 public function create(){ return view('expenses.form',['expense'=>new Expense(),'categories'=>ExpenseCategory::all(),'currencies'=>Currency::where('is_active',true)->get()]); }
 public function store(Request $request){ $data=$request->validate(['expense_category_id'=>'required|exists:expense_categories,id','currency_id'=>'required|exists:currencies,id','exchange_rate'=>'nullable|numeric','expense_date'=>'required|date','amount'=>'required|numeric|min:0','notes'=>'nullable']); $data['expense_no']='EXP-'.now()->format('YmdHis'); $data['user_id']=Auth::id(); Expense::create($data); return redirect()->route('expenses.index')->with('success','Expense saved.'); }
 public function edit(Expense $expense){ return view('expenses.form',['expense'=>$expense,'categories'=>ExpenseCategory::all(),'currencies'=>Currency::where('is_active',true)->get()]); }
 public function update(Request $request,Expense $expense){ $data=$request->validate(['expense_category_id'=>'required|exists:expense_categories,id','currency_id'=>'required|exists:currencies,id','exchange_rate'=>'nullable|numeric','expense_date'=>'required|date','amount'=>'required|numeric|min:0','notes'=>'nullable']); $expense->update($data); return redirect()->route('expenses.index')->with('success','Expense updated.'); }
}
