mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
763 lines
33 KiB
PHP
Executable File
763 lines
33 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Investigations\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\HospitalInformation;
|
|
use Streamline\Models\Lab;
|
|
use Streamline\Models\LabForm;
|
|
use Streamline\Models\LabUsageListing;
|
|
use Streamline\Models\OrderedInvestigation;
|
|
use Streamline\Models\Supplier;
|
|
use Streamline\Models\UnitOfMeasure;
|
|
use Streamline\Models\Requisition;
|
|
|
|
class LabController extends Controller
|
|
{
|
|
public function __construct() {
|
|
$this->middleware('auth');
|
|
$this->middleware('permission:lab-management', ['only' => ['index']]);
|
|
$this->middleware('permission:labs-create', ['only' => ['create', 'store']]);
|
|
$this->middleware('permission:labs-bulk-create', ['only' => ['add_bulk', 'store_bulk']]);
|
|
$this->middleware('permission:labs-bulk-edit', ['only' => ['edit_bulk', 'update_bulk']]);
|
|
$this->middleware('permission:labs-edit', ['only' => ['edit', 'update']]);
|
|
$this->middleware('permission:labs-delete', ['only' => ['destroy', 'inactive', 'activate']]);
|
|
$this->middleware('permission:labs-usage-listing', ['only' => ['lab_usage_listing']]);
|
|
$this->middleware('permission:labs-report-usage', ['only' => ['actual_lab_usage', 'labs_usages_report']]);
|
|
$this->middleware('permission:labs-bulk-delete', ['only' => ['delete_bulk']]);
|
|
$this->middleware('permission:requisition-for-labs', ['only' => ['requisition_for_labs','store_lab_requisitions']]);
|
|
$this->middleware('permission:labs-stock-sheet', ['only' => ['stock_sheet', 'update_laboratory_stock_sheet']]);
|
|
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$labs = Lab::orderBy('name', 'asc')->get();
|
|
$labs= get_all_batch_details($labs,['store_stock','lab_stock'],['store_batches','lab_batches'],['total_store_stock','total_lab_stock'],5) ;
|
|
return view('investigations::labs.index', compact('labs'));
|
|
}
|
|
|
|
public function dashboard()
|
|
{
|
|
|
|
$end = Carbon::now()->endOfDay()->toDateTimeString();
|
|
$start = Carbon::now()->startOfDay()->toDateTimeString();
|
|
|
|
$labs = Lab::get();
|
|
$total_investigations = OrderedInvestigation::get();
|
|
$total_paid_investigations = OrderedInvestigation::where('payment_status', 1)->get();
|
|
$total_unpaid_investigations = OrderedInvestigation::where('payment_status', 0)->get();
|
|
$total_pending_investigations = OrderedInvestigation::where('investigation_status', 0)->get();
|
|
$total_investigation_deposits = DB::table('investigation_deposits')
|
|
->whereBetween('created_at', [$start, $end]);
|
|
|
|
return view('investigations::labs.dashboard', compact('labs', 'total_investigations', 'total_paid_investigations', 'total_unpaid_investigations', 'total_pending_investigations', 'total_investigation_deposits'));
|
|
}
|
|
|
|
public function lab_usage_listing()
|
|
{
|
|
/*$labs = DB::table('labs')->where('deleted_at', '=',null)->where('store_stock', '>', 0)->get();*/
|
|
$labs = Lab::orderBy('name', 'asc')->get();
|
|
$hospital_information = HospitalInformation::first();
|
|
|
|
$labs_usages = LabUsageListing::orderBy('id','desc')->limit(20)->get();
|
|
|
|
return view('investigations::labs.lab_usage_listing', compact('labs', 'hospital_information', 'labs_usages'));
|
|
}
|
|
|
|
public function clear_stock()
|
|
{
|
|
$lab_s = Lab::get();
|
|
foreach ($lab_s as $lab) {
|
|
$lab->delete();
|
|
}
|
|
$labs = Lab::get();
|
|
return view('investigations::labs.index', compact('labs'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
|
|
$units = UnitOfMeasure::get();
|
|
$suppliers = Supplier::get();
|
|
$forms = LabForm::get();
|
|
$chart_of_accounts = ChartOfAccount::where(['type' => 1])
|
|
->orderBy('name', 'asc')
|
|
->pluck('name', 'id')
|
|
->toArray();
|
|
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
|
|
|
|
$expense_accounts = ChartOfAccount::where(['type' => 2])
|
|
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$expense_accounts = ['' => '- select -'] + $expense_accounts;
|
|
|
|
$payables_accounts = ChartOfAccount::where(['type' => 6])
|
|
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$payables_accounts = ['' => '- select -'] + $payables_accounts;
|
|
|
|
return view('investigations::labs.create', compact('units', 'suppliers', 'forms', 'chart_of_accounts', 'expense_accounts', 'payables_accounts'));
|
|
}
|
|
|
|
public function add_bulk()
|
|
{
|
|
$units_of_measure = UnitOfMeasure::get();
|
|
$suppliers = Supplier::get();
|
|
$forms = LabForm::get();
|
|
//$chart_of_accounts = ChartOfAccount::get();
|
|
$chart_of_accounts = ChartOfAccount::where(['type' => 1])
|
|
->orderBy('name', 'asc')
|
|
->pluck('name', 'id')
|
|
->toArray();
|
|
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
|
|
|
|
$expense_accounts = ChartOfAccount::where(['type' => 2])
|
|
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$expense_accounts = ['' => '- select -'] + $expense_accounts;
|
|
|
|
$payables_accounts = ChartOfAccount::where(['type' => 6])
|
|
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$payables_accounts = ['' => '- select -'] + $payables_accounts;
|
|
|
|
return view('investigations::labs.bulk.create', compact('units_of_measure', 'suppliers', 'forms', 'chart_of_accounts', 'expense_accounts', 'payables_accounts'));
|
|
}
|
|
|
|
public function get_lab_quantity(Request $request)
|
|
{
|
|
if (track_items_using_batches()){
|
|
$lab_stock =track_batch_stock(5,$request->id,'lab_stock');
|
|
$total_stock = 0;
|
|
foreach ($lab_stock as $lab) {
|
|
$total_stock += $lab->lab_stock;
|
|
}
|
|
$data = [
|
|
'laboratory_stock' => $total_stock,
|
|
'cost_price' => $lab_stock[0]->cost_price??0,
|
|
];
|
|
return $data;
|
|
}
|
|
return Lab::where('id', $request->id)->first();
|
|
}
|
|
|
|
public function actual_lab_usage(Request $request)
|
|
{
|
|
$lab_counter = 0;
|
|
$lab_item_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($lab_item_array); $i++) {
|
|
if ($lab_item_array[$i]) {
|
|
if(track_items_using_batches()){
|
|
$datat=get_lab_batches_to_use_based_on_needed_quantity($lab_item_array[$i],5,$usage_qty_array[$i],'lab_stock');
|
|
$cost_value=0;
|
|
foreach ($datat as $key=> $val){
|
|
$unit_cost_value =(int) get_name($key, 'id', 'cost_price', 'item_batch_watcher');
|
|
//caluclate cost value
|
|
$cost_value += $unit_cost_value * $val;
|
|
reduce_batch_item_stock(5,$lab_item_array[$i], $key, 'labs', 'lab_stock', $val, 'lab_usage_listings','Labs', null, null);
|
|
}
|
|
$usage_qty_cost_array[$i]=$cost_value;
|
|
}
|
|
$remaining_qty = record_lab_usage_listing($lab_item_array[$i],$available_qty_unit_cost_array[$i], $available_qty_array[$i],$usage_qty_array[$i], $usage_qty_cost_array[$i],
|
|
$start,$end);
|
|
if (!($remaining_qty < 0)) {
|
|
$lab = Lab::find($lab_item_array[$i]);
|
|
$lab->laboratory_stock = ($lab->laboratory_stock - (int)$usage_qty_array[$i]);
|
|
$lab->save();
|
|
} else {
|
|
flash('Lab Item\'s ' . get_name($lab_item_array[$i], 'id', 'name', 'labs') . ' requested use quantity exceeds what is available.');
|
|
}
|
|
}
|
|
}
|
|
flash('Labs used from ' . streamline_date($start) . ' to ' . streamline_date($end) . ' have saved successfully')->success();
|
|
return redirect('/labs/usage/listing');
|
|
}
|
|
|
|
public function store_bulk(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
//'name' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
$name_array = $request->name;
|
|
|
|
for ($i = 0; $i < count($name_array); $i++) {
|
|
if ($request->name[$i] != "" && !is_null($request->name[$i])) {
|
|
try {
|
|
$lab = new Lab;
|
|
$lab->name = $request->name[$i];
|
|
// $lab->cost_price = isset($request->cost_price[$i])?$request->cost_price[$i] : null;
|
|
// $lab->non_insured_price = isset($request->non_insured_price[$i])?$request->non_insured_price[$i] : null;
|
|
// $lab->insured_price = isset($request->insured_price[$i])?$request->insured_price[$i] : null;
|
|
// $lab->account_id = isset($request->chart_of_account[$i])? $request->chart_of_account[$i] : null;
|
|
// $lab->store_stock = isset($request->stock[$i])?$request->stock[$i] : 0 ;
|
|
// if ($request->chi_status[$i] == '1') {
|
|
// $lab->insurance_coverage = 1;
|
|
// } else {
|
|
// $lab->insurance_coverage = 0;
|
|
// }
|
|
$lab->reorder_level = isset($request->re_order_level[$i]) ? $request->re_order_level[$i] :0;
|
|
$lab->description = $request->description[$i];
|
|
$lab->form_id = $request->form[$i];
|
|
$lab->unit_id = $request->unit[$i];
|
|
// $lab->expiry_date = isset($request->expiry_date[$i])? $request->expiry_date[$i] : null;
|
|
// $lab->supplier_id = isset($request->supplier[$i])? $request->supplier[$i] : null ;
|
|
$lab->expenses_account_id = isset($request->expenses_account_id[$i]) ? $request->expenses_account_id[$i] : null;
|
|
$lab->created_by = Auth::id();
|
|
$lab->save();
|
|
} catch (\Exception $e) {
|
|
flash('Error: ' . $e->getMessage())->error();
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return redirect('labs');
|
|
}
|
|
|
|
public function edit_bulk()
|
|
{
|
|
$labs = Lab::get();
|
|
$units_of_measure = UnitOfMeasure::get()->pluck('name','id')->toArray();
|
|
$units_of_measure = ['' => '- select -'] + $units_of_measure;
|
|
$suppliers = Supplier::get();
|
|
$forms = LabForm::pluck('name','id')->toArray();
|
|
$forms = ['' => '- select -'] + $forms;
|
|
|
|
$expense_accounts = ChartOfAccount::where(['type' => 2])
|
|
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$expense_accounts = ['' => '- select -'] + $expense_accounts;
|
|
|
|
$payables_accounts = ChartOfAccount::where(['type' => 6])
|
|
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$payables_accounts = ['' => '- select -'] + $payables_accounts;
|
|
|
|
return view('investigations::labs.bulk.edit', compact('units_of_measure', 'suppliers', 'forms', 'labs', 'expense_accounts', 'payables_accounts'));
|
|
}
|
|
|
|
public function update_bulk(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required',
|
|
]);
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
$name_array = $request->name;
|
|
for ($i = 0; $i < count($name_array); $i++) {
|
|
try {
|
|
$lab = Lab::find($request->id[$i]);
|
|
$lab->name = $request->name[$i];
|
|
$lab->cost_price = isset($request->cost_price[$i]) ? $request->cost_price[$i] : null ;
|
|
$lab->expiry_date = isset($request->cost_price[$i]) ? $request->expiry_date[$i] : null;
|
|
$lab->non_insured_price = isset($request->cost_price[$i]) ? $request->non_insured_price[$i] : null;
|
|
$lab->insured_price = isset($request->cost_price[$i]) ? $request->insured_price[$i] : null;
|
|
$lab->account_id = isset($request->chart_of_account[$i]) ? $request->chart_of_account[$i] : null;
|
|
$lab->store_stock = isset($request->stock[$i]) ? $request->stock[$i] : null;
|
|
|
|
$lab->reorder_level = isset($request->re_order_level[$i]) ? $request->re_order_level[$i] : null;
|
|
$lab->description = $request->description[$i];
|
|
$lab->form_id = isset($request->form[$i]) ? $request->form[$i] : null;
|
|
$lab->unit_id = isset($request->unit[$i]) ? $request->unit[$i] : null;
|
|
$lab->supplier_id = isset($request->supplier[$i]) ? $request->supplier[$i] : null;
|
|
$lab->expenses_account_id = isset($request->expenses_account_id[$i]) ? $request->expenses_account_id[$i] : null;
|
|
$lab->updated_by = Auth::id();
|
|
$lab->save();
|
|
} catch (\Exception $e) {
|
|
flash('Error: ' . $e->getMessage())->error();
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
}
|
|
return redirect('labs');
|
|
}
|
|
|
|
public function delete_bulk(Request $request)
|
|
{
|
|
$ids_array = $request->ids;
|
|
for ($i = 0; $i < count($ids_array); $i++) {
|
|
$lab = Lab::find($ids_array[$i]);
|
|
if ($lab) {
|
|
$lab->delete();
|
|
}
|
|
}
|
|
return 'success';
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required',
|
|
]);
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
$lab = new Lab;
|
|
$lab->name = $request->name;
|
|
$lab->cost_price = is_null($request->cost_price) ? null :$request->cost_price;
|
|
$lab->non_insured_price = is_null($request->non_insured_price) ? null :$request->non_insured_price;
|
|
$lab->insured_price =is_null($request->insured_price) ? null : $request->insured_price;
|
|
$lab->account_id = is_null($request->chart_of_account) ? null :$request->chart_of_account;
|
|
$lab->expiry_date = is_null($request->expiry_date) ? null : Carbon::createFromFormat('d/m/Y', $request->expiry_date)->toDateString();
|
|
$lab->store_stock = is_null($request->stock) ? 0 : $request->stock;
|
|
if ($request->chi_status == '1') {
|
|
$lab->insurance_coverage = 1;
|
|
} else {
|
|
$lab->insurance_coverage = 0;
|
|
}
|
|
$lab->reorder_level = is_null($request->re_order_level) ? 0 : $request->re_order_level;
|
|
$lab->description = $request->description;
|
|
$lab->form_id = $request->form;
|
|
$lab->unit_id = $request->unit;
|
|
$lab->supplier_id = $request->supplier;
|
|
$lab->expenses_account_id = $request->expenses_account_id;
|
|
//$lab->payables_account_id = $request->payables_account_id;
|
|
$lab->created_by = Auth::id();
|
|
flash("New lab has been added")->success();
|
|
$lab->save();
|
|
|
|
return redirect('labs');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
$lab = Lab::find($id);
|
|
$units = UnitOfMeasure::get()->pluck('name', 'id')->toArray();
|
|
$units = ['' => '- select -'] + $units;
|
|
$suppliers = Supplier::get();
|
|
$forms = LabForm::get()->pluck('name', 'id')->toArray();
|
|
$forms = ['' => '- select -'] + $forms;
|
|
$chart_of_accounts = ChartOfAccount::where(['type' => 1])
|
|
->orderBy('name', 'asc')
|
|
->pluck('name', 'id')
|
|
->toArray();
|
|
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
|
|
|
|
$expense_accounts = ChartOfAccount::where(['type' => 2])
|
|
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$expense_accounts = ['' => '- select -'] + $expense_accounts;
|
|
|
|
$payables_accounts = ChartOfAccount::where(['type' => 6])
|
|
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
$payables_accounts = ['' => '- select -'] + $payables_accounts;
|
|
|
|
return view('investigations::labs.edit', compact('lab', 'units', 'suppliers', 'forms', 'chart_of_accounts', '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)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required',
|
|
]);
|
|
if ($validator->fails()) {
|
|
$string = "";
|
|
foreach ($validator->errors()->getMessages() as $item) {
|
|
$string .= "{$item[0]}<br>";
|
|
}
|
|
flash($string)->error();
|
|
return back()->withErrors($validator)->withInput();
|
|
} else {
|
|
$lab = Lab::find($id);
|
|
$lab->name = $request->name;
|
|
$lab->cost_price = is_null($request->cost_price) ? null :$request->cost_price;
|
|
$lab->non_insured_price = is_null($request->non_insured_price) ? null :$request->non_insured_price;
|
|
$lab->insured_price =is_null($request->insured_price) ? null : $request->insured_price;
|
|
$lab->account_id = is_null($request->chart_of_account) ? null :$request->chart_of_account;
|
|
$lab->store_stock =is_null($request->stock) ? null : $request->stock;
|
|
|
|
$lab->reorder_level = is_null($request->re_order_level) ? null :$request->re_order_level;
|
|
$lab->description = $request->description;
|
|
$lab->form_id = $request->form;
|
|
$lab->unit_id = $request->unit;
|
|
$lab->expiry_date = is_null($request->expiry_date) ? null : $request->expiry_date;
|
|
$lab->supplier_id = is_null($request->supplier) ? null :$request->supplier;
|
|
$lab->expenses_account_id = $request->expenses_account_id;
|
|
//$lab->payables_account_id = $request->payables_account_id;
|
|
$lab->updated_by = Auth::id();
|
|
|
|
try {
|
|
$lab->save();
|
|
} catch (\Exception $e) {
|
|
}
|
|
}
|
|
|
|
return redirect('labs');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$lab = Lab::find($id);
|
|
|
|
if ($lab->delete()) {
|
|
flash("Lab has been deleted.")->success();
|
|
return redirect('labs');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the inactive resource(s).
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function inactive()
|
|
{
|
|
$labs = Lab::onlyTrashed()
|
|
->orderBy('name', 'asc')
|
|
->get();
|
|
|
|
if (count($labs) < 1) {
|
|
flash()->error("There is no inactive labs");
|
|
return redirect('labs');
|
|
}
|
|
|
|
return view('investigations::labs.inactive', compact('labs'));
|
|
}
|
|
|
|
/**
|
|
* Activate the specified resource in storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function activate($id)
|
|
{
|
|
$lab = Lab::withTrashed()->find($id);
|
|
|
|
if ($lab->restore()) {
|
|
flash("Labs has been activated.")->success();
|
|
return redirect('/labs/inactive');
|
|
}
|
|
}
|
|
|
|
/* requisition for labs */
|
|
public function requisition_for_labs(Request $request)
|
|
{
|
|
$results = Lab::orderBy('name', 'asc')->get();
|
|
$names_array = Lab::orderBy('name')->distinct()->pluck('name');
|
|
$labs = Lab::orderBy('name')->pluck('name', 'id');
|
|
|
|
// add start and end date
|
|
|
|
$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]);
|
|
}
|
|
|
|
return view('investigations::labs.requisition_for_labs', compact('results', 'names_array', 'labs'));
|
|
}
|
|
|
|
/**
|
|
* Previous lab requisitions
|
|
* return @var object
|
|
*/
|
|
public function previous_requisition_for_labs(Request $request)
|
|
{
|
|
$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, ['created_at', '>=', $last_day]);
|
|
$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, ['created_at', $selected_date_filter]);
|
|
$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, ['created_at', '>=', $start_date_search]);
|
|
array_push($filters, ['created_at', '<=', $end_date_search]);
|
|
|
|
$search_string = "<h4 class='label label-info'>Showing between " . streamline_date($start_date_search) . " and " . streamline_date($end_date_search) . "</h4>";
|
|
}else{
|
|
// last 24 hours
|
|
$last_day = Carbon::now()->subDay();
|
|
array_push($filters, ['created_at', '>=', $last_day]);
|
|
$search_string = "<h4 class='label label-info'>Showing results of " . streamline_date($last_day) . "</h4>";
|
|
}
|
|
$data['previous_requisitions_results'] =Requisition::where('quotation_type_id', '=', 5)->where($filters)->orderBy('created_at', 'desc')->get();
|
|
$data['search_string'] = $search_string;
|
|
return view('investigations::labs.pre_requisition_for_labs', compact('data'));
|
|
}
|
|
|
|
public function store_lab_requisitions(Request $request)
|
|
{
|
|
$drug_id_array = $request->drug_id;
|
|
$quantity_array = $request->quantity;
|
|
$item_type = $request->item_type;
|
|
$user_id = Auth::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->requisition_origin = 2; //1-pharmacy, 2-lab, 3-imaging
|
|
$new_requisition->save();
|
|
flash('Requisition number ' . $new_requisition->id . ' made successfully')->success();
|
|
//redirect to requisition
|
|
return redirect('/print_requisition_receipt/' . $new_requisition->id);
|
|
}
|
|
}
|
|
|
|
/* filter out items to requisition */
|
|
public function requisition_for_labs_search(Request $request)
|
|
{
|
|
$item_type = $request->item_type;
|
|
$item_ids_array = $request->items_ids;
|
|
$results = null;
|
|
$filters = [];
|
|
$items = null;
|
|
|
|
$results = Lab::whereIn('id', $item_ids_array)->orderBy('name', 'asc')->get();
|
|
$labs = Lab::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 =Requisition::where('quotation_type_id', '=', 5)->where($date_filter)->orderBy('created_at', 'desc')->get();
|
|
|
|
return view('investigations::labs.requisition_for_labs', compact('items', 'item_type', 'labs', 'results', 'previous_requisitions_results'));
|
|
}
|
|
|
|
/* view stock sheet */
|
|
public function stock_sheet()
|
|
{
|
|
$labs = DB::table('labs')->whereNull('deleted_at')->orderBy('name', 'asc')->paginate(1000);
|
|
// getting the labs stock batches
|
|
$labs= calculate_stock_value($labs,5,'lab_stock');
|
|
if (empty($labs)) {
|
|
flash()->error("There is out of stock of labs");
|
|
return redirect('/labs/');
|
|
} else {
|
|
return view('investigations::labs.labs_stock_sheet', compact('labs'));
|
|
}
|
|
}
|
|
|
|
public function update_laboratory_stock_sheet(Request $request)
|
|
{
|
|
$lab_ids_array = $request->lab_id;
|
|
$quantities_array = $request->quantity;
|
|
$expiry_dates_array = $request->expiry_date;
|
|
$batch_numbers_array = $request->batch_number;
|
|
$batch_quantity_balances_array = $request->batch_quantity_balance;
|
|
$batch_unit_cost = $request->batch_unit_cost;
|
|
$batch_db_record_ids_array = $request->batch_drug_id;
|
|
$batch_expiry_dates_array = $request->expiry_date;
|
|
$batch_drug_ids_array = $request->batch_item_id;
|
|
//update batch tracking tables with reconciliations
|
|
for ($i=0; $i < count($batch_numbers_array) ; $i++) {
|
|
if (!is_null($batch_numbers_array[$i]) && !is_null($batch_quantity_balances_array[$i]) ){
|
|
reconcile_batches(5, $batch_drug_ids_array[$i], $batch_numbers_array[$i], $batch_quantity_balances_array[$i], $batch_expiry_dates_array[$i], "lab", $batch_unit_cost[$i], $batch_db_record_ids_array[$i]??0, null);
|
|
}
|
|
}
|
|
for ($i = 0; $i < count($lab_ids_array); $i++) {
|
|
$lab = Lab::withTrashed()->find($lab_ids_array[$i]);
|
|
$lab->laboratory_stock = $quantities_array[$i];
|
|
// $lab->expiry_date = $expiry_dates_array[$i];
|
|
$lab->save();
|
|
}
|
|
|
|
flash('stock sheet has been updated')->success();
|
|
return redirect('laboratory_stock_sheet');
|
|
}
|
|
|
|
public function labs_usages_report(Request $request)
|
|
{
|
|
$labs = [];
|
|
// $labs = DB::table('labs')->orderBy('name', 'asc')->where('deleted_at', '=', null)->get();
|
|
$hospital_information = HospitalInformation::first();
|
|
|
|
$labs_options = Lab::orderBy('name', 'asc')->pluck('name', 'id')->prepend('- All labs - ', 'all_labs');
|
|
|
|
$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->lab_id != 'all_labs') {
|
|
array_push($filters, ['lab_id', '=', $request->lab_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->lab_id != 'all_labs') {
|
|
array_push($filters, ['lab_id', '=', $request->lab_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->lab_id != 'all_labs') {
|
|
array_push($filters, ['lab_id', '=', $request->lab_id]);
|
|
}
|
|
$search_string = "<h4 class='label label-info'>Showing between " . streamline_date($start_date_search) . " and " . streamline_date($end_date_search) . "</h4>";
|
|
}
|
|
|
|
$labs_usages = LabUsageListing::where($filters)->orderBy('id', 'desc')->limit(500)->get();
|
|
|
|
return view('investigations::labs.lab_usages_report', compact('labs', 'hospital_information', 'labs_usages', 'labs_options', 'search_string'));
|
|
}
|
|
}
|