skip to Main Content

I have a staff controller and transactions model. In the staff controller, I am fetching staff transactions with other data as below
StaffController.php

    $transactions = Transaction::thisMerchant()
->earnedByStaff($staff)
  ->where(function ($q) use ($staffID) {
    $q->where('staff_id', $staffID)
      ->orWhere(function ($qq) use ($staffID) {
        $qq
          ->whereNull('staff_id')
          ->whereHas('items', function ($qqq) use ($staffID) {
            $qqq->where('staff_id', $staffID);
          });
      })
      ->orWhereHas('associateCommissions', function ($qq) use ($staffID) {
        $qq->where('staff_id', $staffID);
      });
  })
  ->when($start_date && $end_date, function ($q) use (
    $start_date,
    $end_date
  ) {
    $q->whereBetween(DB::raw('date(date)'), [
      $start_date . ' 00:00:00',
      $end_date . ' 23:59:59',
    ]);
  })
  ->with('client')
  ->orderBy('date', 'DESC')
  ->where('is_void', null)
  ->where('is_draft', null)
  ->with('items.sellable.sellable')
  ->with('associateCommissions')
  ->paginate(10);

In the transactions model, I have this
Transaction.php

  public function earnedFromTransaction($staff_id)
{
$staff = Staff::find($staff_id);
if ($this->is_void == '1') {
  return 0;
}

if ($this->provider() == 'Empty') {
  return 0;
}

if ($this->provider() == 'Split') {
  $amounts = Titem::where('transaction_id', $this->id)
    ->where('staff_id', $staff_id)
    ->whereHas('product', function ($q) {
      $q->whereNull('commission');
    })
    ->whereHas('transaction', function ($q) {
      $q->where('is_void', null);
    })
    ->sum('frozen_staff_commission');

  $discounts = Titem::where('transaction_id', $this->id)
    ->where('staff_id', $staff_id)
    ->whereHas('product', function ($q) {
      $q->whereNull('commission');
    })
    ->whereHas('transaction', function ($q) {
      $q->where('is_void', null);
    })
    ->sum('discount');

  // When the frozen staff rate is zero, just give sellable commission
  $titem_commissions = Titem::where('transaction_id', $this->id)
    ->where('staff_id', $staff_id)
    ->where('frozen_staff_commission', '<=', 0)
    ->whereHas('product', function ($q) {
      $q->whereNotNull('commission');
    })
    ->sum('commission');

  $total =
    floatval($amounts) +
    floatval($titem_commissions) -
    floatval($discounts);

  return $total;
}

return floatval($this->frozen_staff_commission);
  }

  public function earnedByStaff(Staff $staff)
  {
   return StaffCommissionEngine::earnedFromTransaction($this, $staff);
  }

I am getting this error BadMethodCallException: Call to undefined method IlluminateDatabaseEloquentBuilder::earnedByStaff() in file

I have tried using $transactions = Transaction::earnedByStaff($staff)... but this returns

Error: Non-static method AppTransaction::earnedByStaff() cannot be called statically in file 

How do I call the earnedByStaff() method and pass the $staff to it?

2

Answers


  1. Make the function ‘earnedByStaff’ static.

    public static function earnedByStaff( Staff $staff)
    {
       return StaffCommissionEngine::earnedFromTransaction($this, $staff);
    } 
    

    After that it will work fine.

    Login or Signup to reply.
  2. You can use scope method

    public function scopeEarnedByStaff($query,$arg)
        {
            .....
        }
    

    You can also use the call method on the query builder to call a function on the model class

     MyModel::where('column', 'value')->get();
         $transactions = Transaction::thisMerchant()
        ->call('earnedByStaff', [$staff])->where()
        ..
        ..
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search