<?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 App\Models\PackingList;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class PackingListOrderedByInvoiceDate implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return Builder|mixed
     */
    public static function apply(Builder $builder, $value)
    {
        return $builder->select('packing_lists.*')->join('transactions', function($join){
            $join->on('transactions.owner_id', '=', 'packing_lists.id');
            $join->where('transactions.owner_type', '=', PackingList::class);
            $join->where('transactions.status', '=', ApprovalStatus::APPROVED);
        })->orderBy('transactions.updated_at', 'DESC');
    }

}
