mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
268 lines
11 KiB
PHP
268 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Modules\ClinicalData\Http\Controllers;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\EyeClinicMainExam;
|
|
use Streamline\Models\EyeGlasses;
|
|
use Streamline\Http\Controllers\Controller;
|
|
use Streamline\Models\OrderedEyeGlasses;
|
|
use Streamline\Models\Patient;
|
|
use Streamline\Services\ItemsStockService;
|
|
use Carbon\Carbon;
|
|
|
|
class EyeGlassesController extends Controller {
|
|
public function __construct(private ItemsStockService $itemsStockService) {
|
|
$this->middleware('auth');
|
|
$this->middleware('permission:eye_glasses-list', ['only' => ['index']]);
|
|
$this->middleware('permission:eye_glasses-create', ['only' => ['create', 'store']]);
|
|
$this->middleware('permission:eye_glasses-edit', ['only' => ['edit', 'update']]);
|
|
$this->middleware('permission:eye_glasses-delete', ['only' => ['destroy', 'inactive', 'activate']]);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index() {
|
|
$eye_glasses = EyeGlasses::orderBy('name', 'asc')->get();
|
|
|
|
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id');
|
|
|
|
return view('clinical_data::eye_glasses.index', compact('eye_glasses', 'chart_of_accounts'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create() {
|
|
$cost_of_goods_accounts = ChartOfAccount::where(['type' => 7])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$inventory_asset_accounts = ChartOfAccount::where(['type' => 10])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$chart_of_accounts = ChartOfAccount::where(['type' => 1])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
|
|
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
|
|
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
|
|
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
|
|
|
|
return view('clinical_data::eye_glasses.create', compact('chart_of_accounts', 'cost_of_goods_accounts', 'inventory_asset_accounts'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request) {
|
|
request()->validate([
|
|
'name' => 'required',
|
|
'price' => 'required',
|
|
'account_id' => 'required'
|
|
]);
|
|
|
|
//validation passed
|
|
$eye_glass = new EyeGlasses;
|
|
$logged_in_user_id = Auth::user()->id;
|
|
|
|
$eye_glass->name = $request->name;
|
|
$eye_glass->buying_price = $request->price;
|
|
$eye_glass->non_insured_price = $request->non_insured_price;
|
|
$eye_glass->account_id = $request->account_id;
|
|
$eye_glass->cost_of_goods_account = $request->cost_of_goods_account;
|
|
$eye_glass->inventory_account = $request->inventory_account;
|
|
$eye_glass->created_by = $logged_in_user_id;
|
|
$eye_glass->updated_by = $logged_in_user_id;
|
|
|
|
try {
|
|
$eye_glass->save();
|
|
flash($request->name . " Eye glasses has been saved")->success();
|
|
return redirect("/opticals/");
|
|
} catch (QueryException $e) {
|
|
flash("An error occurred!")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show($id) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id) {
|
|
$eye_glass = EyeGlasses::where(['id' => $id])->first();
|
|
|
|
$cost_of_goods_accounts = ChartOfAccount::where(['type' => 7])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$inventory_asset_accounts = ChartOfAccount::where(['type' => 10])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$chart_of_accounts = ChartOfAccount::where(['type' => 1])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
|
|
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
|
|
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
|
|
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
|
|
|
|
if (!$eye_glass) {
|
|
flash()->error("Eye Glasses not found");
|
|
return redirect('/opticals/');
|
|
} else {
|
|
return view('clinical_data::eye_glasses.edit', compact('eye_glass', 'chart_of_accounts', 'cost_of_goods_accounts', 'inventory_asset_accounts'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id) {
|
|
request()->validate([
|
|
'name' => 'required',
|
|
'account_id' => 'required'
|
|
]);
|
|
|
|
//validation passed
|
|
$eye_glass = EyeGlasses::find($id);
|
|
$eye_glass->name = $request->name;
|
|
$eye_glass->buying_price = $request->buying_price;
|
|
$eye_glass->non_insured_price = $request->non_insured_price;
|
|
$eye_glass->cost_of_goods_account = $request->cost_of_goods_account;
|
|
$eye_glass->inventory_account = $request->inventory_account;
|
|
$eye_glass->account_id = $request->account_id;
|
|
|
|
try {
|
|
$eye_glass->save();
|
|
flash($request->name . " Eye glasses has been updated")->success();
|
|
return redirect("/opticals/");
|
|
} catch (QueryException $e) {
|
|
flash("An error occurred!")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id) {
|
|
$eye_glass = EyeGlasses::find($id);
|
|
|
|
if ($eye_glass->delete()):
|
|
flash("Eye glasses has been deleted.")->success();
|
|
return redirect('/opticals/');
|
|
endif;
|
|
}
|
|
|
|
public function inactive() {
|
|
$eye_glasses = EyeGlasses::onlyTrashed()
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id');
|
|
|
|
return view('clinical_data::eye_glasses.inactive', compact('eye_glasses', 'chart_of_accounts'));
|
|
}
|
|
|
|
public function activate($id) {
|
|
$eye_glass = EyeGlasses::withTrashed()->find($id);
|
|
|
|
if ($eye_glass->restore()):
|
|
flash("Eye glasses have been activated.")->success();
|
|
return redirect('/opticals/inactive');
|
|
endif;
|
|
}
|
|
public function get_optical_pricing(Request $request){
|
|
$item_id = $request->item;
|
|
$patient_id = $request->patient_id;
|
|
$patient_insurance_status = $request->patient_insurance_status ?? 0;
|
|
$optical = EyeGlasses::find($item_id);
|
|
if(is_numeric($patient_insurance_status) && $patient_insurance_status == 1 && $patient_id != 0 && is_numeric($patient_id)) {
|
|
$selling_price = get_item_insurance_co_payment($patient_id, $optical->id, 7, false, 0);
|
|
} else {
|
|
// check if the patient category is attached to a price list
|
|
$price_list_id = is_patient_category_attached_to_price_list($patient_id);
|
|
if ($price_list_id) {
|
|
$selling_price = get_price_list_category_price($price_list_id, 7, $optical->id);
|
|
} else {
|
|
$selling_price = get_name($optical->id, "id", "non_insured_price", "eye_glasses");
|
|
}
|
|
}
|
|
if (stock_levels_to_consider() == 0) {
|
|
$total_stock = $this->itemsStockService->getItemAllQuantityByTotal($optical->id, 7);
|
|
} else {
|
|
$total_stock = $this->itemsStockService->getItemPharmacyQuantity($optical->id, 7);
|
|
}
|
|
$out_of_stock = $total_stock < 1;
|
|
$out_of_stock = ($out_of_stock)?1:0;
|
|
$drug_expiry_date = $this->itemsStockService->getItemLatestExpiryDate($optical->id, 7) ?? $optical->expiry_date;
|
|
if (Carbon::createFromFormat('Y-m-d', $drug_expiry_date)->isBefore(Carbon::tomorrow())) {
|
|
$optical_is_expired = 1;
|
|
} else {
|
|
$optical_is_expired = 0;
|
|
}
|
|
return response()->json(['selling_price' => $selling_price,'total_stock'=>$out_of_stock,'expiry'=>$optical_is_expired]);
|
|
}
|
|
public function order(){
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
$patient = Patient::where('id', $patient_id)->first();
|
|
|
|
$eye_glasses = EyeGlasses::orderBy('name')->pluck('name', 'id');
|
|
|
|
$ordered_eye_glasses = OrderedEyeGlasses::where([
|
|
'patient_id' => $patient_id,
|
|
'episode_id' => $episode_id,
|
|
'payment_status' => 0
|
|
])->first();
|
|
|
|
return view('clinical_data::eye_glasses.order', compact('patient', 'patient_id', 'episode_id', 'eye_glasses', 'ordered_eye_glasses'));
|
|
}
|
|
|
|
public function submit_order(Request $request){
|
|
if(is_null($request->optical_id) || is_null($request->optical_id[0])) {
|
|
flash("No optical item has been selected")->error();
|
|
return back()->withInput();
|
|
} else {
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
|
|
$optical_ids_array = $request->optical_id;
|
|
$quantity_array = $request->item_quantity;
|
|
|
|
if (isset($request->order_id) && get_name($request->order_id, 'id', 'payment_status', 'ordered_eye_glasses') == 0) {
|
|
$optical_order = OrderedEyeGlasses::find($request->order_id);
|
|
$optical_order->updated_by = Auth::id();
|
|
} else {
|
|
$optical_order = new OrderedEyeGlasses;
|
|
$optical_order->patient_id = $patient_id;
|
|
$optical_order->episode_id = $episode_id;
|
|
$optical_order->payment_status = 0;
|
|
$optical_order->created_by = Auth::id();
|
|
}
|
|
|
|
$optical_order->eye_glasses_id = implode(",", $optical_ids_array);
|
|
$optical_order->quantity = implode(",", $quantity_array);
|
|
$optical_order->save();
|
|
|
|
flash("Optical Items have been saved")->success();
|
|
|
|
// redirect to consultation or patient_episode page depending on where the user is from
|
|
if (session()->has('redirect_to_consultation')) {
|
|
$url = session()->get('redirect_to_consultation');
|
|
session()->forget('redirect_to_consultation');
|
|
return redirect($url);
|
|
} else {
|
|
return redirect("/patient_episodes");
|
|
}
|
|
}
|
|
}
|
|
|
|
public function cancel_ordered_glasses($order_id) {
|
|
$orders = OrderedEyeGlasses::where(['id' => $order_id, 'payment_status' => 0])->delete();
|
|
|
|
if ($orders) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|