diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 3d19ebd..45fb878 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -2,9 +2,72 @@ namespace App\Http\Controllers; +use App\Models\PatienRegistration; +use App\Models\Transaction; +use App\Models\Treatment; use Illuminate\Http\Request; class TransactionController extends Controller { - // + public function index($id) + { + // Fetch the registration data with related models + $registration = PatienRegistration::with(['patient', 'insurance', 'service_room'])->where('id', $id)->first(); + $transactions = Transaction::with(['treatment']) + ->where('patien_registration_id', $id) + ->get(); + $treatments = Treatment::all(); + + // Pass the data to the view + return view('registration.transaction.index', compact('registration', 'transactions', 'treatments')); + } + + public function store(Request $request, $id) + { + // Validate the request data + $validatedData = $request->validate([ + 'treatment_id' => 'required|exists:treatments,id', + 'amount' => 'required|numeric|min:0', + ]); + + // Create a new transaction + Transaction::create([ + 'patien_registration_id' => $id, + 'treatment_id' => $validatedData['treatment_id'], + 'amount' => $validatedData['amount'], + 'user_id' => auth()->user()->id, + ]); + + // Redirect back with a success message + return redirect()->back()->with('success', 'Transaction created successfully.'); + } + + public function update(Request $request, $transId) + { + // Validate the request data + $validatedData = $request->validate([ + 'treatment_id' => 'required|exists:treatments,id', + 'amount' => 'required|numeric|min:0', + ]); + + // Find the transaction and update it + $transaction = Transaction::findOrFail($transId); + $transaction->update([ + 'treatment_id' => $validatedData['treatment_id'], + 'amount' => $validatedData['amount'], + ]); + + // Redirect back with a success message + return redirect()->back()->with('success', 'Transaction updated successfully.'); + } + + public function destroy($transId) + { + // Find the transaction and delete it + $transaction = Transaction::findOrFail($transId); + $transaction->delete(); + + // Redirect back with a success message + return redirect()->back()->with('success', 'Transaction deleted successfully.'); + } } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 6c595d4..bdcd447 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -9,4 +9,12 @@ use Illuminate\Database\Eloquent\SoftDeletes; class Transaction extends Model { use HasFactory, SoftDeletes; + + protected $table = "transactions"; + protected $guarded = ["id"]; + + public function treatment() + { + return $this->belongsTo(Treatment::class, 'treatment_id'); + } } diff --git a/resources/views/registration/index.blade.php b/resources/views/registration/index.blade.php index ba91af3..f4cabd9 100644 --- a/resources/views/registration/index.blade.php +++ b/resources/views/registration/index.blade.php @@ -56,11 +56,13 @@