| 
									
										
										
										
											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-12 06:11:42 +00:00
										 |  |  | use App\InsuranceAction; | 
					
						
							| 
									
										
										
										
											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-11-12 06:11:42 +00:00
										 |  |  | use App\Models\NhInsurancesSubscription; | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |      *                      "response" : {{"id":3,"insured_id":"GJZF4VK69V6S","start_at":"2021-11-08T13:13:27.000000Z", | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |      *     "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", | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |      *     "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, | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |      *     "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, | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |      *     "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"}}}}, | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |      *                      "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'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |         $query = NhInsurance::with(['network:id,name', 'beneficiaries'])->where('user_id', $userId); | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($request->has('type') && $type == 'EDITABLE') { | 
					
						
							| 
									
										
										
										
											2021-11-10 10:42:05 +00:00
										 |  |  |             $query = $query->whereIn('state', [InsuranceState::PAID]); | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $insurances = $query->orderBy('created_at', 'DESC')->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($insurances as $insurance) { | 
					
						
							|  |  |  |             $insurance->state = trans('states.' . $insurance->state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($type == 'EDITABLE') { | 
					
						
							| 
									
										
										
										
											2021-11-10 10:42:05 +00:00
										 |  |  |                 // Nécessaire seulement lors de la modification ( ajout de ayant droit)
 | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |                 $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; | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |             $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) { | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |                 $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); | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |         $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')])); | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |         $user = $insurance->user; | 
					
						
							|  |  |  |         $identification = $insurance->user->identification; | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |         $nbOfBeneficiaries = $insurance->beneficiaries()->count(); | 
					
						
							|  |  |  |         $networkConfig = NhNetworksConfig::where('network_id', $insurance->network_id)->first(); | 
					
						
							| 
									
										
										
										
											2021-11-19 16:07:42 +00:00
										 |  |  |         if ((sizeof($request->input('beneficiaries', [])) + $nbOfBeneficiaries) > $networkConfig->max_number_of_beneficiaries) | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |             return $this->errorResponse(trans('errors.number_of_beneficiaries_exceeded')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |         $monthPrice = $networkConfig->monthsPricesGrid()->where('number_of_months', $insurance->number_of_months)->first(); | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |         if (!isset($monthPrice)) | 
					
						
							|  |  |  |             return $this->errorResponse(trans('errors.incorrect_selected_amount')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             DB::beginTransaction(); | 
					
						
							|  |  |  |             $datetime = $this->getCurrentTimeByCountryCode($networkConfig->network->country->code_country); | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |             // Creer une nouvelle subscription
 | 
					
						
							|  |  |  |             $newSubscription = NhInsurancesSubscription::create([ | 
					
						
							|  |  |  |                 'network_id' => $insurance->network_id, | 
					
						
							|  |  |  |                 'user_id' => $insurance->user_id, | 
					
						
							|  |  |  |                 'insurance_subscription_id' => $this->generateSubscriptionID(), | 
					
						
							| 
									
										
										
										
											2021-11-19 16:07:42 +00:00
										 |  |  |                 'number_of_beneficiaries' => sizeof($request->input('beneficiaries', [])), | 
					
						
							| 
									
										
										
										
											2021-11-12 06:11:42 +00:00
										 |  |  |                 'number_of_months' => $monthPrice->number_of_months, | 
					
						
							|  |  |  |                 'bonus_amount' => $monthPrice->min_amount, | 
					
						
							|  |  |  |                 'insurance_action' => InsuranceAction::ADDITION_OF_BENEFICIARY | 
					
						
							|  |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Ajouter les nouveaux ayant droit
 | 
					
						
							|  |  |  |             $beneficiariesBonus = $this->storeBeneficiariesAndGetBonus($newSubscription, $request, $networkConfig, $monthPrice, $datetime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-15 09:40:06 +00:00
										 |  |  |             $newSubscription->total_bonus_amount = $beneficiariesBonus; | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |             $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, | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 10:42:05 +00:00
										 |  |  |             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, | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |                 '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(); | 
					
						
							| 
									
										
										
										
											2021-11-10 10:42:05 +00:00
										 |  |  |             return $this->successResponse(trans('messages.insurance_addition_beneficiary_successful')); | 
					
						
							| 
									
										
										
										
											2021-11-09 08:30:31 +00:00
										 |  |  |         } 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
										 |  |  | } |