mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
531 lines
20 KiB
PHP
Executable File
531 lines
20 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Investigations\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Streamline\Models\Dental;
|
|
use Streamline\Models\DentalUsage;
|
|
use Streamline\Models\Requisition;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\HospitalInformation;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\QueryException;
|
|
|
|
class DentalController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->middleware('permission:dental-list', ['only' => ['index']]);
|
|
$this->middleware('permission:dental-create', ['only' => ['create', 'store']]);
|
|
$this->middleware('permission:dental-edit', ['only' => ['edit', 'update', 'edit_all', 'update_all']]);
|
|
$this->middleware('permission:dental-delete', ['only' => ['destroy', 'inactive', 'activate']]);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$dentals = Dental::orderBy('name', 'asc')->paginate(2000);
|
|
|
|
return view('investigations::dentals.index', compact('dentals'));
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
$expense_accounts = ChartOfAccount::where(['type' => 2])
|
|
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$payables_accounts = ChartOfAccount::where(['type' => 6])
|
|
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
|
|
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
|
|
$expense_accounts = ['' => '- select -'] + $expense_accounts;
|
|
$payables_accounts = ['' => '- select -'] + $payables_accounts;
|
|
|
|
return view('investigations::dentals.create', compact('cost_of_goods_accounts', 'expense_accounts', 'payables_accounts'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
request()->validate([
|
|
'name' => 'required'
|
|
]);
|
|
|
|
$logged_in_user_id = Auth::user()->id;
|
|
$dental = new Dental;
|
|
|
|
$dental->name = $request->name;
|
|
$dental->expenses_account_id = $request->expenses_account_id;
|
|
$dental->payables_account_id = $request->payables_account_id;
|
|
$dental->cost_of_goods_account = $request->cost_of_goods_account;
|
|
$dental->created_by = $logged_in_user_id;
|
|
$dental->updated_by = $logged_in_user_id;
|
|
try {
|
|
$dental->save();
|
|
flash($request->name . " Dental has been saved")->success();
|
|
return redirect("/dentals/");
|
|
} 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)
|
|
{
|
|
$dental = Dental::where(['id' => $id])->first();
|
|
|
|
$expense_accounts = ChartOfAccount::where(['type' => 2])
|
|
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$payables_accounts = ChartOfAccount::where(['type' => 6])
|
|
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
|
|
$expense_accounts = ['' => '- select -'] + $expense_accounts;
|
|
$payables_accounts = ['' => '- select -'] + $payables_accounts;
|
|
|
|
if (!$dental) {
|
|
flash()->error("There is no such dental");
|
|
return redirect('/dentals/');
|
|
} else {
|
|
return view('investigations::dentals.edit', compact('dental', 'expense_accounts', 'payables_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'
|
|
]);
|
|
|
|
$logged_in_user_id = Auth::user()->id;
|
|
|
|
$dental = Dental::find($id);
|
|
$dental->name = $request->name;
|
|
$dental->expenses_account_id = $request->expenses_account_id;
|
|
$dental->payables_account_id = $request->payables_account_id;
|
|
$dental->updated_by = $logged_in_user_id;
|
|
|
|
try {
|
|
$dental->save();
|
|
flash($request->name . " dental has been updated")->success();
|
|
return redirect("/dentals/");
|
|
} 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)
|
|
{
|
|
$dental = Dental::find($id);
|
|
|
|
if ($dental->delete()) {
|
|
flash("dental has been deleted.")->success();
|
|
return redirect('/dentals/');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the inactive resource(s).
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function inactive()
|
|
{
|
|
$dentals = Dental::onlyTrashed()
|
|
->orderBy('name', 'asc')
|
|
->paginate(50);
|
|
|
|
if (count($dentals) < 1) {
|
|
flash()->error("There is no inactive dental item");
|
|
return redirect('/dentals/');
|
|
} else {
|
|
return view('investigations::dentals.inactive', compact('dentals'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Activate the specified resource in storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function activate($id)
|
|
{
|
|
$dental = Dental::withTrashed()->find($id);
|
|
|
|
if ($dental->restore()) {
|
|
flash("dental has been activated.")->success();
|
|
return redirect('/dentals/inactive');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Search a resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function search(Request $request)
|
|
{
|
|
$query_name = $request->query_name;
|
|
$query_active = $request->query_active;
|
|
|
|
if ($query_name != "") {
|
|
$dentals = Dental::where([
|
|
['active', '=', $query_active],
|
|
['name', 'LIKE', '%' . $query_name . '%']
|
|
])
|
|
->orderBy('name', 'asc')
|
|
->paginate(10)
|
|
->setPath('');
|
|
|
|
$dentals->appends(array(
|
|
'query_name' => $query_name,
|
|
'query_active' => $query_active
|
|
));
|
|
|
|
if (count($dentals)) {
|
|
if ($query_active) {
|
|
return view('investigations::dentals.index', compact('dentals')) //;
|
|
->withDetails($dentals)
|
|
->withQuery($query_name, $query_active);
|
|
} else {
|
|
return view('investigations::dentals.inactive', compact('dentals')) //;
|
|
->withDetails($dentals)
|
|
->withQuery($query_name, $query_active);
|
|
}
|
|
}
|
|
}
|
|
flash()->error("No Details found. Try searching again!");
|
|
return redirect('/dentals/');
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the active resources for bulk editing.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit_all()
|
|
{
|
|
$dentals = Dental::orderBy('name', 'asc')->paginate(2000);
|
|
|
|
if (count($dentals) < 1) {
|
|
flash()->error("There is no active dental");
|
|
return redirect('/dentals/');
|
|
} else {
|
|
return view('investigations::dentals.edit.all', compact('dentals'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update all the resources in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update_all(Request $request)
|
|
{
|
|
request()->validate([
|
|
'name' => 'required'
|
|
]);
|
|
|
|
$logged_in_user_id = Auth::user()->id;
|
|
|
|
$id_array = $request->id;
|
|
$name_array = $request->name;
|
|
|
|
for ($x = 0; $x < count($id_array); $x++) {
|
|
$dental = Dental::find($id_array[$x]);
|
|
|
|
$dental->name = $name_array[$x];
|
|
$dental->updated_by = $logged_in_user_id;
|
|
|
|
try {
|
|
$dental->save();
|
|
} catch (QueryException $e) {
|
|
flash("An error occurred")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
flash("Dentals have been updated")->success();
|
|
return redirect("/dentals/");
|
|
}
|
|
|
|
public function get_dentals()
|
|
{
|
|
//code to be returned to view
|
|
$code = "<option> -- select -- </option>";
|
|
|
|
$dentals = Dental::orderBy('name', 'asc')->get();
|
|
|
|
foreach ($dentals as $dental) {
|
|
$code .= "<option value='" . $dental->id . "'>" . $dental->name . "</option>";
|
|
}
|
|
|
|
return $code;
|
|
}
|
|
|
|
public function dental_usage_report(Request $request)
|
|
{
|
|
$dentals = Dental::orderBy('name', 'asc')->get();
|
|
$hospital_information = HospitalInformation::first();
|
|
|
|
$dental_options = DB::table('dentals')->orderBy('name', 'asc')->pluck('name', 'id')->prepend('- All Dentals - ', 'all_dentals');
|
|
|
|
$search_by = $request->search_by;
|
|
$reg_date = $request->reg_date;
|
|
$start_date = $request->start_date;
|
|
$end_date = $request->end_date;
|
|
$filters = [];
|
|
$search_string = "";
|
|
|
|
if ($search_by == 0) {
|
|
// last 24 hours
|
|
$last_day = Carbon::now()->subDay();
|
|
array_push($filters, ['start_date', '>', $last_day]);
|
|
if ($request->dental_id != 'all_dentals') {
|
|
array_push($filters, ['dental_id', '=', $request->dental_id]);
|
|
}
|
|
$search_string = "<h4 class='label label-info'>Showing results of ".streamline_date($last_day)."</h4>";
|
|
} elseif ($search_by == 1) {
|
|
// custom date
|
|
if (is_null($request->reg_date)) {
|
|
flash('Please select a date')->error();
|
|
return redirect()->back();
|
|
}
|
|
$selected_date_filter = Carbon::createFromFormat('d/m/Y', $request->reg_date)->toDateString();
|
|
array_push($filters, ['start_date', $selected_date_filter]);
|
|
if ($request->dental_id != 'all_dentals') {
|
|
array_push($filters, ['dental_id', '=', $request->dental_id]);
|
|
}
|
|
$search_string = "<h4 class='label label-info'>Showing results of ".streamline_date($selected_date_filter)."</h4>";
|
|
} elseif ($search_by == 2) {
|
|
// custom date range
|
|
if (is_null($start_date) || is_null($end_date)) {
|
|
flash('Please select a date')->error();
|
|
return redirect()->back();
|
|
}
|
|
$start_date_search = Carbon::createFromFormat('d/m/Y', $start_date)->startOfDay()->toDateTimeString();
|
|
$end_date_search = Carbon::createFromFormat('d/m/Y', $end_date)->endOfDay()->toDateTimeString();
|
|
|
|
array_push($filters, ['start_date', '>', $start_date_search]);
|
|
array_push($filters, ['end_date', '<', $end_date_search]);
|
|
if ($request->dental_id != 'all_dentals') {
|
|
array_push($filters, ['dental_id', '=', $request->dental_id]);
|
|
}
|
|
$search_string = "<h4 class='label label-info'>Showing between ".streamline_date($start_date_search). " and ".streamline_date($end_date_search)."</h4>";
|
|
}
|
|
|
|
$dental_usages = (!empty($search_by) && !empty($end_date) && !empty($start_date) && !empty($reg_date))? DentalUsage::where($filters)->orderBy('id','desc')->limit(500)->get(): DentalUsage::orderBy('id','desc')->limit(500)->get();
|
|
return view('investigations::dentals.dental_usage_report', compact('dentals', 'hospital_information', 'dental_usages', 'dental_options', 'search_string'));
|
|
|
|
}
|
|
|
|
public function dental_requisitions(Request $request)
|
|
{
|
|
$results = Dental::orderBy('name','asc')->get();
|
|
$names_array = Dental::orderBy('name')->distinct()->pluck('name');
|
|
$labs = Dental::orderBy('name')->pluck('name', 'id');
|
|
|
|
$date_filter = [];
|
|
if(isset($request->start_date) && isset($request->end_date)){
|
|
|
|
if (isset($request->quotation_type)) {
|
|
$item_type = $request->quotation_type;
|
|
$start_date = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end_date = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
|
|
array_push($date_filter, ['created_at', '>', $start_date]);
|
|
array_push($date_filter, ['created_at', '<', $end_date]);
|
|
}
|
|
} else {
|
|
$date_from = Carbon::today()->subDays(30)->format('Y-m-d');
|
|
$date_to = Carbon::today()->addDays(1)->format('Y-m-d');
|
|
array_push($date_filter, ['created_at', '>', $date_from]);
|
|
array_push($date_filter, ['created_at', '<', $date_to]);
|
|
}
|
|
$previous_requisitions_results = DB::table('requisitions')->where('quotation_type_id', '=', 3)->where($date_filter)->orderBy('created_at','desc')->get();
|
|
|
|
return view('investigations::dentals.dental_requisition', compact('results', 'names_array', 'labs', 'previous_requisitions_results'));
|
|
}
|
|
|
|
public function dental_usage_listing()
|
|
{
|
|
$dentals = Dental::orderBy('name', 'asc')->get();
|
|
$hospital_information = HospitalInformation::first();
|
|
$dental_usages = DentalUsage::orderBy('id','desc')->limit(20)->get();
|
|
|
|
return view('investigations::dentals.dental_usage', compact('dentals', 'hospital_information', 'dental_usages'));
|
|
}
|
|
|
|
public function store_dental_usage(Request $request)
|
|
{
|
|
$dental_items_array = $request->labItem;
|
|
$available_qty_array = $request->availableQty;
|
|
$available_qty_unit_cost_array = $request->unitCost;
|
|
$usage_qty_array = $request->requiredQty;
|
|
$usage_qty_cost_array = $request->totalCost;
|
|
|
|
if($request->dates == 'today'){
|
|
$start = Carbon::now()->startOfDay()->toDateTimeString();
|
|
$end = Carbon::now()->endOfDay()->toDateTimeString();
|
|
}else{
|
|
$start = Carbon::parse($request->start_date)->endOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
}
|
|
|
|
for($i = 0; $i < count($dental_items_array); $i++){
|
|
|
|
if($dental_items_array[$i]){
|
|
$dental_usage = new DentalUsage;
|
|
$dental_usage->dental_id = $dental_items_array[$i];
|
|
$dental_usage->unit_cost_price = $unit_cost = $available_qty_unit_cost_array[$i];
|
|
$dental_usage->available_quantity = $available_qty_array[$i];
|
|
$dental_usage->available_quantity_cost = ((int)$unit_cost * $available_qty_array[$i]);
|
|
$dental_usage->usage_quantity = $usage_qty_array[$i];
|
|
$dental_usage->usage_quantity_cost = $usage_qty_cost_array[$i];
|
|
$dental_usage->remaining_quantity = $remaining_qty = ((int)$available_qty_array[$i] - (int)$usage_qty_array[$i]);
|
|
$dental_usage->remaining_quantity_cost = ($remaining_qty * $unit_cost);
|
|
$dental_usage->created_by = Auth::id();
|
|
$dental_usage->start_date = $start;
|
|
$dental_usage->end_date = $end;
|
|
|
|
if(!($remaining_qty < 0)){
|
|
$dental = Dental::find($dental_items_array[$i]);
|
|
$dental->pharmacy_stock = ($dental->pharmacy_stock - (int)$usage_qty_array[$i]);
|
|
$dental_usage->save();
|
|
$dental->save();
|
|
}else{
|
|
flash('Item\'s '.get_name($dental_items_array[$i], 'id', 'name', 'labs').' requested use quantity exceeds what is available.');
|
|
}
|
|
}
|
|
}
|
|
flash('Dentals to be used from '.streamline_date($start).' to '.streamline_date($end).' have saved successfully')->success();
|
|
return redirect()->route('dentals.usage_listing');
|
|
|
|
}
|
|
|
|
public function get_dental_quantity(Request $request){
|
|
$dental_item = Dental::where('id', $request->id)->first();
|
|
return $dental_item;
|
|
}
|
|
|
|
public function dentals_requisition_search(Request $request)
|
|
{
|
|
$item_type = $request->item_type;
|
|
$item_ids_array = $request->items_ids;
|
|
$results = null;
|
|
$filters = [];
|
|
$items = null;
|
|
|
|
$results = Dental::whereIn('id', $item_ids_array)->orderBy('name','asc')->get();
|
|
$labs = Dental::orderBy('name')->pluck('name', 'id');
|
|
|
|
$date_filter = [];
|
|
if(isset($request->start_date) && isset($request->end_date)){
|
|
|
|
if (isset($request->quotation_type)) {
|
|
$item_type = $request->quotation_type;
|
|
$start_date = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end_date = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
|
|
array_push($date_filter, ['created_at', '>', $start_date]);
|
|
array_push($date_filter, ['created_at', '<', $end_date]);
|
|
}
|
|
} else {
|
|
$date_from = Carbon::today()->subDays(30)->format('Y-m-d');
|
|
$date_to = Carbon::today()->addDays(1)->format('Y-m-d');
|
|
array_push($date_filter, ['created_at', '>', $date_from]);
|
|
array_push($date_filter, ['created_at', '<', $date_to]);
|
|
}
|
|
$previous_requisitions_results = DB::table('requisitions')->where('quotation_type_id', '=', 5)->where($date_filter)->orderBy('created_at','desc')->get();
|
|
|
|
return view('investigations::dentals.dental_requisition',compact('items','item_type','labs','results','previous_requisitions_results'));
|
|
}
|
|
|
|
public function store_dental_requisitions(Request $request)
|
|
{
|
|
$drug_id_array = $request->drug_id;
|
|
$quantity_array = $request->quantity;
|
|
$item_type = $request->item_type;
|
|
$user_id = auth()->user()->id;
|
|
|
|
/* return back if no quantity has been filled */
|
|
if (array_sum($quantity_array) < 1) {
|
|
flash('Please insert some values!')->error();
|
|
return redirect()->back()->withInput();
|
|
}
|
|
|
|
if (isset($request->complete_request)) {
|
|
$filtered_drugs_array = [];
|
|
$filtered_quantities_array = [];
|
|
$associative_requested_drugs_array = [];
|
|
for ($i=0; $i < count($drug_id_array) ; $i++) {
|
|
if ($quantity_array[$i] != "") {
|
|
$associative_requested_drugs_array[$drug_id_array[$i]] = $quantity_array[$i];
|
|
$filtered_drugs_array[] = $drug_id_array[$i];
|
|
$filtered_quantities_array[] = $quantity_array[$i];
|
|
}
|
|
}
|
|
$new_requisition = new Requisition;
|
|
$new_requisition->quotation_type_id = $item_type;
|
|
$new_requisition->drug_id = implode(',', $filtered_drugs_array);
|
|
$new_requisition->quantity_requested = implode(',', $filtered_quantities_array);
|
|
$new_requisition->created_by = $user_id;
|
|
$new_requisition->save();
|
|
|
|
flash('Requisition number ' . $new_requisition->id . ' made successfully')->success();
|
|
return redirect('/dentals');
|
|
}
|
|
}
|
|
}
|