Files
streamline-emr/docker/_streamline-src/app/Http/Controllers/DataExtractionController.php
T

202 lines
8.1 KiB
PHP
Executable File

<?php
namespace Streamline\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Streamline\Models\ChartOfAccount;
use Symfony\Component\Process\Process;
class DataExtractionController extends Controller {
public function select_data_type(){
return view('data_extraction.select_data_type');
}
public function import_csv ($tag_id) {
if ($tag_id == 3) {
$tag_name = "drugs";
} else if ($tag_id == 2) {
$tag_name = "investigations";
} else if ($tag_id == 4) {
$tag_name = "procedures";
} else if ($tag_id == 5) {
$tag_name = "sundries";
} else if ($tag_id == 6) {
$tag_name = "services";
} else {
$tag_name = "None";
}
$chart_of_accounts = ChartOfAccount::where(['type' => 1])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
$cost_of_goods_accounts = ChartOfAccount::where(['type' => 7])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$inventory_asset_accounts = ChartOfAccount::where(['type' => 10])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
return view('data_extraction.import_csv', compact('tag_id', 'tag_name', 'chart_of_accounts',
'cost_of_goods_accounts', 'inventory_asset_accounts'));
}
public function import_csv_upload(Request $request) {
$csv_data = file($request->file('file'));
$file_destination_path = 'uploads/csv_imports/';
// check if the csv has data
if (count($csv_data) > 0) {
$first_row_data = explode(",", $csv_data[0]);
$number_of_columns_in_data = count($first_row_data);
// check if the csv has the required number of columns
if (!((in_array($request->tag_id, [2, 4, 6]) && $number_of_columns_in_data == 3) ||
($request->tag_id == 5 && $number_of_columns_in_data == 4) ||
($request->tag_id == 3 && $number_of_columns_in_data == 7))) {
flash("The CSV you uploaded does not contain or contains more than all the required number of column fields")->error();
return redirect('/data_extraction/import_csv/' . $request->tag_id);
}
$attach_path = $request->file('file');
$file_name = $attach_path->getClientOriginalName();
$attach_path->move($file_destination_path, $file_name);
$account_id = NULL;
$inventory_account = NULL;
$cost_of_goods_account = NULL;
switch ($request->tag_id) {
case 2:
case 4:
case 6:
$account_id = $request->account_id;
break;
case 5:
case 3:
$account_id = $request->account_id;
$inventory_account = $request->inventory_account;
$cost_of_goods_account = $request->cog_account;
break;
}
$insert_id = DB::table('csv_imports_processes')->insertGetId([
"csv_path" => $file_destination_path . $file_name,
"tag_id" => $request->tag_id,
"account_id" => $account_id,
"inventory_account" => $inventory_account,
"cost_of_goods_account" => $cost_of_goods_account,
"has_price_list" => 0,
"created_by" => Auth::id(),
"created_at" => date('Y-m-d H:i:s')
]);
$exit_code = Artisan::call("import:csv $insert_id");
return redirect('/data_extraction/import_csv_progress/' . $insert_id);
} else {
flash("You have uploaded an empty CSV")->error();
return redirect('/data_extraction/import_csv/' . $request->tag_id);
}
}
public function import_price_list_csv_upload(Request $request) {
$csv_data = file($request->file('file'));
$file_destination_path = 'uploads/csv_imports/';
// check if the csv has data
if (count($csv_data) > 0) {
$first_row_data = explode(",", $csv_data[0]);
$number_of_columns_in_data = count($first_row_data);
// check if the csv has the required number of columns
if ($number_of_columns_in_data > 2) {
for ($i = 1; $i < count($first_row_data); $i++){
// confirm that all patient categories exist
$category_id = get_name(trim($first_row_data[$i]), 'name', 'id', 'patient_categories');
if ($category_id == 'N/A') {
flash("The CSV you uploaded contains a patient category; " . $first_row_data[$i] . " which does not exist in Stre@mline")->error();
return redirect('/data_extraction/import_price_list_csv/' . $request->tag_id);
}
}
} else {
flash("The CSV you uploaded does not contain or contains more than all the required number of column fields")->error();
return redirect('/data_extraction/import_price_list_csv/' . $request->tag_id);
}
$attach_path = $request->file('file');
$file_name = "pl_" . $attach_path->getClientOriginalName();
$attach_path->move($file_destination_path, $file_name);
$insert_id = DB::table('csv_imports_processes')->insertGetId([
"csv_path" => $file_destination_path . $file_name,
"tag_id" => $request->tag_id,
"account_id" => 0,
"inventory_account" => 0,
"cost_of_goods_account" => 0,
"has_price_list" => 1,
"created_by" => Auth::id(),
"created_at" => date('Y-m-d H:i:s')
]);
$exit_code = Artisan::call("import:price_list $insert_id");
return redirect('/data_extraction/import_csv_progress/' . $insert_id);
} else {
flash("You have uploaded an empty CSV")->error();
return redirect('/data_extraction/import_csv/' . $request->tag_id);
}
}
public function import_csv_progress($id) {
return view('data_extraction.import_csv_progress', compact('id'));
}
public function fetch_progress($id) {
$record = DB::table('csv_imports_processes')->where('id', $id)->first();
return $record->records_completed;
}
public function import_price_list_csv ($tag_id) {
if ($tag_id == 3) {
$tag_name = "drugs";
} else if ($tag_id == 2) {
$tag_name = "investigations";
} else if ($tag_id == 4) {
$tag_name = "procedures";
} else if ($tag_id == 5) {
$tag_name = "sundries";
} else if ($tag_id == 6) {
$tag_name = "services";
} else {
$tag_name = "None";
}
$chart_of_accounts = ChartOfAccount::where(['type' => 1])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$chart_of_accounts = ['' => '- select -'] + $chart_of_accounts;
$cost_of_goods_accounts = ChartOfAccount::where(['type' => 7])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$inventory_asset_accounts = ChartOfAccount::where(['type' => 10])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$inventory_asset_accounts = ['' => '- select -'] + $inventory_asset_accounts;
return view('data_extraction.import_price_list_csv', compact('tag_id', 'tag_name', 'chart_of_accounts',
'cost_of_goods_accounts', 'inventory_asset_accounts'));
}
public function start_process($id) {
$exit_code = Artisan::call("import:csv $id");
}
public function start_price_list_process($id) {
$exit_code = Artisan::call("import:price_list $id");
}
}