2021-11-19 16:07:42 +00:00
< ? php
namespace App\Http\Controllers ;
2021-11-26 07:28:58 +00:00
use App\Events\InsuredConsultation ;
2021-11-25 06:59:44 +00:00
use App\HealthCareSheetType ;
2021-11-19 16:07:42 +00:00
use App\InsuranceState ;
use App\InsuranceSubscriptionState ;
use App\Models\AgentPlus ;
use App\Models\NhAct ;
use App\Models\NhDrugsAndDevice ;
2021-11-25 06:59:44 +00:00
use App\Models\NhExam ;
2021-11-19 16:07:42 +00:00
use App\Models\NhHealthCareSheet ;
2021-11-25 06:59:44 +00:00
use App\Models\NhHealthCareSheetsExam ;
2021-11-19 16:07:42 +00:00
use App\Models\NhHealthCareSheetsHistory ;
use App\Models\NhHealthCareSheetsPerformance ;
use App\Models\NhHealthCareSheetsPrescription ;
2021-11-26 07:28:58 +00:00
use App\Models\NhInfosHealthCareSheets ;
2021-11-19 16:07:42 +00:00
use App\Models\NhInsurance ;
use App\Models\NhInsurancesSubscription ;
use App\Models\NhMedicalPrescription ;
use App\Models\NhNetworksConfig ;
use App\Models\NhPerformance ;
2021-11-25 06:59:44 +00:00
use App\Models\NhProviderClass ;
2021-11-19 16:07:42 +00:00
use App\Traits\ApiResponser ;
use App\Traits\Helper ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\DB ;
2021-11-26 07:28:58 +00:00
use Illuminate\Support\Facades\Event ;
2021-11-19 16:07:42 +00:00
use Illuminate\Support\Facades\Log ;
2021-11-29 12:02:14 +00:00
use stdClass ;
2021-11-19 16:07:42 +00:00
use Throwable ;
class HealthCareSheetController extends Controller
{
use ApiResponser ;
use Helper ;
/**
* @ OA\Get (
* path = " /drugs-and-devices " ,
* summary = " Rechercher les medicaments / appareillaages ( par reseau et par nom) " ,
* tags = { " Médicaments / Appareillages " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " network_id " ,
* name = " network_id " ,
* description = " ID du reseau " ,
* @ OA\Schema (
2021-11-25 06:59:44 +00:00
* type = " integer " ,
* default = 250
2021-11-19 16:07:42 +00:00
* ),
* in = " query " ,
* required = true
* ),
* @ OA\Parameter (
* parameter = " name " ,
* name = " name " ,
* description = " Nom du médicament / appareillage " ,
* @ OA\Schema (
* type = " string "
* ),
* in = " query " ,
* required = true
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : {{ " id " : 2 , " network_id " : 250 , " code " : " ABD " , " name " : " Nivaquine " , " type " : " Comprimé " ,
* " on_prescription " : false , " created_at " : " 2021-11-16T09:13:30.000000Z " , " updated_at " : " 2021-11-16T09:13:30.000000Z " }},
* " error " : null
* }
* )
* )
* )
*/
public function getDrugsAndDevices ( Request $request )
{
$this -> validate ( $request , [
'network_id' => 'required|integer' ,
'name' => 'required|string'
]);
$drugs = NhDrugsAndDevice :: where ( 'network_id' , $request -> input ( 'network_id' ))
-> where ( 'name' , 'like' , '%' . $request -> input ( 'name' ) . '%' ) -> get ();
2021-11-26 07:28:58 +00:00
foreach ( $drugs as $drug ) {
$drug -> type = trans ( 'states.' . $drug -> type );
}
2021-11-19 16:07:42 +00:00
return $this -> successResponse ( $drugs );
}
/**
* @ OA\Post (
* path = " /drugs-and-devices " ,
* summary = " Ajouter les medicaments / appareillages " ,
* tags = { " Médicaments / Appareillages " },
* security = {{ " api_key " : {}}},
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* @ OA\Property (
* property = " network_id " ,
* description = " ID du reseau " ,
* type = " integer " ,
* example = 250
* ),
* @ OA\Property (
* property = " code " ,
* description = " Code du médicament / appareillage " ,
* type = " string " ,
* example = " ABD "
* ),
* @ OA\Property (
* property = " name " ,
* description = " Nom du médicament / appareillage " ,
* type = " string " ,
* example = " Nivaquine "
* ),
* @ OA\Property (
* property = " type " ,
* description = " Type de médicament / appareillage " ,
* type = " string " ,
* enum = { " COMPRESSED " , " SYRUP " , " SOLUTION " , " SUPPOSITORY " , " DEVICE " },
* example = " COMPRESSED "
* ),
* @ OA\Property (
* property = " on_prescription " ,
* description = " Sous ordornance ou pas " ,
* type = " bool " ,
* example = " false "
* )
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
2021-11-29 12:02:14 +00:00
* " response " : { " network_id " : 250 , " code " : " ABD " , " name " : " Nivaquine " , " type " : " COMPRESSED " , " on_prescription " : false , " updated_at " : " 2021-11-29T11:32:31.000000Z " , " created_at " : " 2021-11-29T11:32:31.000000Z " , " id " : 4 },
2021-11-19 16:07:42 +00:00
* " error " : null
* }
* )
* )
* )
*/
public function storeDrugsAndDevices ( Request $request )
{
$this -> validate ( $request , [
'network_id' => 'required|integer|exists:networks,id' ,
'name' => 'required|string' ,
'code' => 'required|string' ,
'type' => 'required|string|in:COMPRESSED,SYRUP,SOLUTION,SUPPOSITORY,DEVICE' ,
'on_prescription' => 'required|boolean'
]);
$drug = NhDrugsAndDevice :: where ( 'network_id' , $request -> input ( 'network_id' ))
-> where ( 'code' , $request -> input ( 'code' )) -> first ();
if ( isset ( $drug )) {
return $this -> errorResponse ( trans ( 'errors.drug_device_already_exists' ));
}
2021-11-29 12:02:14 +00:00
$drug = NhDrugsAndDevice :: create ( $request -> all ());
return $this -> successResponse ( $drug );
2021-11-19 16:07:42 +00:00
}
2021-11-25 06:59:44 +00:00
/**
* @ OA\Get (
* path = " /provider-classes " ,
* summary = " Obtenir toutes les classes de prestataires d'un reseau " ,
* tags = { " Classes de prestataires " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " network_id " ,
* name = " network_id " ,
* description = " ID du reseau " ,
* @ OA\Schema (
* type = " integer " ,
* default = 250
* ),
* in = " query " ,
* required = true
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : {{ " id " : 2 , " name " : " Pharmacien " }},
* " error " : null
* }
* )
* )
* )
*/
public function getNetworkProviderClasses ( Request $request )
{
$this -> validate ( $request , [
'network_id' => 'required|integer|exists:networks,id' ,
]);
$network_id = $request -> input ( 'network_id' );
$classes = NhProviderClass :: whereHas ( 'network_config' , function ( $query ) use ( $network_id ) {
return $query -> where ( 'network_id' , $network_id );
}) -> get ([ 'id' , 'name' ]);
return $this -> successResponse ( $classes );
}
/**
* @ OA\Get (
* path = " /acts " ,
* summary = " Obtenir ou rechercher par code tous les actes d'un reseau " ,
* tags = { " Actes " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " network_id " ,
* name = " network_id " ,
* description = " ID du reseau " ,
* @ OA\Schema (
* type = " integer " ,
* default = 250
* ),
* in = " query " ,
* required = true
* ),
* @ OA\Parameter (
* parameter = " code " ,
* name = " code " ,
* description = " Code de l'acte " ,
* @ OA\Schema (
* type = " string " ,
* default = " CODE1 "
* ),
* in = " query " ,
* required = false
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : {{ " id " : 2 , " code " : " CODE2 " , " name " : " Les actes infirmiers " }},
* " error " : null
* }
* )
* )
* )
*/
public function getNetworkActs ( Request $request )
{
$this -> validate ( $request , [
'network_id' => 'required|integer|exists:networks,id' ,
'code' => 'nullable|string' ,
]);
$network_id = $request -> input ( 'network_id' );
$query = NhAct :: whereHas ( 'network_config' , function ( $query ) use ( $network_id ) {
return $query -> where ( 'network_id' , $network_id );
});
if ( $request -> has ( 'code' )) {
$code = $request -> input ( 'code' );
$query = $query -> where ( 'code' , 'like' , '%' . $code . '%' );
}
$classes = $query -> get ([ 'id' , 'code' , 'name' ]);
return $this -> successResponse ( $classes );
}
2021-11-29 12:02:14 +00:00
/**
* @ OA\Post (
* path = " /health-care-sheets/performances-amount " ,
* summary = " Calculer le montant des prestations lors d'une consulation " ,
* tags = { " Feuilles de soins " },
* security = {{ " api_key " : {}}},
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* @ OA\Property ( property = " network_id " ,
* type = " integer " ,
* example = 250 ,
* description = " ID du reseau de l'assureur "
* ),
* @ OA\Property ( property = " care_condition " ,
* description = " Condition de prise en charge " ,
* type = " string " ,
* enum = { " CURRENT_AFFECTION " , " LONG_TERM_AFFECTION " , " EXONERATION " },
* example = " CURRENT_AFFECTION "
* ),
2021-11-30 14:18:09 +00:00
* @ OA\Property ( property = " amount " ,
* type = " integer " ,
* example = 20000 ,
* description = " Montant de la prestation "
2021-11-29 12:02:14 +00:00
* ),
* ),
2021-11-30 14:18:09 +00:00
* example = { " network_id " : 250 , " care_condition " : " CURRENT_AFFECTION " , " amount " : 20000 }
2021-11-29 12:02:14 +00:00
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
2021-11-30 14:18:09 +00:00
* example = { " status " : 200 , " response " : { " moderator_ticket " : " 4 \ u202f000 FCFA " , " insurance_amount " : " 16 \ u202f000 FCFA " }, " error " : null },
2021-11-29 12:02:14 +00:00
* )
* )
* )
*/
public function calculateConsultationPerformancesAmount ( Request $request )
{
$this -> validate ( $request , [
'network_id' => 'required|integer|exists:networks,id' ,
'care_condition' => 'required|in:CURRENT_AFFECTION,LONG_TERM_AFFECTION,EXONERATION' ,
2021-11-30 14:18:09 +00:00
'amount' => 'required|numeric' ,
2021-11-29 12:02:14 +00:00
]);
$nhConfig = NhNetworksConfig :: where ( 'network_id' , $request -> input ( 'network_id' )) -> first ();
if ( ! isset ( $nhConfig )) {
return $this -> errorResponse ( trans ( 'errors.nano_health_not_activated' ));
}
$currency_code = $this -> getNetworkCurrency ( $request -> input ( 'network_id' ));
$parts = $this -> getConfigInsuranceParts ( $nhConfig , $request -> input ( 'care_condition' ));
2021-11-30 14:18:09 +00:00
$result = new stdClass ();
$result -> moderator_ticket = $this -> toMoneyWithCurrencyCode ( $parts -> insured_part * $request -> input ( 'amount' ), $currency_code );
$result -> insurance_amount = $this -> toMoneyWithCurrencyCode ( $parts -> insurer_part * $request -> input ( 'amount' ), $currency_code ); //
2021-11-29 12:02:14 +00:00
return $this -> successResponse ( $result );
}
2021-11-25 06:59:44 +00:00
/**
* @ OA\Post (
* path = " /health-care-sheets/consultation " ,
2021-11-26 07:28:58 +00:00
* summary = " Consulter ou prescritpion d'un assuré " ,
2021-11-25 06:59:44 +00:00
* tags = { " Feuilles de soins " },
* security = {{ " api_key " : {}}},
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* schema = " consult_health_care_sheet " ,
* title = " Consulter ou prescrire une feuille de soins " ,
2021-11-26 07:28:58 +00:00
* required = { " insured_id " , " network_agent_id " , " password " , " practitioner_lastname " , " practitioner_provider_class_id " , " care_condition " ,
* " performances " },
2021-11-25 06:59:44 +00:00
* @ OA\Property (
* property = " insured_id " ,
* description = " Numéro immatriculation de l’ assuré " ,
* type = " string " ,
* example = " INSUF45548 "
* ),
* @ OA\Property (
* property = " network_agent_id " ,
* description = " ID de l'agent qui saisit la feuille de soin dans le reseau " ,
* type = " integer " ,
* example = 4325
* ),
* @ OA\Property (
* property = " password " ,
* description = " Mot de passe de l'agent " ,
* type = " string " ,
* example = " password "
* ),
* @ OA\Property (
* 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 = " practitioner_lastname " ,
* description = " Nom du pratricien " ,
* type = " string " ,
* example = " Dr DIETCHI "
* ),
* @ OA\Property (
* property = " practitioner_firstname " ,
* description = " Prenom du pratricien " ,
* type = " string " ,
* example = " Djery "
* ),
* @ OA\Property (
* property = " practitioner_provider_class_id " ,
* description = " ID de la classe de prestataire du praticien " ,
* type = " integer " ,
* example = 12
* ),
* @ OA\Property (
* property = " care_condition " ,
* description = " Condition de prise en charge " ,
* type = " string " ,
* enum = { " CURRENT_AFFECTION " , " LONG_TERM_AFFECTION " , " EXONERATION " },
* example = " CURRENT_AFFECTION "
* ),
* @ OA\Property (
* property = " accident_date " ,
* description = " Date de l'accident en cas d'accident " ,
* type = " string " ,
* example = " 2021-10-01 "
* ),
* @ OA\Property (
* property = " pregnancy_start_at " ,
* description = " Date de début de la grossesse (si grossesse) " ,
* type = " string " ,
* example = " 2021-01-01 "
* ),
* @ OA\Property (
* property = " pregnancy_end_at " ,
* description = " Date de fin de la grossese (si grossesse) " ,
* type = " string " ,
* example = " 2021-09-01 "
* ),
* @ OA\Property ( property = " performances " ,
* type = " array " ,
* description = " Listes des prestations " ,
* @ OA\Items ( ref = " #/components/schemas/add_performance " )
* ),
* @ OA\Property ( property = " prescriptions " ,
* type = " array " ,
* description = " Listes des prescriptions medicales " ,
* @ OA\Items ( ref = " #/components/schemas/add_prescription " )
* ),
* @ OA\Property ( property = " exams " ,
* type = " array " ,
* description = " Listes des examens " ,
* @ OA\Items ( ref = " #/components/schemas/add_exam " )
* )
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
2021-11-29 07:34:35 +00:00
* " response " : " Consultation ou prescription effectuée " ,
2021-11-25 06:59:44 +00:00
* " error " : null
* }
* )
* )
* )
*/
public function storeHealthCareSheetConsultation ( Request $request )
2021-11-19 16:07:42 +00:00
{
2021-11-25 06:59:44 +00:00
/**
* @ OA\Schema (
* schema = " add_performance " ,
* title = " Ajouter une prestation " ,
* required = { " act_id " , " amount " },
* @ OA\Property ( property = " act_id " ,
* type = " integer " ,
* example = 2 ,
* description = " ID de l'acte "
* ),
* @ OA\Property ( property = " amount " ,
* type = " number " ,
* example = 30000 ,
* description = " Montant de la prestation "
* ),
* @ OA\Property ( property = " home_visit_fees " ,
* type = " number " ,
* example = 5000 ,
* description = " Frais de deplacement pour visiste à domicile "
* )
* )
*
* @ OA\Schema (
* schema = " add_prescription " ,
* title = " Ajouter une prescription medicale " ,
* required = { " drug_or_device_id " , " dosage " , " quantity " },
* @ OA\Property ( property = " drug_or_device_id " ,
* type = " integer " ,
* example = 2 ,
* description = " ID du medicament ou appareillage "
* ),
* @ OA\Property ( property = " dosage " ,
* type = " string " ,
* example = " 3 fois / jour pendant 1e semaine " ,
* description = " Posologie ou frequence de consommation "
* ),
* @ OA\Property ( property = " quantity " ,
* type = " int " ,
* example = 5 ,
* description = " Quantité "
* )
* )
*
* @ OA\Schema (
* schema = " add_exam " ,
* title = " Ajouter un examen " ,
* required = { " act_id " , " description " , " quantity " },
* @ OA\Property ( property = " act_id " ,
* type = " integer " ,
* example = 2 ,
* description = " ID de l'acte "
* ),
* @ OA\Property ( property = " description " ,
* type = " string " ,
* example = " Une descrition de l'examen " ,
* description = " Description de l'examene "
* ),
* @ OA\Property ( property = " quantity " ,
* type = " int " ,
* example = 5 ,
* description = " Quantité "
* )
* )
*/
2021-11-19 16:07:42 +00:00
$this -> validate ( $request , [
'insured_id' => 'required|string' ,
'network_agent_id' => 'required|integer|exists:networks_agents,id' ,
'password' => 'required|string' ,
'beneficiary_id' => 'nullable|int|exists:nh_having_rights,id' ,
'practitioner_lastname' => 'required|string' ,
'practitioner_firstname' => 'nullable|string' ,
'practitioner_provider_class_id' => 'required|integer' ,
'care_condition' => 'required|in:CURRENT_AFFECTION,LONG_TERM_AFFECTION,EXONERATION' ,
'accident_date' => 'nullable|date_format:Y-m-d|before:today' ,
'pregnancy_start_at' => 'nullable|date_format:Y-m-d|before:today' ,
2021-11-29 10:22:32 +00:00
'pregnancy_end_at' => 'nullable|date_format:Y-m-d|after:pregnancy_start_at' ,
2021-11-26 07:28:58 +00:00
'performances' => 'required|array' ,
2021-11-25 06:59:44 +00:00
'performances.*.act_id' => 'required|integer|exists:nh_acts,id' ,
2021-11-19 16:07:42 +00:00
'performances.*.amount' => 'required|numeric' ,
'performances.*.home_visit_fees' => 'nullable|numeric' ,
'prescriptions' => 'nullable|array' ,
2021-11-23 16:16:17 +00:00
'prescriptions.*.drug_or_device_id' => 'required|integer|exists:nh_drugs_and_devices,id' ,
2021-11-19 16:07:42 +00:00
'prescriptions.*.dosage' => 'required|string' ,
'prescriptions.*.quantity' => 'required|integer' ,
2021-11-25 06:59:44 +00:00
'exams' => 'nullable|array' ,
'exams.*.act_id' => 'required|integer|exists:nh_acts,id' ,
'exams.*.description' => 'required|string' ,
'exams.*.quantity' => 'required|integer' ,
2021-11-19 16:07:42 +00:00
]);
$insurance = NhInsurance :: where ( 'insured_id' , $request -> input ( 'insured_id' )) -> where ( 'state' , InsuranceState :: PAID ) -> first ();
if ( ! isset ( $insurance )) {
return $this -> errorResponse ( trans ( 'errors.not_insured' ));
}
$agent = AgentPlus :: where ( 'network_agent_id' , $request -> input ( 'network_agent_id' )) -> first ();
if ( ! checkPassword ( $request -> input ( 'password' ), $agent -> encrypted_password , $agent -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
2021-11-29 07:34:35 +00:00
$beneficiary_id = $request -> input ( 'beneficiary_id' );
if ( isset ( $beneficiary_id )) {
$beneficiary = $insurance -> beneficiaries () -> where ( 'nh_having_rights.id' , $beneficiary_id ) -> first ();
2021-11-19 16:07:42 +00:00
if ( ! isset ( $beneficiary )) {
2021-11-25 06:59:44 +00:00
return $this -> errorResponse ( trans ( 'errors.beneficiary_not_found' ));
2021-11-19 16:07:42 +00:00
}
}
$nhConfig = NhNetworksConfig :: where ( 'network_id' , $insurance -> network_id ) -> first ();
if ( ! isset ( $nhConfig )) {
return $this -> errorResponse ( trans ( 'errors.nano_health_not_activated' ));
}
2021-11-29 12:02:14 +00:00
$parts = $this -> getConfigInsuranceParts ( $nhConfig , $request -> input ( 'care_condition' ));
2021-11-19 16:07:42 +00:00
try {
DB :: beginTransaction ();
$datetime = $this -> getCurrentTimeByCountryCode ( $insurance -> network -> country -> code_country );
$healthCareSheet = NhHealthCareSheet :: create ( array_merge ( $request -> all (), [
'health_care_sheet_id' => $this -> generateSheetID (),
'insurance_id' => $insurance -> id ,
'patient_lastname' => isset ( $beneficiary ) ? $beneficiary -> lastname : $insurance -> user -> lastname ,
'patient_firstname' => isset ( $beneficiary ) ? $beneficiary -> firstname : $insurance -> user -> firstname ,
'patient_situation' => isset ( $beneficiary ) ? 'HAVING_RIGHT' : 'INSURED' ,
2021-11-25 06:59:44 +00:00
'type' => HealthCareSheetType :: CONSULTATION ,
2021-11-19 16:07:42 +00:00
'state' => InsuranceSubscriptionState :: UNDER_VALIDATION
]));
foreach ( $request -> input ( 'performances' , []) as $p ) {
$performance = NhPerformance :: create ([
2021-11-25 06:59:44 +00:00
'act_id' => $p [ 'act_id' ],
2021-11-19 16:07:42 +00:00
'amount' => $p [ 'amount' ],
2021-11-30 09:45:59 +00:00
'home_visit_fees' => ! empty ( $p [ 'home_visit_fees' ]) ? $p [ 'home_visit_fees' ] : null ,
'moderator_ticket' => $parts -> insured_part * $p [ 'amount' ], // to calculate,
'insurance_amount' => $parts -> insurer_part * $p [ 'amount' ], // to calculate, montant de l'assurance
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
NhHealthCareSheetsPerformance :: create ([
'sheet_id' => $healthCareSheet -> id ,
'performance_id' => $performance -> id ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
}
foreach ( $request -> input ( 'prescriptions' , []) as $p ) {
$prescription = NhMedicalPrescription :: create ( array_merge ( $p , [
'created_at' => $datetime , 'updated_at' => $datetime ,
]));
NhHealthCareSheetsPrescription :: create ([
'sheet_id' => $healthCareSheet -> id ,
'prescription_id' => $prescription -> id ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
}
foreach ( $request -> input ( 'exams' , []) as $e ) {
$exam = NhExam :: create ([
'act_id' => $e [ 'act_id' ],
'description' => $e [ 'description' ],
'quantity' => $e [ 'quantity' ] ? ? null ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
NhHealthCareSheetsExam :: create ([
'sheet_id' => $healthCareSheet -> id ,
'exam_id' => $exam -> id ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
}
NhHealthCareSheetsHistory :: create ([
'action' => 'ADD' ,
'health_care_sheet_id' => $healthCareSheet -> health_care_sheet_id ,
'state' => $healthCareSheet -> state ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
$healthCareSheet -> user = $insurance -> user ;
Event :: dispatch ( new InsuredConsultation ( $healthCareSheet , trans ( 'messages.consultation_or_prescription_carried_out' ), trans ( 'messages.consultation_or_prescription_carried_out_mail' , [ 'name' => $insurance -> user -> lastname , 'insured_id' => $insurance -> insured_id ,
'patient_name' => $healthCareSheet -> patient_lastname . ' ' . $healthCareSheet -> patient_firstname , 'patient_situation' => trans ( 'states.' . $healthCareSheet -> patient_situation ), 'care_condition' => trans ( 'states.' . $healthCareSheet -> care_condition ),
'gender' => trans ( 'states.' . $insurance -> user -> identification -> gender ), 'insurance_name' => $nhConfig -> network -> name , 'practitioner_name' => $healthCareSheet -> practitioner_lastname . ' ' . $healthCareSheet -> practitioner_firstname , 'institution_name' => $healthCareSheet -> institution -> lastname ]),
trans ( 'messages.consultation_or_prescription_carried_out_notification' )));
DB :: commit ();
return $this -> successResponse ( trans ( 'messages.consultation_or_prescription_carried_out' ));
} catch ( Throwable $e ) {
Log :: error ( $e -> getMessage () . '\n' . $e -> getTraceAsString ());
DB :: rollBack ();
return $this -> errorResponse ( trans ( 'errors.unexpected_error' ), 500 );
}
}
public function storeHealthCareSheetExecution ( Request $request )
{
$this -> validate ( $request , [
'health_care_sheet_id' => 'required|string' ,
'network_agent_id' => 'required|integer|exists:networks_agents,id' ,
'password' => 'required|string' ,
'practitioner_lastname' => 'required|string' ,
'practitioner_firstname' => 'nullable|string' ,
'practitioner_provider_class_id' => 'required|integer' ,
'prescriptions' => 'nullable|array' ,
'prescriptions.*.id' => 'required|integer|exists:nh_drugs_and_devices,id' ,
'prescriptions.*.unit_price' => 'required|numeric' ,
'exams' => 'nullable|array' ,
'exams.*.id' => 'required|integer|exists:nh_acts,id' ,
'exams.*.unit_price' => 'required|numeric' ,
]);
2021-11-30 14:18:09 +00:00
$sheet = NhHealthCareSheet :: where ( 'id' , $request -> input ( 'health_care_sheet_id' )) -> where ( 'state' , InsuranceState :: PAID ) -> first ();
2021-11-30 09:45:59 +00:00
if ( ! isset ( $insurance )) {
return $this -> errorResponse ( trans ( 'errors.not_insured' ));
}
$agent = AgentPlus :: where ( 'network_agent_id' , $request -> input ( 'network_agent_id' )) -> first ();
if ( ! checkPassword ( $request -> input ( 'password' ), $agent -> encrypted_password , $agent -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
$beneficiary_id = $request -> input ( 'beneficiary_id' );
if ( isset ( $beneficiary_id )) {
$beneficiary = $insurance -> beneficiaries () -> where ( 'nh_having_rights.id' , $beneficiary_id ) -> first ();
if ( ! isset ( $beneficiary )) {
return $this -> errorResponse ( trans ( 'errors.beneficiary_not_found' ));
}
}
$nhConfig = NhNetworksConfig :: where ( 'network_id' , $insurance -> network_id ) -> first ();
if ( ! isset ( $nhConfig )) {
return $this -> errorResponse ( trans ( 'errors.nano_health_not_activated' ));
}
$parts = $this -> getConfigInsuranceParts ( $nhConfig , $request -> input ( 'care_condition' ));
try {
DB :: beginTransaction ();
$datetime = $this -> getCurrentTimeByCountryCode ( $insurance -> network -> country -> code_country );
$healthCareSheet = NhHealthCareSheet :: create ( array_merge ( $request -> all (), [
'health_care_sheet_id' => $this -> generateSheetID (),
'insurance_id' => $insurance -> id ,
'patient_lastname' => isset ( $beneficiary ) ? $beneficiary -> lastname : $insurance -> user -> lastname ,
'patient_firstname' => isset ( $beneficiary ) ? $beneficiary -> firstname : $insurance -> user -> firstname ,
'patient_situation' => isset ( $beneficiary ) ? 'HAVING_RIGHT' : 'INSURED' ,
'type' => HealthCareSheetType :: CONSULTATION ,
'state' => InsuranceSubscriptionState :: UNDER_VALIDATION
]));
foreach ( $request -> input ( 'performances' , []) as $p ) {
$performance = NhPerformance :: create ([
'act_id' => $p [ 'act_id' ],
'amount' => $p [ 'amount' ],
'home_visit_fees' => ! empty ( $p [ 'home_visit_fees' ]) ? $p [ 'home_visit_fees' ] : null ,
2021-11-29 12:02:14 +00:00
'moderator_ticket' => $parts -> insured_part * $p [ 'amount' ], // to calculate,
'insurance_amount' => $parts -> insurer_part * $p [ 'amount' ], // to calculate, montant de l'assurance
2021-11-19 16:07:42 +00:00
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
NhHealthCareSheetsPerformance :: create ([
'sheet_id' => $healthCareSheet -> id ,
'performance_id' => $performance -> id ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
}
foreach ( $request -> input ( 'prescriptions' , []) as $p ) {
$prescription = NhMedicalPrescription :: create ( array_merge ( $p , [
'created_at' => $datetime , 'updated_at' => $datetime ,
]));
NhHealthCareSheetsPrescription :: create ([
'sheet_id' => $healthCareSheet -> id ,
'prescription_id' => $prescription -> id ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
}
2021-11-25 06:59:44 +00:00
foreach ( $request -> input ( 'exams' , []) as $e ) {
$exam = NhExam :: create ([
'act_id' => $e [ 'act_id' ],
'description' => $e [ 'description' ],
'quantity' => $e [ 'quantity' ] ? ? null ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
NhHealthCareSheetsExam :: create ([
'sheet_id' => $healthCareSheet -> id ,
'exam_id' => $exam -> id ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
}
2021-11-19 16:07:42 +00:00
NhHealthCareSheetsHistory :: create ([
'action' => 'ADD' ,
'health_care_sheet_id' => $healthCareSheet -> health_care_sheet_id ,
'state' => $healthCareSheet -> state ,
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
2021-11-26 07:28:58 +00:00
$healthCareSheet -> user = $insurance -> user ;
Event :: dispatch ( new InsuredConsultation ( $healthCareSheet , trans ( 'messages.consultation_or_prescription_carried_out' ), trans ( 'messages.consultation_or_prescription_carried_out_mail' , [ 'name' => $insurance -> user -> lastname , 'insured_id' => $insurance -> insured_id ,
'patient_name' => $healthCareSheet -> patient_lastname . ' ' . $healthCareSheet -> patient_firstname , 'patient_situation' => trans ( 'states.' . $healthCareSheet -> patient_situation ), 'care_condition' => trans ( 'states.' . $healthCareSheet -> care_condition ),
'gender' => trans ( 'states.' . $insurance -> user -> identification -> gender ), 'insurance_name' => $nhConfig -> network -> name , 'practitioner_name' => $healthCareSheet -> practitioner_lastname . ' ' . $healthCareSheet -> practitioner_firstname , 'institution_name' => $healthCareSheet -> institution -> lastname ]),
trans ( 'messages.consultation_or_prescription_carried_out_notification' )));
2021-11-19 16:07:42 +00:00
DB :: commit ();
2021-11-25 06:59:44 +00:00
return $this -> successResponse ( trans ( 'messages.consultation_or_prescription_carried_out' ));
2021-11-19 16:07:42 +00:00
} catch ( Throwable $e ) {
Log :: error ( $e -> getMessage () . '\n' . $e -> getTraceAsString ());
DB :: rollBack ();
return $this -> errorResponse ( trans ( 'errors.unexpected_error' ), 500 );
}
}
2021-11-26 07:28:58 +00:00
/**
* @ OA\Get (
* path = " /health-care-sheets " ,
* summary = " Obtenir les feuilles de soins d'un utilisateur " ,
* tags = { " Feuilles de soins " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " user_id " ,
* name = " user_id " ,
* description = " ID de l'utilisateur " ,
* @ OA\Schema (
* type = " integer " ,
* default = 349
* ),
* in = " query " ,
* required = true
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : {{ " insured_id " : " GJKS8ZGBEJTL " , " network_id " : 250 , " user_id " : 349 , " id " : 8 , " health_care_sheet_id " : " XLLLCY75OU8H " , " insurance_id " : 4 , " network_agent_id " : 43510 , " patient_lastname " : " Dietchi " ,
* " patient_firstname " : " Djery " , " patient_situation " : " AYANT DROIT " , " practitioner_lastname " : " Dr Luc " , " practitioner_firstname " : " Boris " , " practitioner_provider_class_id " : " 1 " , " care_condition " : " AFFECTION COURANTE " ,
* " accident_date " : null , " pregnancy_start_at " : null , " pregnancy_end_at " : null , " state " : " EN COURS DE VALIDATION " , " created_at " : " 2021-11-25T04:39:06.000000Z " , " updated_at " : " 2021-11-25T04:39:06.000000Z " , " type " : " CONSULTATION " ,
* " practitioner_provider_class " : " Chirugien " , " institution_name " : " Agent 2 cnamgs fond 4 " , " institution_code " : " aon8K9BZOn " , " currency_code " : " XAF " , " performances " : {{ " id " : 10 , " act_id " : 5 , " amount " : " 5 000 FCFA " , " moderator_ticket " : " 1 000 FCFA " ,
* " insurance_amount " : " 4 000 FCFA " , " home_visit_fees " : " 2 000 FCFA " , " created_at " : " 2021-11-25T05:39:06.000000Z " , " updated_at " : " 2021-11-25T05:39:06.000000Z " , " act " : { " id " : 5 , " code " : " CODE2 " , " name " : " Les actes infirmiers " }}}, " exams " : {{ " id " : 2 , " act_id " : 7 , " description " : " Une description de l'examen " , " quantity " : 5 , " unit_price " : null , " insured_paid_amount " : null , " insurer_paid_amount " : null , " created_at " : " 2021-11-25T04:39:06.000000Z " ,
* " updated_at " : " 2021-11-25T04:39:06.000000Z " , " laravel_through_key " : 8 , " act " : { " id " : 7 , " code " : " CODE4 " , " name " : " Les analyses de biologie m \ u00e9dicale " }}}, " prescriptions " : {{ " id " : 4 , " drug_or_device_id " : 1 , " dosage " : " 3 fois \ /jour " , " quantity " : 5 , " unit_price " : null , " insured_paid_amount " : null , " insurer_paid_amount " : null , " created_at " : " 2021-11-25T05:39:06.000000Z " , " updated_at " : " 2021-11-25T05:39:06.000000Z " ,
* " drug_or_device " : { " id " : 1 , " name " : " Paracetamol " }}}}},
* " error " : null
* }
* )
* )
* )
*/
public function getHealthCareSheets ( Request $request )
{
$this -> validate ( $request , [
'user_id' => 'required|integer|exists:users,id'
]);
$sheets = NhInfosHealthCareSheets :: with ([ 'performances.act:id,code,name' , 'exams.act:id,code,name' , 'prescriptions.drug_or_device:id,name' ]) -> where ( 'user_id' , $request -> input ( 'user_id' )) -> get ();
foreach ( $sheets as $sheet ) {
$sheet -> state = trans ( 'states.' . $sheet -> state );
$sheet -> patient_situation = trans ( 'states.' . $sheet -> patient_situation );
$sheet -> care_condition = trans ( 'states.' . $sheet -> care_condition );
foreach ( $sheet -> performances as $p ) {
$p -> amount = $this -> toMoneyWithCurrencyCode ( $p -> amount , $sheet -> currency_code );
$p -> moderator_ticket = $this -> toMoneyWithCurrencyCode ( $p -> moderator_ticket , $sheet -> currency_code );
$p -> insurance_amount = $this -> toMoneyWithCurrencyCode ( $p -> insurance_amount , $sheet -> currency_code );
$p -> home_visit_fees = isset ( $p -> home_visit_fees ) ? $this -> toMoneyWithCurrencyCode ( $p -> home_visit_fees , $sheet -> currency_code ) : null ;
}
foreach ( $sheet -> exams as $e ) {
$this -> formatExamAndPrescriptionAmounts ( $e , $sheet );
}
foreach ( $sheet -> prescriptions as $p ) {
$this -> formatExamAndPrescriptionAmounts ( $p , $sheet );
}
}
return $this -> successResponse ( $sheets );
}
private function formatExamAndPrescriptionAmounts ( $e , $sheet ) : void
{
$e -> unit_price = isset ( $e -> unit_price ) ? $this -> toMoneyWithCurrencyCode ( $e -> unit_price , $sheet -> currency_code ) : null ;
$e -> insured_paid_amount = isset ( $e -> insured_paid_amount ) ? $this -> toMoneyWithCurrencyCode ( $e -> insured_paid_amount , $sheet -> currency_code ) : null ;
$e -> insurer_paid_amount = isset ( $e -> insurer_paid_amount ) ? $this -> toMoneyWithCurrencyCode ( $e -> insurer_paid_amount , $sheet -> currency_code ) : null ;
}
2021-11-29 07:34:35 +00:00
/**
* @ OA\Put (
* path = " /health-care-sheets " ,
* summary = " Accepter ou Rejeter une feuille de soins " ,
* tags = { " Feuilles de soins " },
* security = {{ " api_key " : {}}},
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema (
* required = { " health_care_sheet_id " , " user_id " , " action " },
* @ OA\Property (
* property = " health_care_sheet_id " ,
* description = " ID de la feuille de soins " ,
* type = " integer " ,
* example = 4
* ),
* @ OA\Property (
* property = " user_id " ,
* description = " ID de l'utilisateur " ,
* type = " integer " ,
* example = 349
* ),
* @ OA\Property (
* property = " action " ,
* description = " Action à effectuer " ,
2021-11-29 10:22:32 +00:00
* enum = { " ACCEPT " , " REJECT " , " RESUBMIT " },
2021-11-29 07:34:35 +00:00
* example = " ACCEPT "
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : " La feuille de soins a été acceptée " ,
* " error " : null
* }
* )
* )
* )
*/
public function treatHealthCareSheet ( Request $request )
{
$this -> validate ( $request , [
'health_care_sheet_id' => 'required|integer|exists:nh_health_care_sheets,id' ,
'user_id' => 'required|integer|exists:users,id' ,
2021-11-29 10:22:32 +00:00
'action' => 'required|in:ACCEPT,REJECT,RESUBMIT'
2021-11-29 07:34:35 +00:00
]);
$action = $request -> input ( 'action' );
$user_id = $request -> input ( 'user_id' );
$sheet = NhHealthCareSheet :: findOrFail ( $request -> input ( 'health_care_sheet_id' ));
if ( $sheet -> insurance -> user_id != $user_id ) {
return $this -> errorResponse ( trans ( 'errors.unauthorized' ));
}
2021-11-29 10:22:32 +00:00
if ( $action != 'RESUBMIT' && $sheet -> state != InsuranceSubscriptionState :: UNDER_VALIDATION ) {
2021-11-29 07:34:35 +00:00
return $this -> errorResponse ( trans ( 'errors.care_sheet_already_been_processed' ));
}
$datetime = $this -> getCurrentTimeByCountryCode ( $sheet -> insurance -> network -> country -> code_country );
if ( $action == 'ACCEPT' ) {
$sheet -> state = InsuranceSubscriptionState :: ACCEPTED ;
$sheet -> updated_at = $datetime ;
$sheet -> save ();
return $this -> successResponse ( trans ( 'messages.care_sheet_accepted' ));
2021-11-29 10:22:32 +00:00
} else if ( $action == 'REJECT' ) {
2021-11-29 07:34:35 +00:00
$sheet -> state = InsuranceSubscriptionState :: REJECTED ;
$sheet -> updated_at = $datetime ;
$sheet -> save ();
return $this -> successResponse ( trans ( 'messages.care_sheet_rejected' ));
2021-11-29 10:22:32 +00:00
} else {
$sheet -> state = InsuranceSubscriptionState :: UNDER_VALIDATION ;
$sheet -> updated_at = $sheet -> created_at = $datetime ;
$sheet -> save ();
return $this -> successResponse ( trans ( 'messages.care_sheet_resubmitted' ));
2021-11-29 07:34:35 +00:00
}
}
2021-11-29 12:02:14 +00:00
private function getConfigInsuranceParts ( NhNetworksConfig $nhConfig , $care_condition ) : stdClass
{
$insuredPart = 0 ;
$insurerPart = 0 ;
switch ( $care_condition ) {
case 'CURRENT_AFFECTION' :
$insurerPart = $nhConfig -> current_affection_percentage_insurer / 100 ;
$insuredPart = $nhConfig -> current_affection_percentage_insured / 100 ;
break ;
case 'LONG_TERM_AFFECTION' :
$insurerPart = $nhConfig -> long_term_affection_percentage_insurer / 100 ;
$insuredPart = $nhConfig -> long_term_affection_percentage_insured / 100 ;
break ;
case 'EXONERATION' :
$insurerPart = $nhConfig -> exoneration_percentage_insurer / 100 ;
$insuredPart = $nhConfig -> exoneration_percentage_insured / 100 ;
break ;
}
$result = new stdClass ();
$result -> insured_part = $insuredPart ;
$result -> insurer_part = $insurerPart ;
return $result ;
}
2021-11-19 16:07:42 +00:00
public function generateSheetID () : string
{
do {
$code = generateTransactionCode ();
$codeCorrect = NhHealthCareSheet :: where ( 'health_care_sheet_id' , $code ) -> count () < 0 ;
} while ( $codeCorrect );
return $code ;
}
}