<?php

namespace App\Classes\General\Eloquent\Filters;


use Illuminate\Database\Eloquent\Builder;

class WithOrderReferenceLike implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return mixed
     */
    public static function apply(Builder $builder, $value)
    {
        return $builder->join('transactions as t2', 't2.payment_reference', '=', 'transactions.bill_no')
            ->join('transactions as t3', 't3.id', '=', 't2.owner_id')
            ->join('packing_lists', 'packing_lists.id', '=', 't3.owner_id')
            ->join('orders', function ($join) use ($value) {
                $join->on('orders.id', '=', 'packing_lists.owner_id')
                    ->where('orders.reference', 'LIKE', '%'.$value.'%');
            })
            ->addSelect(['transactions.*', 't2.id as paymentTransactionId', 't3.id as invoiceTransactionId', 'packing_lists.id as packingListId', 'orders.reference as orderReference']);
    }
}