middleware('permission:radiologies-management', ['only' => ['index']]);
$this->middleware('permission:radiologies-create', ['only' => ['create', 'store']]);
$this->middleware('permission:radiologies-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:radiologies-delete', ['only' => ['destroy']]);
$this->middleware('permission:radiologies-usage-listing',['only'=>['radiology_usage_listing','store_radiology_usage']]);
$this->middleware('permission:radiologies-report-usage', ['only'=>['radiology_usages_report']]);
$this->middleware('permission:radiologies-stock-sheet', ['only'=>['radiology_stock_sheet']]);
$this->middleware('permission:requisition-for-radiologies', ['only'=>['requisition_for_radiologies','update_imaging_stock_sheet']]);
}
public function index()
{
$radiologies = Radiology::orderBy('name', 'asc')->get();
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id');
$accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$radiologies= get_all_batch_details($radiologies,['store_stock','lab_stock'],['store_batches','lab_batches'],['total_store_stock','total_lab_stock'],4);
return view('investigations::radiologies.index', compact('radiologies', 'chart_of_accounts', 'accounts'));
}
/**
* 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();
$forms = ['' => '- select -'] + GeneralForm::where('item_type',4)->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::radiologies.create', compact('cost_of_goods_accounts','forms', 'payables_accounts', 'expense_accounts'));
}
//edit all radiologies
public function edit_all(){
$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();
$forms = ['' => '- select -'] + GeneralForm::where('item_type',4)->pluck('name', 'id')->toArray();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$expense_accounts = ['' => '- select -'] + $expense_accounts;
$payables_accounts = ['' => '- select -'] + $payables_accounts;
$radiologies = Radiology::orderBy('name', 'asc')->get();
return view('investigations::radiologies.edit_all', compact('cost_of_goods_accounts','radiologies','forms', 'payables_accounts', 'expense_accounts'));
}
//update all radiologies
public function update_all(Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required',
'cost_price' => 'required',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}
";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$id_array = $request->id;
$name_array = $request->name;
$cost_price_array = $request->cost_price;
$payables_account_id_array = $request->payables_account_id;
$expenses_account_id_array = $request->expenses_account_id;
$form_id_array = $request->form_id;
$re_order_level_array = $request->re_order_level;
for ($x = 0; $x < count($id_array); $x++):
$radiology = Radiology::find($id_array[$x]);
$radiology->name = $name_array[$x];
$radiology->cost_price = $cost_price_array[$x];
$radiology->payables_account_id = $payables_account_id_array[$x];
$radiology->expenses_account_id = $expenses_account_id_array[$x];
$radiology->form_id = $form_id_array[$x];
$radiology->reorder_level = $re_order_level_array[$x];
$radiology->updated_by = $logged_in_user_id;
try {
$radiology->save();
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
endfor;
flash("Radiology has been updated")->success();
return redirect("/radiology/");
}
}
//function to add Radiology form
public function create_radiology_form()
{
return view('investigations::radiologies.create_radiology_form');
}
//functiion to store radiology form
public function store_radiology_form (Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}
";
}
return back()->withErrors($validator)->withInput();
} else {
$data['name']=$request->name;
$data['type']= 4;
try{
//create new radiology form
$new_form = GeneralForm::add_new_form((object)$data);
if($new_form){
flash("Radiology form has been created successfully")->success();
return redirect('/radiology');
}
}catch (QueryException $e){
flash("Something went wrong. Please try again")->error();
return back()->withInput();
}
}
}
//edit form
public function edit_form($id){
$form = GeneralForm::find($id);
if (!$form) flash()->error("Sundry Form not found");return redirect()->route('radiology_forms.all');
return view('investigations::radiologies.edit_radiology_form', compact('form'));
}
//update form
public function update_radiology_form(Request $request, $id){
$validator = Validator::make($request->all(), [
'name' => 'required',
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}
";
}
return back()->withErrors($validator)->withInput();
} else {
$data['name']=$request->name;
$data['type']= 4;
try{
//update radiology form
$form = GeneralForm::find($id);
$form->update($data);
flash("Radiology form has been updated successfully")->success();
return redirect('/radiology_forms');
}catch (QueryException $e){
flash("Something went wrong. Please try again")->error();
return back()->withInput();
}
}
}
//delete form
public function delete_form($id){
try{
//delete radiology form
$form = GeneralForm::find($id);
$form->delete();
flash("Radiology form has been deleted successfully")->success();
return redirect('/radiology_forms');
}catch (QueryException $e){
flash("Something went wrong. Please try again")->error();
return back()->withInput();
}
}
//show inactive forms
public function inactive_forms(){
$inactive_forms = GeneralForm::onlyTrashed()
->where('item_type', 4)
->orderBy('name', 'asc')
->get();
if (count($inactive_forms) < 1) {
flash()->error("There is no inactive Radiology forms");
return redirect('radiology_forms');
}
return view('investigations::radiologies.inactive_radiology_forms', compact('inactive_forms'));
}
// reactivate form
public function activate_form($id) {
$lab_form = GeneralForm::withTrashed()->find($id);
if($lab_form->restore()){
flash("Radiology Form has been activated.")->success();
return redirect('/radiology_forms');
}
}
//get all radiology forms
public function get_all_radiology_form(){
$all_forms = GeneralForm::get_all_forms_type(4);
return view('investigations::radiologies.all_radiology_form',compact('all_forms'));
}
/**
* 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',
'cost_price' => 'required'
]);
$radiology = new Radiology();
$logged_in_user_id = Auth::user()->id;
$radiology->name = $request->name;
$radiology->cost_price = $request->cost_price;
$radiology->expenses_account_id = $request->expenses_account_id;
$radiology->payables_account_id = $request->payables_account_id;
$radiology->form_id = $request->form_id;
$radiology->reorder_level = $request->re_order_level;
$radiology->created_by = $logged_in_user_id;
$radiology->updated_by = $logged_in_user_id;
try {
$radiology->save();
flash("Radiology has been saved")->success();
return redirect("/radiology/");
} catch (QueryException $e) {
flash("Something went wrong. Please try again")->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)
{
$radiology = Radiology::where(['id' => $id])->first();
$payables_accounts = ChartOfAccount::where(['type' => 6])->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$expenses_accounts = ChartOfAccount::where(['type' => 2])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$expenses_accounts = ['' => '- select -'] + $expenses_accounts;
$payables_accounts = ['' => '- select -'] + $payables_accounts;
$forms = ['' => '- select -'] + GeneralForm::where('item_type',4)->pluck('name', 'id')->toArray();
if (!$radiology) {
flash()->error("Radiology not found");
return redirect('/radiology/');
} else {
return view('investigations::radiologies.edit', compact('radiology', 'forms','payables_accounts', 'expenses_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',
'cost_price' => 'required',
]);
//validation passed
$radiology = Radiology::find($id);
$logged_in_user_id = Auth::user()->id;
$radiology->name = $request->name;
$radiology->cost_price = $request->cost_price;
$radiology->expenses_account_id = $request->expenses_account_id;
$radiology->payables_account_id= $request->payables_account_id;
$radiology->form_id = $request->form_id;
$radiology->reorder_level = $request->re_order_level;
$radiology->updated_by = $logged_in_user_id;
try {
$radiology->save();
flash("Radiology has been updated")->success();
return redirect("/radiology/");
} 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)
{
$radiology = Radiology::find($id);
if ($radiology->delete()) {
flash("The item has been deleted.")->success();
return redirect('/radiology/');
}
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
*/
public function inactive() {
$radiologies = Radiology::onlyTrashed()->get();
$accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
if (count($radiologies) < 1) {
flash()->error("There is no inactive Radiology");
return redirect('/radiology/');
} else {
return view('investigations::radiologies.inactive', compact('radiologies', 'accounts'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function activate($id) {
$radiology = Radiology::withTrashed()->find($id);
if($radiology->restore()){
flash("A Radiology has been activated.")->success();
return redirect('/radiology/inactive');
}
}
public function radiology_usage_listing(){
$radiologies = DB::table('radiologies')->orderBy('name', 'asc')->where('deleted_at', '=',null)->get();
$hospital_information = HospitalInformation::first();
return view('investigations::radiologies.radiology_usage', compact('radiologies', 'hospital_information'));
}
public function store_radiology_usage(Request $request){
$lab_counter = 0;
$radiologies_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($radiologies_item_array); $i++){
if($radiologies_item_array[$i]){
if(track_items_using_batches()){
$datat=get_lab_batches_to_use_based_on_needed_quantity($radiologies_item_array[$i],4,$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(4,$radiologies_item_array[$i], $key, 'radiologies', 'lab_stock', $val, 'radiology_usages','Imaging', null, null);
}
$usage_qty_cost_array[$i]=$cost_value;
}
$remaining_qty = record_radiology_usage_listing($radiologies_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)){
$radiology = Radiology::find($radiologies_item_array[$i]);
$radiology->pharmacy_stock = ($radiology->pharmacy_stock - (int)$usage_qty_array[$i]);
$radiology->save();
}else{
flash('Item\'s '.get_name($radiologies_item_array[$i], 'id', 'name', 'labs').' requested use quantity exceeds what is available.');
}
}
}
flash('Radiologies Used from '.streamline_date($start).' to '.streamline_date($end).' have saved successfully')->success();
return redirect('/radiology_listing');
}
public function get_radiology_quantity(Request $request){
if (track_items_using_batches()){
// that is if batches is enabled
$lab_stock =track_batch_stock(4,$request->id,'lab_stock');
$total_stock = 0;
foreach ($lab_stock as $lab) {
$total_stock += $lab->lab_stock;
}
$data = [
'pharmacy_stock' => $total_stock,
'cost_price' => $lab_stock[0]->cost_price??0,
];
return $data;
}
return Radiology::where('id', $request->id)->first();
}
public function requisition_for_radiologies(Request $request)
{
$results = Radiology::orderBy('name','asc')->get();
$names_array = Radiology::orderBy('name')->distinct()->pluck('name');
$labs = Radiology::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', '=', 4)->where($date_filter)->orderBy('created_at','desc')->get();
$results = get_all_batch_details($results,['store_stock','lab_stock'],['store_batches','lab_batches'],['total_store_stock','total_lab_stock'],4);
return view('investigations::radiologies.requisition_for_radiologies', compact('results', 'names_array', 'labs', 'previous_requisitions_results'));
}
public function previous_requisition_for_radiologies(Request $request){
$search_by = $request->search_by;
$reg_date = $request->reg_date;
$start_date = $request->start_date;
$end_date = $request->end_date;
$date_filter = [];
$search_string = "";
if ($search_by == 0) {
// last 24 hours
$last_day = Carbon::now()->subDay();
array_push($date_filter, ['created_at', '>=', $last_day]);
$search_string = "