<?php

namespace App\Classes\General\Eloquent\Filters;

use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class Monetary implements Filter
{
    /**
     * @param Builder $builder
     * @param $value
     * @return mixed
     */
    public static function apply(Builder $builder, $value)
    {
        return $builder->whereHas('transactions', function ($query) use ($value){
    		$query->where('transactions.type', TransactionType::PAYMENT)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
    		$query->havingRaw('SUM(amount) >= ' . $value);
        });
    }
}