Files
streamline-emr/docker/statistics/Modules/PatientFinance/Http/Controllers/PatientPaymentMethodsController.php
T
2025-03-31 03:46:05 +03:00

188 lines
6.7 KiB
PHP
Executable File

<?php
namespace Modules\PatientFinance\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Streamline\Models\FinancePointTag;
use Streamline\Models\PatientPaymentMethod;
use Streamline\Models\PaymentMethodsTransaction;
use Streamline\Models\User;
use Streamline\Services\PatientFinance\PatientPaymentMethodService;
use Streamline\Services\UserService;
class PatientPaymentMethodsController extends Controller {
public function __construct(
protected UserService $userService,
protected PatientPaymentMethodService $paymentMethodService,
)
{
}
public function index() {
$methods = PatientPaymentMethod::get();
$users = $this->userService->pluckUserFullName();
return view('patient_finance::patient_payment_methods.index', compact('methods', 'users'));
}
public function create() {
return view('patient_finance::patient_payment_methods.create');
}
public function store(Request $request) {
request()->validate([
'name' => 'required'
]);
$method = new PatientPaymentMethod;
$logged_in_user_id = Auth::user()->id;
$method->name = $request->name;
$method->is_ref_required = $request->is_ref_required;
$method->created_by = $logged_in_user_id;
$method->updated_by = $logged_in_user_id;
try {
if ($method->name != "" || !is_null($method->name)) {
$method->save();
}
flash("Payment method has been saved")->success();
return redirect("/patient_payment_methods");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
public function edit($id) {
$method = PatientPaymentMethod::where(['id' => $id])->first();
if (!$method) {
flash()->error("Payment method not found");
return redirect('/patient_payment_methods');
} else {
return view('patient_finance::patient_payment_methods.edit', compact('method'));
}
}
public function update(Request $request, $id) {
request()->validate([
'name' => 'required'
]);
//validation passed
$method = PatientPaymentMethod::find($id);
$method->name = $request->name;
$method->is_ref_required = $request->is_ref_required;
try {
$method->save();
flash("Payment method has been updated")->success();
return redirect("/patient_payment_methods");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
public function delete($id) {
$method = PatientPaymentMethod::find($id);
if ($method->delete()) {
flash("Payment method has been deleted.")->success();
return redirect('/patient_payment_methods');
}
}
public function inactive() {
$methods = PatientPaymentMethod::onlyTrashed()
->orderBy('name', 'asc')
->paginate(50);
if (count($methods) < 1) {
flash()->error("There is no inactive payment method");
return redirect('/patient_payment_methods');
} else {
return view('patient_finance::patient_payment_methods.inactive', compact('methods'));
}
}
public function activate($id) {
$method = PatientPaymentMethod::withTrashed()->find($id);
if($method->restore()){
flash("Payment method has been activated.")->success();
return redirect('/patient_payment_methods/inactive');
}
}
public function reports(Request $request) {
$search_text = "";
$filters = [];
$payment_methods = PatientPaymentMethod::pluck('name', 'id');
$tags = FinancePointTag::pluck('name', 'id');
if (isset($request->payment_method) && $request->payment_method != -1) {
array_push($filters, ['payment_method_id', '=', $request->payment_method]);
$search_text .= "Payment Method: " . (($request->payment_method == 0) ? "Cash" : $payment_methods[$request->payment_method]) . " | ";
}
if (isset($request->date_period)) {
switch ($request->date_period) {
default:
case "today":
$search_text .= "Today" . " | ";
$start = Carbon::today()->startOfDay();
$end = Carbon::today()->endOfDay();
break;
case "yesterday":
$search_text .= "Yesterday" . " | ";
$start = Carbon::yesterday()->startOfDay();
$end = Carbon::yesterday()->endOfDay();
break;
case "custom_date":
$search_text .= "On: " . streamline_date($request->start_date) . " | ";
$start = Carbon::createFromFormat('d-m-Y', $request->start_date)->startOfDay();
$end = Carbon::createFromFormat('d-m-Y', $request->start_date)->endOfDay();
break;
case "custom_date_range":
$search_text .= "From: " . streamline_date($request->start_date) . " to " . streamline_date($request->end_date) . " | ";
$start = Carbon::createFromFormat('d-m-Y', $request->start_date)->startOfDay();
$end = Carbon::createFromFormat('d-m-Y', $request->end_date)->endOfDay();
break;
}
} else {
$search_text .= "Today" . " | ";
$start = Carbon::today()->startOfDay();
$end = Carbon::today()->endOfDay();
}
if (isset($request->staff_member) && $request->staff_member != 0) {
$records = PaymentMethodsTransaction::where('created_by', $request->staff_member)
->where($filters)
->whereBetween('created_at', [$start, $end])
->get();
$search_text .= "Staff Member: " . get_full_name($request->staff_member, 'id', 'first_name', 'last_name', 'users');
} else {
$search_text .= "Staff Member: All";
$records = PaymentMethodsTransaction::where($filters)->whereBetween('created_at', [$start, $end])->get();
}
$users = $this->userService->pluckUserFullName();
return view('patient_finance::patient_payment_methods.reports', compact('users', 'search_text', 'records', 'payment_methods', 'tags'));
}
public function isRefRequired(Request $request): int
{
return $this->paymentMethodService->isRefRequired($request->id);
}
}