mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-08-05 03:16:14 -04:00
142 lines
6.9 KiB
PHP
142 lines
6.9 KiB
PHP
<?php
|
|
|
|
|
|
namespace Modules\Subscription\Traits;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Subscription\Entities\CustomerSubscription;
|
|
use Modules\Subscription\Entities\UserCredit;
|
|
|
|
trait SubscriptionTrait
|
|
{
|
|
public function subscriptions()
|
|
{
|
|
return $this->hasMany(CustomerSubscription::class, 'customer_id', 'id');
|
|
}
|
|
|
|
public function currentSubscription()
|
|
{
|
|
//check active subscription first
|
|
$active_subscription = $this->subscriptions()->where('status','active')->first();
|
|
if($active_subscription){
|
|
return $active_subscription;
|
|
}
|
|
|
|
return $this->hasOne(CustomerSubscription::class, 'customer_id', 'id')->latestOfMany()->first();
|
|
}
|
|
|
|
public function getSubscriptionStatusAttribute()
|
|
{
|
|
$currentSubscription = $this->currentSubscription();
|
|
|
|
if(isModuleActive('WTIJobBoard')){
|
|
|
|
$payment_details = '';
|
|
if($currentSubscription){
|
|
|
|
$price = $currentSubscription->planPrice->price;
|
|
$price_monthly = $currentSubscription->planPrice->price / $currentSubscription->planPrice->custom_interval;
|
|
|
|
if ($currentSubscription->planPrice->interval == 'Custom') {
|
|
|
|
if($currentSubscription->payment_mode == 'Monthly'){
|
|
|
|
$payment_details .= "\$".number_format($price_monthly,2)." / mo for {$currentSubscription->planPrice->custom_interval} Months";
|
|
|
|
} else {
|
|
|
|
$payment_details .= "\$".number_format($price,2)." for {$currentSubscription->planPrice->custom_interval} {$currentSubscription->planPrice->custom_interval_type}";
|
|
}
|
|
|
|
} else {
|
|
$payment_details .= "\${$currentSubscription->planPrice->price} {$currentSubscription->planPrice->interval}";
|
|
}
|
|
|
|
|
|
if($currentSubscription->discount != null){
|
|
$duration = $currentSubscription->discount->coupon->duration;
|
|
$coupon_payment_mode = $currentSubscription->discount->coupon->payment_mode;
|
|
$discount = $currentSubscription->discount->coupon->discount;
|
|
|
|
if ($currentSubscription->discount->coupon->type == 'Amount') {
|
|
$discount_label = "<span class=\"text-success\">Save \${$discount}</span>";
|
|
$discounted_price_monthly = number_format($price_monthly - $discount,2);
|
|
$discounted_price_all = number_format( $price - $discount,2);
|
|
|
|
} else {
|
|
$discount_label = "<span class=\"text-success\">Save \${$discount}%</span>";
|
|
$discounted_price_monthly = number_format($price_monthly - (($discount / 100) * $price_monthly),2);
|
|
$discounted_price_all = $currentSubscription->discount->coupon->duration == 'Once' ? number_format($price - (($discount / 100) * $price_monthly),2) : number_format($price - (($discount / 100) * $price),2);
|
|
}
|
|
|
|
$payment_details .= "<br> (<span class=\"badge bg-info\">{$currentSubscription->discount->coupon->code}</span>";
|
|
|
|
if ($currentSubscription->payment_mode == 'Monthly' && ($coupon_payment_mode == 'Monthly' || $coupon_payment_mode == 'Both') ) {
|
|
$payment_details .= $duration == 'Once' ? " - \${$discounted_price_monthly} 1st month only)" : " | \${$discounted_price_monthly} per month)";
|
|
}
|
|
|
|
if($currentSubscription->payment_mode == 'All' && ($coupon_payment_mode == 'All' || $coupon_payment_mode == 'Both') ) {
|
|
$payment_details .= " - \${$discounted_price_all} for {$currentSubscription->planPrice->custom_interval} Months )";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return [
|
|
'plan' => $currentSubscription ? $currentSubscription->planPrice->plan->title : null,
|
|
'plan_image' => $currentSubscription ? $currentSubscription->planPrice->plan->image_url : null,
|
|
'approved' => $currentSubscription ? $currentSubscription->approved : false,
|
|
'expired_at' => $currentSubscription && $currentSubscription->status == 'active' ? \Carbon\Carbon::parse($currentSubscription->renewal_start_date == null ? $currentSubscription->created_at : $currentSubscription->renewal_start_date)->addMonths($currentSubscription->planPrice->custom_interval)->format('j M Y, g:i a') : null,
|
|
'payment_details' => $payment_details,
|
|
'status' => $currentSubscription ? $currentSubscription->status : false
|
|
];
|
|
} else {
|
|
return $currentSubscription;
|
|
}
|
|
}
|
|
|
|
public function creditPoints()
|
|
{
|
|
return $this->hasOne(UserCredit::class, 'user_id', 'id');
|
|
}
|
|
|
|
public function getCreditPointsStatusAttribute()
|
|
{
|
|
$usage = [];
|
|
$categories = \Modules\WTIJobBoard\Entities\JobRequestCategory::get();
|
|
foreach ($categories as $key => $category) {
|
|
$query = \Modules\WTIJobBoard\Entities\JobRequest::query()->where('client_id',auth()->user()->id)->where('type',$category->id)->where('status','publish')->where('is_rejected',false)->whereNull('deleted_at');
|
|
$usage[$category->name] = [
|
|
'subscription' => (int)$query->clone()->where('credit_type','subscription')->sum('credits'),
|
|
'alacarte' => (int)$query->clone()->where('credit_type','alacarte')->sum('credits')
|
|
];
|
|
}
|
|
|
|
$total_used_subscription_credits = \Modules\WTIJobBoard\Entities\JobRequest::where('client_id',auth()->user()->id)->where('status','publish')->where('is_rejected',false)->whereNull('deleted_at')->where('credit_type','subscription')->sum('credits');
|
|
$total_used_alacarte_credits = \Modules\WTIJobBoard\Entities\JobRequest::where('client_id',auth()->user()->id)->where('status','publish')->where('is_rejected',false)->whereNull('deleted_at')->where('credit_type','alacarte')->sum('credits');
|
|
|
|
$total_subscription_credits = auth()->user()->creditPoints->subscription_credits;
|
|
$total_alacarte_credits = auth()->user()->creditPoints->alacarte_credits;
|
|
|
|
return [
|
|
'total_credits' => [
|
|
'subscription' => (int)$total_subscription_credits,
|
|
'alacarte' => (int)$total_alacarte_credits,
|
|
],
|
|
'total_used_credits' => [
|
|
'subscription' => (int)$total_used_subscription_credits,
|
|
'alacarte' => (int)$total_used_alacarte_credits,
|
|
],
|
|
'total_available_credits' => [
|
|
'subscription' => (int)$total_subscription_credits - $total_used_subscription_credits,
|
|
'alacarte' => (int)$total_alacarte_credits - $total_used_alacarte_credits,
|
|
],
|
|
'usage' => $usage,
|
|
];
|
|
|
|
}
|
|
|
|
}
|