2021-10-19 14:35:01 +00:00
< ? php
namespace App\Http\Controllers ;
2021-10-27 12:48:07 +00:00
use App\Events\InsuranceEvent ;
2021-11-09 08:30:31 +00:00
use App\InsuranceState ;
2021-10-19 14:35:01 +00:00
use App\InsuranceSubscriptionState ;
use App\Models\CountriesCurrency ;
2021-11-04 17:07:15 +00:00
use App\Models\NhInsurance ;
2021-10-19 14:35:01 +00:00
use App\Models\NhInsurancesSubscriptionsHistory ;
use App\Models\NhNetworksConfig ;
use App\Traits\Helper ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Event ;
use Illuminate\Support\Facades\Log ;
use Throwable ;
class InsuranceController extends Controller
{
use Helper ;
/**
* Create a new controller instance .
*
* @ return void
*/
public function __construct ()
{
//
}
/**
* @ OA\Get (
2021-11-09 08:30:31 +00:00
* path = " /insurances/networks " ,
* summary = " Afficher la liste des reseaux d'assurances ( par pays ) " ,
2021-10-19 14:35:01 +00:00
* tags = { " Assurances " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
2021-10-27 12:48:07 +00:00
* parameter = " country_id " ,
* name = " country_id " ,
2021-10-19 14:35:01 +00:00
* description = " ID du pays " ,
2021-10-27 12:48:07 +00:00
* in = " query " ,
2021-10-19 14:35:01 +00:00
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* default = 78
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
2021-10-20 13:05:23 +00:00
* " response " : {{ " id " : 250 , " name " : " Cnamgs-pharmacies " , " age_limit_of_insured_and_spouse " : 30 ,
* " age_limit_of_child_beneficiary " : 25 , " max_number_of_beneficiaries " : " 5 " , " months_prices " : {{ " id " : 1 , " number_of_months " : " 3 " , " min_amount " : " 150000 XAF " }}}},
2021-10-19 14:35:01 +00:00
* " error " : null
* }
* )
* )
* )
*/
2021-11-09 08:30:31 +00:00
public function getInsurancesNetworks ( Request $request )
2021-10-19 14:35:01 +00:00
{
2021-10-27 12:48:07 +00:00
$this -> validate ( $request , [
'country_id' => 'nullable|integer|exists:countries,id'
]);
$country = CountriesCurrency :: findOrFail ( $request -> input ( 'country_id' ));
2021-10-19 14:35:01 +00:00
2021-10-20 13:05:23 +00:00
$insurances = DB :: select ( " SELECT n.id , n.name , nhc.age_limit_of_insured_and_spouse, nhc.age_limit_of_child_beneficiary, nhc.max_number_of_beneficiaries, nhc.id as nhc_id
FROM networks n JOIN configWallet cw ON cw . id_network = n . id JOIN nh_networks_configs nhc
2021-10-27 12:48:07 +00:00
ON nhc . network_id = n . id WHERE n . country_id = : countryId AND cw . type = 'ilink_sante' AND n . status = 1 " , ['countryId' => $request->input ('country_id')]);
2021-10-19 14:35:01 +00:00
foreach ( $insurances as $insurance ) {
$months_prices = DB :: select ( " SELECT id, number_of_months , min_amount FROM nh_months_prices_grid WHERE nh_network_config_id = :nhc_id " ,
[ 'nhc_id' => $insurance -> nhc_id ]);
foreach ( $months_prices as $mp ) {
$mp -> min_amount = $this -> toMoneyWithCurrencyCode ( $mp -> min_amount , $country -> currency_code ? ? 'XAF' );
}
$insurance -> months_prices = $months_prices ;
unset ( $insurance -> nhc_id );
}
return $this -> successResponse ( $insurances );
}
2021-11-09 08:30:31 +00:00
/**
* @ OA\Get (
* path = " /insurances " ,
* summary = " Afficher la liste des assurances ( par utilisateur et par type ) " ,
* tags = { " Assurances " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " user_id " ,
* name = " user_id " ,
* description = " ID de l'utilisateur " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* default = 78
* )
* ),
* @ OA\Parameter (
* parameter = " type " ,
* name = " type " ,
* description = " Type d'assurance " ,
* in = " query " ,
* required = false ,
* @ OA\Schema (
* type = " string " ,
* enum = { " ALL " , " EDITABLE " }
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = {
* " status " : 200 ,
* " response " : {{ " id " : 3 , " insurance_subscription_id " : " 3QM2DS9V4KEG " , " insured_id " : " GJZF4VK69V6S " , " start_at " : " 2021-11-08T13:13:27.000000Z " ,
* " end_at " : " 2022-02-08T13:13:27.000000Z " , " state " : " PAY \ u00c9E " , " created_at " : " 2021-11-08T12:13:27.000000Z " , " updated_at " : " 2021-11-08T12:13:27.000000Z " ,
* " subscription " : { " id " : 2 , " insurance_subscription_id " : " 3QM2DS9V4KEG " , " network_id " : 250 , " user_id " : 349 , " number_of_months " : 3 , " bonus_amount " : " 150 \ u202f000 \ u00a0FCFA " ,
* " number_of_beneficiaries " : 2 , " total_bonus_amount " : " 495 \ u202f000 \ u00a0FCFA " , " state " : " ACCEPT \ u00c9E " , " created_at " : " 2021-11-01T15:18:34.000000Z " ,
* " updated_at " : " 2021-11-05T16:43:00.000000Z " , " reason " : " Quel est ton vrai age ? " , " network " : { " id " : 250 , " name " : " Cnamgs-pharmacies " , " age_limit_of_insured_and_spouse " : 30 ,
* " age_limit_of_child_beneficiary " : 25 , " max_number_of_beneficiaries " : 5 }, " beneficiaries " : {{ " id " : 5 , " insurance_subscription_id " : " 3QM2DS9V4KEG " ,
* " lastname " : " Djery " , " firstname " : " DI " , " gender " : " M " , " birthdate " : " 2001-10-05T00:00:00.00000 0Z " , " affiliation " : " CHILD " , " bonus_amount " : " 195 \ u202f000 \ u00a0FCFA " ,
* " birthdate_proof " : " CERTIFIED_COPY " , " birthdate_proof_doc " : " birth.jpg " , " justice_doc " : " just.png " , " marriage_certificate_doc " : null , " id_document_type " : null ,
* " id_document_front " : null , " id_document_back " : null , " deleted_at " : null , " created_at " : " 2021-11-01T15:18:34.000000Z " , " updated_at " : " 2021-11-01T15:18:34.000000Z " , " affiliation_tr " : " ENFANT " }}}}},
* " error " : null
* }
* )
* )
* )
*/
public function getInsurances ( Request $request )
{
$this -> validate ( $request , [
'user_id' => 'required|integer|exists:users,id' ,
'type' => 'nullable|in:ALL,EDITABLE'
]);
$userId = $request -> input ( 'user_id' );
$type = $request -> input ( 'type' );
$query = NhInsurance :: with ([ 'subscription' , 'subscription.network:id,name' , 'subscription.beneficiaries' ]) -> whereHas ( 'subscription' , function ( $query ) use ( $userId ) {
return $query -> where ( 'user_id' , $userId );
});
if ( $request -> has ( 'type' ) && $type == 'EDITABLE' ) {
$query = $query -> whereIn ( 'state' , [ InsuranceState :: PAID , InsuranceState :: REMAINS ]);
}
$insurances = $query -> orderBy ( 'created_at' , 'DESC' ) -> get ();
foreach ( $insurances as $insurance ) {
$insurance -> state = trans ( 'states.' . $insurance -> state );
if ( $type == 'EDITABLE' ) {
//Necessaire seulement lors de la modification ( ajout de ayant droit;
$config = NhNetworksConfig :: where ( 'network_id' , $insurance -> subscription -> network -> id ) -> firstOrFail ();
$insurance -> subscription -> network -> age_limit_of_insured_and_spouse = $config -> age_limit_of_insured_and_spouse ;
$insurance -> subscription -> network -> age_limit_of_child_beneficiary = $config -> age_limit_of_child_beneficiary ;
$insurance -> subscription -> network -> max_number_of_beneficiaries = $config -> max_number_of_beneficiaries ;
}
$currency_code = $this -> getNetworkCurrency ( $insurance -> subscription -> network_id );
$insurance -> subscription -> state = trans ( 'states.' . $insurance -> subscription -> state );
$insurance -> subscription -> bonus_amount = $this -> toMoneyWithCurrencyCode ( $insurance -> subscription -> bonus_amount , $currency_code );
$insurance -> subscription -> total_bonus_amount = $this -> toMoneyWithCurrencyCode ( $insurance -> subscription -> total_bonus_amount , $currency_code );
foreach ( $insurance -> subscription -> beneficiaries as $b ) {
$b -> bonus_amount = $this -> toMoneyWithCurrencyCode ( $b -> bonus_amount , $currency_code );
}
}
return $this -> successResponse ( $insurances );
}
/**
* @ OA\Put (
2021-11-09 08:36:23 +00:00
* path = " /insurances/ { id}/add-beneficiaries " ,
2021-11-09 08:30:31 +00:00
* summary = " Ajouter des ayants droits ou beneficiaires à une assurance " ,
* tags = { " Assurances " },
* security = {{ " api_key " : {}}},
* @ OA\Parameter (
* parameter = " id " ,
* name = " id " ,
* description = " ID de l'assurance " ,
* in = " path " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* default = 12
* )
* ),
* @ OA\RequestBody (
* description = " Corps de la requete " ,
* required = true ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/add_beneficiaries " ),
* example = { " password " : " 1234 " , " beneficiaries " : {{ " lastname " : " Djery " , " firstname " : " DI " , " gender " : " M " , " birthdate " : " 2001-10-05 " ,
* " affiliation " : " CHILD " , " birthdate_proof " : " CERTIFIED_COPY " , " birthdate_proof_doc " : " birth.jpg " , " justice_doc " : " just.png " , " marriage_certificate_doc " : " mariage.png " ,
* " id_document_type " : " CNI " , " id_document_front " : " cni_front.jpg " , " id_document_back " : " cni_front.jpg " }}}
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " OK " ,
* @ OA\JsonContent (
* ref = " #/components/schemas/ApiResponse " ,
* example = { " status " : 200 , " response " : " Transaction réussie " , " error " : null }
* )
* )
* )
*/
public function addBeneficiaries ( Request $request , $id )
{
/**
* @ OA\Schema (
* schema = " add_beneficiaries " ,
* title = " Ajouter des beneficiaires à une assurance " ,
* required = { " password " , " beneficiaries " },
* @ OA\Property ( property = " password " ,
* type = " string " ,
* example = " 2021469 " ,
* description = " Mot de passe de l'utilisateur assuré "
* ),
* @ OA\Property ( property = " beneficiaries " ,
* type = " array " ,
* description = " Listes des beneficiaires ou ayants droit " ,
* @ OA\Items ( ref = " #/components/schemas/beneficiaries " )
* )
* )
*
*/
$this -> validate ( $request , [
'password' => 'required|string' ,
'beneficiaries' => 'nullable|array' ,
'beneficiaries.*.lastname' => 'required|string' ,
'beneficiaries.*.gender' => 'required|in:M,F' ,
'beneficiaries.*.birthdate' => 'required|date_format:Y-m-d|before:today' ,
'beneficiaries.*.affiliation' => 'required|in:CHILD,SPOUSE' ,
'beneficiaries.*.birthdate_proof' => 'required_if:beneficiaries.*.affiliation,CHILD|in:CERTIFIED_COPY,CERTIFICATE' ,
'beneficiaries.*.birthdate_proof_doc' => 'required_if:beneficiaries.*.affiliation,CHILD|string' ,
'beneficiaries.*.justice_doc' => 'nullable|string' ,
'beneficiaries.*.marriage_certificate_doc' => 'required_if:beneficiaries.*.affiliation,SPOUSE|string' ,
'beneficiaries.*.id_document_type' => 'required_if:beneficiaries.*.affiliation,SPOUSE|string' ,
'beneficiaries.*.id_document_front' => 'required_if:beneficiaries.*.affiliation,SPOUSE|string' ,
'beneficiaries.*.id_document_back' => 'required_if:beneficiaries.*.affiliation,SPOUSE|string' ,
]);
$insurance = NhInsurance :: findOrFail ( $id );
$subscription = $insurance -> subscription ;
if ( $subscription -> state != InsuranceSubscriptionState :: ACCEPTED ) {
return $this -> errorResponse ( trans ( 'errors.subscription_cannot_be_updated' ));
}
$user = $subscription -> user ;
$identification = $subscription -> user -> identification ;
if ( ! isset ( $identification ) || $identification -> status == 0 )
return $this -> errorResponse ( trans ( 'errors.user_identification_required' ));
if ( ! checkPassword ( $request -> password , $user -> encrypted_password , $user -> salt ))
return $this -> errorResponse ( trans ( 'messages.incorrect_user_password' ));
$nbOfBeneficiaries = $subscription -> beneficiaries () -> count ();
$networkConfig = NhNetworksConfig :: where ( 'network_id' , $subscription -> network_id ) -> first ();
if (( sizeof ( $request -> input ( 'beneficiaries' )) + $nbOfBeneficiaries ) > $networkConfig -> max_number_of_beneficiaries )
return $this -> errorResponse ( trans ( 'errors.number_of_beneficiaries_exceeded' ));
$monthPrice = $networkConfig -> monthsPricesGrid () -> where ( 'number_of_months' , $subscription -> number_of_months ) -> first ();
if ( ! isset ( $monthPrice ))
return $this -> errorResponse ( trans ( 'errors.incorrect_selected_amount' ));
try {
DB :: beginTransaction ();
$datetime = $this -> getCurrentTimeByCountryCode ( $networkConfig -> network -> country -> code_country );
// Creer une nouvelle subscription en clonant l'anciene
$newSubscription = $subscription -> replicate ();
$newSubscription -> insurance_subscription_id = $this -> generateSubscriptionID ();
$newSubscription -> number_of_beneficiaries += sizeof ( $request -> input ( 'beneficiaries' ));
$newSubscription -> push ();
//reset relations on EXISTING MODEL (this way you can control which ones will be loaded
$subscription -> setRelations ([]);
//load relations on EXISTING MODEL
$subscription -> load ( 'beneficiaries' );
foreach ( $subscription -> getRelations () as $relation => $items ) {
foreach ( $items as $item ) {
$item -> insurance_subscription_id = $newSubscription -> insurance_subscription_id ;
unset ( $item -> id );
$newSubscription -> { $relation }() -> create ( $item -> toArray ());
}
}
// Ajouter les nouveaux ayant droit
$beneficiariesBonus = $this -> storeBeneficiariesAndGetBonus ( $newSubscription , $request , $networkConfig , $monthPrice , $datetime );
$newSubscription -> total_bonus_amount += $beneficiariesBonus ;
$newSubscription -> created_at = $newSubscription -> updated_at = $datetime ;
$newSubscription -> save ();
$insurance -> insurance_subscription_id = $newSubscription -> insurance_subscription_id ;
$insurance -> state = InsuranceState :: REMAINS ;
$insurance -> remaining_amount = $beneficiariesBonus ;
$insurance -> save ();
NhInsurancesSubscriptionsHistory :: create ([
'action' => 'ADD' ,
'insurance_subscription_id' => $newSubscription -> insurance_subscription_id ,
'insurance_subscription_state' => $newSubscription -> state ,
'insurance_subscription' => json_encode ( $newSubscription ),
'created_at' => $datetime , 'updated_at' => $datetime ,
]);
Event :: dispatch ( new InsuranceEvent ( $newSubscription , trans ( 'messages.insurance_subscription_updated' ), trans ( 'messages.insurance_subscription_mail' , [ 'name' => $newSubscription -> user -> lastname , 'subscription_id' => $newSubscription -> insurance_subscription_id ,
'bonus_amount' => $this -> toMoneyWithNetwork ( $newSubscription -> total_bonus_amount , $newSubscription -> network_id ), 'number_of_beneficiaries' => $newSubscription -> number_of_beneficiaries ,
'gender' => trans ( 'states.' . $identification -> gender ), 'insurance_name' => $networkConfig -> network -> name ])));
DB :: commit ();
return $this -> successResponse ( trans ( 'messages.insurance_subscription_updated_successful' ));
} catch ( Throwable $e ) {
Log :: error ( $e -> getMessage () . '\n' . $e -> getTraceAsString ());
DB :: rollBack ();
return $this -> errorResponse ( trans ( 'errors.unexpected_error' ), 500 );
}
}
2021-10-19 14:35:01 +00:00
}