mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
496 lines
20 KiB
PHP
Executable File
496 lines
20 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\ClinicalData\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Http\Controllers\StreamlineSetupManager;
|
|
use Streamline\Models\InsuranceClaim;
|
|
use Streamline\Models\PriceListCategories;
|
|
use Streamline\Models\Sundry;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\QueryException;
|
|
use Streamline\Models\Patient;
|
|
use Streamline\Models\GeneralForm;
|
|
use Streamline\Models\OrderedSundry;
|
|
use Streamline\Models\User;
|
|
use Streamline\Services\StreamlineSetupServiceInterface;
|
|
|
|
class SundryController extends Controller {
|
|
protected StreamlineSetupServiceInterface $setupService;
|
|
public function __construct(StreamlineSetupServiceInterface $setupService) {
|
|
$this->middleware('auth');
|
|
$this->middleware('permission:sundry-list', ['only' => ['index']]);
|
|
$this->middleware('permission:sundry-create', ['only' => ['create', 'store']]);
|
|
$this->middleware('permission:sundry-edit', ['only' => ['edit', 'update']]);
|
|
$this->middleware('permission:sundry-delete', ['only' => ['destroy', 'inactive', 'activate']]);
|
|
$this->setupService = $setupService;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index() {
|
|
$getSundries = Sundry::get();
|
|
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id');
|
|
|
|
$sundries = $getSundries->map(function ($user) {
|
|
return collect($user->toArray())
|
|
->all();
|
|
});
|
|
|
|
$ordered_sundries_array = [];
|
|
$ordered_sundries = OrderedSundry::distinct('sundries_id')->get(['sundries_id']);
|
|
foreach ($ordered_sundries as $ordered_sundry) {
|
|
$actual = explode(',', $ordered_sundry->sundries_id);
|
|
foreach ($actual as $value) $ordered_sundries_array[] = $value;
|
|
}
|
|
$ordered_sundries_ids = array_unique($ordered_sundries_array);
|
|
|
|
return view('clinical_data::sundries.index', compact('sundries', 'chart_of_accounts', 'ordered_sundries_ids'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
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();
|
|
$income_accounts = ChartOfAccount::where(['type' => 1])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$forms = ['' => '- select -'] + GeneralForm::where('item_type',2)->pluck('name', 'id')->toArray();
|
|
|
|
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
|
|
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
|
|
$income_accounts = ['' => '- select -'] + $income_accounts;
|
|
|
|
$sundries = StreamlineSetupManager::get_sundries_array();
|
|
|
|
return view('clinical_data::sundries.create', compact('income_accounts','forms', 'inventory_asset_accounts', 'cost_of_goods_accounts','sundries'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request) {
|
|
|
|
if (session()->has('streamline_setup') && isset($request->skip)){
|
|
//Artisan::call('db:seed', ['--class' => 'SundriesTableSeeder']);
|
|
//update the streamline setup table with the new finished step
|
|
$this->setupService->saveStep("sundries registration", 1);
|
|
return redirect("general_settings/edit");
|
|
} else {
|
|
|
|
/*request()->validate([
|
|
'name' => 'required',
|
|
'price' => 'required',
|
|
'account_id' => 'required'
|
|
]);*/
|
|
//dd($request->all());
|
|
|
|
// get all current price lists
|
|
$price_list = PriceListCategories::withTrashed()->select('id')->get();
|
|
$price_list_category = [];
|
|
$price_list_price = [];
|
|
|
|
foreach ($price_list as $record){
|
|
array_push($price_list_category, $record->id);
|
|
array_push($price_list_price, $request->non_insured_price);
|
|
}
|
|
|
|
//validation passed
|
|
$sundry = new Sundry;
|
|
$logged_in_user_id = Auth()->user()->id;
|
|
|
|
$sundry->name = $request->name;
|
|
$sundry->insurance = 0;
|
|
$sundry->available = $request->available;
|
|
$sundry->cost_price = $request->price;
|
|
$sundry->form_id=$request->form_id;
|
|
$sundry->insured_price = 0;
|
|
$sundry->non_insured_price = $request->non_insured_price;
|
|
|
|
if (isset($request->price_list_category_id) && isset($request->price_list_price)) {
|
|
$sundry->price_list_category = !is_null($request->price_list_category_id) ? implode(",", $request->price_list_category_id) : null;
|
|
$sundry->price_list_price = !is_null($request->price_list_price) ? implode(",", $request->price_list_price) : null;
|
|
} else {
|
|
$sundry->price_list_category = implode(",", $price_list_category);
|
|
$sundry->price_list_price = implode(",", $price_list_price);
|
|
}
|
|
|
|
$sundry->account_id = $request->account_id;
|
|
$sundry->inventory_account = $request->inventory_account;
|
|
$sundry->cost_of_goods_account = $request->cog_account;
|
|
$sundry->created_by = $logged_in_user_id;
|
|
$sundry->updated_by = $logged_in_user_id;
|
|
$sundry->created_at = Carbon::now();
|
|
$sundry->updated_at = Carbon::now();
|
|
|
|
try {
|
|
if ($sundry->name != "" && !is_null($sundry->name)) {
|
|
$sundry->save();
|
|
}
|
|
|
|
flash($request->name . " Sundry has been saved")->success();
|
|
|
|
if (session()->has('streamline_setup')) {
|
|
if (isset($request->selected_sundries)) {
|
|
$selected_sundries_array = $request->selected_sundries;
|
|
if (!empty($selected_sundries_array)) {
|
|
|
|
for ($i=0; $i < count($selected_sundries_array) ; $i++) {
|
|
$default_sundry = new Sundry;
|
|
$default_sundry->name = $selected_sundries_array[$i];
|
|
$default_sundry->account_id = 12; //12 == Sundry chart of account
|
|
$default_sundry->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
//update the streamline setup table with the new finished step
|
|
$this->setupService->saveStep("sundries registration", 1);
|
|
return redirect("general_settings/edit");
|
|
}
|
|
return redirect("/sundries/");
|
|
} catch (QueryException $e) {
|
|
flash("An error occurred")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id) {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id) {
|
|
$sundry = Sundry::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();
|
|
$income_accounts = ChartOfAccount::where(['type' => 1])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$forms = ['' => '- select -'] + GeneralForm::where('item_type',2)->pluck('name', 'id')->toArray();
|
|
|
|
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
|
|
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
|
|
$income_accounts = ['' => '- select -'] + $income_accounts;
|
|
|
|
if (!$sundry) {
|
|
flash()->error("Sundry not found");
|
|
return redirect('/sundries/');
|
|
} else {
|
|
return view('clinical_data::sundries.edit', compact('sundry','forms', 'cost_of_goods_accounts', 'inventory_asset_accounts', 'income_accounts'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id) {
|
|
request()->validate([
|
|
'name' => 'required',
|
|
'price' => 'required',
|
|
'account_id' => 'required'
|
|
]);
|
|
|
|
//validation passed
|
|
$sundry = Sundry::find($id);
|
|
|
|
$sundry->name = $request->name;
|
|
$sundry->insurance = 0;
|
|
$sundry->cost_price = $request->price;
|
|
$sundry->available = $request->available;
|
|
$sundry->form_id=$request->form_id;
|
|
$sundry->insured_price = 0;
|
|
$sundry->non_insured_price = $request->non_insured_price;
|
|
$sundry->price_list_category = !is_null($request->price_list_category_id) ? implode(",", $request->price_list_category_id) : null;
|
|
$sundry->price_list_price = !is_null($request->price_list_price) ? implode(",", $request->price_list_price) : null;
|
|
$sundry->account_id = $request->account_id;
|
|
$sundry->inventory_account = $request->inventory_account;
|
|
$sundry->cost_of_goods_account = $request->cog_account;
|
|
|
|
try {
|
|
$sundry->save();
|
|
flash($request->name . " Sundry has been updated")->success();
|
|
return redirect("/sundries/");
|
|
} catch (QueryException $e) {
|
|
flash("An error occurred")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id) {
|
|
$sundry = Sundry::find($id);
|
|
if(empty($sundry->pharmacy_stock) && empty($sundry->store_stock) && empty($sundry->opening_stock)){
|
|
if ($sundry->delete()):
|
|
flash("Sundry has been deleted.")->success();
|
|
return redirect('/sundries/');
|
|
endif;
|
|
}else {
|
|
flash()->error($sundry->name ." is still in stock.");
|
|
return redirect('/sundries/');
|
|
}
|
|
}
|
|
|
|
public function inactive() {
|
|
$sundries = Sundry::onlyTrashed()
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id');
|
|
|
|
if (empty($sundries)) {
|
|
flash()->error("There is no inactive sundry");
|
|
return redirect('/sundries/');
|
|
} else {
|
|
return view('clinical_data::sundries.inactive', compact('sundries', 'chart_of_accounts'));
|
|
}
|
|
}
|
|
|
|
public function activate($id) {
|
|
$sundry = Sundry::withTrashed()->find($id);
|
|
|
|
if ($sundry->restore()):
|
|
flash("Sundry has been activated.")->success();
|
|
return redirect('/sundries/inactive');
|
|
endif;
|
|
}
|
|
|
|
/*
|
|
* order sundries for the patient
|
|
*/
|
|
public function order_sundries() {
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
$patient = Patient::where('id', $patient_id)->first();
|
|
$categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id");
|
|
$sundries = Sundry::where('available', 1)->orderBy('name')->pluck('name', 'id');
|
|
|
|
$ordered_sundries = DB::table('ordered_sundries')
|
|
->whereNull('deleted_at')
|
|
->where(['patient_id'=>$patient_id, 'episode_id'=>$episode_id, 'payment_status' => 0])
|
|
->first();
|
|
|
|
$users_collection = DB::table('users')->orderBy("first_name","asc")->select("id")->get()->toArray();
|
|
$users_array = [];
|
|
|
|
foreach ($users_collection as $value){
|
|
$user = User::find($value->id);
|
|
if (!is_null($user)) {
|
|
$users_array[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users');
|
|
}
|
|
}
|
|
|
|
$users_array = ['' => '- select -'] + $users_array;
|
|
|
|
$employees = DB::table('users')->whereNull('deleted_at')->orderBy('first_name', 'asc')->get();
|
|
|
|
return view('clinical_data::sundries.order_sundries',compact('patient_id','episode_id','patient','categories','employees','sundries','ordered_sundries', 'users_array'));
|
|
}
|
|
|
|
/*
|
|
* store ordered sundries for a patient episode
|
|
*/
|
|
public function store_ordered_sundries(Request $request) {
|
|
if(is_null($request->sundries_id) || is_null($request->sundries_id[0])) {
|
|
flash("No sundry has been selected")->error();
|
|
return back()->withInput();
|
|
} else {
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
$patient_insurance_status = $request->patient_insurance_status ?? 0;
|
|
|
|
$sundry_ids_array = $request->sundries_id;
|
|
$quantity_array = $request->item_quantity;
|
|
if (isset($request->order_id) && get_name($request->order_id, 'id', 'payment_status', 'ordered_sundries') == 0) {
|
|
$sundry_order = OrderedSundry::find($request->order_id);
|
|
} else {
|
|
$sundry_order = new OrderedSundry;
|
|
$sundry_order->patient_id = $patient_id;
|
|
$sundry_order->episode_id = $episode_id;
|
|
$sundry_order->payment_status = 0; //0 by default to mean not paid
|
|
$sundry_order->created_by = auth()->user()->id;
|
|
}
|
|
|
|
/* === save current selling price for future billing not to use the original selling price == */
|
|
$unit_selling_prices_array = [];
|
|
$price_list_id = is_patient_category_attached_to_price_list($patient_id);
|
|
for ($i=0; $i < count($sundry_ids_array) ; $i++) {
|
|
if($patient_insurance_status == 1 && $patient_id != 0 && is_numeric($patient_id)) {
|
|
$opd_sundry_amount = get_item_insurance_co_payment($patient_id, $sundry_ids_array[$i], 5, false, 0);
|
|
} else {
|
|
if ($price_list_id) {
|
|
$opd_sundry_amount = get_price_list_category_price($price_list_id, 5, $sundry_ids_array[$i]);
|
|
} else {
|
|
$opd_sundry_amount = get_name($sundry_ids_array[$i], "id", "non_insured_price", "sundries");
|
|
}
|
|
}
|
|
$unit_selling_prices_array[] = $opd_sundry_amount;
|
|
}
|
|
/* ================ */
|
|
|
|
$sundry_order->sundries_id = implode(",", $sundry_ids_array);
|
|
$sundry_order->quantity = implode(",", $quantity_array);
|
|
$sundry_order->sundries_amount = implode(",", $unit_selling_prices_array);
|
|
$sundry_order->save();
|
|
//removed sundry stock reduction to only reduce at payment irrespective of the setting.
|
|
// if (get_inventory_reduction_point() == 1) {
|
|
// for ($i=0; $i < count($sundry_ids_array) ; $i++) {
|
|
// $sundry = \Streamline\Models\Sundry::withTrashed()->find($sundry_ids_array[$i]);
|
|
// $current_stock = $sundry->store_stock;
|
|
// $new_stock = $current_stock - $quantity_array[$i];
|
|
// $sundry->store_stock = $new_stock;
|
|
// $sundry->update();
|
|
// }
|
|
// }
|
|
|
|
// create an insurance claim for the ordered items
|
|
if ($request->patient_insurance_status == 1) {
|
|
generate_insurance_claim($sundry_order->id, 5);
|
|
}
|
|
|
|
flash("Sundries 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);
|
|
} elseif (session()->has('anc_visit_redirect')) {
|
|
$url = session()->get('anc_visit_redirect');
|
|
session()->forget('anc_visit_redirect');
|
|
return redirect($url);
|
|
} else {
|
|
return redirect("/patient_episodes");
|
|
}
|
|
}
|
|
}
|
|
|
|
public function cancel_ordered_sundries($order_id) {
|
|
$ordered_sundries = OrderedSundry::find($order_id);
|
|
|
|
try {
|
|
$ordered_sundries->delete();
|
|
|
|
// check if there are any insurance claims available
|
|
$claim = InsuranceClaim::where('order_id', $order_id)->where('item_type', 5)->first();
|
|
|
|
if ($claim) {
|
|
$claim->delete();
|
|
}
|
|
|
|
return 1;
|
|
} catch (\Exception $exception) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public function expiring_sundries(Request $request) {
|
|
$today = Carbon::now();
|
|
|
|
if ($request->time_period == 0) {
|
|
$sundries = Sundry::where('expiry_date', '<', DATE($today))
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
if (count($sundries) < 1) {
|
|
flash()->error("There are no expired sundries");
|
|
}
|
|
} elseif ($request->time_period == 1) {
|
|
$sundries = Sundry::whereBetween('expiry_date', [DATE($today), DATE($today->addWeek())])
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
if (count($sundries) < 1) {
|
|
flash()->error("There are no sundries expiring in a week");
|
|
}
|
|
} elseif ($request->time_period == 2) {
|
|
$sundries = Sundry::whereBetween('expiry_date', [DATE($today), DATE($today->addWeeks(2))])
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
if (count($sundries) < 1) {
|
|
flash()->error("There are no sundries expiring in two weeks");
|
|
}
|
|
} elseif ($request->time_period == 4) {
|
|
$sundries = Sundry::whereBetween('expiry_date', [DATE($today), DATE($today->addWeeks(4))])
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
if (count($sundries) < 1) {
|
|
flash()->error("There are no sundries expiring in a month");
|
|
}
|
|
} else {
|
|
$sundries = Sundry::where('expiry_date', '<', DATE($today))
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
if (count($sundries) < 1) {
|
|
flash()->error("There are no expired sundries");
|
|
}
|
|
}
|
|
|
|
return view('clinical_data::sundries.expiring_sundries', compact('sundries'));
|
|
}
|
|
|
|
public function get_sundry_details(Request $request) {
|
|
$patient_id = $request->patient_id;
|
|
$patient_insurance_status = $request->patient_insurance_status ?? 0;
|
|
$ward_id = $request->ward_id ?? 0;
|
|
|
|
$sundry = DB::table('sundries')->find($request->sundry_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, $sundry->id, 5, false, $ward_id);
|
|
} 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, 5, $sundry->id);
|
|
} else {
|
|
$selling_price = $sundry->non_insured_price;
|
|
}
|
|
}
|
|
|
|
if (is_chi_enabled() && is_patient_item_covered($patient_id, $sundry->id, 5)) {
|
|
$covered_by_chi = ' <br><span style="color: darkgreen"><b>Covered by CHI</b></span>';
|
|
} else {
|
|
$covered_by_chi = "";
|
|
}
|
|
|
|
return json_encode([
|
|
"selling_price" => $selling_price,
|
|
"covered_by_chi" => $covered_by_chi,
|
|
]);
|
|
}
|
|
}
|