324 lines
15 KiB
PHP
324 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Events\InsuranceEvent;
|
|
use App\InsuranceAction;
|
|
use App\InsuranceState;
|
|
use App\InsuranceSubscriptionState;
|
|
use App\Models\CountriesCurrency;
|
|
use App\Models\NhInsurance;
|
|
use App\Models\NhInsurancesSubscription;
|
|
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
|
|
{
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/insurances/networks",
|
|
* summary="Afficher la liste des reseaux d'assurances ( par pays )",
|
|
* tags={"Assurances"},
|
|
* security={{"api_key":{}}},
|
|
* @OA\Parameter(
|
|
* parameter="country_id",
|
|
* name="country_id",
|
|
* description="ID du pays",
|
|
* in="query",
|
|
* required=true,
|
|
* @OA\Schema(
|
|
* type="integer",
|
|
* default=78
|
|
* )
|
|
* ),
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="OK",
|
|
* @OA\JsonContent(
|
|
* ref="#/components/schemas/ApiResponse",
|
|
* example = {
|
|
* "status" : 200,
|
|
* "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"}}}},
|
|
* "error":null
|
|
* }
|
|
* )
|
|
* )
|
|
* )
|
|
*/
|
|
public function getInsurancesNetworks(Request $request)
|
|
{
|
|
$this->validate($request, [
|
|
'country_id' => 'nullable|integer|exists:countries,id'
|
|
]);
|
|
|
|
$country = CountriesCurrency::findOrFail($request->input('country_id'));
|
|
|
|
$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
|
|
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')]);
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
/**
|
|
* @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,"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",
|
|
* "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","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(['network:id,name', 'beneficiaries'])->where('user_id', $userId);
|
|
|
|
if ($request->has('type') && $type == 'EDITABLE') {
|
|
$query = $query->whereIn('state', [InsuranceState::PAID]);
|
|
}
|
|
|
|
$insurances = $query->orderBy('created_at', 'DESC')->get();
|
|
|
|
foreach ($insurances as $insurance) {
|
|
$insurance->state = trans('states.' . $insurance->state);
|
|
|
|
if ($type == 'EDITABLE') {
|
|
// Nécessaire seulement lors de la modification ( ajout de ayant droit)
|
|
$config = NhNetworksConfig::where('network_id', $insurance->network->id)->firstOrFail();
|
|
$insurance->network->age_limit_of_insured_and_spouse = $config->age_limit_of_insured_and_spouse;
|
|
$insurance->network->age_limit_of_child_beneficiary = $config->age_limit_of_child_beneficiary;
|
|
$insurance->network->max_number_of_beneficiaries = $config->max_number_of_beneficiaries;
|
|
}
|
|
|
|
$currency_code = $this->getNetworkCurrency($insurance->network_id);
|
|
$insurance->state = trans($insurance->state);
|
|
$insurance->bonus_amount = $this->toMoneyWithCurrencyCode($insurance->bonus_amount, $currency_code);
|
|
$insurance->total_bonus_amount = $this->toMoneyWithCurrencyCode($insurance->total_bonus_amount, $currency_code);
|
|
foreach ($insurance->beneficiaries as $b) {
|
|
$b->bonus_amount = $this->toMoneyWithCurrencyCode($b->bonus_amount, $currency_code);
|
|
}
|
|
}
|
|
|
|
return $this->successResponse($insurances);
|
|
}
|
|
|
|
/**
|
|
* @OA\Put(
|
|
* path="/insurances/{id}/add-beneficiaries",
|
|
* 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);
|
|
$latestSubscription = NhInsurancesSubscription::where('network_id', $request->input('network_id'))->where('user_id', $request->input('user_id'))
|
|
->whereNotIn('state', [InsuranceSubscriptionState::REJECTED])->orderBy('created_at', 'DESC')->first();
|
|
|
|
if (isset($latestSubscription)) {
|
|
return $this->errorResponse(trans('errors.subscription_cannot_be_submitted', ['state' => mb_strtolower(trans('states.' . $latestSubscription->state), 'UTF-8')]));
|
|
}
|
|
$user = $insurance->user;
|
|
$identification = $insurance->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 = $insurance->beneficiaries()->count();
|
|
$networkConfig = NhNetworksConfig::where('network_id', $insurance->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', $insurance->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
|
|
$newSubscription = NhInsurancesSubscription::create([
|
|
'network_id' => $insurance->network_id,
|
|
'user_id' => $insurance->user_id,
|
|
'insurance_subscription_id' => $this->generateSubscriptionID(),
|
|
'number_of_beneficiaries' => sizeof($request->input('beneficiaries', [])),
|
|
'number_of_months' => $monthPrice->number_of_months,
|
|
'bonus_amount' => $monthPrice->min_amount,
|
|
'insurance_action' => InsuranceAction::ADDITION_OF_BENEFICIARY
|
|
]);
|
|
|
|
// 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();
|
|
|
|
NhInsurancesSubscriptionsHistory::create([
|
|
'action' => 'ADD',
|
|
'insurance_subscription_id' => $newSubscription->insurance_subscription_id,
|
|
'insurance_subscription_state' => $newSubscription->state,
|
|
'created_at' => $datetime, 'updated_at' => $datetime,
|
|
]);
|
|
|
|
Event::dispatch(new InsuranceEvent($newSubscription, trans('messages.insurance_addition_beneficiary'), trans('messages.insurance_addition_beneficiary_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_addition_beneficiary_successful'));
|
|
} catch (Throwable $e) {
|
|
Log::error($e->getMessage() . '\n' . $e->getTraceAsString());
|
|
DB::rollBack();
|
|
return $this->errorResponse(trans('errors.unexpected_error'), 500);
|
|
}
|
|
}
|
|
|
|
}
|