validate($request,[ 'user_id' => 'required|integer|exists:users,id', 'network_id' => 'required|integer|exists:networks,id', 'customer_account_type_id' => 'required|integer|exists:customer_account_types,id', 'title' => 'required|string', 'lastname' => 'required|string', 'firstname' => 'nullable|string', 'spouse_name' => 'nullable|string', 'nationality' => 'required|string', 'birth_date' => 'required|date_format:Y-m-d|before:today', 'birth_country' => 'required|string', 'birth_city' => 'required|string', 'birth_locality' => 'nullable|string', 'father_lastname' => 'required|string', 'father_firstname' => 'nullable|string', 'mother_birth_lastname' => 'required|string', 'mother_firstname' => 'nullable|string', 'marital_status' => 'required|string', 'spouse_lastname' => 'nullable|string', 'spouse_firstname' => 'nullable|string', 'profession' => 'nullable|string', 'business_activity' => 'nullable|string', 'sub_sector_business_activity' => 'nullable|string', 'tax_number' => 'nullable|string', 'security_number' => 'nullable|string', 'employee_number' => 'nullable|string', 'function' => 'nullable|string', 'employee_name' => 'nullable|string', 'employee_address' => 'nullable|string', 'residential_status' => 'required|string', 'residential_country' => 'required|string', 'residence_permit_number' => 'nullable|string', 'residence_permit_issued_date' => 'nullable|date_format:Y-m-d|before:today', 'residence_permit_expiry_date' => 'nullable|date_format:Y-m-d|after:residence_permit_issued_date', 'address_justification_doc' => 'required|string', 'address' => 'required|string', 'po_box' => 'nullable|string', 'phone_number' => 'required|string', 'email_1' => 'required|string', 'email_2' => 'nullable|string', 'person_to_contact_in_case_of_needs_name' => 'required|string', 'person_to_contact_in_case_of_needs_phone_number' => 'required|string', 'identification_document_type' => 'required|string', 'identification_document_number' => 'required|string', 'identification_document_issued_date' => 'required|date_format:Y-m-d|before:today', 'identification_document_expiry_date' => 'required|date_format:Y-m-d|after:identification_document_issued_date', 'identification_document_issuance_city' => 'required|string', 'identification_document_issuance_country' => 'required|string', 'customer_under_guardianship' => 'required|bool', 'guardian_fullname' => 'nullable|string', 'guardian_phone_number' => 'nullable|string', 'how_did_you_know_company' => 'required|string', 'introducer_fullname' => 'nullable|string', 'introducer_address' => 'nullable|string', 'expected_service' => 'required|string', 'income_sources_and_frequency' => 'nullable|string', 'business_partners' => 'nullable|string', 'bank_references' => 'nullable|string', 'has_account_with_other_bank' => 'required|bool', 'has_operative_other_bank_account' => 'required|bool', 'has_engagement_with_other_banks' => 'required|bool', 'is_politically_exposed_person' => 'required|bool', 'is_politically_exposed_person_in_service' => 'required|bool', 'has_relationship_with_politically_exposed_person' => 'required|bool', 'relationship_with_politically_exposed_person' => 'nullable|string', 'email_alerts' => 'required|bool', 'e_statement_frequency' => 'required|string', 'e_package' => 'nullable|bool', 'debit_card_type' => 'required|string', ]); $accountType = CustomerAccountType::with('documents')->find($request->input('customer_account_type_id')); $documents = []; if(!empty($accountType->documents)){ $rules[] = []; foreach ($accountType->documents as $document){ $rules[$document->name] = 'required|image|mimes:jpeg,png,jpg,jpeg|max:1024'; } $this->validate($request, $rules); foreach ($accountType->documents as $document){ $doc = []; $doc['name'] = $document->name; $doc['description'] = $document->description; $doc['url'] = $this->uploadImage($request,$document->name, 'CRD', 'requests-docs'); $documents[] = $doc; } } if(CustomerAccountRequest::where('user_id', $request->input('user_id'))->where('network_id', $request->input('network_id')) ->where('status', CustomerAccountRequest::UNDER_VALIDATION)->exists()){ return $this->errorResponse(__('messages.customer_account_opening_request_pending')); } try{ DB::beginTransaction(); $accountRequest = CustomerAccountRequest::create(array_merge($request->all(),[ 'unique_id' => strtoupper(Str::random(12)) ])); $documents = array_map(function ($row) use ($accountRequest) { $row['request_id'] = $accountRequest->id; $row['created_at'] = Carbon::now(); $row['updated_at'] = Carbon::now(); return $row; }, $documents); CustomerAccountRequestDocument::insert($documents); DB::commit(); }catch (Throwable $exception){ Log::error($exception->getMessage()); DB::rollBack(); return $this->errorResponse(trans('errors.unexpected_error'), 500); } $mailParams = ['name' => $accountRequest->user->lastname, 'request_id' => $accountRequest->unique_id, 'gender' => $accountRequest->title, 'account_type' => $accountRequest->account_type->name, 'opening_amount' => $this->toMoneyWithNetwork($accountRequest->account_type->opening_amount, $accountRequest->network_id) ]; Event::dispatch(new CustomerAccountRequestEvent($accountRequest, __('messages.customer_account_opening_request'), __('messages.customer_account_opening_request_mail',$mailParams))); return $this->successResponse(__('messages.customer_account_opening_request_submitted')); } public function getCustomerRequests($user_id) { $requests = CustomerAccountRequest::where('user_id', $user_id)->get(); return $this->successResponse($requests); } public function treatRequest($id, Request $request) { $this->validate($request, [ 'nh_validating_agent_id' => 'required_without:agent_id|nullable|integer|exists:nh_validating_agents,id', 'agent_id' => 'required_without:nh_validating_agent_id|nullable|integer|exists:agents,id', 'type' => 'required|in:ACCEPT,REJECT,MORE_INFORMATION', 'reason' => 'nullable|string' ]); $accountRequest = CustomerAccountRequest::findOrFail($id); if(!in_array($accountRequest->status,[CustomerAccountRequest::UNDER_VALIDATION, CustomerAccountRequest::AWAITING_FURTHER_INFORMATION])){ return $this->errorResponse(__('errors.request_already_processed')); } try { DB::beginTransaction(); $datetime = $this->getCurrentTimeByCountryCode($accountRequest->user->network->country->code_country); $type = $request->input('type'); $mailParams = ['name' => $accountRequest->user->lastname, 'request_id' => $accountRequest->unique_id, 'reason' => $request->input('reason'), 'gender' => $accountRequest->title, 'account_type' => $accountRequest->account_type->name, 'opening_amount' => $this->toMoneyWithNetwork($accountRequest->account_type->opening_amount, $accountRequest->network_id) ]; $notificationParams = ['request_id' => $accountRequest->unique_id]; switch ($type){ case 'REJECT': $accountRequest->status = CustomerAccountRequest::REJECTED; $mailTitle = trans('messages.customer_account_opening_request_rejected'); $mailMessage = trans('messages.customer_account_opening_request_rejected_mail', $mailParams); $notification = trans('messages.customer_account_opening_request_rejected_notification', $notificationParams); break; case 'ACCEPT': $accountRequest->status = CustomerAccountRequest::ACCEPTED; $mailTitle = trans('messages.customer_account_opening_request_accepted'); $mailMessage = trans('messages.customer_account_opening_request_accepted_mail', $mailParams); $notification = trans('messages.customer_account_opening_request_accepted_notification', $notificationParams); break; default: $accountRequest->status = CustomerAccountRequest::AWAITING_FURTHER_INFORMATION; $mailTitle = trans('messages.customer_account_opening_request_awaiting_more_information'); $mailMessage = trans('messages.customer_account_opening_request_awaiting_more_information_mail', $mailParams); $notification = trans('messages.customer_account_opening_request_awaiting_more_information_notification', $notificationParams); } $accountRequest->validating_agent_id = $request->input('nh_validating_agent_id') ?? $request->input('agent_id'); $accountRequest->reject_reason = $request->input('reason'); $accountRequest->updated_at = $datetime; $accountRequest->save(); Event::dispatch(new CustomerAccountRequestEvent($accountRequest, $mailTitle, $mailMessage, $notification)); DB::commit(); return $this->successResponse(trans('messages.successful_transaction')); } catch (Throwable $e) { Log::error($e->getMessage() . '\n' . $e->getTraceAsString()); DB::rollBack(); return $this->errorResponse(trans('errors.unexpected_error'), 500); } } /** * @OA\Get( * path="/customers_accounts_requests/formData", * summary="Recuperer les informations necessaires avant la soumission du formulaire", * tags={"Agency Banking"}, * security={{"api_key":{}}}, * @OA\Parameter( * parameter="user_id", * name="user_id", * description="ID de l'utilisateur", * @OA\Schema( * type="integer" * ), * in="query", * required=true * ), * @OA\Parameter( * parameter="network_id", * name="network_id", * description="ID du reseau", * @OA\Schema( * type="integer", * ), * in="query", * required=true * ), * @OA\Response( * response=200, * description="OK", * @OA\JsonContent( * ref="#/components/schemas/ApiResponse", * example = { * "status" : 200, * "response" : {"identification": { "id": 15, "firstname": "", "lastname": " John doe", "birth_date": "2020-07-28T00:00:00.000000Z", "gender": "M", "town": "Libreville", "country": "Gabon", "identity_document": "Carte d'identité", * "id_identity_document": "1234567890", "expiry_date_document": "2020-07-28T00:00:00.000000Z", "id_user": 321, "status": 1, "createdAt": "2020-07-28T17:01:26.000000Z", "user_image": null, "document_image_front": null, "document_image_back": null, * "idNetwork": 239, "country_id": null }, "customer_account_types": {{ "id": 1, "name": "Compte courant", "description": null, "children": {{ "id": 4, "name": "Compte courant regulier salarie", "description": "Compte courant regulier", "parent_id": 1, * "documents": { { "name": "photo", "description": "photo", "account_type_id": 4 } } } } } }, "marital_status": { { "title": "Single", "value": "SINGLE" } }, "residential_status": { { "title": "Résident", "value": "RESIDENT" } }, * "address_justification_docs": { { "title": "Plan de localisation", "value": "LOCALISATION_PLAN" } }, "e_statement_frequencies": { { "title": "Daily", "value": "DAILY" } }, "debit_card_types": { { "title": "MASTER Card Classic", * "value": "MASTER_CARD_CLASSIC" } }}, * "error":null * } * ) * ) * ) */ // Fetch mandatory form data before submit request public function getFormData(Request $request) { $this->validate($request,[ 'user_id' => 'required|integer|exists:users,id', 'network_id' => 'required|integer|exists:networks,id', ]); if(CustomerAccountRequest::where('user_id', $request->input('user_id'))->where('network_id', $request->input('network_id')) ->where('status', CustomerAccountRequest::UNDER_VALIDATION)->exists()){ return $this->errorResponse(__('messages.customer_account_opening_request_pending')); } $data['identification'] = Identification::where('id_user',$request->input('user_id'))->first(); $data['customer_account_types'] = CustomerAccountType::has('children')->with(['children:id,name,description,parent_id','children.documents:name,description,account_type_id']) ->where('network_id',$request->input('network_id'))->select(['id','name','description'])->get(); foreach (MaritalStatus::all() as $key => $value){ $data['marital_status'][] = [ 'title' => $value, 'value' => $key ]; } foreach (ResidentialStatus::all() as $key => $value){ $data['residential_status'][] = [ 'title' => $value, 'value' => $key ]; } foreach (DocumentType::all() as $key => $value){ $data['address_justification_docs'][] = [ 'title' => $value, 'value' => $key ]; } foreach (Frequency::all() as $key => $value){ $data['e_statement_frequencies'][] = [ 'title' => $value, 'value' => $key ]; } foreach (CreditCardType::all() as $key => $value){ $data['debit_card_types'][] = [ 'title' => $value, 'value' => $key ]; } return $this->successResponse($data); } }