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

332 lines
14 KiB
PHP
Executable File

<?php
namespace Streamline\Http\Controllers;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Streamline\Models\Patient;
use Streamline\Models\PatientCategory;
use Streamline\Models\SentSmsMessage;
use Streamline\Models\StaffPositions;
use Streamline\Models\Village;
class SMSController extends Controller {
public $sms_api_uri = "http://41.191.79.246:8110";
public function send_sms(Request $request) {
$search_text = "Please use search above to start sending SMS";
$sms_group = ['' => '--select--', 1 => 'Patients', 2 => 'Users', 3 => 'Custom Phone Numbers'];
$patient_groups = ['' => '--select patient grouping--', 1 => 'All Patients', 2 => 'By Patient Groups'];
$user_groups = ['' => '--select user grouping--', 1 => 'All Users', 2 => 'By User Positions'];
$patient_categories = PatientCategory::pluck('name', 'id')->toArray();
$patient_categories = ['' => '- select patient category -'] + $patient_categories;
$user_positions = StaffPositions::pluck('name', 'id')->toArray();
$user_positions = ['' => '- select user position -'] + $user_positions;
/**
* Selected Options
* 1 - All patients
* 2 - Patient Categories
* 3 - All Users
* 4 - User Positions
* 5 - Custom
* 6 - Patient Appointments
* 7 - Investigation Results
*/
$selected_send_option = 0;
$valid_patients = [];
$total_patients_no = 0;
$counter = 0;
$category_position_id = 0;
$valid_users = [];
$total_users_no = 0;
if ($request->sms_option == 1){
if ($request->patient_groups == 1) {
$search_text = "Sending SMS to all patients";
$selected_send_option = 1;
$patients = DB::table('patients')
->select('id', 'phone')
->get();
foreach ($patients as $patient) {
if (strlen($patient->phone) == 10 && in_array(str_split($patient->phone, 3)[0], ["077", "070", "075", "078"])){
$valid_patients[$counter]['id'] = $patient->id;
$valid_patients[$counter]['phone'] = $patient->phone;
$counter++;
}
}
$total_patients_no = count($patients);
} elseif ($request->patient_groups == 2) {
if ($request->patient_groups_search) {
$search_text = "Sending SMS to patients in the " . get_name($request->patient_groups_search, 'id', 'name', 'patient_categories') . " patient category";
$selected_send_option = 2;
$category_position_id = $request->patient_groups_search;
$patients = DB::table('patients')
->where('category_id', $category_position_id)
->select('id', 'phone')
->get();
foreach ($patients as $patient) {
if (strlen($patient->phone) == 10 && in_array(str_split($patient->phone, 3)[0], ["077", "070", "075", "078"])){
$valid_patients[$counter]['id'] = $patient->id;
$valid_patients[$counter]['phone'] = $patient->phone;
$counter++;
}
}
$total_patients_no = count($patients);
} else {
flash("Please select a patient category to continue")->error();
}
} else {
flash("Please select a patient grouping to continue")->error();
}
} elseif ($request->sms_option == 2){
if ($request->user_groups == 1) {
$search_text = "Sending SMS to all staff";
$selected_send_option = 3;
$users = DB::table('users')
->select('id', 'phone')
->get();
foreach ($users as $user) {
if (strlen($user->phone) == 10 && in_array(str_split($user->phone, 3)[0], ["077", "070", "075", "078"])){
$valid_users[$counter]['id'] = $user->id;
$valid_users[$counter]['phone'] = $user->phone;
$counter++;
}
}
$total_users_no = count($users);
} elseif ($request->user_groups == 2) {
if ($request->user_position_search) {
$search_text = "Sending SMS to staff in the " . get_name($request->user_position_search, 'id', 'name', 'staff_positions') . " Positions";
$selected_send_option = 4;
$category_position_id = $request->user_position_search;
$users = DB::table('users')
->where('position_id', $category_position_id)
->select('id', 'phone')
->get();
foreach ($users as $user) {
if (strlen($user->phone) == 10 && in_array(str_split($user->phone, 3)[0], ["077", "070", "075", "078"])){
$valid_users[$counter]['id'] = $user->id;
$valid_users[$counter]['phone'] = $user->phone;
$counter++;
}
}
$total_users_no = count($users);
} else {
flash("Please select a user position to continue")->error();
}
} else {
flash("Please select a user grouping to continue")->error();
}
} else {
flash("Please select an SMS option to continue")->error();
}
return view('sms.send_sms', compact('sms_group', 'patient_groups', 'user_groups', 'patient_categories', 'user_positions',
'total_patients_no', 'valid_patients', 'search_text', 'selected_send_option', 'category_position_id', 'valid_users', 'total_users_no'));
}
public function process_sms(Request $request) {
$client = new Client(['base_uri' => $this->sms_api_uri]);
switch ($request->selected_send_option) {
case 1:
case 2:
default:
$valid_patients = json_decode($request->valid_patients);
$ids = [];
$numbers = [];
foreach ($valid_patients as $patient){
$ids[] = $patient->id;
$numbers[] = $patient->phone;
}
$response = $client->request('GET', '/sms/can_sms_be_sent/' . count($valid_patients));
if ($response->getBody()->getContents() == 1){
$this->save_sms_to_table($request->selected_send_option, $request->category_position_id, $ids, $request->sms_message, $request->comment, null,
$numbers, auth()->id());
flash("All SMS messages have been sent")->success();
return redirect('sms/send_sms');
} else {
flash("Insufficient balance on your account. Please contact the Stre@mline Team to recharge")->error();
return back()->withInput();
}
break;
case 3:
case 4:
$valid_users = json_decode($request->valid_users);
$ids = [];
$numbers = [];
foreach ($valid_users as $user){
$ids[] = $user->id;
$numbers[] = $user->phone;
}
$response = $client->request('GET', '/sms/can_sms_be_sent/' . count($valid_users));
if ($response->getBody()->getContents() == 1){
$this->save_sms_to_table($request->selected_send_option, $request->category_position_id, $ids, $request->sms_message, $request->comment, null,
$numbers, auth()->id());
flash("All SMS messages have been sent")->success();
return redirect('sms/send_sms');
} else {
flash("Insufficient balance on your account. Please contact the Stre@mline Team to recharge")->error();
return back()->withInput();
}
break;
}
}
public function save_sms_to_table($sms_group, $category_position_id, $patient_user_id, $sms_message,
$comment, $custom_name, $phone_numbers, $created_by) {
$client = new Client(['base_uri' => $this->sms_api_uri]);
// loop through all the numbers and send message
foreach ($phone_numbers as $number){
$response = $client->request('POST', '/sms/receive_message_to_send', [
'form_params' => [
'message' => $sms_message,
'number' => "+256" . substr($number, 1)
]
]);
}
// save records to the database
$sent_sms = new SentSmsMessage();
$sent_sms->sms_group = $sms_group;
$sent_sms->category_position_id = $category_position_id;
$sent_sms->patient_user_id = implode(",", $patient_user_id);
$sent_sms->sms_message = $sms_message;
$sent_sms->comment = $comment;
$sent_sms->custom_name = $custom_name;
$sent_sms->phone_number = implode(",", $phone_numbers);
$sent_sms->total_sms_sent = count($phone_numbers);
$sent_sms->created_by = $created_by;
$sent_sms->save();
return true;
}
public function view_sms_reports(Request $request) {
if (isset($request->start_date)){
$start_date = $request->start_date;
$end_date = $request->end_date;
} else {
$start_date = Carbon::now()->startOfDay();
$end_date = Carbon::now()->endOfDay();
}
$search_text = "Searching SMS alerts sent from " . streamline_date($start_date) . " to " . streamline_date($end_date);
$sms_sent_to_all_patients = DB::table('sent_sms_messages')
->where('sms_group', 1)
->whereBetween('created_at', [$start_date, $end_date])
->sum('total_sms_sent');
$sms_sent_to_patient_categories = DB::table('sent_sms_messages')
->where('sms_group', 2)
->whereBetween('created_at', [$start_date, $end_date])
->sum('total_sms_sent');
$sms_sent_to_all_users = DB::table('sent_sms_messages')
->where('sms_group', 3)
->whereBetween('created_at', [$start_date, $end_date])
->sum('total_sms_sent');
$sms_sent_to_user_positions = DB::table('sent_sms_messages')
->where('sms_group', 4)
->whereBetween('created_at', [$start_date, $end_date])
->sum('total_sms_sent');
$sms_sent_to_custom = DB::table('sent_sms_messages')
->where('sms_group', 5)
->whereBetween('created_at', [$start_date, $end_date])
->sum('total_sms_sent');
$sms_sent_for_patient_appointments = DB::table('sent_sms_messages')
->where('sms_group', 6)
->whereBetween('created_at', [$start_date, $end_date])
->sum('total_sms_sent');
$sms_sent_for_investigation_results = DB::table('sent_sms_messages')
->where('sms_group', 7)
->whereBetween('created_at', [$start_date, $end_date])
->sum('total_sms_sent');
$total_sms_sent = $sms_sent_for_investigation_results + $sms_sent_for_patient_appointments + $sms_sent_to_custom +
$sms_sent_to_user_positions + $sms_sent_to_all_users + $sms_sent_to_patient_categories + $sms_sent_to_all_patients;
return view('sms.view_sms_reports', compact('sms_sent_to_all_patients', 'sms_sent_to_patient_categories', 'sms_sent_to_all_users',
'sms_sent_to_user_positions', 'sms_sent_to_custom', 'sms_sent_for_patient_appointments', 'sms_sent_for_investigation_results', 'search_text', 'total_sms_sent'));
}
public function send_investigation_alert($patient_id) {
$client = new Client(['base_uri' => $this->sms_api_uri]);
$patient = Patient::find($patient_id);
if (strlen($patient->phone) == 10 && in_array(str_split($patient->phone, 3)[0], ["077", "070", "075", "078"])){
$response = $client->request('GET', '/sms/can_sms_be_sent/1');
if ($response->getBody()->getContents() == 1){
$sms_message = "Your investigation results are ready - " . get_name(1, 'id', 'name', 'hospital_information');
$this->save_sms_to_table(7, 0, [$patient->id], $sms_message, "", null,
[$patient->phone], auth()->id());
return "Alert has been sent to patient";
} else {
return "Insufficient balance on your account. Please contact the Stre@mline Team to recharge";
}
} else {
return "Failed to send alert because patient phone number is incorrect";
}
}
public function send_appointments_alert($patient_id, $sms_message) {
$client = new Client(['base_uri' => $this->sms_api_uri]);
$patient = Patient::find($patient_id);
$user_id = is_null(auth()->id()) ? 1 : auth()->id();
if (strlen($patient->phone) == 10 && in_array(str_split($patient->phone, 3)[0], ["077", "070", "075", "078"])){
$response = $client->request('GET', '/sms/can_sms_be_sent/1');
if ($response->getBody()->getContents() == 1){
$this->save_sms_to_table(6, 0, [$patient->id], $sms_message, "", null,
[$patient->phone], $user_id);
}
}
}
}