<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class DisbursementOffers extends Model
{
    use SoftDeletes;

    protected $table = 'disbursement_offers_disbursed';

    public static function getOffers($id){
        $offers = static::where('offer_id', $id)->get();
        if (!count($offers)){
            $offersData = new \stdClass();
            $offersData->per_disbursement_amount = '';
            $offersData->day_from_disbursement_date = '';
            $offers[] = $offersData;
        }
        return $offers;
    }

    public function getOfferDisbursement($offer_id, $phase, $application_id){
        $totalPhase = $this->where('offer_id',$offer_id)->where('application_id', $application_id)->get();
        if($phase > count($totalPhase)){
            $phase = $phase - 1;
        }
        return $this->where('offer_id',$offer_id)->where('application_id', $application_id)->where('phase', $phase)->first();
    }

    public function getCurrentOfferDisbursement($offer_id, $phase, $application_id){
        return $this->where('offer_id',$offer_id)->where('application_id', $application_id)->where('phase', ($phase))->first();
    }

    public function getOfferByID($id, $application_id){
        return $this->where('id',$id)->where('application_id', $application_id)->first();
    }
    public function disbursementLogs(){
        return $this->hasMany('App\DisbursementLogs', 'disbursement_offer_id', 'id')
        ->join('disbursement_offers_disbursed', 'disbursement_offers_disbursed.application_id', '=', 'disbursement_log.application_id');
    }
}
