<?php


namespace App\Filters;

use Illuminate\Http\Request;

class SupplierFilters
{
    protected $request;
    protected $queryBuilder;
    protected $filters = [];
    protected $array_filter;
    protected $params = [
        'name','contact_man','phone','invoice_title','bank','bank_account','opening_bank'
    ];
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function apply($builder)
    {
        $this->queryBuilder = $builder;
        $filters = array_filter($this->request->only($this->filters));
        foreach ($filters as $filter => $value) {
            if (method_exists($this, $filter)) {
                $this->$filter($value, $this->queryBuilder);
            }
        }
        $this->afterApply();
        return $this->queryBuilder;
    }
    private function afterApply()
    {

    }

    private function name($name)
    {
        $this->queryBuilder->where('name','like',"{$name}%");
    }

    private function contact_man($contact_man)
    {
        $this->queryBuilder->where('contact_man','like',"{$contact_man}%");
    }

    private function phone($phone)
    {
        $this->queryBuilder->where('phone','like',"{$phone}%");
    }

    private function invoice_title($invoice_title)
    {
        $this->queryBuilder->where('invoice_title','like',"{$invoice_title}%");
    }

    private function bank($bank)
    {
        $this->queryBuilder->where('bank','like',"{$bank}%");
    }
    private function bank_account($bank_account)
    {
        $this->queryBuilder->where('bank_account','like',"{$bank_account}%");
    }
    private function opening_bank($opening_bank)
    {
        $this->queryBuilder->where('opening_bank','like',"{$opening_bank}%");
    }

}
