<?php

namespace App\Classes\General\Eloquent\Filters;

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

class WithAgingColumn implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return mixed
     */
    public static function apply(Builder $builder, $value)
    {
        $today = Carbon::now();
        return $builder->select('packing_lists.*')
        ->addSelect(DB::raw("DATEDIFF('$today', transactions.updated_at) as days_over_duedate"))
        ->addSelect(DB::raw("CASE
        WHEN DATEDIFF('$today', transactions.updated_at) <= 0 THEN 0
        WHEN DATEDIFF('$today', transactions.updated_at) > 0 AND DATEDIFF('$today', transactions.updated_at) <= 30 THEN 1
        WHEN DATEDIFF('$today', transactions.updated_at) > 30 AND DATEDIFF('$today', transactions.updated_at) <= 60 THEN 2
        WHEN DATEDIFF('$today', transactions.updated_at) > 60 AND DATEDIFF('$today', transactions.updated_at) <= 90 THEN 3
        ELSE 4
        END AS due_date_number"));
    }
}