<?php

namespace App\Classes\General\Eloquent\Filters;

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

class IsNotFullyRefunded implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return Builder|mixed
     */
    public static function apply(Builder $builder, $value)
    {
        return $builder->withSum(['transactions as total_refund_amount' => function($q) {
            $q->refunds()->where('status', ApprovalStatus::APPROVED);
        }], 'original_amount')
        ->havingRaw('total_refund_amount < original_amount');
    }
}
