GoalAdvertisement.php 10.4 KB
<?php

namespace App\Models\Pivots;

use App\Models\Advertisement;
use App\Models\YandexError;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\Pivots\GoalAdvertisement
 *
 * @property int $id
 * @property int|null $external_id
 * @property int|null $goal_v_card_external_id
 * @property int|null $dictionary_campaign_external_id
 * @property int|null $goal_ad_group_external_id
 * @property int $dictionary_campaign_id
 * @property int|null $goal_v_card_id
 * @property int $goal_ad_group_id
 * @property int $advertisement_id
 * @property \Illuminate\Support\Carbon|null $external_upload_at
 * @property \Illuminate\Support\Carbon|null $external_updated_at
 * @property \Illuminate\Support\Carbon|null $updated_need
 * @property \Illuminate\Support\Carbon|null $archived_need
 * @property \Illuminate\Support\Carbon|null $reserve_archive_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read Advertisement $advertisement
 * @method static Builder|GoalAdvertisement forExternal()
 * @method static Builder|GoalAdvertisement forNotExternal()
 * @method static Builder|GoalAdvertisement forNotReserveCreate()
 * @method static Builder|GoalAdvertisement forNotReserveUpdate()
 * @method static Builder|GoalAdvertisement forNotReserveArchive()
 * @method static Builder|GoalAdvertisement needUpdated()
 * @method static Builder|GoalAdvertisement needArchived()
 * @method static Builder|GoalAdvertisement notNeedArchived()
 * @method static Builder|GoalAdvertisement newModelQuery()
 * @method static Builder|GoalAdvertisement newQuery()
 * @method static Builder|GoalAdvertisement query()
 * @method static Builder|GoalAdvertisement whereCreatedAt($value)
 * @method static Builder|GoalAdvertisement whereDictionaryCampaignExternalId($value)
 * @method static Builder|GoalAdvertisement whereDictionaryCampaignId($value)
 * @method static Builder|GoalAdvertisement whereExternalId($value)
 * @method static Builder|GoalAdvertisement whereExternalUpdatedAt($value)
 * @method static Builder|GoalAdvertisement whereExternalUploadAt($value)
 * @method static Builder|GoalAdvertisement whereGoalAdGroupExternalId($value)
 * @method static Builder|GoalAdvertisement whereGoalAdGroupId($value)
 * @method static Builder|GoalAdvertisement whereId($value)
 * @method static Builder|GoalAdvertisement whereAdvertisementId($value)
 * @method static Builder|GoalAdvertisement whereUpdatedAt($value)
 * @method static Builder|GoalAdvertisement whereUpdatedNeed($value)
 * @mixin \Eloquent
 */
class GoalAdvertisement extends Pivot
{
    use SoftDeletes;

    protected $table = 'goal_advertisements';

    protected $fillable = [
        'external_id',
        'goal_v_card_external_id',
        'goal_sitelink_external_id',
        'goal_ad_group_external_id',
        'dictionary_campaign_external_id',
        'goal_v_card_id',
        'goal_sitelink_id',
        'goal_ad_group_id',
        'dictionary_campaign_id',
        'advertisement_id',
        'external_upload_at',
        'external_updated_at',
        'updated_need',
        'reserve_create_at',
        'reserve_update_at',
        'archive_at',
        'archived_need',
        'reserve_archive_at',
        'deleted_need',
        'reserve_delete_at',
        'suspended_need',
        'suspended_at',
        'reserve_suspend_at',
    ];

    protected $casts = [
        'external_upload_at' => 'datetime',
        'external_updated_at' => 'datetime',
        'updated_need' => 'datetime',
        'reserve_create_at' => 'datetime',
        'reserve_update_at' => 'datetime',
        'archive_at' => 'datetime',
        'archived_need' => 'datetime',
        'reserve_archive_at' => 'datetime',
        'deleted_need' => 'datetime',
        'reserve_delete_at' => 'datetime',
        'suspended_need' => 'datetime',
        'suspended_at' => 'datetime',
        'reserve_suspend_at' => 'datetime',
    ];

    public $incrementing = true;
    public $timestamps = false;

    static public function getWithPivot()
    {
        return [
            'id',
            'external_id',
            'goal_v_card_external_id',
            'goal_sitelink_external_id',
            'goal_ad_group_external_id',
            'dictionary_campaign_external_id',
            'goal_v_card_id',
            'goal_sitelink_id',
            'goal_ad_group_id',
            'dictionary_campaign_id',
            'advertisement_id',
            'external_upload_at',
            'external_updated_at',
            'updated_need',
            'reserve_create_at',
            'reserve_update_at',
            'deleted_need',
            'reserve_delete_at',
            'archived_need',
            'reserve_archive_at',
            'archive_at',
        ];
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForExternal($query)
    {
        return $query->whereNotNull('external_id');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotExternal($query)
    {
        return $query->whereNull('external_id');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveCreate($query)
    {
        return $query->whereNull('reserve_create_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveUpdate($query)
    {
        return $query->whereNull('reserve_update_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNeedUpdated($query)
    {
        return $query->whereNotNull('updated_need');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForArchived($query)
    {
        return $query->whereNotNull('archive_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotArchived($query)
    {
        return $query->whereNull('archive_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNeedArchived($query)
    {
        return $query->whereNotNull('archived_need')
            ->where(function (Builder $query) {
                return $query->whereNull('suspended_need')
                    ->orWhere(function (Builder $query) {
                        return $query->whereNotNull('suspended_need')
                            ->whereNotNull('suspended_at');
                    });
            });
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNotNeedArchived($query)
    {
        return $query->whereNull('archived_need');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveArchive($query)
    {
        return $query->whereNull('reserve_archive_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNeedSuspended($query)
    {
        return $query->whereNotNull('suspended_need');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveSuspend($query)
    {
        return $query->whereNull('reserve_suspend_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotSuspended($query)
    {
        return $query->whereNull('suspended_at');
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeNeedDeleted($query)
    {
        return $query->whereNull('goal_advertisements.deleted_at')
            ->whereNull('goal_advertisements.archive_at')
            ->whereNull('goal_advertisements.archived_need')
            ->whereNull('goal_advertisements.suspended_need')
            ->where(function (Builder $query) {
                return $query->whereNotNull('deleted_need')
                    ->orWhere(function (Builder $query) {
                        return $query->whereExists(function (\Illuminate\Database\Query\Builder $query) {
                            $query->select(DB::raw(1))->from('advertisements')
                                ->whereNotNull('advertisements.deleted_at')
                                ->whereColumn('goal_advertisements.advertisement_id', 'advertisements.id');
                        });
                    })->orWhere(function (Builder $query) {
                        return $query->whereExists(function (\Illuminate\Database\Query\Builder $query) {
                            $query->select(DB::raw(1))
                                ->from('goal_ad_groups')
                                ->join('ad_groups', 'goal_ad_groups.ad_group_id', '=', 'ad_groups.id')
                                ->whereNull('goal_ad_groups.deleted_at')
                                ->whereNotNull('goal_ad_groups.external_id')
                                ->whereNotNull('ad_groups.deleted_at')
                                ->whereColumn('goal_advertisements.goal_ad_group_id', 'goal_ad_groups.id');
                        });
                    });
            });
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeForNotReserveDelete($query)
    {
        return $query->whereNull('reserve_delete_at');
    }

    public function advertisement()
    {
        return $this->belongsTo(Advertisement::class, 'advertisement_id');
    }

    public function dictionaryCampaign()
    {
        return $this->belongsTo(DictionaryCampaign::class, 'dictionary_campaign_id');
    }

    public function goalVCard()
    {
        return $this->belongsTo(GoalVCard::class, 'goal_v_card_id');
    }

    public function goalAdGroup()
    {
        return $this->belongsTo(GoalAdGroup::class, 'goal_ad_group_id');
    }

    public function goalAdExtensions()
    {
        return $this->belongsToMany(GoalAdExtension::class, GoalAdvertisementGoalAdExtension::getModel()->getTable(), 'goal_advertisement_id', 'goal_ad_extension_id')
            ->using(GoalAdvertisementGoalAdExtension::class)
            ->withPivot(GoalAdvertisementGoalAdExtension::getWithPivot())
            ->withTimestamps();
    }

    public function goalAdExtensionsForNotExternal()
    {
        return $this->goalAdExtensions()->forNotExternal();
    }

    public function errors()
    {
        return $this->morphMany(YandexError::class, 'cause');
    }

}