referrer_id) { $videos = StudioPlusVideo::where('studio_plus_channel_id', $studioPlusVideo->channel->id) ->whereIn('status', [StudioPlusVideo::STATUS_EDITED, StudioPlusVideo::STATUS_AMAZON_APPROVED]); Log::info('Referral bonus disbursement'); Log::info('Video Count: ' . $videos->count()); $referror = User::find($user->referrer_id); if ($referror) { $referral_incentive = StudioPlusIncentive::where('title', 'Staff Referral')->first(); if ($referral_incentive) { $invoices = Invoice::where('user_id', $referror->id)->whereHas('items', function($query) use($referral_incentive, $user) { $query->where('studio_plus_incentive_id', $referral_incentive->id); })->get(); if ($invoices->count() < 1) { Log::info('Referral incentives found'); if ($videos->count() == $referral_incentive->target_rule) { $latestInvoiceNumber = $this->invoiceSettingService->getLatestInvoiceNumber($referror); $invoice = $this->invoiceRepository->getInvoiceOfMonth($referror, $latestInvoiceNumber); $invoice->items()->updateOrCreate( [ 'item_no' => 1, 'name' => "{$referral_incentive->title} - {$user->first_name} {$user->last_name}", 'description' => $referral_incentive->description, 'amount' => $referral_incentive->amount_value_earned, 'quantity' => 1, 'issue_date' => now()->toDateString(), 'studio_plus_incentive_id' => $referral_incentive->id ] ); Log::info('Referral bonus given'); } else { Log::info('Insufficent number of videos'); } } else { Log::info('Referral bonus already given'); } } else { Log::info('No referral incentives found'); } } else { Log::warning('Referror not found'); } } } }