Files

995 lines
43 KiB
PHP
Executable File

<?php
namespace Modules\ClinicalData\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\Drug;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\QueryException;
use Streamline\Models\DrugCategory;
use Streamline\Models\DrugForm;
use Streamline\Models\DrugUnit;
use Streamline\Models\PriceListCategories;
use Streamline\Models\Supplier;
use Illuminate\Support\Facades\Artisan;
use Streamline\Http\Controllers\StreamlineSetupManager;
use Streamline\Models\Treatment;
use Streamline\Models\WardTreatment;
use Streamline\Models\ItemBatchWatcher;
use Streamline\Services\ItemsStockService;
use Streamline\Services\StreamlineSetupServiceInterface;
class DrugController extends Controller {
protected StreamlineSetupServiceInterface $setupService;
protected ItemsStockService $itemsStockService;
function __construct(
StreamlineSetupServiceInterface $setupService,
ItemsStockService $itemsStockService
) {
/*$this->middleware('auth');
$this->middleware('permission:drug-list', ['only' => ['index']]);
$this->middleware('permission:drug-detail', ['only' => ['show']]);
$this->middleware('permission:drug-create', ['only' => ['create', 'store']]);
$this->middleware('permission:drug-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:drug-delete', ['only' => ['destroy']]);*/
$this->setupService = $setupService;
$this->itemsStockService = $itemsStockService;
}
/**
* Display a listing of the resource.
*
*/
public function index() {
$drugs = Drug::orderBy('name', 'asc')->paginate(1500);
$ordered_drugs = [];
$treatment_ids = Treatment::distinct('drugs')->select('drugs');
$ward_treatment_ids = WardTreatment::distinct('drugs')->select('drugs')->union($treatment_ids)->get();
foreach ($ward_treatment_ids as $ward_treatment_id) {
$drug_ids = explode(',', $ward_treatment_id->drugs);
foreach ($drug_ids as $drug_id) if(!empty($drug_id) && !in_array($drug_id, $ordered_drugs)) $ordered_drugs[$drug_id] = $drug_id;
}
return view('clinical_data::drugs.index', compact('drugs', 'ordered_drugs'));
}
/**
* Show the form for creating a new resource.
*
*/
public function create() {
$drug_categories = DrugCategory::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$drug_categories = ['' => '- select -'] + $drug_categories;
$drug_units = DrugUnit::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$drug_units = ['' => '- select -'] + $drug_units;
$drug_forms = DrugForm::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$drug_forms = ['' => '- select -'] + $drug_forms;
$suppliers = Supplier::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$suppliers = ['' => '- select -'] + $suppliers;
$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();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
$income_accounts = ['' => '- select -'] + $income_accounts;
$default_drugs = StreamlineSetupManager::get_drugs_array();
$package_units = DB::table('package_units')->whereNull('deleted_at')->orderBy('name')->pluck('name','id')->toArray();
$package_units = ['' => '- select -'] + $package_units;
return view('clinical_data::drugs.create', compact('drug_categories', 'drug_units', 'drug_forms', 'suppliers',
'income_accounts','default_drugs', 'inventory_asset_accounts', 'cost_of_goods_accounts', 'package_units'));
}
/**
* Store a newly created resource in storage.
*
*/
public function store(Request $request) {
if (session()->has('streamline_setup') && isset($request->skip)){
//Artisan::call('db:seed', ['--class' => 'DrugsTableSeeder']);
//update the streamline setup table with the new finished step
$this->setupService->saveStep("drugs registration", 1);
// flash("Default drugs list has been added")->success();
return redirect("procedures/create");
} else {
// 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);
}
$logged_in_user_id = Auth()->user()->id;
$drug = new Drug;
$drug->name = $request->name;
$drug->category_id = $request->category_id;
$drug->available = $request->available;
$drug->form_id = $request->form_id;
$drug->unit_id = $request->unit_id;
$drug->reference_areas = !empty($request->reference_areas)? implode(',',$request->reference_areas):null;
$drug->reference_name = !empty($request->reference_name)? implode(',',$request->reference_name):null;
$drug->pack = $request->pack;
$drug->strength = $request->strength;
$drug->prompt = $request->prompt;
$drug->info_english = $request->info_english;
$drug->info_vernacular = $request->info_vernacular;
$drug->store_stock = 0; //$request->store_stock;
$drug->pharmacy_stock = 0; //$request->pharmacy_stock;
$drug->reorder_level = $request->reorder_level;
$drug->long_term = $request->long_term;
/*if ($request->is_initial_stock_count == 1) {
$drug->opening_stock = $drug->store_stock + $drug->pharmacy_stock;
$drug->opening_cost_price = $request->cost_price;
}*/
$drug->expiry_date = $request->expiry_date;
$drug->supplier_id = $request->supplier_id;
if (isset($request->price_list_category_id)) {
$drug->price_list_category = !is_null($request->price_list_category_id) ? implode(",", $request->price_list_category_id) : null;
$drug->price_list_price = !is_null($request->price_list_price) ? implode(",", $request->price_list_price) : null;
} else {
$drug->price_list_category = NULL;
$drug->price_list_price = NULL;
}
$drug->insurance_coverage = 0;
$drug->cost_price = $request->cost_price;
$drug->non_insured_price = $request->non_insured_price;
$drug->insured_price = 0;
$drug->account_id = $request->account_id;
$drug->inventory_account = $request->inventory_account;
$drug->cost_of_goods_account = $request->cog_account;
$drug->description = $request->description;
$drug->package_unit_id = $request->package_unit_id;
$drug->does_package_unit_have_sub_packages = $request->does_package_unit_have_sub_packages;
$drug->quantity_in_each_package_unit = $request->quantity_in_each_package_unit;
$drug->quantity_in_each_sub_package = $request->quantity_in_each_sub_package;
$drug->number_of_sub_packages_in_main_package_unit = $request->number_of_sub_packages_in_main_package;
$drug->created_by = $logged_in_user_id;
$drug->updated_by = $logged_in_user_id;
$drug->created_at = Carbon::now();
$drug->updated_at = Carbon::now();
try {
if ($drug->name != "" && !is_null($drug->name)) {
$drug->save();
}
if (session()->has('streamline_setup')) {
if (isset($request->selected_drugs)) {
$selected_drugs_array = $request->selected_drugs;
if (!empty($selected_drugs_array)) {
for ($i=0; $i < count($selected_drugs_array) ; $i++) {
/* try this magic to get details for the selected service */
$drugs_array = StreamlineSetupManager::get_drugs_array();
$key=array_search($selected_drugs_array[$i], array_column($drugs_array, 'name'));
$drug_details = $drugs_array[$key];
/* end of magic trial */
$default_drug = new Drug;
$default_drug->name = $drug_details['name'];
$default_drug->cost_price = $drug_details['cost_price'];
$default_drug->insured_price = 0;
$default_drug->non_insured_price = $drug_details['non_insured_price'];
$default_drug->category_id = $drug_details['category_id'];
$drug->account_id = $request->account_id;
$drug->inventory_account = $request->inventory_account;
$drug->cost_of_goods_account = $request->cog_account;
$default_drug->form_id = $drug_details['form_id'];
$default_drug->unit_id = $drug_details['unit_id'];
$default_drug->expiry_date = Carbon::now()->addWeek();
$default_drug->save();
}
}
}
//update the streamline setup table with the new finished step
$this->setupService->saveStep("drugs registration", 1);
// $streamline_setup = new \Streamline\Models\StreamlineSetupStep;
// $streamline_setup->step = "drug registration";
// $streamline_setup->completion_status = 1;
// $streamline_setup->save();
flash("Drugs have been added. PLease ensure that you edit the expiry date before the end of the week")->success();
return redirect("procedures/create");
}
flash($request->name . " Drug has been saved")->success();
return redirect("/drugs/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
/**
* Display the specified resource.
*
* @param int $id
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*/
public function edit($id) {
$drug_categories = DrugCategory::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$drug_units = DrugUnit::orderBy('name', 'asc')->pluck('name', 'id');
$drug_forms = DrugForm::orderBy('name', 'asc')->pluck('name', 'id');
$drug = Drug::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();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
$income_accounts = ['' => '- select -'] + $income_accounts;
if (!$drug) {
flash()->error("There is no such drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit', compact('drug_units', 'drug_forms', 'drug', 'drug_categories', 'cost_of_goods_accounts', 'inventory_asset_accounts',
'income_accounts'));
}
}
/**
* Update the specified resource in storage.
*
* @param int $id
*/
public function update(Request $request, $id) {
/**
* So a little explanation for this point, there are four methods for updating the column values
* for a drug in the drugs table and am using one update method to route them through here,
* differentiate them by the $request->id passed along since i guess no one will dare use the id
* field or value and then according to whatever value is passed in the id using if elseif statements
* tried a switch but the code was really messy and an if is the best, feel free to edit to your heart's content ;)
*/
if (isset($request->id)) {
if ($request->id == 0) {
$validator = Validator::make($request->all(), [
'name' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth()->user()->id;
$drug = Drug::find($id);
$drug->name = $request->name;
$drug->prompt = $request->prompt;
$drug->info_vernacular = $request->info_vernacular;
$drug->info_english = $request->info_english;
$drug->pharmacy_comment = $request->pharmacy_comment;
$drug->description = $request->description;
$drug->updated_by = $logged_in_user_id;
try {
$drug->save();
flash($request->name . " Drug has been updated")->success();
return redirect("/drugs/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
} elseif ($request->id == 1) {
$validator = Validator::make($request->all(), [
'name' => 'required',
'cost_price' => 'required',
'non_insured_price' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth()->user()->id;
$drug = Drug::find($id);
$drug->name = $request->name;
$drug->cost_price = $request->cost_price;
$drug->non_insured_price = $request->non_insured_price;
$drug->insured_price = 0;
$drug->price_list_category = !is_null($request->price_list_category_id) ? implode(",", $request->price_list_category_id) : null;
$drug->price_list_price = !is_null($request->price_list_price) ? implode(",", $request->price_list_price) : null;
$drug->pricing_factor_infant = $request->pricing_factor_infant;
$drug->pricing_factor_children = $request->pricing_factor_children;
$drug->pricing_factor_adult = $request->pricing_factor_adult;
$drug->ip_daily_cost_adult = $request->ip_daily_cost_adult;
$drug->ip_daily_cost_children = $request->ip_daily_cost_children;
$drug->ip_daily_cost_infant = $request->ip_daily_cost_infant;
$drug->ip_daily_cost_adult_insured = 0;
$drug->ip_daily_cost_children_insured = 0;
$drug->ip_daily_cost_infant_insured = 0;
$drug->updated_by = $logged_in_user_id;
try {
$drug->save();
flash($request->name . " Drug has been updated")->success();
return redirect("/drugs/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
} elseif ($request->id == 2) {
$validator = Validator::make($request->all(), [
'name' => 'required',
'form_id' => 'required',
'pack' => 'required',
'strength' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth()->user()->id;
$drug = Drug::find($id);
$drug->name = $request->name;
$drug->form_id = $request->form_id;
$drug->pack = $request->pack;
$drug->strength = $request->strength;
$drug->updated_by = $logged_in_user_id;
if ($drug->save()):
flash($request->name . " has been saved")->success();
return redirect("/drugs/");
else:
flash("There was an error")->error();
endif;
}
}
} else {
$validator = Validator::make($request->all(), [
'name' => 'required',
'form_id' => 'required',
'unit_id' => 'required',
'pharmacy_stock' => 'required',
'store_stock' => 'required',
'reorder_level' => 'required',
'long_term' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth()->user()->id;
$drug = Drug::find($id);
$drug->name = $request->name;
$drug->category_id = $request->category_id;
$drug->form_id = $request->form_id;
$drug->available = $request->available;
$drug->reference_areas = !empty($request->reference_areas)? implode(',',$request->reference_areas):null;
$drug->reference_name = !empty($request->reference_name)? implode(',',$request->reference_name):null;
$drug->unit_id = $request->unit_id;
$drug->store_stock = $request->store_stock;
$drug->pharmacy_stock = $request->pharmacy_stock;
$drug->reorder_level = $request->reorder_level;
$drug->long_term = $request->long_term;
$drug->hssip = $request->hssip;
$drug->insurance_coverage = 0;
$drug->account_id = $request->account_id;
$drug->inventory_account = $request->inventory_account;
$drug->cost_of_goods_account = $request->cog_account;
$drug->updated_by = $logged_in_user_id;
if ($drug->save()):
flash($request->name . " has been saved")->success();
return redirect("/drugs/");
else:
flash("There was an error")->error();
endif;
}
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$drug = Drug::find($id);
if (empty($drug->pharmacy_stock) && empty($drug->store_stock) && empty($drug->opening_stock)) {
if ($drug->delete()):
flash("Drug has been deleted.")->success();
return redirect('/drugs/');
endif;
} else {
flash()->error($drug->name ." is still in stock.");
return redirect('/drugs/');
}
}
public function inactive() {
$drugs = Drug::onlyTrashed()->orderBy('name', 'asc')->paginate(50);
if (empty($drugs)) {
flash()->error("There is no inactive drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.inactive', compact('drugs'));
}
}
public function activate($id) {
$drug = Drug::withTrashed()->find($id);
if ($drug->restore()):
flash("Drug has been activated.")->success();
return redirect('/drugs/');
endif;
}
public function edit_drugs_calculations($id) {
$drug_forms = DrugForm::orderBy('name', 'asc')->pluck('name', 'id');
$drug = Drug::where(['id' => $id])->first();
if (!$drug) {
flash()->error("There is no such drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.drugs_calculations', compact('drug_forms', 'drug'));
}
}
public function edit_drugs_pricing($id) {
$drug = Drug::where(['id' => $id])->first();
if (!$drug) {
flash()->error("There is no such drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.drugs_pricing', compact('drug'));
}
}
public function edit_drugs_prompts($id) {
$drug = Drug::where(['id' => $id])->first();
if (!$drug) {
flash()->error("There is no such drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.drugs_prompts', compact('drug'));
}
}
public function index_drugs_expiring(Request $request) {
$today = Carbon::now(); $range = !empty($request->time_period)? $request->time_period : 0;
$started =$request->start_date; $ended = $request->end_date;
if ($request->time_period) {
if ($request->time_period == 0) {
$drugs = ItemBatchWatcher::where(function ($query) {
$query->where('store_stock', '>', 0)
->orWhere('pharmacy_stock', '>', 0);
})->where('item_type', 1)->where('expiry_date', '<', DATE($today))
->orderBy('expiry_date', 'asc')
->paginate(50);
if (count($drugs) < 1) {
flash()->error("There are no expired drugs");
}
} elseif ($request->time_period == 1) {
$drugs = ItemBatchWatcher::where(function ($query) {
$query->where('store_stock', '>', 0)
->orWhere('pharmacy_stock', '>', 0);
})->where('item_type', 1)->whereBetween('expiry_date', [DATE($today), DATE($today->addWeek())])
->orderBy('expiry_date', 'asc')
->paginate(50);
if (count($drugs) < 1) {
flash()->error("There are no drugs expiring in a week");
}
} elseif ($request->time_period == 2) {
$drugs = ItemBatchWatcher::where(function ($query) {
$query->where('store_stock', '>', 0)
->orWhere('pharmacy_stock', '>', 0);
})->where('item_type', 1)->whereBetween('expiry_date', [DATE($today), DATE($today->addWeeks(2))])
->orderBy('expiry_date', 'asc')
->paginate(50);
if (count($drugs) < 1) {
flash()->error("There are no drugs expiring in two weeks");
}
} elseif ($request->time_period == 3) {
$request->validate([
'start_date' => 'required|date|required_with:end_date',
'end_date' => 'bail|required|required_with:start_date|date|after:start_date',
]);
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
$drugs = ItemBatchWatcher::where(function ($query) {
$query->where('store_stock', '>', 0)
->orWhere('pharmacy_stock', '>', 0);
})->where('item_type', 1)->whereBetween('expiry_date', [$start, $end])
->orderBy('expiry_date', 'asc')
->paginate(50);
if (count($drugs) < 1) flash()->error("There are no drugs expiring between ".$start. " and " .$end);
} elseif ($request->time_period == 4) {
$drugs = ItemBatchWatcher::where(function ($query) {
$query->where('store_stock', '>', 0)
->orWhere('pharmacy_stock', '>', 0);
})->where('item_type', 1)->whereBetween('expiry_date', [DATE($today), DATE($today->addWeeks(4))])
->orderBy('expiry_date', 'asc')
->paginate(50);
if (count($drugs) < 1) {
flash()->error("There are no drugs expiring in a month");
}
}
} else {
$drugs = ItemBatchWatcher::where(function ($query) {
$query->where('store_stock', '>', 0)
->orWhere('pharmacy_stock', '>', 0);
})->where('item_type', 1)->where('expiry_date', '<', DATE($today))
->orderBy('expiry_date', 'asc')
->paginate(50);
if (count($drugs) < 1) {
flash()->error("There are no expired drugs");
}
}
return view('clinical_data::drugs.index_drugs_expiring', compact('drugs', 'range', 'started', 'ended'));
}
public function index_drugs_low_stock() {
$drugs = Drug::whereColumn('store_stock', '<', 'reorder_level')
->orWhere('store_stock', '<', '20')
->orderBy('name', 'asc')
->paginate(500);
if (count($drugs) < 1) {
flash()->error("There is no out of stock drugs");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.index_drugs_low_stock', compact('drugs'));
}
}
public function edit_all() {
$drugs = Drug::orderBy('name', 'asc')
->get();
$drug_units = DrugUnit::orderBy('name', 'asc')
->pluck('name', 'id');
$drug_forms = DrugForm::orderBy('name', 'asc')
->pluck('name', 'id');
if (count($drugs) < 1) {
flash()->error("There is no active drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.all', compact('drugs', 'drug_units', 'drug_forms'));
}
}
public function update_all(Request $request) {
$validator = Validator::make($request->all(), [
//'name' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth()->user()->id;
$id_array = $request->id;
$name_array = $request->name;
$form_id_array = $request->form_id;
$unit_id_array = $request->unit_id;
$store_stock_array = $request->store_stock;
$pharmacy_stock_array = $request->pharmacy_stock;
$reorder_level_array = $request->reorder_level;
$long_term_array = $request->long_term;
$hssip_array = $request->hssip;
for ($x = 0; $x < count($id_array); $x++) {
$drug = Drug::find($id_array[$x]);
$drug->name = isset($name_array[$x]) ? $name_array[$x] : $drug->name;
$drug->form_id = isset($form_id_array[$x]) ? $form_id_array[$x] : $drug->form_id;
$drug->unit_id = isset($unit_id_array[$x]) ? $unit_id_array[$x] : $drug->unit_id;
$drug->store_stock = isset($store_stock_array[$x]) ? $store_stock_array[$x] : $drug->store_stock;
$drug->pharmacy_stock = isset($pharmacy_stock_array[$x]) ? $pharmacy_stock_array[$x] : $drug->pharmacy_stock;
$drug->reorder_level = isset($reorder_level_array[$x]) ? $reorder_level_array[$x] : $drug->reorder_level;
$drug->long_term = isset($long_term_array[$x]) ? $long_term_array[$x] : $drug->long_term;
$drug->hssip = isset($hssip_array[$x]) ? $hssip_array[$x] : $drug->hssip;
$drug->insurance_coverage = 0;
$drug->updated_by = $logged_in_user_id;
try {
$drug->save();
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
flash("Drugs have been updated")->success();
return redirect("/drugs/");
}
}
public function edit_all_prompts() {
$drugs = Drug::orderBy('name', 'asc')->get();
if (count($drugs) < 1) {
flash()->error("There is no active drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.all_prompts', compact('drugs'));
}
}
public function update_all_prompts(Request $request) {
$validator = Validator::make($request->all(), [
//'name' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth()->user()->id;
$id_array = $request->id;
$name_array = $request->name;
$prompt_array = $request->prompt;
$info_vernacular_array = $request->info_vernacular;
$info_english_array = $request->info_english;
$pharmacy_comment_array = $request->pharmacy_comment;
for ($x = 0; $x < count($id_array); $x++) {
$drug = Drug::find($id_array[$x]);
$drug->name = isset($name_array[$x]) ? $name_array[$x] : $drug->name;
$drug->prompt = isset($prompt_array[$x]) ? $prompt_array[$x] : $drug->prompt;
$drug->info_vernacular = isset($info_vernacular_array[$x]) ? $info_vernacular_array[$x] : $drug->info_vernacular;
$drug->info_english = isset($info_english_array[$x]) ? $info_english_array[$x] : $drug->info_english;
$drug->pharmacy_comment = isset($pharmacy_comment_array[$x]) ? $pharmacy_comment_array[$x] : $drug->pharmacy_comment;
$drug->updated_by = $logged_in_user_id;
try {
$drug->save();
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
flash("Drug Prompts have been updated")->success();
return redirect("/drugs/");
}
}
public function edit_all_pricing() {
$drugs = Drug::orderBy('name', 'asc')
->get();
if (count($drugs) < 1) {
flash()->error("There is no active drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.all_pricing', compact('drugs'));
}
}
public function update_all_pricing(Request $request) {
$validator = Validator::make($request->all(), [
//'name' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
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;
$non_insured_price_array = $request->non_insured_price;
$pricing_factor_infant_array = $request->pricing_factor_infant;
$pricing_factor_children_array = $request->pricing_factor_children;
$pricing_factor_adult_array = $request->pricing_factor_adult;
$ip_daily_cost_adult_array = $request->ip_daily_cost_adult;
$ip_daily_cost_children_array = $request->ip_daily_cost_children;
$ip_daily_cost_infant_array = $request->ip_daily_cost_infant;
for ($x = 0; $x < count($id_array); $x++) {
$drug = Drug::find($id_array[$x]);
$drug->name = isset($name_array[$x]) ? $name_array[$x] : $drug->name;
$drug->cost_price = isset($cost_price_array[$x]) ? $cost_price_array[$x] : $drug->cost_price;
$drug->non_insured_price = isset($non_insured_price_array[$x]) ? $non_insured_price_array[$x] : $drug->non_insured_price;
$drug->insured_price = 0;
$drug->pricing_factor_infant = isset($pricing_factor_infant_array[$x]) ? $pricing_factor_infant_array[$x] : $drug->pricing_factor_infant;
$drug->pricing_factor_children = isset($pricing_factor_children_array[$x]) ? $pricing_factor_children_array[$x] : $drug->pricing_factor_children;
$drug->pricing_factor_adult = isset($pricing_factor_adult_array[$x]) ? $pricing_factor_adult_array[$x] : $drug->pricing_factor_adult;
$drug->ip_daily_cost_adult = isset($ip_daily_cost_adult_array[$x]) ? $ip_daily_cost_adult_array[$x] : $drug->ip_daily_cost_adult;
$drug->ip_daily_cost_children = isset($ip_daily_cost_children_array[$x]) ? $ip_daily_cost_children_array[$x] : $drug->ip_daily_cost_children;
$drug->ip_daily_cost_infant = isset($ip_daily_cost_infant_array[$x]) ? $ip_daily_cost_infant_array[$x] : $drug->ip_daily_cost_infant;
$drug->ip_daily_cost_adult_insured = 0;
$drug->ip_daily_cost_children_insured = 0;
$drug->ip_daily_cost_infant_insured = 0;
$drug->updated_by = $logged_in_user_id;
try {
$drug->save();
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
flash("Drugs Pricing have been updated")->success();
return redirect("/drugs/");
}
}
public function edit_all_calculations() {
$drugs = Drug::orderBy('name', 'asc')->get();
$drug_forms = DrugForm::orderBy('name', 'asc')->pluck('name', 'id');
if (count($drugs) < 1) {
flash()->error("There is no active drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.all_calculations', compact('drugs', 'drug_forms'));
}
}
public function update_all_calculations(Request $request) {
$validator = Validator::make($request->all(), [
//'name' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}<br>";
}
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth()->user()->id;
$id_array = $request->id;
$name_array = $request->name;
$form_array = $request->form_id;
$pack_array = $request->pack;
$strength_array = $request->strength;
for ($x = 0; $x < count($id_array); $x++) {
$drug = Drug::find($id_array[$x]);
$drug->name = $name_array[$x];
$drug->form_id = $form_array[$x];
$drug->pack = $pack_array[$x];
$drug->strength = $strength_array[$x];
$drug->updated_by = $logged_in_user_id;
try {
$drug->save();
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
flash("Drug Calculations have been updated")->success();
// This data is needed for redirection
$drugs = Drug::orderBy('name', 'asc')->get();
$drug_forms = DrugForm::orderBy('name', 'asc')->pluck('name', 'id');
return view('clinical_data::drugs.edit.all_calculations', compact('drugs', 'drug_forms'));
}
}
public function bulk_create() {
$drug_categories = DrugCategory::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$drug_categories = ['' => '- select -'] + $drug_categories;
$drug_units = DrugUnit::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$drug_units = ['' => '- select -'] + $drug_units;
$drug_forms = DrugForm::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$drug_forms = ['' => '- select -'] + $drug_forms;
$suppliers = Supplier::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$suppliers = ['' => '- select -'] + $suppliers;
$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();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
$income_accounts = ['' => '- select -'] + $income_accounts;
return view('clinical_data::drugs.bulk_create', compact('drug_categories', 'drug_units', 'drug_forms', 'suppliers',
'income_accounts', 'inventory_asset_accounts', 'cost_of_goods_accounts'));
}
public function bulk_store(Request $request) {
$logged_in_user_id = Auth()->user()->id;
$counter = 0;
$name_array = $request->name;
$category_id_array = $request->category_id;
$form_id_array = $request->form_id;
$unit_id_array = $request->unit_id;
$prompt_array = $request->prompt;
$info_english_array = $request->info_english;
$info_vernacular_array = $request->info_vernacular;
$store_stock_array = $request->store_stock;
$reorder_level_array = $request->reorder_level;
$expiry_date_array = $request->expiry_date;
$supplier_id_array = $request->supplier_id;
$cost_price_array = $request->cost_price;
$non_insured_price_array = $request->non_insured_price;
$account_id_array = $request->account_id;
$inventory_account_array = $request->inventory_account;
$cost_of_goods_account_array = $request->cog_account;
$description_array = $request->description;
for ($x = 0; $x < count($name_array); $x++) {
if ($name_array[$x]) {
$counter += 1;
$drug = new Drug;
$drug->name = $name_array[$x];
$drug->category_id = $category_id_array[$x];
$drug->form_id = $form_id_array[$x];
$drug->unit_id = $unit_id_array[$x];
$drug->prompt = $prompt_array[$x];
$drug->info_english = $info_english_array[$x];
$drug->info_vernacular = $info_vernacular_array[$x];
$drug->store_stock = $store_stock_array[$x];
$drug->reorder_level = $reorder_level_array[$x];
$drug->expiry_date = $expiry_date_array[$x];
$drug->supplier_id = $supplier_id_array[$x];
$drug->insurance_coverage = 0;
$drug->cost_price = $cost_price_array[$x];
$drug->non_insured_price = $non_insured_price_array[$x];
$drug->insured_price = 0;
$drug->account_id = $account_id_array[$x];
$drug->cost_of_goods_account = $cost_of_goods_account_array[$x];
$drug->inventory_account = $inventory_account_array[$x];
$drug->description = $description_array[$x];
$drug->created_by = $logged_in_user_id;
$drug->updated_by = $logged_in_user_id;
try {
$drug->save();
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
}
flash($counter . " drugs have been saved")->success();
return redirect("/drugs/");
}
public function edit_purchase_packaging(Request $request, $id)
{
$drug = Drug::find($id);
if (!$drug) {
flash()->error("There is no such drug");
return redirect('/drugs/');
} else {
return view('clinical_data::drugs.edit.items_purchase_packaging', compact('drug'));
}
}
public function update_purchase_packaging(Request $request)
{
$drug = Drug::find($request->id);
$drug->does_package_unit_have_sub_packages = $request->does_package_unit_have_sub_packages;
$drug->quantity_in_each_package_unit = $request->quantity_in_each_package_unit;
$drug->quantity_in_each_sub_package = $request->quantity_in_each_sub_package;
$drug->number_of_sub_packages_in_main_package_unit = $request->number_of_sub_packages_in_main_package;
$drug->update();
return redirect('drugs');
}
public function get_drug_store_stock($id){
$drug = Drug::find($id);
if ($drug) {
$stock_level = $this->itemsStockService->getItemAllQuantityByTotal($id, 1);
if ($stock_level > 0) {
$message ='&nbsp;&nbsp;<br><span style="color: darkgreen"><b>'.$stock_level.' Units in Stock</b></span>';
} else {
$message = '&nbsp;&nbsp;<br><span style="color: darkorange"><b>Drug is out of stock</b></span>';
}
} else {
$message = "";
}
return $message;
}
}