Skip to content

Commit

Permalink
Merge pull request #27 from palladiumkenya/main
Browse files Browse the repository at this point in the history
changes involving nishauri drug delivery
  • Loading branch information
Murithijoshua authored May 13, 2024
2 parents 4705080 + 35313c7 commit 266ab7e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 34 deletions.
86 changes: 58 additions & 28 deletions app/Http/Controllers/NishauriController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,28 @@
use Carbon\Carbon;
use App\Models\NishauriDrugDelivery;
use App\Models\NishauriDrugOrder;
use Illuminate\Support\Facades\Http;


class NishauriController extends Controller
{
private function send_message($source, $destination, $msg)
{
$key = env('SMS_SERVICE_KEY', '');
$host = env('SMS_SERVICE_HOST', '');

$this->httpresponse = Http::
withoutVerifying()
->withHeaders(['api-token'=> "$key"])
->post("$host", [
'destination' => $destination,
'msg' => $msg,
'sender_id' => $destination,
'gateway' => $source,
]);

return json_decode( $this->httpresponse->getBody(), true);
}
public function reschedule()
{
$data = [];
Expand Down Expand Up @@ -850,25 +868,25 @@ public function drug_delivery_list()
{
if (Auth::user()->access_level == 'Facility') {

$drug_delivery = NishauriDrugDelivery::select('tbl_nishauri_drug_order.id as order_id', 'tbl_appointment.appntmnt_date', 'tbl_client.clinic_number', 'tbl_nishauri_drug_order.mode', 'tbl_nishauri_drug_order.delivery_method', 'tbl_nishauri_drug_order.delivery_person', 'tbl_nishauri_drug_order.delivery_person_contact', 'tbl_nishauri_drug_order.delivery_pickup_time', 'tbl_nishauri_drug_order.status', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_appointment', 'tbl_appointment.id', '=', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_client', 'tbl_client.id', '=', 'tbl_nishauri_drug_order.program_identifier')
->where('tbl_client.mfl_code', Auth::user()->facility_id)
->where('tbl_nishauri_drug_order.status', '=', 'Pending')
->get();
$drug_dispatch = NishauriDrugDelivery::select('tbl_nishauri_drug_order.id as order_id', 'tbl_appointment.appntmnt_date', 'tbl_client.clinic_number', 'tbl_nishauri_drug_order.mode', 'tbl_nishauri_drug_order.delivery_method', 'tbl_nishauri_drug_order.delivery_person', 'tbl_nishauri_drug_order.delivery_person_contact', 'tbl_nishauri_drug_order.delivery_pickup_time', 'tbl_nishauri_drug_order.status', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_appointment', 'tbl_appointment.id', '=', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_client', 'tbl_client.id', '=', 'tbl_nishauri_drug_order.program_identifier')
->where('tbl_client.mfl_code', Auth::user()->facility_id)
->where('tbl_nishauri_drug_order.status', '=', 'Approved')
->get();

$drug_fullfilled = NishauriDrugDelivery::select('tbl_nishauri_drug_order.id as order_id', 'tbl_appointment.appntmnt_date', 'tbl_client.clinic_number', 'tbl_nishauri_drug_order.mode', 'tbl_nishauri_drug_order.delivery_method', 'tbl_nishauri_drug_order.delivery_person', 'tbl_nishauri_drug_order.delivery_person_contact', 'tbl_nishauri_drug_order.delivery_pickup_time', 'tbl_nishauri_drug_order.status', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_appointment', 'tbl_appointment.id', '=', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_client', 'tbl_client.id', '=', 'tbl_nishauri_drug_order.program_identifier')
->where('tbl_client.mfl_code', Auth::user()->facility_id)
->where('tbl_nishauri_drug_order.status', '=', 'Fullfilled')
->get();
$drug_delivery = NishauriDrugDelivery::select('tbl_nishauri_drug_order.id as order_id', 'tbl_appointment.appntmnt_date', 'tbl_client.clinic_number', 'tbl_nishauri_drug_order.mode', 'tbl_nishauri_drug_order.delivery_method', 'tbl_nishauri_drug_order.delivery_person', 'tbl_nishauri_drug_order.delivery_person_contact', 'tbl_nishauri_drug_order.delivery_pickup_time', 'tbl_nishauri_drug_order.status', 'tbl_nishauri_drug_order.appointment_id', 'tbl_nishauri_drug_order.client_phone_no')
->leftJoin('tbl_appointment', 'tbl_appointment.id', '=', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_client', 'tbl_client.id', '=', 'tbl_nishauri_drug_order.program_identifier')
->where('tbl_client.mfl_code', Auth::user()->facility_id)
->where('tbl_nishauri_drug_order.status', '=', 'Pending')
->get();
$drug_dispatch = NishauriDrugDelivery::select('tbl_nishauri_drug_order.id as order_id', 'tbl_appointment.appntmnt_date', 'tbl_client.clinic_number', 'tbl_nishauri_drug_order.mode', 'tbl_nishauri_drug_order.delivery_method', 'tbl_nishauri_drug_order.delivery_person', 'tbl_nishauri_drug_order.delivery_person_contact', 'tbl_nishauri_drug_order.delivery_pickup_time', 'tbl_nishauri_drug_order.status', 'tbl_nishauri_drug_order.appointment_id', 'tbl_nishauri_drug_order.client_phone_no')
->leftJoin('tbl_appointment', 'tbl_appointment.id', '=', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_client', 'tbl_client.id', '=', 'tbl_nishauri_drug_order.program_identifier')
->where('tbl_client.mfl_code', Auth::user()->facility_id)
->where('tbl_nishauri_drug_order.status', '=', 'Approved')
->get();

$drug_fullfilled = NishauriDrugDelivery::select('tbl_nishauri_drug_order.id as order_id', 'tbl_appointment.appntmnt_date', 'tbl_client.clinic_number', 'tbl_nishauri_drug_order.mode', 'tbl_nishauri_drug_order.delivery_method', 'tbl_nishauri_drug_order.delivery_person', 'tbl_nishauri_drug_order.delivery_person_contact', 'tbl_nishauri_drug_order.delivery_pickup_time', 'tbl_nishauri_drug_order.status', 'tbl_nishauri_drug_order.appointment_id', 'tbl_nishauri_drug_order.comment')
->leftJoin('tbl_appointment', 'tbl_appointment.id', '=', 'tbl_nishauri_drug_order.appointment_id')
->leftJoin('tbl_client', 'tbl_client.id', '=', 'tbl_nishauri_drug_order.program_identifier')
->where('tbl_client.mfl_code', Auth::user()->facility_id)
->where('tbl_nishauri_drug_order.status', '=', 'Fullfilled')
->get();

return view('nishauri.drug_delivery', compact('drug_delivery', 'drug_dispatch', 'drug_fullfilled'));
}
Expand All @@ -880,10 +898,11 @@ public function delivery_approval(Request $request)
$client = NishauriDrugDelivery::where('appointment_id', $request->appointment_id)
->update([
'status' => 'Approved',
'updated_at' => date('Y-m-d H:i:s')
'updated_at' => now(),
'approved_date' => now()
]);

// dd( $client);
// dd( $client);
if ($client) {
NishauriDrugOrder::create([
'order_id' => $request->order_id,
Expand All @@ -902,20 +921,31 @@ public function delivery_approval(Request $request)
public function delivery_dispatch(Request $request)
{
try {
$order = $request->order_id;
$order_no = 'ORN' . sprintf('%03d', $order);

$client = NishauriDrugDelivery::where('appointment_id', $request->appointment_id)
->update([
'status' => 'Dispatched',
'updated_at' => date('Y-m-d H:i:s')
'updated_at' => date('Y-m-d H:i:s'),
'dispatched_date' => date('Y-m-d H:i:s'),
'confirmation_code' => $order_no
]);

// dd( $client);
// dd( $client);
if ($client) {
NishauriDrugOrder::where('order_id', $request->order_id)
->update([
'initiated_by' => Auth::user()->id,
'dispatch_notes' => $request->dispatch_notes,
'updated_at' => date('Y-m-d H:i:s')
]);
->update([
'initiated_by' => Auth::user()->id,
'dispatch_notes' => $request->dispatch_notes,
'updated_at' => date('Y-m-d H:i:s')
]);

// Send SMS notification
$destination = $request->client_phone_no;
$msg = "Your drugs for order No $order_no has been dispatched. Kindly use your order number provided for confirmation once you received the drugs";
$source = 40149;
$this->send_message($source, $destination, $msg);
Alert::success('Success', 'Delivery Successfully Dispatched');
return redirect('delivery/list');
} else {
Expand Down
51 changes: 47 additions & 4 deletions app/Http/Controllers/ScheduleSMSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\ClientOutgoing;
use App\Models\OutgoingSms;
use App\Models\Wellness;
use App\Models\UnsentSMS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -502,6 +503,48 @@ public function ltfuScheduler()

}

public function resend() //resend all outgoing sms that were not sent during normal scheduling
{
$messages = UnsentSMS::all();
foreach($messages as $message)
{
$clnt_outgoing_id = $message->id;
$destination = $message->destination;
$msg = $message->msg;
$source = $message->source;

//call sms service
$result = $this->send_message($source,$destination,$clnt_outgoing_id,$msg);

$status = $result['status'];
$messageid = '';
$cost = 0;

foreach($result as $row)
{
if($status == 'success')
{
foreach($result['data'] as $data)
{
foreach($data['Recipients'] as $Recipient)
{
$messageid = $Recipient['messageId'];
$cost = $Recipient['cost'];
}
}
}

//update the sent message with the sms cost and send status
$sms = ClientOutgoing::find($clnt_outgoing_id);
$sms->status = $status;
$sms->cost = $cost;
$sms->message_id = $messageid;
$sms->save();

}
}
}

public function sender()
{
try{
Expand Down Expand Up @@ -530,7 +573,7 @@ public function sender()
->where('msg','like', '%'.$msg.'%')
->where('destination', $destination)
->where('status', 'Sent')
->whereRaw('created_at between (CURDATE() - INTERVAL 1 DAY) AND (CURDATE() + INTERVAL 1 DAY) ')
->whereRaw('DATE(created_at) between (CURDATE() - INTERVAL 1 DAY) AND (CURDATE() + INTERVAL 1 DAY) ')
->doesntExist()) //Message has not been sent, send the current message
{
// $queries = DB::getQueryLog();
Expand All @@ -544,7 +587,7 @@ public function sender()
}

//call sms service
$result = $this->send_message($source,$destination , $msg);
$result = $this->send_message($source,$destination,$clnt_outgoing_id,$msg);

$status = $result['status'];
$messageid = '';
Expand Down Expand Up @@ -585,7 +628,7 @@ public function sender()
}
}

private function send_message($source, $destination, $msg)
private function send_message($source, $destination,$sender_id, $msg)
{
$key = env('SMS_SERVICE_KEY', '');
$host = env('SMS_SERVICE_HOST', '');
Expand All @@ -596,7 +639,7 @@ private function send_message($source, $destination, $msg)
->post("$host", [
'destination' => $destination,
'msg' => $msg,
'sender_id' => $destination,
'sender_id' => $sender_id,
'gateway' => $source,
]);

Expand Down
13 changes: 13 additions & 0 deletions app/Models/UnsentSMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UnsentSMS extends Model
{
use HasFactory;

public $table = "vw_unsent_sms";
}
7 changes: 5 additions & 2 deletions resources/views/nishauri/drug_delivery.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@
<th>Delivery Method</th>
<th>Delivery Person</th>
<th>Delivery Person Contact</th>
<th>Pick Up Time</th>
<th>Status</th>
<th>Comment</th>

</tr>
</thead>
Expand All @@ -188,8 +188,8 @@
<td> {{$result->delivery_method}}</td>
<td> {{$result->delivery_person}}</td>
<td> {{$result->delivery_person_contact}}</td>
<td> {{$result->delivery_pickup_time}}</td>
<td> {{$result->status}}</td>
<td> {{$result->comment}}</td>


</tr>
Expand Down Expand Up @@ -272,6 +272,8 @@
<div class="row">
<input type="hidden" name="appointment_id" id="dispatch_appointment_id">
<input type="hidden" name="order_id" id="dispatch_order_id">
<input type="hidden" name="client_phone_no" id="dispatch_client_phone_no">

<div class="col-md-6 form-group mb-3">
<label for="firstName1">CCC Number</label>
<input type="text" class="form-control" id="dispatch_clinic_number" name="clinic_number" placeholder="CCC Number" readonly />
Expand Down Expand Up @@ -379,6 +381,7 @@ function dispatchModal(result) {
$('#dispatch_delivery_pickup_time').val(result.delivery_pickup_time);
$('#dispatch_appointment_id').val(result.appointment_id);
$('#dispatch_order_id').val(result.order_id);
$('#dispatch_client_phone_no').val(result.client_phone_no);
}
Expand Down
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
//send sms notifications
Route::get('/sms/sender', ['uses' => 'App\Http\Controllers\ScheduleSMSController@sender', 'as' => 'sms-sender']);

//resend sms notifications
Route::get('/sms/resend', ['uses' => 'App\Http\Controllers\ScheduleSMSController@resend', 'as' => 'sms-resend']);

//get KHIS TxCurr
Route::get('/txcurr', ['uses' => 'App\Http\Controllers\TxCurrController@txcurr', 'as' => 'txcurr']);

Expand Down

0 comments on commit 266ab7e

Please sign in to comment.