mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-08-01 17:17:12 -04:00
78 lines
3.4 KiB
PHP
78 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\ReferralSystem\Services;
|
|
|
|
use App\Models\User;
|
|
use Modules\StudioPlus\Entities\StudioPlusVideo;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\StaffPayroll\Entities\Invoice;
|
|
use Modules\StaffPayroll\Repositories\InvoiceRepository;
|
|
use Modules\StaffPayroll\Repositories\InvoiceSettingRepository;
|
|
use Modules\StaffPayroll\Services\InvoiceSettingService;
|
|
use Modules\StudioPlusWtiiabaddon\Entities\StudioPlusIncentive;
|
|
|
|
class CommonService
|
|
{
|
|
public function __construct(
|
|
private InvoiceRepository $invoiceRepository,
|
|
private InvoiceSettingRepository $invoiceSettingRepository,
|
|
private InvoiceSettingService $invoiceSettingService
|
|
) {
|
|
}
|
|
|
|
public function disburseReferralBonusIfQualified($user, StudioPlusVideo $studioPlusVideo) {
|
|
if ($user->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');
|
|
}
|
|
}
|
|
}
|
|
}
|