Файловый менеджер - Редактировать - /home/clickysoft/public_html/somni.clickysoft.net/app/Repositories/LeaseRepository.php
Назад
<?php namespace App\Repositories; use App\Interfaces\LeaseRepositoryInterface; use App\Models\Property; use App\Models\Unit; use App\Models\User; use App\Models\City; use App\Models\Lease; use App\Models\Template; use App\Models\Event; use App\Models\Setting; use App\Models\PropertyUnitTenant; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; use Illuminate\Support\Facades\Storage; use DocuSign\eSign\Configuration; use DocuSign\eSign\Api\EnvelopesApi; use DocuSign\eSign\Client\ApiClient; use Datatables; use Carbon\Carbon; class LeaseRepository implements LeaseRepositoryInterface { public function leaseDocumentUpload($file,$landlord = null, $tenant = null){ $userFolder = "user_{$landlord->id}"; if (!Storage::exists($userFolder)) { Storage::makeDirectory($userFolder,0777, true, true); } // Store the file in the user-specific folder $filePath = $file->storeAs($userFolder, $file->getClientOriginalName(), 'public'); // Save the file path in the database (replace this with your actual model and database logic) // For example, assuming you have a 'leases' table with 'user_id' and 'file_path' columns // $doc = Lease::create([ // 'name' => $file->getClientOriginalName(), // 'landlord_id' => $landlord->id, // 'document_path' => $filePath, // ]); $doc = Template::create([ 'user_id' => $landlord->id, 'document_path' => $filePath, 'type' => 'user' ]); return $doc; } public function getLeaseById($lease_id){ return Lease::whereId($lease_id)->first(); } public function getDocById($doc_id){ return Template::whereId($doc_id)->first(); } public function getTemplatesByType($type){ // return Storage::disk('public')->files('/lease_templates'); return Template::where('type',$type)->get(); } public function getTemplatesByUserIdAndType($user_id,$type){ // return Storage::disk('public')->files('/lease_templates'); return Template::where('user_id',$user_id)->where('type',$type)->get(); } public function sendLeaseForSigning($doc_id,$email, $doc_name = null){ //First we check if the email provided is valid, meaning that the person with the email is in one of his units $userRepo = new UserRepository(); $tenant = $userRepo->getUserByKey('email', $email); if(!$tenant){ return [ 'flag' => 0, 'message' => 'User Not Found', ]; // 0 means no user is found } $user = auth()->user(); $unitIds = $user->properties->flatMap(function ($property) { return $property->propertyUnits->pluck('id'); })->toArray(); $isUserInTable = PropertyUnitTenant::whereIn('unit_id', $unitIds) ->where('tenant_user_id', $tenant->id) ->exists(); if(!$isUserInTable){ return [ 'flag' => 1, 'message' => 'User with the email is not assigned to any of your units', ]; // 1 Means user is present but not assigned to any of the landlord's unit } \Session::put(['doc_id' => $doc_id]); $check = $this->callback(null); // dd($check === true); if($check === true){ //Call function for envelope $sent = $this->sendLease($doc_id,$email, $doc_name ?? null); if($sent['code'] == 200){ return [ 'flag' => 2, 'lease' => $sent['lease'], 'message' => 'Redirecting!' ]; } return [ 'flag' => 1, 'message' => 'Unknown Error!', ]; }else{ return [ 'flag' => 3, 'url' => $check, 'message' => 'Redirecting!' ]; } // dd($unitIds); // Here we send lease // $botUrl = $this->connectDocusign(); } public function connectDocusign(){ try { $params = [ 'response_type' => 'code', 'scope' => 'signature', 'client_id' => config('constants.docusign.docu_sign_i_key'), // 'state' => 'a39fh23hnf23', 'redirect_uri' => route('docu.callback'), ]; $queryBuild = http_build_query($params); $url = "https://account-d.docusign.com/oauth/auth?"; $botUrl = $url . $queryBuild; return $botUrl; } catch (Exception $e) { return redirect()->back()->with('error', 'Something Went wrong !'); } } public function callback($code = null) { if($code == null){ $tokenObj = Setting::where('setting_key' , config('constants.docusign.docu_sign_code_key'))->first(); if($tokenObj){ $code = $tokenObj->setting_value; }else{ $url = $this->connectDocusign(); return $url; } } $client_id = config('constants.docusign.docu_sign_i_key');//env('DOCUSIGN_CLIENT_ID'); $client_secret = config('constants.docusign.docu_sign_s_key');//env('DOCUSIGN_CLIENT_SECRET'); $integrator_and_secret_key = "Basic " . utf8_decode(base64_encode("{$client_id}:{$client_secret}")); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://account-d.docusign.com/oauth/token'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); $post = array( 'grant_type' => 'authorization_code', 'code' => $code, ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $headers = array(); $headers[] = 'Cache-Control: no-cache'; $headers[] = "authorization: $integrator_and_secret_key"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); $decodedData = json_decode($result); \Log::info('token Ye decodedData: ',[$decodedData]); if(isset($decodedData->access_token) && !empty($decodedData->access_token)){ $match_this = ['user_id' => 1, 'setting_key' => config('constants.docusign.docu_sign_access_token_key')]; $update = Setting::updateOrCreate($match_this,['setting_value'=> $decodedData->access_token]); $match_this = ['user_id' => 1, 'setting_key' => config('constants.docusign.docu_sign_refresh_token_key')]; $update = Setting::updateOrCreate($match_this,['setting_value'=> $decodedData->refresh_token]); return true; }else{ //Check for refresh token $refresh = $this->accessTokenUsingRefreshToken(); if(isset($refresh->access_token) && !empty($refresh->access_token)){ $match_this = ['user_id' => 1, 'setting_key' => config('constants.docusign.docu_sign_access_token_key')]; $update = Setting::updateOrCreate($match_this,['setting_value'=> $refresh->access_token]); \Log::info('token Ye hai: ',[$refresh->access_token]); $match_this = ['user_id' => 1, 'setting_key' => config('constants.docusign.docu_sign_refresh_token_key')]; $update = Setting::updateOrCreate($match_this,['setting_value'=> $refresh->refresh_token]); return true; }else{ //Create all new $botUrl = $this->connectDocusign(); return $botUrl; } } // $request->session()->put('docusign_auth_code', $decodedData->access_token); // return redirect()->route('docusign')->with('success', 'Docusign Succesfully Connected'); } public function accessTokenUsingRefreshToken(){ $tokenObj = Setting::where('setting_key' , config('constants.docusign.docu_sign_refresh_token_key'))->first(); $client_id = config('constants.docusign.docu_sign_i_key');//env('DOCUSIGN_CLIENT_ID'); $client_secret = config('constants.docusign.docu_sign_s_key');//env('DOCUSIGN_CLIENT_SECRET'); $integrator_and_secret_key = "Basic " . utf8_decode(base64_encode("{$client_id}:{$client_secret}")); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://account-d.docusign.com/oauth/token'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); $post = array( 'grant_type' => 'refresh_token', 'refresh_token' => $tokenObj->setting_value, ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $headers = array(); $headers[] = 'Cache-Control: no-cache'; $headers[] = "authorization: $integrator_and_secret_key"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); $decodedData = json_decode($result); return $decodedData; } public function sendLease($doc_id,$email, $doc_name = null){ $tenant = User::where('email',$email)->first(); $document = Template::whereId($doc_id)->first(); $envelope_definition = $this->makeEnvelope($doc_id,$email, ($document->type == 'general') ? 'general' : 'user'); $envelope_api = $this->getEnvelopeApi(); $access_obj = Setting::where('setting_key', config('constants.docusign.docu_sign_access_token_key'))->first(); $config = new Configuration(); $config->setHost(config('constants.docusign.docu_sign_dev_api_url')); // $config->setHost(config("https://demo.docusign.net/restapi")); $config->addDefaultHeader('Authorization', 'Bearer ' . $access_obj->setting_value); $api_client = new \DocuSign\eSign\client\ApiClient($config); $envelope_api = new \DocuSign\eSign\Api\EnvelopesApi($api_client); $results = $envelope_api->createEnvelope(config('constants.docusign.docu_sign_account_id'), $envelope_definition); // $envelope_id = $results->getEnvelopeId(); // $envelope_url = $results->getUrl(); $envelope_id = $results['envelope_id']; $envelope_url = $results['uri']; //CREATE LEASE RECORD $lease['name'] = ($doc_name) ? $doc_name : basename($document->document_path); $lease['landlord_id'] = auth()->user()->id; $lease['tenant_id'] = $tenant->id; $lease['unit_id'] = $tenant->tenantUnits[0]->id ?? null; $lease['template_id'] = $doc_id; $lease['document_path'] = $document->document_path; $lease['document_id'] = $document->id; $lease['envelope_id'] = $envelope_id; $lease['status'] = 0; $lease['size'] = 0; $lease_created = Lease::create($lease); if($lease_created){ return [ 'code' => 200, 'lease' => $lease_created, ]; } return [ 'code' => 400, 'message' => 'Error', ]; // dd($envelope_definition); } public function makeEnvelope($doc_id,$email, $type){ $document = Template::whereId($doc_id)->first(); $tenant = User::where('email',$email)->first(); $filename = basename($document->document_path); if($type == 'general'){ $demo_docs_path = asset(env('APP_URL').'/storage/lease_templates/'.$filename); }else{ $demo_docs_path = asset(env('APP_URL').'/storage/user_'.auth()->user()->id.'/'.$filename); } $arrContextOptions=array( "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false, ), ); $content_bytes = file_get_contents($demo_docs_path,false, stream_context_create($arrContextOptions)); // dd($content_bytes); $base64_file_content = base64_encode($content_bytes); // dd($base64_file_content); // Create the document model $document_to_sign = new \DocuSign\eSign\Model\Document([// create the DocuSign document object 'document_base64' => $base64_file_content, 'name' => 'Example document', // can be different from actual file name 'file_extension' => 'pdf', // many different document types are accepted 'document_id' => $document->id, // a label used to reference the doc ]); // Create the signer recipient model $signer = new \DocuSign\eSign\Model\Signer([// The signer 'email' => $email, 'name' => $tenant->name, 'recipient_id' => $tenant->id, // 'routing_order' => "1", // Setting the client_user_id marks the signer as embedded // 'client_user_id' => $tenant->id, ]); // Create a sign_here tab (field on the document) // $sign_here = new \DocuSign\eSign\Model\SignHere([// DocuSign SignHere field/tab // 'anchor_string' => '/sn1/', 'anchor_units' => 'pixels', // 'anchor_y_offset' => '10', 'anchor_x_offset' => '20', // ]); // Add the tabs model (including the sign_here tab) to the signer // The Tabs object wants arrays of the different field/tab types // $signer->settabs(new \DocuSign\eSign\Model\Tabs(['sign_here_tabs' => [$sign_here]])); // Next, create the top level envelope definition and populate it. $envelope_definition = new \DocuSign\eSign\Model\EnvelopeDefinition([ 'email_subject' => "Please sign this document sent from the SOMNI", 'documents' => [$document_to_sign], // The Recipients object wants arrays for each recipient type 'recipients' => new \DocuSign\eSign\Model\Recipients(['signers' => [$signer]]), 'status' => "sent", // requests that the envelope be created and sent. ]); return $envelope_definition; } public function getEnvelopeApi(){ $access_token = Setting::where('setting_key', config('constants.docusign.docu_sign_access_token_key'))->first()->pluck('setting_value'); $config = new Configuration(); $config->setHost(config('constants.docusign.docu_sign_dev_url')."/restapi/"); $config->addDefaultHeader('Authorization', 'Bearer ' . $access_token); $apiClient = new ApiClient($config); return new EnvelopesApi($apiClient); } public function getAllLeases($filters,$landlord_id = null){ // dd($filters); $leases_query = Lease::where('landlord_id',$landlord_id); if(count($filters) > 0){ if(!empty($filters['search_lease'])){ $leases_query = $leases_query->where('name','LIKE','%'.$filters['search_lease'].'%'); } if(!empty($filters['sort'])){ $sort_by = $filters['sort']; switch ($sort_by) { case 'latest': $leases_query = $leases_query->orderBy('created_at','DESC'); break; case 'name_asc': $leases_query = $leases_query->orderBy('name','ASC'); break; case 'size_asc': $leases_query = $leases_query->orderBy('size','ASC'); break; default: $leases_query = $leases_query->orderBy('id','DESC'); break; } } if(!empty($filters['per_page'])){ $leases_query = $leases_query->paginate($filters['per_page']); }else{ $leases_query = $leases_query->paginate(12); } }else{ $leases_query = $leases_query->paginate(12); } return $leases_query; } public function deleteLeases($ids){ $deleted = Lease::destroy($ids); return $deleted; } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка