Files
wticreatorstudio/Modules/Subscription/Traits/SubscriptionTrait.php
T

215 lines
13 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;
use \Carbon\Carbon;
trait SubscriptionTrait
{
public function subscriptions()
{
return $this->hasMany(CustomerSubscription::class, 'customer_id', 'id');
}
public function subscription()
{
return $this->hasOne(CustomerSubscription::class, 'customer_id', 'id')->latestOfMany();
}
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 = '';
$subscription_price = 0;
if($currentSubscription){
$currentSubscription->load('paymentCode');
$price = $currentSubscription->planPrice->price;
$price_monthly = $currentSubscription->planPrice->price / $currentSubscription->planPrice->custom_interval;
$payment_details = '';
if ($currentSubscription->planPrice->billing_type == 'Recurring') {
$price = $currentSubscription->planPrice->price;
$price_monthly = $currentSubscription->planPrice->price / $currentSubscription->planPrice->custom_interval;
if ($currentSubscription->planPrice->interval == 'Custom') {
if($currentSubscription->payment_mode == 'Monthly'){
$subscription_price = number_format($price_monthly,2);
$payment_details .= "\$".number_format($price_monthly,2)." / monthly for {$currentSubscription->planPrice->custom_interval} Months";
} else {
$subscription_price = number_format($price,2);
$payment_details .= "\$".number_format($price,2)." for {$currentSubscription->planPrice->custom_interval} {$currentSubscription->planPrice->custom_interval_type}";
}
} else {
$subscription_price = number_format($currentSubscription->planPrice->price,2);
$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 = $price_monthly - $discount;
$discounted_price_all = $price - $discount;
} else {
$discount_label = "<span class=\"text-success\">Save \${$discount}%</span>";
$discounted_price_monthly = $price_monthly - (($discount / 100) * $price_monthly);
$discounted_price_all = $currentSubscription->discount->coupon->duration == 'Once' ? $price - (($discount / 100) * $price_monthly) : $price - (($discount / 100) * $price);
}
$payment_details .= "<br> <span class=\"badge bg-info\">{$currentSubscription->discount->coupon->code}</span>";
if ($currentSubscription->payment_mode == 'Monthly' && $coupon_payment_mode == 'Monthly' ) {
$payment_details .= $duration == 'Once' ? " - \${$discounted_price_monthly} 1st month only)" : " | \${$discounted_price_monthly} per month";
if($duration == 'Once'){
if($currentSubscription->paid_invoice == 1){
$subscription_price = number_format($discounted_price_monthly,2);
}
} else {
$subscription_price = number_format($discounted_price_monthly,2);
}
}
if($currentSubscription->payment_mode == 'All' && $coupon_payment_mode == 'All' ) {
$subscription_price = number_format($discounted_price_all,2);
$payment_details .= " - \${$discounted_price_all} for {$currentSubscription->planPrice->custom_interval} Months ";
}
}
} else if ($currentSubscription->planPrice->billing_type == 'One-Time') {
$price = $currentSubscription->planPrice->price;
if($currentSubscription->paymentCode != null){
$payment_details .= "Will expire on ".Carbon::parse($currentSubscription->created_at)->addMonths($currentSubscription->planPrice->access_expire_duration)->format('M d, Y');;
} else {
if($currentSubscription->payment_status == 'uncaptured'){
$payment_details .= 'Expires on '. Carbon::parse($currentSubscription->created_at)->addDays(7)->format('M d, Y').' if not yet authorize / captured';
} else {
if($currentSubscription->planPrice->access_expire_duration_type == 'Days'){
$payment_details .= "\${$price} {$currentSubscription->planPrice->billing_type} {$currentSubscription->planPrice->access} access, will expire on ".Carbon::parse($currentSubscription->created_at)->addDays($currentSubscription->planPrice->access_expire_duration)->format('M d, Y');
} else if($currentSubscription->planPrice->access_expire_duration_type == 'Weeks'){
$payment_details .= "\${$price} {$currentSubscription->planPrice->billing_type} {$currentSubscription->planPrice->access} access, will expire on ".Carbon::parse($currentSubscription->created_at)->addWeeks($currentSubscription->planPrice->access_expire_duration)->format('M d, Y');
} else if($currentSubscription->planPrice->access_expire_duration_type == 'Months'){
$payment_details .= "\${$price} {$currentSubscription->planPrice->billing_type} {$currentSubscription->planPrice->access} access, will expire on ".Carbon::parse($currentSubscription->created_at)->addMonths($currentSubscription->planPrice->access_expire_duration)->format('M d, Y');
} else if($currentSubscription->planPrice->access_expire_duration_type == 'Years'){
$payment_details .= "\${$price} {$currentSubscription->planPrice->billing_type} {$currentSubscription->planPrice->access} access, will expire on ".Carbon::parse($currentSubscription->created_at)->addYears($currentSubscription->planPrice->access_expire_duration)->format('M d, Y');
}
}
}
}
}
return [
'subscription_id' => $currentSubscription ? $currentSubscription->txn_id : null,
'plan' => $currentSubscription ? $currentSubscription->planPrice->plan->title : null,
'subscription_price' => (float)$subscription_price,
'coupon' => $currentSubscription && $currentSubscription->discount != null ? [
'duration' => $currentSubscription->discount->coupon->duration,
'payment_mode' => $currentSubscription->discount->coupon->payment_mode,
'discount' => (float)$currentSubscription->discount->coupon->discount
] : null,
'plan_image' => $currentSubscription ? $currentSubscription->planPrice->plan->image_url : null,
'plan_total_credits' => $currentSubscription ? $currentSubscription->planPrice->total_credits : 0,
'plan_months' => $currentSubscription ? $currentSubscription->planPrice->custom_interval : 0,
'plan_status' => $currentSubscription ? $currentSubscription->planPrice->plan->status : false,
'approved' => $currentSubscription ? $currentSubscription->approved : false,
'subscription_end' => $currentSubscription ? \Carbon\Carbon::parse($currentSubscription->renewal_start_date == null ? $currentSubscription->created_at : $currentSubscription->renewal_start_date)->addMonths($currentSubscription->payment_method == 'prepaid' ? $currentSubscription->planPrice->access_expire_duration : $currentSubscription->planPrice->custom_interval)->format('j M Y, g:i a') : null,
'subscription_expired' => $currentSubscription && $currentSubscription->expired_at != null ? \Carbon\Carbon::now()->gt(\Carbon\Carbon::parse($currentSubscription->expired_at)) : true,
'subscription_expired_at' => $currentSubscription && $currentSubscription->expired_at != null ? \Carbon\Carbon::parse($currentSubscription->expired_at)->format('j M Y, g:i a') : null,
'last_payment_date' => $currentSubscription && $currentSubscription->last_payment_date != null ? \Carbon\Carbon::parse($currentSubscription->last_payment_date)->format('M j, Y') : null,
'next_payment_date' => $currentSubscription && $currentSubscription->next_payment_at != null ? \Carbon\Carbon::parse($currentSubscription->next_payment_at)->format('M j, Y') : null,
'payment_details' => $payment_details,
'days_left' => $currentSubscription && $currentSubscription->next_payment_at != null ? \Carbon\Carbon::parse($currentSubscription->next_payment_at)->diffInDays(Carbon::now()) : 0,
'payment_code' => $currentSubscription && $currentSubscription->paymentCode != null ? [
'code' => $currentSubscription->paymentCode->code,
'addons' => $currentSubscription->paymentCode->addons,
] : null,
'payment_method' => $currentSubscription ? $currentSubscription->payment_method : null,
'payment_mode' => $currentSubscription ? $currentSubscription->payment_mode : null,
'paid_invoice' => $currentSubscription ? $currentSubscription->paid_invoice : 0,
'trialing' => $currentSubscription ? $currentSubscription->trialing : false,
'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,
];
}
}