<?php

namespace Gisplus\Package\Crm\Models;

use DB;
use Auth;
use Gisplus\Package\Crm\Models\BusinessReferral;
use Illuminate\Database\Eloquent\Model;

class ReferralHistory extends Model
{
    public function businessReferral()
    {
        return $this->belongsTo(BusinessReferral::class , 'business_referral_id')->withDefault();

    }

    public function storeReferralHistory($object)
    {
   
        return DB::transaction(function() use ($object){
        
            $referral_history = new ReferralHistory;

                $referral_history->lead_id                   = $object['lead'];
                $referral_history->business_referral_id      = $object['business_referral'];
                $referral_history->added_by                  = Auth::user()->id;
                // dd($referral_history);
                $referral_history->save();

            return with($referral_history);
        });

    }

    public function updateReferralHistory($object)
    {
        return DB::transaction(function() use ($object){
    
         $referral_history = ReferralHistory::find($object['referral_history']);


                $referral_history->business_referral_id      = $object['business_referral'];
                $referral_history->added_by                  = Auth::user()->id;
                // dd($referral_history);
                $referral_history->update();
                // dd($referral_history);

            return with($referral_history);
        });

    }
    
    public function getReferralHistoryByBusinessReferral($id)
    {
        return ReferralHistory::where(['business_referral_id' => $id])->first();

    }
    public function getReferralHistoryById($id)
    {
        return ReferralHistory::where(['id' => $id])->first();

    }
    
}
