Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.43 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use App\Mail\RegisterUser;
  6. use App\Mail\ResetPassword;
  7. use App\Traits\HasPermissions;
  8. use App\Traits\WorkExperienceTrait;
  9. use Carbon\Carbon;
  10. use Illuminate\Notifications\Notifiable;
  11. use Illuminate\Foundation\Auth\User as Authenticatable;
  12. use Illuminate\Support\Facades\Hash;
  13. use Mail;
  14. use Spatie\Newsletter\NewsletterFacade;
  15.  
  16. /**
  17.  * App\User
  18.  *
  19.  * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
  20.  * @mixin \Eloquent
  21.  * @property int $id
  22.  * @property string $name
  23.  * @property string $email
  24.  * @property string $password
  25.  * @property string $avatar
  26.  * @property string $social_link
  27.  * @property bool $confirmed
  28.  * @property int $role
  29.  * @property string $remember_token
  30.  * @property \Carbon\Carbon $created_at
  31.  * @property \Carbon\Carbon $updated_at
  32.  * @method static \Illuminate\Database\Query\Builder|\App\User whereAvatar($value)
  33.  * @method static \Illuminate\Database\Query\Builder|\App\User whereConfirmed($value)
  34.  * @method static \Illuminate\Database\Query\Builder|\App\User whereCreatedAt($value)
  35.  * @method static \Illuminate\Database\Query\Builder|\App\User whereEmail($value)
  36.  * @method static \Illuminate\Database\Query\Builder|\App\User whereId($value)
  37.  * @method static \Illuminate\Database\Query\Builder|\App\User whereName($value)
  38.  * @method static \Illuminate\Database\Query\Builder|\App\User wherePassword($value)
  39.  * @method static \Illuminate\Database\Query\Builder|\App\User whereRememberToken($value)
  40.  * @method static \Illuminate\Database\Query\Builder|\App\User whereRole($value)
  41.  * @method static \Illuminate\Database\Query\Builder|\App\User whereSocialLink($value)
  42.  * @method static \Illuminate\Database\Query\Builder|\App\User whereUpdatedAt($value)
  43.  * @property bool $disabled
  44.  * @property string $ip
  45.  * @method static \Illuminate\Database\Query\Builder|\App\User whereDisabled($value)
  46.  * @method static \Illuminate\Database\Query\Builder|\App\User whereIp($value)
  47.  * @property string $last_login
  48.  * @method static \Illuminate\Database\Query\Builder|\App\User whereLastLogin($value)
  49.  * @property string $verification_code
  50.  * @property-read \App\BlogSubscription $blogSubscription
  51.  * @method static \Illuminate\Database\Query\Builder|\App\User whereVerificationCode($value)
  52.  * @property string $first_name
  53.  * @property string $last_name
  54.  * @property string $phone
  55.  * @property int $state_id
  56.  * @property-read \App\Company $company
  57.  * @property-read \App\FulcrumtestsAccount $fulcrumtestsAccount
  58.  * @property-read \App\NotificationsSetting $notificationsSettings
  59.  * @property-read \App\State $state
  60.  * @method static \Illuminate\Database\Query\Builder|\App\User whereFirstName($value)
  61.  * @method static \Illuminate\Database\Query\Builder|\App\User whereLastName($value)
  62.  * @method static \Illuminate\Database\Query\Builder|\App\User wherePhone($value)
  63.  * @method static \Illuminate\Database\Query\Builder|\App\User whereStateId($value)
  64.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPost[] $jobPosts
  65.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\Resume[] $resumes
  66.  * @property-read \App\JobPostsSubscription $jobPostsSubscription
  67.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\UploadedResume[] $uploadedResumes
  68.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPost[] $savedJobPosts
  69.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPostApplication[] $jobPostsApplications
  70.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPostApplication[] $jobPostApplications
  71.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPostApplication[] $jobPostApplicationsForEmployer
  72.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPostApplication[] $newJobPostApplicationsForEmployer
  73.  * @property int $balance
  74.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\UserAssessment[] $assessments
  75.  * @method static \Illuminate\Database\Query\Builder|\App\User whereBalance($value)
  76.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $favouriteApplicants
  77.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPostBoost[] $jobPostBoosts
  78.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\Resume[] $shortlistedResumes
  79.  * @property-read \App\EmployerEmailMessage $emailMessage
  80.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\JobPostAssessment[] $jobPostAssessments
  81.  * @property bool $sending_email
  82.  * @method static \Illuminate\Database\Query\Builder|\App\User whereSendingEmail($value)
  83.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\Transaction[] $transactions
  84.  * @property int $applications_unlocked
  85.  * @method static \Illuminate\Database\Query\Builder|\App\User whereApplicationsUnlocked($value)
  86.  * @property int $parent_id
  87.  * @method static \Illuminate\Database\Query\Builder|\App\User whereParentId($value)
  88.  * @property-read \App\User $parent
  89.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $subUsers
  90.  * @property-read \App\UserPermissions $permissions
  91.  * @property int $actual_user_id
  92.  * @method static \Illuminate\Database\Query\Builder|\App\User whereActualUserId($value)
  93.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $subManaged
  94.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $subManagedUsers
  95.  * @property-read \App\User $actual
  96.  * @property-read \Illuminate\Database\Eloquent\Collection|\App\Unsubscribe[] $unsubscribes
  97.  */
  98. class User extends Authenticatable
  99. {
  100.     use Notifiable, HasPermissions, WorkExperienceTrait;
  101.  
  102.     const ADMIN_ROLE = 0;
  103.     const EMPLOYER_ROLE = 1;
  104.     const APPLICANT_ROLE = 2;
  105.     const CUSTOM_USER_ROLE = 3;
  106.  
  107.     const MAXIMUM_BALANCE = 100000000;
  108.  
  109.     const SUB_USERS_PER_PAGE = 10;
  110.  
  111.     static $rolesAllowedToBeRegistered = [self::EMPLOYER_ROLE => 'Employer', self::APPLICANT_ROLE => 'Applicant'];
  112.  
  113.     static $allRoles = [self::ADMIN_ROLE => 'Admin', self::EMPLOYER_ROLE => 'Employer', self::APPLICANT_ROLE => 'Applicant', self::CUSTOM_USER_ROLE => 'Sub User'];
  114.  
  115.     /**
  116.      * The attributes that are mass assignable.
  117.      *
  118.      * @var array
  119.      */
  120.     protected $fillable = ['email', 'password', 'role', 'avatar', 'social_link', 'confirmed', 'disabled', 'first_name', 'last_name', 'phone', 'state_id', 'parent_id'];
  121.  
  122.     protected $dates = ['last_login'];
  123.  
  124.     /**
  125.      * The attributes that should be hidden for arrays.
  126.      *
  127.      * @var array
  128.      */
  129.     protected $hidden = ['password', 'remember_token',];
  130.  
  131.     public function notificationsSettings()
  132.     {
  133.         return $this->hasOne(NotificationsSetting::class);
  134.     }
  135.  
  136.     public function favouriteApplicants()
  137.     {
  138.         return $this->belongsToMany(User::class, 'favourite_applicants', 'employer_id', 'applicant_id')->withPivot('id', 'employer_id', 'applicant_id');
  139.     }
  140.  
  141.     public function hasInFavourites($userId)
  142.     {
  143.         return $this->favouriteApplicants()->where('applicant_id', $userId)->exists();
  144.     }
  145.  
  146.     public function getFavouriteApplicantEntityId($userId)
  147.     {
  148.         return $this->favouriteApplicants()->where('applicant_id', $userId)->first()->pivot->id;
  149.     }
  150.  
  151.     public function blogSubscription()
  152.     {
  153.         if ($this->hasOne(BlogSubscription::class)->exists()) {
  154.             return $this->hasOne(BlogSubscription::class);
  155.         }
  156.  
  157.         return $this->hasOne(BlogSubscription::class, 'email', 'email');
  158.     }
  159.  
  160.     public function state()
  161.     {
  162.         return $this->belongsTo(State::class);
  163.     }
  164.  
  165.     public function jobPosts()
  166.     {
  167.         return $this->hasMany(JobPost::class);
  168.     }
  169.  
  170.     public function savedJobPosts()
  171.     {
  172.         return $this->morphedByMany(JobPost::class, 'savable', 'saved_job_posts')->where('job_posts.draft', 0);
  173.     }
  174.  
  175.     public function savedExternalJobPosts()
  176.     {
  177.         return $this->morphedByMany(ExternalJobPost::class, 'savable', 'saved_job_posts');
  178.     }
  179.  
  180.     public function getSavingInstanceForJobPost($jobPost)
  181.     {
  182.         return SavedJobPost::where('user_id', $this->id)->where('savable_id', $jobPost->id)->where('savable_type', get_class($jobPost))->first();
  183.     }
  184.  
  185.     public function hasSavedJobPost($jobPost)
  186.     {
  187.         if ($jobPost instanceof JobPost) {
  188.             return $this->savedJobPosts()->where('job_posts.id', $jobPost->id)->exists();
  189.         }
  190.  
  191.         return $this->savedExternalJobPosts()->where('external_job_posts.id', $jobPost->id)->exists();
  192.     }
  193.  
  194.     public function hasSavedJobPosts()
  195.     {
  196.         return $this->savedJobPosts()->exists();
  197.     }
  198.  
  199.  
  200.     public function company()
  201.     {
  202.         return $this->hasOne(Company::class);
  203.     }
  204.  
  205.     public function assessments()
  206.     {
  207.         return $this->hasMany(UserAssessment::class);
  208.     }
  209.  
  210.     public function hasRequiredAssessmentsForApplying($jobPostId)
  211.     {
  212.         $jobPost = JobPost::find($jobPostId);
  213.         $userAssessments = $this->assessments;
  214.         foreach ($jobPost->requiredAssessments as $requiredAssessment) {
  215.             if ($userAssessments->where('assessment_id', $requiredAssessment->assessment_id)->where('score', '>=', $requiredAssessment->score)->isEmpty()) {
  216.                 return false;
  217.             }
  218.         }
  219.  
  220.         return true;
  221.     }
  222.  
  223.     public function hasPreferredAssessmentsForApplying($jobPostId)
  224.     {
  225.         $jobPost = JobPost::find($jobPostId);
  226.         $userAssessments = $this->assessments;
  227.         foreach ($jobPost->preferredAssessments as $preferredAssessment) {
  228.             if ($userAssessments->where('assessment_id', $preferredAssessment->assessment_id)->where('score', '>=', $preferredAssessment->score)->isEmpty()) {
  229.                 return false;
  230.             }
  231.         }
  232.  
  233.         return true;
  234.     }
  235.  
  236.     public function resumes()
  237.     {
  238.         return $this->hasMany(Resume::class);
  239.     }
  240.  
  241.     public function hasResume($resumeId)
  242.     {
  243.         return $this->resumes()->where('resumes.id', $resumeId)->exists();
  244.     }
  245.  
  246.     public function hasResumes()
  247.     {
  248.         return $this->resumes()->exists();
  249.     }
  250.  
  251.     public function parent()
  252.     {
  253.         return $this->belongsTo(User::class, 'parent_id');
  254.     }
  255.  
  256.     public function actual()
  257.     {
  258.         return $this->belongsTo(User::class, 'actual_user_id');
  259.     }
  260.  
  261.     public function subUsers()
  262.     {
  263.         return $this->hasMany(User::class, 'parent_id');
  264.     }
  265.  
  266.     public function subManagedUsers()
  267.     {
  268.         return $this->hasMany(User::class, 'actual_user_id');
  269.     }
  270.  
  271.     public function transactions()
  272.     {
  273.         return $this->hasMany(Transaction::class);
  274.     }
  275.  
  276.     public function sendRegistrationEmail($temporaryPassword = false)
  277.     {
  278.         \Mail::to($this->email)->send(new RegisterUser($this, $temporaryPassword));
  279.     }
  280.  
  281.     public function jobPostsSubscription()
  282.     {
  283.         return $this->hasOne(JobPostsSubscription::class);
  284.     }
  285.  
  286.     public function jobPostApplications()
  287.     {
  288.         return $this->hasManyThrough(JobPostApplication::class, Resume::class);
  289.     }
  290.  
  291.     public function jobPostAssessments()
  292.     {
  293.         return $this->hasManyThrough(JobPostAssessment::class, JobPost::class);
  294.     }
  295.  
  296.     public function jobPostApplicationsForEmployer()
  297.     {
  298.         return $this->hasManyThrough(JobPostApplication::class, JobPost::class);
  299.     }
  300.  
  301.     public function newJobPostApplicationsForEmployer()
  302.     {
  303.         if ($this->isCustomUser() && $this->canReviewJobApplications()) {
  304.             return $this->parent->hasManyThrough(JobPostApplication::class, JobPost::class)->where('job_post_applications.status', JobPostApplication::NEW_STATUS);
  305.         }
  306.  
  307.         return $this->hasManyThrough(JobPostApplication::class, JobPost::class)->where('job_post_applications.status', JobPostApplication::NEW_STATUS);
  308.     }
  309.  
  310.     public function emailMessage()
  311.     {
  312.         return $this->hasOne(EmployerEmailMessage::class);
  313.     }
  314.  
  315.     public function hasJobPostsWithNewApplications()
  316.     {
  317.         if ($this->isCustomUser() && $this->canReviewJobApplications() && $this->parent->newJobPostApplicationsForEmployer()->exists()) {
  318.             return true;
  319.         }
  320.  
  321.         return $this->newJobPostApplicationsForEmployer()->exists();
  322.     }
  323.  
  324.     public function getApplicationForJob($jobPostId)
  325.     {
  326.         if ($this->isCustomUser() && $this->canReviewJobApplications()) {
  327.             return $this->parent->jobPostApplications()->where('job_post_applications.job_post_id', $jobPostId)->first();
  328.         }
  329.  
  330.         return $this->jobPostApplications()->where('job_post_applications.job_post_id', $jobPostId)->first();
  331.     }
  332.  
  333.     public function getAppliedJobPosts()
  334.     {
  335.         $appliedJobPostsIds = $this->jobPostApplications()->whereHas('jobPost', function ($query) {
  336.             $query->where('draft', 0)->verified();
  337.         })->pluck('job_post_id')->toArray();
  338.  
  339.         return JobPost::whereIn('id', $appliedJobPostsIds)->orderBy('id', 'DESC')->paginate(JobPost::POSTS_ON_ONE_PAGE_AMOUNT);
  340.     }
  341.  
  342.  
  343.     public function hasAppliedForAJob($jobPostId = null)
  344.     {
  345.         if (is_null($jobPostId)) {
  346.             return $this->jobPostApplications()->exists();
  347.         }
  348.  
  349.         return $this->jobPostApplications()->where('job_post_applications.job_post_id', $jobPostId)->exists();
  350.     }
  351.  
  352.     public function fulcrumtestsAccount()
  353.     {
  354.         return $this->hasOne(FulcrumtestsAccount::class);
  355.     }
  356.  
  357.     public function hasCompany()
  358.     {
  359.         if ($this->isCustomUser() && $this->parent->isEmployer() && $this->parent->company()->exists()) {
  360.             return true;
  361.         }
  362.  
  363.         return $this->company()->exists();
  364.     }
  365.  
  366.     public function hasJobPosts()
  367.     {
  368.         return $this->jobPosts()->exists();
  369.     }
  370.  
  371.     public function hasJobPost($jobPostId)
  372.     {
  373.         return $this->jobPosts()->where('job_posts.id', $jobPostId)->exists();
  374.     }
  375.  
  376.     public function isAdmin()
  377.     {
  378.         if ($this->isCustomUser() && $this->parent->role == self::ADMIN_ROLE) {
  379.             return true;
  380.         }
  381.  
  382.         return $this->role == self::ADMIN_ROLE;
  383.     }
  384.  
  385.     public function isApplicant()
  386.     {
  387.         return $this->role == self::APPLICANT_ROLE;
  388.     }
  389.  
  390.     public function isEmployer()
  391.     {
  392.         if ($this->isCustomUser() && $this->parent->role == self::EMPLOYER_ROLE) {
  393.             return true;
  394.         }
  395.  
  396.         return $this->role == self::EMPLOYER_ROLE;
  397.     }
  398.  
  399.     public function isCustomUser()
  400.     {
  401.         return $this->role == self::CUSTOM_USER_ROLE;
  402.     }
  403.  
  404.     public function isActualUser()
  405.     {
  406.         return $this->actual_user_id == 0;
  407.     }
  408.  
  409.     public function canBeSwitchedTo(User $userToSwitchTo)
  410.     {
  411.         if ($this->isActualUser() && $userToSwitchTo->actual_user_id == $this->id) {
  412.             return true;
  413.         }
  414.         if ( ! $this->isActualUser()) {
  415.             if ($userToSwitchTo->isActualUser() && $userToSwitchTo->id == $this->actual_user_id) {
  416.                 return true;
  417.             }
  418.             if ( ! $userToSwitchTo->isActualUser() && $userToSwitchTo->actual_user_id == $this->actual_user_id) {
  419.                 return true;
  420.             }
  421.         }
  422.  
  423.         return false;
  424.     }
  425.  
  426.     public function permissions()
  427.     {
  428.         return $this->hasOne(UserPermissions::class);
  429.     }
  430.  
  431.     public function sendPasswordResetNotification($token)
  432.     {
  433.         Mail::to($this->email)->send(new ResetPassword($this, $token));
  434.  
  435.     }
  436.  
  437.     public function disable()
  438.     {
  439.         $this->disabled = 1;
  440.     }
  441.  
  442.     public function enable()
  443.     {
  444.         $this->disabled = 0;
  445.     }
  446.  
  447.     public function isBanned()
  448.     {
  449.         return $this->hasBannedIp() || Helper::ipIsBanned(Helper::getUserIp()) || $this->disabled;
  450.     }
  451.  
  452.     public function hasBannedIp()
  453.     {
  454.         return Helper::ipIsBanned($this->ip);
  455.     }
  456.  
  457.     public function subscribeToPromotions()
  458.     {
  459.         $settings = $this->notificationsSettings()->firstOrNew([]);
  460.         $settings->receive_promotions_notifications = 1;
  461.         $settings->save();
  462.         $mailChimpService = new MailChimpService();
  463.         $mailChimpService->subscribeOrUpdate($this->email, ['FNAME'=>$this->first_name, 'LNAME'=>$this->last_name]);
  464.     }
  465.  
  466.     public function updateAccessSettings($newEmail, $newPassword)
  467.     {
  468.         if ($newEmail) {
  469.             $mailChimpService = new MailChimpService();
  470.             $mailChimpService->updateEmailAddress($this->email, $newEmail);
  471.             $this->email = $newEmail;
  472.         }
  473.  
  474.         if ($newPassword) {
  475.             $this->password = bcrypt($newPassword);
  476.         }
  477.  
  478.         return $this;
  479.     }
  480.  
  481.     public function passwordConfirmed($passwordToCheck)
  482.     {
  483.         $result = Hash::check($passwordToCheck, $this->password);
  484.  
  485.         return $result;
  486.     }
  487.  
  488.     public function getRoleName()
  489.     {
  490.         return self::$allRoles[$this->role];
  491.     }
  492.  
  493.     public function getFullName()
  494.     {
  495.         $fullName = '';
  496.         $user = $this->isActualUser() ? $this : $this->actual;
  497.         if ($user->first_name) {
  498.             $fullName .= mb_convert_case(mb_strtolower($user->first_name), MB_CASE_TITLE, "UTF-8");
  499.         }
  500.         if ($user->last_name) {
  501.             $fullName .= ' ' . mb_strtoupper(mb_substr($user->last_name, 0, 1, 'UTF8'));
  502.             $fullName .= '.';
  503.         }
  504.  
  505.         return trim($fullName);
  506.     }
  507.  
  508.     public function setLastLogin()
  509.     {
  510.         $lastLoginTimeStamp = Carbon::now();
  511.         $this->last_login = $lastLoginTimeStamp;
  512.         $this->save();
  513.     }
  514.  
  515.     public function getLastLogin()
  516.     {
  517.         if ($this->last_login) {
  518.             $lastLogin = $this->last_login;
  519.         } else {
  520.             $lastLogin = $this->created_at;
  521.         }
  522.         $lastLogin->setTimezone('GMT');
  523.         $lastLogin = $lastLogin->format('g:i A  d-m-Y  T');
  524.  
  525.         return $lastLogin;
  526.     }
  527.  
  528.  
  529.     public function subscribedToBlog()
  530.     {
  531.         return $this->blogSubscription()->exists();
  532.     }
  533.  
  534.     public function receivesJobPostsNotification()
  535.     {
  536.         return $this->jobPostsSubscription()->exists();
  537.     }
  538.  
  539.     public function receivesLearningCenterNotifications()
  540.     {
  541.         return $this->notificationsSettings ? $this->notificationsSettings->receive_learning_center_notifications : 0;
  542.     }
  543.  
  544.     public function receivesJobPostApplicationsNotifications()
  545.     {
  546.         return $this->notificationsSettings ? $this->notificationsSettings->receive_job_post_applications_notifications : 0;
  547.     }
  548.  
  549.     public function receivesPromotionsNotifications()
  550.     {
  551.         return $this->notificationsSettings ? $this->notificationsSettings->receive_promotions_notifications : 0;
  552.     }
  553.  
  554.     public function receivesJobPostsVerificationNotifications()
  555.     {
  556.         return $this->notificationsSettings ? $this->notificationsSettings->receive_job_post_verification_notifications : 0;
  557.     }
  558.  
  559.     public function getRegistrationDate()
  560.     {
  561.         return $this->created_at->format('d-m-Y');
  562.     }
  563.  
  564.     public function hasLinkedFulcrumtestsAccount()
  565.     {
  566.         return $this->fulcrumtestsAccount()->exists();
  567.     }
  568.  
  569.     public function getJobPostsSubscriptionPeriodicity()
  570.     {
  571.         if ( ! $this->receivesJobPostsNotification()) {
  572.             return JobPostsSubscription::DISABLED_PERIODICITY;
  573.         }
  574.  
  575.         return $this->jobPostsSubscription->periodicity;
  576.     }
  577.  
  578.     public function createdResume()
  579.     {
  580.         return $this->resumes()->whereNull('path')->first();
  581.     }
  582.  
  583.  
  584.     public function canAddResume()
  585.     {
  586.         return ($this->resumes()->whereNull('path')->count()) < Resume::MAX_CREATED_RESUMES_PER_USER;
  587.     }
  588.  
  589.     public function canUploadResume()
  590.     {
  591.         return ($this->resumes()->whereNotNull('path')->count()) < Resume::MAX_UPLOADED_RESUMES_PER_USER;
  592.     }
  593.  
  594.     public function hasEnoughBalanceForJobPostBoosting($period = 1)
  595.     {
  596.         return $this->balance >= Setting::getValue('boosting_cost_per_day') * $period;
  597.     }
  598.  
  599.     public function hasEnoughBalanceForPostingExternalUrlInJob()
  600.     {
  601.         return $this->balance >= JobPost::EXTERNAL_LINK_PRICE_TEMP_CONST;
  602.     }
  603.  
  604.     public function jobPostBoosts()
  605.     {
  606.         return $this->hasManyThrough(JobPostBoost::class, JobPost::class);
  607.     }
  608.  
  609.     public function getBalance()
  610.     {
  611.         return $this->balance / 100;
  612.     }
  613.  
  614.     public function chargeFromBalance($amount, $description = '', $balanceIncreased = 0, $status = Transaction::STATUS_SUCCESS)
  615.     {
  616.         if ($this->balance - $amount < 0) {
  617.             $amount = $this->balance;
  618.         }
  619.         $this->balance -= $amount;
  620.  
  621.         $this->transactions()->create([
  622.             'amount' => $amount,
  623.             'description' => $description,
  624.             'balance_increased' => $balanceIncreased,
  625.             'status' => $status,
  626.         ]);
  627.  
  628.         return $this;
  629.     }
  630.  
  631.     public function addToBalance($amount, $description = '', $balanceIncreased = 1, $status = Transaction::STATUS_SUCCESS)
  632.     {
  633.         if ($this->balance + $amount > self::MAXIMUM_BALANCE) {
  634.             $amount = self::MAXIMUM_BALANCE - $this->balance;
  635.         }
  636.         $this->balance += $amount;
  637.  
  638.         $this->transactions()->create([
  639.             'amount' => $amount,
  640.             'description' => $description,
  641.             'balance_increased' => $balanceIncreased,
  642.             'status' => $status,
  643.         ]);
  644.  
  645.         return $this;
  646.     }
  647.  
  648.  
  649.     public function hasUnlockedApplications()
  650.     {
  651.         if ($this->isCustomUser()) {
  652.             return $this->parent->applications_unlocked;
  653.         }
  654.  
  655.         return $this->applications_unlocked;
  656.     }
  657.  
  658.     public function hasEnoughMoneyForUnlockingApplications()
  659.     {
  660.         return $this->balance >= Setting::getValue('unlock_applications_cost');
  661.     }
  662.  
  663.  
  664.     public function getEmployerEmailData()
  665.     {
  666.         $emailData = $this->emailMessage ?? $this->emailMessage()->create(['subject' => 'Topreqs: Your CV was viewed', 'message' => 'Hello, I viewed your CV...']);
  667.  
  668.         return $emailData;
  669.     }
  670.  
  671.     public function unlockJobPostsApplications()
  672.     {
  673.         foreach ($this->getLockedJobPostsWithApplications() as $jobPost) {
  674.             $jobPost->unlockApplications()->save();
  675.         }
  676.  
  677.         return $this;
  678.     }
  679.  
  680.     public function getCompany()
  681.     {
  682.         if ($this->isCustomUser() && $this->parent->isEmployer() && $this->parent->company()->exists()) {
  683.             return $this->parent->company;
  684.         }
  685.  
  686.         return $this->company;
  687.     }
  688.  
  689.  
  690.     public function getApplicationsToUnlockCount()
  691.     {
  692.         return JobPostApplication::whereIn('job_post_id', $this->getLockedJobPostsWithApplications()->pluck('id')->toArray())->count();
  693.     }
  694.  
  695.     public function getJobPostsToUnlockCount()
  696.     {
  697.         return $this->getLockedJobPostsWithApplications()->count();
  698.     }
  699.  
  700.     public function getLockedJobPostsWithApplications()
  701.     {
  702.         return $this->jobPosts()->where('applications_unlocked', 0)->whereHas('applications')->get();
  703.     }
  704.  
  705.     public function wasLastCommunicatedAt()
  706.     {
  707.         $contact = ContactWithApplicant::where('employer_id', Helper::getParentUserIfExists()->id)->where('applicant_id', $this->id)->first();
  708.         if ($contact) {
  709.             return $contact->updated_at->diffForHumans();
  710.         }
  711.  
  712.         return '';
  713.     }
  714.  
  715.     public function updateFulcrumtestsAssessments($assessments)
  716.     {
  717.         $this->assessments()->delete();
  718.         foreach ($assessments as $assessment) {
  719.             $this->assessments()->create($assessment);
  720.         }
  721.  
  722.         return $this;
  723.     }
  724. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement