<?php

namespace App\Classes\General\Eloquent\Filters;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Classes\ValueObjects\Constants\TransactionType;
use Illuminate\Database\Eloquent\Builder;

class PurchaseOrderApproval implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return Builder|mixed
     */
    public static function apply(Builder $builder, $value)
    {
        return $builder->whereHas('service', function($service){
            $service->whereHas('constants', function($query) {
                $query->where('reference', SegmentConstants::SERVICE_TYPE)->where('detail->is_billable', true);
            });
        })->whereHas('transactions', function($transaction){
            return $transaction->where('type', TransactionType::PURCHASE_ORDER)->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION]);
        });
    }

}