From 1314e0219e89f851b8c732908b5da0ec3acc03c0 Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Fri, 4 Feb 2022 07:38:50 +0100 Subject: [PATCH] Update authorization requests endpoints --- .../AuthorizationCareRequestController.php | 51 +++++++++-- .../Controllers/HealthCareSheetController.php | 91 +++++++++++++++++-- .../InsuranceSubscriptionController.php | 1 - app/Models/NhAuthorizationOfCareRequest.php | 23 +++-- ...h_authorization_of_care_requests_table.php | 7 +- ...os_authorization_of_care_requests_view.php | 8 +- 6 files changed, 152 insertions(+), 29 deletions(-) diff --git a/app/Http/Controllers/AuthorizationCareRequestController.php b/app/Http/Controllers/AuthorizationCareRequestController.php index ca40ca6..2079726 100755 --- a/app/Http/Controllers/AuthorizationCareRequestController.php +++ b/app/Http/Controllers/AuthorizationCareRequestController.php @@ -20,6 +20,7 @@ use App\Models\NhHealthCareSheetsPrescription; use App\Models\NhInfosAuthorizationOfCareRequest; use App\Models\NhInfosHealthCareSheets; use App\Models\NhInsurance; +use App\Models\NhInsurancesHavingRight; use App\Models\NhMedicalPrescription; use App\Models\NhNetworksConfig; use App\Models\NhPerformance; @@ -62,8 +63,14 @@ class AuthorizationCareRequestController extends Controller * example= 5 * ), * @OA\Property( - * property="user_id", - * description = "ID de l'utilisateur", + * property="beneficiary_id", + * description = "ID du beneficiaire , s'il s'agit d'une feuille de soins pour beneficiaire", + * type="integer", + * example= 4 + * ), + * @OA\Property( + * property="insurance_id", + * description = "ID de l'assurance", * type="integer", * example= 301 * ), @@ -94,16 +101,39 @@ class AuthorizationCareRequestController extends Controller { $this->validate($request, [ 'act_id' => 'required|integer|exists:nh_acts,id', - 'user_id' => 'required|integer|exists:users,id', + 'insurance_id' => 'required|integer|exists:nh_insurances,id', + 'beneficiary_id' => 'nullable|int|exists:nh_having_rights,id', 'password' => 'required|string' ]); - $user = User::find($request->input('user_id')); $act_id = $request->input('act_id'); $password = $request->input('password'); + $insurance = NhInsurance::find($request->input('insurance_id')); + if ($insurance->state != InsuranceState::PAID) { + return $this->errorResponse(__('errors.insurance_not_in_order')); + } + + $user = $insurance->user; if (!checkPassword($password, $user->encrypted_password, $user->salt)) return $this->errorResponse(trans('messages.incorrect_user_password')); + $beneficiary_id = $request->input('beneficiary_id'); + if (!empty($beneficiary_id)) { + $beneficiary = NhInsurancesHavingRight::where('insurance_id', $insurance->id)->where('having_right_id', $beneficiary_id)->first(); + if (!isset($beneficiary)) { + return $this->errorResponse(trans('errors.beneficiary_not_found')); + } + } else { + $beneficiary_id = null; + } + + $currentRequest = NhAuthorizationOfCareRequest::where('insurance_id', $insurance->id)->where('act_id', $act_id) + ->where('beneficiary_id', $beneficiary_id)->where('state', InsuranceSubscriptionState::UNDER_VALIDATION)->first(); + if (isset($currentRequest)) { + return $this->errorResponse(__('errors.act_application_already_pending')); + } + + $act = NhAct::find($act_id); try { $datetime = $this->getCurrentTimeByCountryCode($user->network->country->code_country); @@ -111,7 +141,9 @@ class AuthorizationCareRequestController extends Controller DB::beginTransaction(); NhAuthorizationOfCareRequest::create([ 'request_id' => $this->generateRequestID(), - 'user_id' => $user->id, + 'insurance_id' => $insurance->id, + 'beneficiary_id' => $beneficiary_id, + 'to' => isset($beneficiary) ? 'HAVING_RIGHT' : 'INSURED', 'act_id' => $act_id, 'state' => InsuranceSubscriptionState::UNDER_VALIDATION, 'created_at' => $datetime, 'updated_at' => $datetime @@ -124,7 +156,11 @@ class AuthorizationCareRequestController extends Controller $title = __('messages.new_care_authorisation'); - $message = __('messages.new_care_authorisation_email', ['insured' => $user->lastname . ' ' . $user->firstname, 'act' => $act->name]); + $insured = $user->lastname . ' ' . $user->firstname; + if (isset($beneficiary)) { + $insured = $beneficiary->beneficiary->lastname . ' ' . $beneficiary->beneficiary->firstname; + } + $message = __('messages.new_care_authorisation_email', ['insured' => $insured, 'act' => $act->name]); Mail::mailer('smtp')->raw($message, function ($message) use ($recipients, $title) { $message->subject($title) @@ -156,7 +192,7 @@ class AuthorizationCareRequestController extends Controller return $this->errorResponse(trans('errors.care_request_already_been_processed')); } - $datetime = $this->getCurrentTimeByCountryCode($request->user->network->country->code_country); + $datetime = $this->getCurrentTimeByCountryCode($request->insurance->network->country->code_country); if ($action == 'ACCEPT') { $request->state = InsuranceSubscriptionState::ACCEPTED; @@ -187,6 +223,7 @@ class AuthorizationCareRequestController extends Controller $data = new stdClass(); $data->screen = "demandeAutorisationSoinScreen"; $data->data = new stdClass(); + $data->data->id = $request->id; $body->data = $data; $client->request('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers]); } catch (Throwable $t) { diff --git a/app/Http/Controllers/HealthCareSheetController.php b/app/Http/Controllers/HealthCareSheetController.php index 9a47049..52d5e2c 100755 --- a/app/Http/Controllers/HealthCareSheetController.php +++ b/app/Http/Controllers/HealthCareSheetController.php @@ -9,6 +9,7 @@ use App\InsuranceState; use App\InsuranceSubscriptionState; use App\Models\AgentPlus; use App\Models\NhAct; +use App\Models\NhAuthorizationOfCareRequest; use App\Models\NhDrugsAndDevice; use App\Models\NhExam; use App\Models\NhHealthCareSheet; @@ -22,8 +23,6 @@ use App\Models\NhMedicalPrescription; use App\Models\NhNetworksConfig; use App\Models\NhPerformance; use App\Models\NhProviderClass; -use App\Traits\ApiResponser; -use App\Traits\Helper; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; @@ -253,6 +252,26 @@ class HealthCareSheetController extends Controller * in="query", * required=false * ), + * @OA\Parameter( + * parameter="user_id", + * name="user_id", + * description="ID de utilisateur de l'assuré principal", + * @OA\Schema( + * type="integer", + * ), + * in="query", + * required=false + * ), + * @OA\Parameter( + * parameter="beneficiary_id", + * name="beneficiary_id", + * description="ID du beneficiaire , s'il s'agit d'une feuille de soins pour beneficiaire", + * @OA\Schema( + * type="integer", + * ), + * in="query", + * required=false + * ), * @OA\Response( * response=200, * description="OK", @@ -271,19 +290,23 @@ class HealthCareSheetController extends Controller { $this->validate($request, [ 'network_id' => 'required|integer|exists:networks,id', + 'user_id' => 'nullable|string|exists:users,id', + 'beneficiary_id' => 'nullable|string|exists:nh_having_rights,id', 'code' => 'nullable|string', 'authorization_type' => 'nullable|in:FREE,PRIOR' ]); $network_id = $request->input('network_id'); $authorization_type = $request->input('authorization_type'); + $user_id = $request->input('user_id'); + $beneficiary_id = $request->input('beneficiary_id'); + $code = $request->input('code'); $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'); + if (!empty($code)) { $query = $query->where('code', 'like', '%' . $code . '%'); } @@ -291,7 +314,26 @@ class HealthCareSheetController extends Controller $query = $query->where('authorization_type', $authorization_type); } - $classes = $query->get(['id', 'code', 'name']); + if (!empty($user_id)) { + $query = $query->where('authorization_type', 'FREE'); + + // Recuperer les actes autorisés + $authorized_ids_query = NhAuthorizationOfCareRequest::whereHas('insurance', function ($q) use ($user_id) { + return $q->where('user_id', $user_id); + }); + + if (!empty($beneficiary_id)) { + $authorized_ids_query = $authorized_ids_query->where('beneficiary_id', $beneficiary_id); + } else { + $authorized_ids_query = $authorized_ids_query->whereNull('beneficiary_id'); + } + + $authorized_ids = $authorized_ids_query->where('state', InsuranceSubscriptionState::ACCEPTED)->pluck('act_id')->toArray(); + $query = $query->orWhereIn('id', $authorized_ids); + + } + + $classes = $query->select('id', 'code', 'name')->get(); return $this->successResponse($classes); } @@ -402,6 +444,18 @@ class HealthCareSheetController extends Controller * example= 4 * ), * @OA\Property( + * property="patient_lastname", + * description = "Nom du patient", + * type="string", + * example= "Tom" + * ), + * @OA\Property( + * property="patient_firstname", + * description = "Prenom du patient", + * type="string", + * example= "BOBOL" + * ), + * @OA\Property( * property="practitioner_lastname", * description = "Nom du pratricien", * type="string", @@ -547,6 +601,8 @@ class HealthCareSheetController extends Controller 'network_agent_id' => 'required|integer|exists:networks_agents,id', 'password' => 'required|string', 'beneficiary_id' => 'nullable|int|exists:nh_having_rights,id', + 'patient_lastname' => 'required|string', + 'patient_firstname' => 'nullable|string', 'practitioner_lastname' => 'required|string', 'practitioner_firstname' => 'nullable|string', 'practitioner_provider_class_id' => 'required|integer', @@ -583,6 +639,8 @@ class HealthCareSheetController extends Controller if (!isset($beneficiary)) { return $this->errorResponse(trans('errors.beneficiary_not_found')); } + } else { + $beneficiary_id = null; } $nhConfig = NhNetworksConfig::where('network_id', $insurance->network_id)->first(); @@ -597,13 +655,12 @@ class HealthCareSheetController extends Controller $datetime = $this->getCurrentTimeByCountryCode($insurance->network->country->code_country); - $beneficiary_id = $request->input('beneficiary_id'); $accident_date = $request->input('accident_date'); $pregnancy_start_at = $request->input('pregnancy_start_at'); $pregnancy_end_at = $request->input('pregnancy_end_at'); $healthCareSheet = NhHealthCareSheet::create([ 'health_care_sheet_id' => $this->generateSheetID(), - 'beneficiary_id' => !empty($beneficiary_id) ? $beneficiary_id : null, + 'beneficiary_id' => $beneficiary_id, 'network_agent_id' => $request->input('network_agent_id'), 'care_condition' => $request->input('care_condition'), 'practitioner_lastname' => $request->input('practitioner_lastname'), @@ -637,6 +694,8 @@ class HealthCareSheetController extends Controller 'performance_id' => $performance->id, 'created_at' => $datetime, 'updated_at' => $datetime, ]); + + $this->useAuthorizationRequest($p['act_id'], $insurance->id, $beneficiary_id, $healthCareSheet->id, $datetime); } foreach ($request->input('prescriptions', []) as $p) { @@ -664,6 +723,8 @@ class HealthCareSheetController extends Controller 'exam_id' => $exam->id, 'created_at' => $datetime, 'updated_at' => $datetime, ]); + + $this->useAuthorizationRequest($e['act_id'], $insurance->id, $beneficiary_id, $healthCareSheet->id, $datetime); } NhHealthCareSheetsHistory::create([ @@ -691,6 +752,21 @@ class HealthCareSheetController extends Controller } } + private function useAuthorizationRequest($act_id, $insurance_id, $beneficiary_id, $sheet_id, $datetime): void + { + $authorization_request = NhAuthorizationOfCareRequest::where('act_id', $act_id)->where('insurance_id', $insurance_id); + if (empty($beneficiary_id)) { + $authorization_request = $authorization_request->whereNull('beneficiary_id'); + } else { + $authorization_request = $authorization_request->where('beneficiary_id', $beneficiary_id); + } + $authorization_request->update([ + 'state' => 'USED', + 'health_care_sheet_id' => $sheet_id, + 'updated_at' => $datetime + ]); + } + /** * @OA\Post( * path="/health-care-sheets/execution", @@ -1101,7 +1177,6 @@ class HealthCareSheetController extends Controller } - $query = $query->with(['performances.act:id,code,name', 'exams.act:id,code,name', 'prescriptions.drug_or_device:id,name']); if (!empty($state) && $state == 'TO_BILL') { diff --git a/app/Http/Controllers/InsuranceSubscriptionController.php b/app/Http/Controllers/InsuranceSubscriptionController.php index 6a8a724..5a9b274 100644 --- a/app/Http/Controllers/InsuranceSubscriptionController.php +++ b/app/Http/Controllers/InsuranceSubscriptionController.php @@ -479,7 +479,6 @@ class InsuranceSubscriptionController extends Controller 'insurance_subscription_state' => $subscription->state, 'agent_id' => $request->input('agent_id'), 'nh_validating_agent_id' => $request->input('nh_validating_agent_id'), - 'created_at' => $datetime, 'updated_at' => $datetime, ]); diff --git a/app/Models/NhAuthorizationOfCareRequest.php b/app/Models/NhAuthorizationOfCareRequest.php index 519df52..eeb1e2c 100644 --- a/app/Models/NhAuthorizationOfCareRequest.php +++ b/app/Models/NhAuthorizationOfCareRequest.php @@ -10,13 +10,16 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; /** - * Class AuthorizationOfCareRequest + * Class NhAuthorizationOfCareRequest * * @property int $id * @property string $request_id - * @property int $user_id * @property int $act_id + * @property int $insurance_id + * @property int|null $beneficiary_id + * @property string $to * @property string $state + * @property int|null $health_care_sheet_id * @property int|null $validating_agent_id * @property Carbon $created_at * @property Carbon $updated_at @@ -28,20 +31,26 @@ class NhAuthorizationOfCareRequest extends Model protected $table = 'nh_authorization_of_care_requests'; protected $casts = [ - 'user_id' => 'int', - 'act_id' => 'int' + 'act_id' => 'int', + 'insurance_id' => 'int', + 'beneficiary_id' => 'int', + 'health_care_sheet_id' => 'int', + 'validating_agent_id' => 'int' ]; protected $fillable = [ 'request_id', - 'user_id', 'act_id', + 'insurance_id', + 'beneficiary_id', + 'to', 'state', + 'health_care_sheet_id', 'validating_agent_id' ]; - public function user() + public function insurance() { - return $this->belongsTo(User::class, 'user_id'); + return $this->belongsTo(NhInsurance::class, 'insurance_id'); } } diff --git a/database/migrations/2022_01_31_144613_create_nh_authorization_of_care_requests_table.php b/database/migrations/2022_01_31_144613_create_nh_authorization_of_care_requests_table.php index 8900da3..d847d40 100644 --- a/database/migrations/2022_01_31_144613_create_nh_authorization_of_care_requests_table.php +++ b/database/migrations/2022_01_31_144613_create_nh_authorization_of_care_requests_table.php @@ -16,9 +16,12 @@ class CreateNhAuthorizationOfCareRequestsTable extends Migration Schema::create('nh_authorization_of_care_requests', function (Blueprint $table) { $table->id(); $table->string('request_id')->unique(); - $table->integer('user_id'); $table->integer('act_id'); - $table->enum('state', ['UNDER_VALIDATION', 'ACCEPTED', 'REJECTED'])->default('UNDER_VALIDATION'); + $table->integer('insurance_id'); + $table->integer('beneficiary_id')->nullable(); + $table->enum('to', ['INSURED', 'HAVING_RIGHT'])->default('INSURED')->comment('Destiné à assuré ou ayant droit'); + $table->enum('state', ['UNDER_VALIDATION', 'ACCEPTED', 'REJECTED', 'USED'])->default('UNDER_VALIDATION'); + $table->integer('health_care_sheet_id')->nullable(); $table->integer('validating_agent_id')->nullable(); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrent(); diff --git a/database/migrations/2022_02_01_074814_creae_nh_infos_authorization_of_care_requests_view.php b/database/migrations/2022_02_01_074814_creae_nh_infos_authorization_of_care_requests_view.php index c2756e0..d4dcd50 100644 --- a/database/migrations/2022_02_01_074814_creae_nh_infos_authorization_of_care_requests_view.php +++ b/database/migrations/2022_02_01_074814_creae_nh_infos_authorization_of_care_requests_view.php @@ -15,10 +15,10 @@ class CreaeNhInfosAuthorizationOfCareRequestsView extends Migration public function up() { DB::statement("CREATE OR REPLACE VIEW nh_infos_authorization_of_care_requests AS - SELECT r.* , na.name as act_name , na.code as act_code, nnc.network_id , u.lastname as user_lastname , u.firstname as user_firstname , u.phone as user_phone, u.email as user_email , nva.lastname as validating_agent_lastname, - nva.firstname as validating_agent_firstname , nva.phone as validating_agent_phone, nva.email as validating_agent_email -FROM nh_authorization_of_care_requests r JOIN users u ON r.user_id = u.id JOIN nh_acts na on na.id = r.act_id JOIN nh_networks_configs nnc on na.nh_network_config_id = nnc.id -LEFT JOIN nh_validating_agents nva on r.validating_agent_id = nva.id"); +SELECT r.* , na.name as act_name , na.code as act_code, nnc.network_id , u.lastname as user_lastname , u.firstname as user_firstname , u.phone as user_phone, u.email as user_email , nva.lastname as validating_agent_lastname, +nva.firstname as validating_agent_firstname , nva.phone as validating_agent_phone, nva.email as validating_agent_email, nhr.lastname as beneficiary_lastname , nhr.firstname as beneficiary_firstname +FROM nh_authorization_of_care_requests r JOIN nh_insurances nhi ON nhi.id = r.insurance_id JOIN users u ON nhi.user_id = u.id JOIN nh_acts na on na.id = r.act_id JOIN nh_networks_configs nnc on na.nh_network_config_id = nnc.id +LEFT JOIN nh_having_rights nhr on nhr.id = r.beneficiary_id LEFT JOIN nh_validating_agents nva on r.validating_agent_id = nva.id"); } /**