<?php

namespace App\Classes\General\Eloquent\Filters;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PackingListType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;

class InDestinationWarehouse implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return mixed
     */
    public static function apply(Builder $builder, $value)
    {
        return $builder->whereHas('packingList', function($packingList){
            return $packingList->where('type', PackingListType::SHIPPING_PACKING_LIST)->where('status', ApprovalStatus::APPROVED);
        })->whereHas('container',
            function($container) {
                return $container->where('containers.status', ApprovalStatus::COMPLETED);
            }
        )->whereDoesntHave('deliverySchedules',
            function($schedule) {
                return $schedule->where('etd', '<', Carbon::now())->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
            }
        );
    }
}