File: /home/xedaptot/ai.naniguide.com/app/Model/OrderItem.php
<?php
namespace App\Model;
use App\Library\OrderFulfillment\OrderItemTypes;
use App\Library\Traits\HasUid;
use Illuminate\Database\Eloquent\Model;
/**
* Pure data row. Business logic lives in App\Library\OrderFulfillment\Handlers\* dispatched
* by FulfillmentService via OrderHandlerResolver. See docs/payment-order-plan-subscription-saas/ORDER-SYSTEM.md.
*/
class OrderItem extends Model
{
use \Illuminate\Database\Eloquent\Factories\HasFactory;
use HasUid;
protected static function newFactory()
{
return \Database\Factories\OrderItemFactory::new();
}
protected $table = 'order_items';
protected $casts = [
'fulfilled_at' => 'datetime',
'refunded_at' => 'datetime',
'attempt_count' => 'int',
];
protected $fillable = [
'uid', 'order_id', 'type', 'amount', 'tax', 'discount',
'title', 'description', 'image_url',
'subscription_id', 'new_plan_id',
'sending_credits', 'email_verification_credits',
'fulfilled_at', 'refunded_at', 'attempt_count', 'last_error',
];
public function order()
{
return $this->belongsTo(Order::class);
}
public function subscription()
{
return $this->belongsTo(Subscription::class, 'subscription_id');
}
public function newPlan()
{
return $this->belongsTo(Plan::class, 'new_plan_id');
}
public static function findByUid($uid)
{
return self::where('uid', '=', $uid)->first();
}
public function scopeChangePlan($query)
{
$query->where('type', OrderItemTypes::SUBSCRIPTION_CHANGE_PLAN);
}
public function scopeNewSubscription($query)
{
$query->where('type', OrderItemTypes::SUBSCRIPTION_NEW);
}
public function scopeRenew($query)
{
$query->where('type', OrderItemTypes::SUBSCRIPTION_RENEW);
}
public function scopeEmailVerificationCredits($query)
{
$query->where('type', OrderItemTypes::CREDIT_VERIFICATION_TOPUP);
}
public function scopeSendingCredits($query)
{
$query->where('type', OrderItemTypes::CREDIT_SENDING_TOPUP);
}
public function scopeUnpaid($query)
{
$query->whereHas('order', function ($q) {
$q->unpaid();
});
}
public function scopePaid($query)
{
$query->whereHas('order', function ($q) {
$q->paid();
});
}
public function subTotal()
{
return $this->amount - $this->discount;
}
public function total()
{
return $this->amount;
}
public function getTax()
{
return ($this->subTotal() * ($this->tax / 100));
}
public static function getTypeSelectOptions()
{
return [
['text' => trans('messages.invoice.type.new_subscription'), 'value' => OrderItemTypes::SUBSCRIPTION_NEW],
['text' => trans('messages.invoice.type.renew_subscription'), 'value' => OrderItemTypes::SUBSCRIPTION_RENEW],
['text' => trans('messages.invoice.type.change_plan'), 'value' => OrderItemTypes::SUBSCRIPTION_CHANGE_PLAN],
['text' => trans('messages.invoice.type.email_verification_credits'), 'value' => OrderItemTypes::CREDIT_VERIFICATION_TOPUP],
['text' => trans('messages.invoice.type.sending_credits'), 'value' => OrderItemTypes::CREDIT_SENDING_TOPUP],
];
}
}