mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-14 09:01:42 -04:00
239 lines
9.8 KiB
PHP
239 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\StudioPlus\Services;
|
|
|
|
use Modules\StaffPayroll\Services\InvoiceSettingService;
|
|
use Modules\StaffPayroll\Repositories\InvoiceRepository;
|
|
use Modules\StaffPayroll\Repositories\InvoiceSettingRepository;
|
|
use Modules\StudioPlus\Entities\StudioPlusVideo;
|
|
use Modules\StudioPlus\Entities\StudioPlusVideoStatusTrack;
|
|
use Modules\StudioPlusWtiiabaddon\Entities\StudioPlusIncentive;
|
|
use Modules\StaffPayroll\Entities\Invoice;
|
|
use Modules\StaffPayroll\Entities\InvoiceSetting;
|
|
use Modules\StaffPayroll\Entities\InvoiceItem;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Models\User;
|
|
|
|
class VideoInvoice
|
|
{
|
|
private $invoiceRepository;
|
|
private $invoiceSettingService;
|
|
public function __construct()
|
|
{
|
|
$invoice = new Invoice;
|
|
$this->invoiceRepository = new InvoiceRepository($invoice);
|
|
|
|
$invoiceSetting = new InvoiceSetting;
|
|
$invoiceSettingRepository = new InvoiceSettingRepository($invoiceSetting);
|
|
$this->invoiceSettingService = new InvoiceSettingService($this->invoiceRepository, $invoiceSettingRepository);
|
|
}
|
|
|
|
private function isValidStatus($video): bool
|
|
{
|
|
return in_array($video->status, [StudioPlusVideo::STATUS_PUBLISHED, StudioPlusVideo::STATUS_AMAZON_APPROVED, StudioPlusVideo::STATUS_AMAZON_UPLOADED]);
|
|
}
|
|
|
|
private function isIncentiveApplicable($incentive, $video): bool
|
|
{
|
|
if ($incentive) {
|
|
|
|
$video_types = $incentive->videoTypes()->pluck('studio_plus_video_type_id');
|
|
if ($video_types->count() > 0) {
|
|
return $video_types->contains($video->studio_plus_video_type_id);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function addIncentiveItem($video)
|
|
{
|
|
|
|
try {
|
|
|
|
if (!$this->invoiceSettingService->isValidUserRole($video->channel->user)) {
|
|
return;
|
|
}
|
|
|
|
if ($this->isValidStatus($video)) {
|
|
|
|
$video = $video->load('asinData');
|
|
$user = $video->channel->user;
|
|
$published_track = StudioPlusVideoStatusTrack::where('studio_plus_video_id',$video->id)->where('name','Published')->first();
|
|
if($published_track){
|
|
$invoice = Invoice::where('user_id',$user->id)->whereYear('issue_date', $published_track->created_at->year)->whereMonth('issue_date', $published_track->created_at->month)->first();
|
|
}
|
|
|
|
if(!$published_track || !$invoice){
|
|
|
|
$invoice = Invoice::query()
|
|
->whereYear('issue_date', now()->year)->whereMonth('issue_date', now()->month)
|
|
->firstOrCreate(
|
|
[
|
|
'user_id' => $user->id,
|
|
],
|
|
[
|
|
'invoice_number' => Invoice::where('user_id',$user->id)->count() + 1,
|
|
'issue_date' => now()->toDateString(),
|
|
'created_by' => auth()->check() ? auth()->id() : null,
|
|
]
|
|
);
|
|
}
|
|
|
|
|
|
if(!$invoice->is_approved){
|
|
|
|
if($video->videoType->need_asin == false){
|
|
|
|
$incentive = StudioPlusIncentive::whereHas('videoTypes', function ($query) use($video) {
|
|
$query->where('studio_plus_video_type_id', $video->studio_plus_video_type_id);
|
|
})->whereHas('asins', function ($query) use($video) {
|
|
$query->where('studio_plus_asin_id', $video->studio_plus_asin_id);
|
|
})->where('target_rule',1)->first();
|
|
|
|
if(!$incentive) return;
|
|
|
|
$invoice->items()->updateOrCreate(
|
|
[
|
|
'studio_plus_video_id' => $video->id,
|
|
],
|
|
[
|
|
'item_no' => 1,
|
|
'name' => 'Video upload payout',
|
|
'description' => $incentive->description,
|
|
'amount' => $incentive->amount_value_earned,
|
|
'quantity' => 1,
|
|
'issue_date' => now()->toDateString(),
|
|
'type' => InvoiceItem::TYPE_VIDEO_UPLOAD_PAYOUT,
|
|
'range_amount' => $video->asinData->range_amount
|
|
]
|
|
);
|
|
|
|
if($incentive->amount_value_earned > 0){
|
|
$this->addIncentiveItemToUpline($user, $video);
|
|
}
|
|
|
|
} else if($video->videoType->vertical_video){
|
|
|
|
$incentive = StudioPlusIncentive::whereHas('videoTypes', function ($query) use($video) {
|
|
$query->where('studio_plus_video_type_id', $video->studio_plus_video_type_id);
|
|
})->doesntHave('asins')->where('target_rule',1)->first();
|
|
|
|
if(!$incentive) return;
|
|
|
|
$invoice->items()->updateOrCreate(
|
|
[
|
|
'studio_plus_video_id' => $video->id,
|
|
],
|
|
[
|
|
'item_no' => 1,
|
|
'name' => 'Video upload payout',
|
|
'description' =>"ASIN {$video->asin} ({$incentive->description})",
|
|
'amount' => $incentive->amount_value_earned,
|
|
'quantity' => 1,
|
|
'issue_date' => now()->toDateString(),
|
|
'type' => InvoiceItem::TYPE_VIDEO_UPLOAD_PAYOUT,
|
|
'range_amount' => null
|
|
]
|
|
);
|
|
|
|
|
|
if($incentive->amount_value_earned > 0){
|
|
$this->addIncentiveItemToUpline($user, $video);
|
|
}
|
|
|
|
} else {
|
|
|
|
$incentives = StudioPlusIncentive::whereHas('videoTypes', function ($query) use($video) {
|
|
$query->where('studio_plus_video_type_id', $video->studio_plus_video_type_id);
|
|
})->whereHas('asins', function ($query) use($video) {
|
|
$query->where('studio_plus_asin_id', $video->studio_plus_asin_id);
|
|
})->whereHas('roles', function ($query) use($video) {
|
|
$query->where('role_id', $video->channel->user->role_id);
|
|
})->where('target_rule',1)->where('incentive_category_id','!=',5)->get();
|
|
|
|
|
|
foreach ($incentives as $incentive) {
|
|
|
|
$invoice->items()->updateOrCreate(
|
|
[
|
|
'studio_plus_video_id' => $video->id,
|
|
],
|
|
[
|
|
'item_no' => 1,
|
|
'name' => 'Video upload payout',
|
|
'description' => "ASIN {$video->asin} ({$incentive->description})",
|
|
'amount' => $incentive->amount_value_earned,
|
|
'quantity' => 1,
|
|
'issue_date' => $published_track ? $published_track->created_at->toDateString() : now()->toDateString(),
|
|
'type' => InvoiceItem::TYPE_VIDEO_UPLOAD_PAYOUT,
|
|
'range_amount' => $video->asinData->range_amount,
|
|
'created_at' => $published_track ? $published_track->created_at : now()->toDateString()
|
|
]
|
|
);
|
|
|
|
}
|
|
|
|
|
|
if($video->asinData->payout_amount > 0){
|
|
$this->addIncentiveItemToUpline($user, $video);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
} else {
|
|
|
|
InvoiceItem::where('studio_plus_video_id', $video->id)
|
|
->delete();
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
Log::error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function addIncentiveItemToUpline($user, $video)
|
|
{
|
|
if (!$user->parent_id || $user->parent_id == null) return;
|
|
|
|
$incentive = StudioPlusIncentive::where('incentive_category_id', 5)->first();
|
|
if($this->isIncentiveApplicable($incentive, $video)){
|
|
|
|
$parentUser = User::where('id', $user->parent_id)->first();
|
|
|
|
$invoice = Invoice::query()
|
|
->whereYear('issue_date', now()->year)->whereMonth('issue_date', now()->month)
|
|
->firstOrCreate(
|
|
[
|
|
'user_id' => $parentUser->id,
|
|
],
|
|
[
|
|
'invoice_number' => Invoice::where('user_id',$parentUser->id)->count() + 1,
|
|
'issue_date' => now()->toDateString(),
|
|
'created_by' => auth()->check() ? auth()->id() : null,
|
|
]
|
|
);
|
|
|
|
|
|
$invoice->items()->updateOrCreate(
|
|
[
|
|
'studio_plus_video_id' => $video->id,
|
|
],
|
|
[
|
|
'item_no' => 1,
|
|
'name' => 'Downline Incentives',
|
|
'description' => "Downline bonus ({$user->first_name})",
|
|
'amount' => $incentive->amount_value_earned,
|
|
'quantity' => 1,
|
|
'issue_date' => now()->toDateString(),
|
|
'type' => InvoiceItem::TYPE_DOWNLINE_INCENTIVES,
|
|
'range_amount' => $video->asinData->range_amount
|
|
]
|
|
);
|
|
}
|
|
}
|
|
}
|