<?php
namespace App\Http\Controllers;
use App\Models\Currency; use App\Models\Supplier; use Illuminate\Http\Request;
class SupplierController extends Controller {
 public function index(){ $suppliers=Supplier::with('currency')->paginate(20); return view('suppliers.index',compact('suppliers')); }
 public function create(){ return view('suppliers.form',['supplier'=>new Supplier(),'currencies'=>Currency::where('is_active',true)->get()]); }
 public function store(Request $request){ $data=$request->validate(['name'=>'required','company_name'=>'nullable','phone'=>'nullable','address'=>'nullable','currency_id'=>'nullable|exists:currencies,id','opening_balance'=>'nullable|numeric']); Supplier::create($data); return redirect()->route('suppliers.index')->with('success','Supplier saved.'); }
 public function edit(Supplier $supplier){ return view('suppliers.form',['supplier'=>$supplier,'currencies'=>Currency::where('is_active',true)->get()]); }
 public function update(Request $request,Supplier $supplier){ $data=$request->validate(['name'=>'required','company_name'=>'nullable','phone'=>'nullable','address'=>'nullable','currency_id'=>'nullable|exists:currencies,id','opening_balance'=>'nullable|numeric']); $supplier->update($data); return redirect()->route('suppliers.index')->with('success','Supplier updated.'); }
}
