Files
wticreatorstudio/Modules/WTIJobBoard/Entities/JobRequest.php
T
Leonard Biano 0ee29bc20c Modules Update
Downline bonus and edit draft jobs
2024-06-10 23:33:00 +08:00

395 lines
12 KiB
PHP

<?php
namespace Modules\WTIJobBoard\Entities;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\StudioPlus\Entities\StudioPlusVideo;
use Modules\WTIJobBoard\Entities\JobRequestStatus;
use Log;
class JobRequest extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $table = "job_requests";
protected $guarded = ["id"];
protected $appends = [
'creator',
'viewer',
'delivery',
'deliverable',
'is_mine',
'creator_application',
'has_pending_application',
'has_winner',
'has_hired',
'is_received',
'is_completed',
'client_status',
'creator_status'
];
protected $casts = [
'is_approved' => 'boolean',
'is_rejected' => 'boolean',
'others' => 'array',
'ship_to' => 'array',
'post_to' => 'array',
'pov_requested' => 'boolean'
];
public static function convertToString($key,$data)
{
if($key == 'post_to'){
$options = JobRequestSocialMediaSite::where('active',1)->pluck('name', 'id')->all();
} else {
$options = [
'us' => 'United States',
'ca' => 'Canada'
];
}
$value = null;
for ($i=0; $i < count($data); $i++) {
$value .= $value == null ? $options[$data[$i]] : ", ".$options[$data[$i]];
}
return $value;
}
public static function boot()
{
parent::boot();
static::updated(function ($model) {
if (auth()->user()->role->type == 'customer') {
$changes = $model->isDirty() ? $model->getDirty() : false;
if($changes){
$changes = \Arr::except($changes, ['credits', 'status', 'job_status', 'track_package_status', 'store', 'url', 'others', 'is_approved', 'is_rejected', 'pov_requested', 'created_at', 'updated_at']);
if(count($changes) > 0){
$fields = [
'title' => 'Name of Product',
'studio_plus_videos_id' => 'Video',
'asin' => 'ASIN/SKU/UPC',
'credit_type' => 'Credit Type',
'type' => 'Type',
'notes' => 'Notes',
'ship_to' => 'Will Ship To',
'post_to' => 'Posting Site'
];
$updates = [];
foreach($changes as $attr => $value){
$old_value = $model->getOriginal($attr);
$new_value = $model->$attr;
if(is_array($old_value)) $old_value = self::convertToString($attr,$old_value);
if(is_array($new_value)) $new_value = self::convertToString($attr,$new_value);
if($attr == 'type'){
if($old_value != '' || $old_value != null){
$old_value = JobRequestCategory::find($model->getOriginal($attr))->label;
}
if($new_value != '' || $new_value != null){
$new_value = JobRequestCategory::find($model->$attr)->label;
}
} if($attr == 'studio_plus_videos_id'){
if($old_value != '' || $old_value != null){
$video = StudioPlusVideo::find($model->getOriginal($attr));
$old_value = "#{$video->id} ({$video->title})";
}
if($new_value != '' || $new_value != null){
$video = StudioPlusVideo::find($model->$attr);
$new_value = "#{$video->id} ({$video->title})";
}
}
if($old_value == '' || $old_value == null){
$updates[] = "Added ".$fields[$attr]." ". $new_value;
} else if($new_value == '' || $new_value == null){
$updates[] = "Removed ".$fields[$attr]." ".$old_value;
} else {
$updates[] = "Updated ".$fields[$attr]." from ". $old_value ." to ". $new_value;
}
}
$edited_status = JobRequestStatus::where('name', 'edited')->first();
$track = new JobRequestTrack();
$track->job_request_id = $model->id;
$track->status_id = $edited_status->id;
$track->save();
$meta = new JobRequestTrackMeta();
$meta->track_id = $track->id;
$meta->name = 'edited';
$meta->value = json_encode($updates);
$meta->save();
}
}
}
});
}
public function client()
{
return $this->hasOne(Client::class, 'id', 'client_id');
}
public function pov_video()
{
return $this->hasOne(StudioPlusVideo::class, 'id', 'studio_plus_videos_id');
}
public function video()
{
return $this->hasOne(StudioPlusVideo::class, 'job_request_id');
}
public function category()
{
return $this->hasOne(JobRequestCategory::class, 'id', 'type');
}
public function product()
{
return $this->hasOne(JobRequestProductDetails::class, 'job_request_id');
}
public function source()
{
return $this->hasOne(JobRequestSource::class, 'id', 'source_id');
}
public function applications()
{
return $this->hasMany(JobRequestApplication::class, 'job_request_id');
}
public function socialMediaPosts()
{
return $this->hasMany(JobRequestSocialMediaPost::class, 'job_request_id');
}
public function hiredCreator()
{
return $this->hasOne(JobRequestApplication::class, 'job_request_id', 'id')
->where('is_hired', 1);
}
public function getCreatorAttribute()
{
$application = $this->applications->where('is_hired', true)->first();
if ($application) {
$application_id = $application->id;
$address = $application->address;
$cover = $application->cover;
$application->creator->{'application_id'} = $application_id;
$application->creator->{'address'} = $address;
$application->creator->{'cover'} = $cover;
return $application->creator;
}
return null;
}
public function track()
{
return $this->hasOne(JobRequestTrack::class, 'job_request_id')->latest('id');
}
public function latestTrack() {
return $this->hasOne(JobRequestTrack::class, 'job_request_id')->latestOfMany();
}
public function tracks()
{
return $this->hasMany(JobRequestTrack::class, 'job_request_id')->latest('id');
}
public function shipment()
{
return $this->hasOne(JobRequestShipment::class, 'job_request_id');
}
public function getDeliveryAttribute()
{
return $this->tracks()->with('status')->whereHas('status', function($query) {
$query->where('name', 'received');
})->first();
}
public function getIsMineAttribute()
{
$user = auth()->user();
$creator = $this->creator;
if ($user && $creator) {
return $user->id == $creator->id;
}
return false;
}
public function getHasPendingApplicationAttribute()
{
$user = auth()->user();
if ($user) {
$application = $this->applications->where('applicant_id', $user->id)->where('is_hired', false)->first();
if ($application) {
return true;
}
}
return false;
}
public function getHasWinnerAttribute()
{
$user = auth()->user();
if ($user) {
return isset($this->others['winner_name']) ? true : false;
}
return false;
}
public function getHasHiredAttribute()
{
$has_hired = $this->applications->where('is_hired',1)->first();
if($has_hired){
return true;
}
return false;
}
public function getCreatorStatusAttribute()
{
$user = auth()->user();
if ($user) {
if($this->is_completed){
return 'Completed';
}
$application = $this->applications->where('applicant_id', $user->id)->first();
if ($application) {
if ($application->is_hired) {
return 'In Progress';
}
return 'Applied';
}
if($this->category->name == 'giveaway' && $this->is_approved){
return 'In Progress';
}
}
return 'Available';
}
public function getClientStatusAttribute()
{
if($this->is_rejected){
return 'Denied';
} else if($this->is_approved){
if($this->is_completed){
return 'Completed';
}
if($this->creator){
return 'In Progress';
}
if($this->deliverable){
return 'In Progress';
}
return 'Approved';
}
return 'Pending';
}
public function getCreatorApplicationAttribute()
{
$user = auth()->user();
if ($user && $user->role->type == 'staff') {
$application = $this->applications->where('applicant_id', $user->id)->where('is_hired', false)->first();
if($application){
return $application->load('address');
}
}
return false;
}
public function getDeliverableAttribute()
{
$deliverable = $this->tracks()->with('metas')->whereHas('status', function($query) {
$query->where('name', 'deliverable');
})->first();
return $deliverable ?? false;
}
public function getIsReceivedAttribute()
{
return $this->tracks()->where('status_id',4)->first() ? true : false;
}
public function getIsCompletedAttribute()
{
$track = $this->track()->first();
return $track && $track->status_id == 5 ? true : false;
}
public function getViewerAttribute()
{
$user = auth()->user();
if ($user) {
$role = $user->role->type;
if (in_array($role, ['superadmin', 'admin']) || permissionCheck('job_board.admin.index')) {
return 'admin';
}
else if ($role == 'staff') {
return 'creator';
}
else {
return 'client';
}
}
}
public function getAccessVideoAttribute()
{
$user = auth()->user();
if ($user) {
$role = $user->role->type;
if (in_array($role, ['superadmin', 'admin']) || permissionCheck('job_board.admin.index')) {
return 'admin';
}
else if ($role == 'staff') {
return 'creator';
}
else {
return 'client';
}
}
}
}