mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-14 09:01:42 -04:00
77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\StudioPlus\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\StaffPayroll\Entities\InvoiceItem;
|
|
use Modules\StaffPayroll\Services\InvoiceService;
|
|
use Modules\StudioPlus\Entities\StudioPlusUser;
|
|
use Modules\StudioPlus\Entities\StudioPlusVideo;
|
|
use Modules\StudioPlusWtiiabaddon\Entities\StudioPlusIncentive;
|
|
|
|
class GenerateVideoInvoiceItem
|
|
{
|
|
public function __invoke()
|
|
{
|
|
$this->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
|
|
]
|
|
);
|
|
});
|
|
}
|
|
}
|