coupon code system eingeführt. coupons werden vom super admin gemanaged. coupons werden mit paddle synchronisiert und dort validiert. plus: einige mobil-optimierungen im tenant admin pwa.
This commit is contained in:
@@ -58,10 +58,13 @@ class CheckoutSession extends Model
|
||||
'package_snapshot' => 'array',
|
||||
'status_history' => 'array',
|
||||
'provider_metadata' => 'array',
|
||||
'coupon_snapshot' => 'array',
|
||||
'discount_breakdown' => 'array',
|
||||
'expires_at' => 'datetime',
|
||||
'completed_at' => 'datetime',
|
||||
'amount_subtotal' => 'decimal:2',
|
||||
'amount_total' => 'decimal:2',
|
||||
'amount_discount' => 'decimal:2',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -71,6 +74,8 @@ class CheckoutSession extends Model
|
||||
'status_history' => '[]',
|
||||
'package_snapshot' => '[]',
|
||||
'provider_metadata' => '[]',
|
||||
'coupon_snapshot' => '[]',
|
||||
'discount_breakdown' => '[]',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
@@ -88,6 +93,11 @@ class CheckoutSession extends Model
|
||||
return $this->belongsTo(Package::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function coupon(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Coupon::class);
|
||||
}
|
||||
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->whereNotIn('status', [
|
||||
|
||||
189
app/Models/Coupon.php
Normal file
189
app/Models/Coupon.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\CouponStatus;
|
||||
use App\Enums\CouponType;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Coupon extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\CouponFactory> */
|
||||
use HasFactory;
|
||||
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'code',
|
||||
'type',
|
||||
'amount',
|
||||
'currency',
|
||||
'status',
|
||||
'is_stackable',
|
||||
'enabled_for_checkout',
|
||||
'auto_apply',
|
||||
'usage_limit',
|
||||
'per_customer_limit',
|
||||
'redemptions_count',
|
||||
'description',
|
||||
'metadata',
|
||||
'starts_at',
|
||||
'ends_at',
|
||||
'paddle_discount_id',
|
||||
'paddle_mode',
|
||||
'paddle_snapshot',
|
||||
'paddle_last_synced_at',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'type' => CouponType::class,
|
||||
'status' => CouponStatus::class,
|
||||
'amount' => 'decimal:2',
|
||||
'is_stackable' => 'boolean',
|
||||
'enabled_for_checkout' => 'boolean',
|
||||
'auto_apply' => 'boolean',
|
||||
'metadata' => 'array',
|
||||
'paddle_snapshot' => 'array',
|
||||
'starts_at' => 'datetime',
|
||||
'ends_at' => 'datetime',
|
||||
'paddle_last_synced_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (self $coupon): void {
|
||||
if ($coupon->code) {
|
||||
$coupon->code = Str::upper($coupon->code);
|
||||
}
|
||||
|
||||
if ($coupon->currency) {
|
||||
$coupon->currency = Str::upper($coupon->currency);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function createdBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
public function updatedBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'updated_by');
|
||||
}
|
||||
|
||||
public function packages(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Package::class)->withTimestamps();
|
||||
}
|
||||
|
||||
public function checkoutSessions(): HasMany
|
||||
{
|
||||
return $this->hasMany(CheckoutSession::class);
|
||||
}
|
||||
|
||||
public function redemptions(): HasMany
|
||||
{
|
||||
return $this->hasMany(CouponRedemption::class);
|
||||
}
|
||||
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('status', CouponStatus::ACTIVE)
|
||||
->where(function ($inner) {
|
||||
$now = now();
|
||||
|
||||
$inner->whereNull('starts_at')->orWhere('starts_at', '<=', $now);
|
||||
})
|
||||
->where(function ($inner) {
|
||||
$now = now();
|
||||
|
||||
$inner->whereNull('ends_at')->orWhere('ends_at', '>=', $now);
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeAutoApply($query)
|
||||
{
|
||||
return $query->where('auto_apply', true);
|
||||
}
|
||||
|
||||
public function remainingUsages(?int $customerUsage = null): ?int
|
||||
{
|
||||
$globalRemaining = $this->usage_limit !== null
|
||||
? max($this->usage_limit - $this->redemptions_count, 0)
|
||||
: null;
|
||||
|
||||
$customerRemaining = $this->per_customer_limit !== null && $customerUsage !== null
|
||||
? max($this->per_customer_limit - $customerUsage, 0)
|
||||
: null;
|
||||
|
||||
if ($globalRemaining === null) {
|
||||
return $customerRemaining;
|
||||
}
|
||||
|
||||
if ($customerRemaining === null) {
|
||||
return $globalRemaining;
|
||||
}
|
||||
|
||||
return min($globalRemaining, $customerRemaining);
|
||||
}
|
||||
|
||||
public function isCurrentlyActive(): bool
|
||||
{
|
||||
if ($this->status !== CouponStatus::ACTIVE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = now();
|
||||
|
||||
if ($this->starts_at && $this->starts_at->isFuture()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->ends_at && $this->ends_at->isPast()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->usage_limit !== null && $this->redemptions_count >= $this->usage_limit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function appliesToPackage(?Package $package): bool
|
||||
{
|
||||
if (! $package) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->relationLoaded('packages')) {
|
||||
return $this->packages->isEmpty() || $this->packages->contains(fn (Package $pkg) => $pkg->is($package));
|
||||
}
|
||||
|
||||
$count = $this->packages()->count();
|
||||
if ($count === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->packages()->whereKey($package->getKey())->exists();
|
||||
}
|
||||
|
||||
protected function code(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn (?string $value) => $value ? Str::upper($value) : $value,
|
||||
set: fn (?string $value) => $value ? Str::upper($value) : $value,
|
||||
);
|
||||
}
|
||||
}
|
||||
84
app/Models/CouponRedemption.php
Normal file
84
app/Models/CouponRedemption.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class CouponRedemption extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\CouponRedemptionFactory> */
|
||||
use HasFactory;
|
||||
|
||||
public const STATUS_PENDING = 'pending';
|
||||
|
||||
public const STATUS_SUCCESS = 'success';
|
||||
|
||||
public const STATUS_FAILED = 'failed';
|
||||
|
||||
protected $fillable = [
|
||||
'coupon_id',
|
||||
'checkout_session_id',
|
||||
'package_id',
|
||||
'tenant_id',
|
||||
'user_id',
|
||||
'paddle_transaction_id',
|
||||
'status',
|
||||
'failure_reason',
|
||||
'amount_discounted',
|
||||
'currency',
|
||||
'metadata',
|
||||
'redeemed_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'amount_discounted' => 'decimal:2',
|
||||
'metadata' => 'array',
|
||||
'redeemed_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function coupon(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Coupon::class);
|
||||
}
|
||||
|
||||
public function checkoutSession(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CheckoutSession::class);
|
||||
}
|
||||
|
||||
public function package(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Package::class);
|
||||
}
|
||||
|
||||
public function tenant(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Tenant::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function markSuccessful(?float $amount = null): void
|
||||
{
|
||||
if ($amount !== null) {
|
||||
$this->amount_discounted = $amount;
|
||||
}
|
||||
|
||||
$this->status = self::STATUS_SUCCESS;
|
||||
$this->failure_reason = null;
|
||||
$this->redeemed_at = now();
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function markFailed(string $reason): void
|
||||
{
|
||||
$this->status = self::STATUS_FAILED;
|
||||
$this->failure_reason = $reason;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
@@ -96,6 +97,11 @@ class Package extends Model
|
||||
return $this->hasMany(PackagePurchase::class);
|
||||
}
|
||||
|
||||
public function coupons(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Coupon::class)->withTimestamps();
|
||||
}
|
||||
|
||||
public function isEndcustomer(): bool
|
||||
{
|
||||
return $this->type === 'endcustomer';
|
||||
|
||||
Reference in New Issue
Block a user