bonus25(); $this->bonus50(); } private function bonus25() { $bonus25 = StudioPlusIncentive::query() ->where('title', 'Bonus 25') ->first(); if (!$bonus25) return; $this->query($bonus25, 25); } private function bonus50() { $bonus50 = StudioPlusIncentive::query() ->where('title', 'Bonus 50') ->first(); if (!$bonus50) return; $this->query($bonus50, 50); } private function query(StudioPlusIncentive $studioPlusIncentive, int $bonus) { return StudioPlusVideo::query() ->with(['channel.user']) ->select('studio_plus_channel_id', DB::raw('COUNT(id) as videos')) ->whereIn('status', [ StudioPlusVideo::STATUS_AMAZON_APPROVED, StudioPlusVideo::STATUS_AMAZON_UPLOADED, StudioPlusVideo::STATUS_PUBLISHED, ]) ->whereBetween('created_at', [now()->startOfMonth()->toDateString(), now()->lastOfMonth()->toDateString()]) ->groupBy('studio_plus_channel_id') ->havingRaw('videos > ?', [$bonus]) ->each(function ($video) use ($studioPlusIncentive, $bonus) { $invoiceOfMonth = (new InvoiceService)->getUserInvoiceOfMonth($video->channel->user); InvoiceItem::updateOrCreate( [ 'invoice_id' => $invoiceOfMonth->id, 'studio_plus_incentive_id' => $studioPlusIncentive->id ], [ 'name' => "Incentive Bonus $bonus", 'item_no' => 1, 'description' => "Incentive Bonus $bonus", 'amount' => $studioPlusIncentive->amount_value_earned, 'quantity' => 1, 'issue_date' => now()->toDateString(), 'type' => InvoiceItem::TYPE_INCENTIVE_BONUS ] ); }); } }