2022-01-31 15:51:39 +00:00
< ? php
namespace App\Http\Controllers ;
use App\Events\InsuredConsultation ;
use App\HealthCareSheetType ;
use App\InsuranceState ;
use App\InsuranceSubscriptionState ;
use App\Models\AgentPlus ;
use App\Models\NhAuthorizationOfCareRequest ;
use App\Models\NhAct ;
use App\Models\NhDrugsAndDevice ;
use App\Models\NhExam ;
use App\Models\NhHealthCareSheet ;
use App\Models\NhHealthCareSheetsExam ;
use App\Models\NhHealthCareSheetsHistory ;
use App\Models\NhHealthCareSheetsPerformance ;
use App\Models\NhHealthCareSheetsPrescription ;
2022-02-01 10:58:58 +00:00
use App\Models\NhInfosAuthorizationOfCareRequest ;
2022-01-31 15:51:39 +00:00
use App\Models\NhInfosHealthCareSheets ;
use App\Models\NhInsurance ;
2022-02-04 06:38:50 +00:00
use App\Models\NhInsurancesHavingRight ;
2022-01-31 15:51:39 +00:00
use App\Models\NhMedicalPrescription ;
use App\Models\NhNetworksConfig ;
use App\Models\NhPerformance ;
use App\Models\NhProviderClass ;
use App\Models\NhValidatingAgent ;
use App\Models\User ;
use App\Traits\ApiResponser ;
use App\Traits\Helper ;
2022-02-01 10:58:58 +00:00
use GuzzleHttp\Client ;
2022-01-31 15:51:39 +00:00
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Event ;
use Illuminate\Support\Facades\Log ;
use Illuminate\Support\Facades\Mail ;
use stdClass ;
use Throwable ;
class AuthorizationCareRequestController extends Controller
{
/**
* @ OA\Post (
* path = " /authorizations-care-requests " ,
* summary = " Demander une autorisation de soin " ,
* tags = { " Demandes d'autorisation de soins " },
* security = {{ " api_key " : {}}},
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* schema = " request_for_authorizations_of_care " ,
* title = " Demande autorisation de soins " ,
* required = { " health_care_sheet_id " , " network_agent_id " , " password " , " practitioner_lastname " , " practitioner_provider_class_id " ,
* " prescriptions " , " exams " },
* @ OA\Property (
* property = " act_id " ,
* description = " ID de l'acte " ,
* type = " integer " ,
* example = 5
* ),
* @ OA\Property (
2022-02-04 06:38:50 +00:00
* property = " beneficiary_id " ,
* description = " ID du beneficiaire , s'il s'agit d'une feuille de soins pour beneficiaire " ,
* type = " integer " ,
* example = 4
* ),
* @ OA\Property (
* property = " insurance_id " ,
* description = " ID de l'assurance " ,
2022-01-31 15:51:39 +00:00
* type = " integer " ,
* example = 301
* ),
* @ OA\Property (
* property = " password " ,
* description = " Mot de passe " ,
* type = " string " ,
* example = " password "
* )
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : " Demande autorisation soin envoyée " ,
* " error " : null
* }
* )
* )
* )
*/
public function store ( Request $request )
{
$this -> validate ( $request , [
'act_id' => 'required|integer|exists:nh_acts,id' ,
2022-02-04 06:38:50 +00:00
'insurance_id' => 'required|integer|exists:nh_insurances,id' ,
'beneficiary_id' => 'nullable|int|exists:nh_having_rights,id' ,
2022-01-31 15:51:39 +00:00
'password' => 'required|string'
]);
$act_id = $request -> input ( 'act_id' );
$password = $request -> input ( 'password' );
2022-02-04 06:38:50 +00:00
$insurance = NhInsurance :: find ( $request -> input ( 'insurance_id' ));
if ( $insurance -> state != InsuranceState :: PAID ) {
return $this -> errorResponse ( __ ( 'errors.insurance_not_in_order' ));
}
$user = $insurance -> user ;
2022-01-31 15:51:39 +00:00
if ( ! checkPassword ( $password , $user -> encrypted_password , $user -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
2022-02-04 06:38:50 +00:00
$beneficiary_id = $request -> input ( 'beneficiary_id' );
if ( ! empty ( $beneficiary_id )) {
$beneficiary = NhInsurancesHavingRight :: where ( 'insurance_id' , $insurance -> id ) -> where ( 'having_right_id' , $beneficiary_id ) -> first ();
if ( ! isset ( $beneficiary )) {
return $this -> errorResponse ( trans ( 'errors.beneficiary_not_found' ));
}
} else {
$beneficiary_id = null ;
}
$currentRequest = NhAuthorizationOfCareRequest :: where ( 'insurance_id' , $insurance -> id ) -> where ( 'act_id' , $act_id )
-> where ( 'beneficiary_id' , $beneficiary_id ) -> where ( 'state' , InsuranceSubscriptionState :: UNDER_VALIDATION ) -> first ();
if ( isset ( $currentRequest )) {
return $this -> errorResponse ( __ ( 'errors.act_application_already_pending' ));
}
2022-01-31 15:51:39 +00:00
$act = NhAct :: find ( $act_id );
try {
2022-02-01 17:19:29 +00:00
$datetime = $this -> getCurrentTimeByCountryCode ( $user -> network -> country -> code_country );
2022-01-31 15:51:39 +00:00
DB :: beginTransaction ();
NhAuthorizationOfCareRequest :: create ([
'request_id' => $this -> generateRequestID (),
2022-02-04 06:38:50 +00:00
'insurance_id' => $insurance -> id ,
'beneficiary_id' => $beneficiary_id ,
'to' => isset ( $beneficiary ) ? 'HAVING_RIGHT' : 'INSURED' ,
2022-01-31 15:51:39 +00:00
'act_id' => $act_id ,
2022-02-01 17:19:29 +00:00
'state' => InsuranceSubscriptionState :: UNDER_VALIDATION ,
'created_at' => $datetime , 'updated_at' => $datetime
2022-01-31 15:51:39 +00:00
]);
DB :: commit ();
$recipients = array_map ( function ( $email ) {
return preg_replace ( " / \ s+/ " , " " , $email );
}, NhValidatingAgent :: where ( 'role' , 'DOCTOR' ) -> whereNotNull ( 'password' ) -> pluck ( 'email' ) -> toArray ());
$title = __ ( 'messages.new_care_authorisation' );
2022-02-04 06:38:50 +00:00
$insured = $user -> lastname . ' ' . $user -> firstname ;
if ( isset ( $beneficiary )) {
$insured = $beneficiary -> beneficiary -> lastname . ' ' . $beneficiary -> beneficiary -> firstname ;
}
$message = __ ( 'messages.new_care_authorisation_email' , [ 'insured' => $insured , 'act' => $act -> name ]);
2022-01-31 15:51:39 +00:00
Mail :: mailer ( 'smtp' ) -> raw ( $message , function ( $message ) use ( $recipients , $title ) {
$message -> subject ( $title )
-> to ( $recipients );
});
} catch ( Throwable $t ) {
DB :: rollBack ();
Log :: error ( '-------- Authorization of care error -----------' );
Log :: error ( $t -> getMessage () . " : \n " . $t -> getTraceAsString ());
}
return $this -> successResponse ( __ ( 'messages.new_care_authorisation_sent' ));
}
2022-02-01 10:58:58 +00:00
public function treatRequest ( Request $request )
{
$this -> validate ( $request , [
'request_id' => 'required|integer|exists:nh_authorization_of_care_requests,id' ,
'validating_agent_id' => 'required|integer|exists:nh_validating_agents,id' ,
'action' => 'required|in:ACCEPT,REJECT'
]);
$action = $request -> input ( 'action' );
$validating_agent_id = $request -> input ( 'validating_agent_id' );
$request = NhAuthorizationOfCareRequest :: findOrFail ( $request -> input ( 'request_id' ));
if ( $request -> state != InsuranceSubscriptionState :: UNDER_VALIDATION ) {
return $this -> errorResponse ( trans ( 'errors.care_request_already_been_processed' ));
}
2022-02-04 06:38:50 +00:00
$datetime = $this -> getCurrentTimeByCountryCode ( $request -> insurance -> network -> country -> code_country );
2022-02-01 10:58:58 +00:00
if ( $action == 'ACCEPT' ) {
$request -> state = InsuranceSubscriptionState :: ACCEPTED ;
$message = trans ( 'messages.care_request_accepted' );
$notification = trans ( 'messages.care_request_accepted_notification' , [ 'request_id' => $request -> request_id ]);
} else {
$request -> state = InsuranceSubscriptionState :: REJECTED ;
$message = trans ( 'messages.care_request_rejected' );
$notification = trans ( 'messages.care_request_rejected_notification' , [ 'request_id' => $request -> request_id ]);
}
$request -> validating_agent_id = $validating_agent_id ;
$request -> updated_at = $datetime ;
$request -> save ();
try {
$client = new Client ([
'base_uri' => config ( 'services.notification_service.base_uri' ),
]);
$headers = [
'Authorization' => config ( 'services.notification_service.key' ),
];
$body = new stdClass ();
$body -> user_code = $request -> user -> user_code ;
$body -> message = $notification ;
$body -> date = $datetime ;
$data = new stdClass ();
2022-02-01 17:19:29 +00:00
$data -> screen = " demandeAutorisationSoinScreen " ;
2022-02-01 10:58:58 +00:00
$data -> data = new stdClass ();
2022-02-04 06:38:50 +00:00
$data -> data -> id = $request -> id ;
2022-02-01 10:58:58 +00:00
$body -> data = $data ;
$client -> request ( 'POST' , '/onesignal/pushToUser' , [ 'json' => $body , 'headers' => $headers ]);
} catch ( Throwable $t ) {
Log :: error ( '-------- Treat Care Request notification not sent-----------' );
Log :: error ( $t -> getMessage () . '\n' . $t -> getTraceAsString ());
}
return $this -> successResponse ( $message );
}
/**
* @ OA\Get (
* path = " /authorizations-care-requests " ,
* summary = " Lister toutes les demandes autorisation de soin " ,
* tags = { " Demandes d'autorisation de soins " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " user_id " ,
* name = " user_id " ,
* description = " ID de l'utilisateur " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* )
* ),
* @ OA\Parameter (
* parameter = " page " ,
* name = " page " ,
* description = " Page " ,
* in = " query " ,
* required = false ,
* @ OA\Schema (
* type = " integer "
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : {{ " id " : 2 , " request_id " : " E7879YZMOFOE " , " user_id " : 349 , " act_id " : 7 , " state " : " EN COURS DE VALIDATION " , " validating_agent_id " : null , " created_at " : " 2022-02-01T10:00:02.000000Z " , " updated_at " : " 2022-02-01T10:00:02.000000Z " , "
* act_name " : " Les analyses de biologie m\u00e9dicale " , " act_code " : " CODE4 " , " network_id " :250, " user_lastname " : " Tom Di " , " user_firstname " :null, " user_phone " : " + 237690716648 " , " user_email " : " ddoubletom @ gmail . com " , " validating_agent_lastname " :null,
* " validating_agent_firstname " : null , " validating_agent_phone " : null , " validating_agent_email " : null , " network " : { " id " : 250 , " name " : " Cnamgs-pharmacies " }}},
* " error " : null
* }
* )
* )
* )
*/
public function getAll ( Request $request )
{
$this -> validate ( $request , [
'user_id' => 'nullable|integer|exists:users,id'
]);
$requests = NhInfosAuthorizationOfCareRequest :: with ([ 'network:id,name' ]) -> where ( 'user_id' , $request -> input ( 'user_id' ))
-> paginate ( $request -> input ( 'perPage' , 10 ));
foreach ( $requests -> items () as $r ) {
$r -> state = trans ( 'states.' . $r -> state );
}
return $this -> successResponse ( $requests );
}
2022-01-31 15:51:39 +00:00
private function generateRequestID () : string
{
do {
$code = generateTransactionCode ();
$codeCorrect = NhAuthorizationOfCareRequest :: where ( 'request_id' , $code ) -> count () < 0 ;
} while ( $codeCorrect );
return $code ;
}
}