Guest User

Untitled

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